System.DateTimeOffset 中新增的Unix 时间戳方法

// System.DateTimeOffset
[__DynamicallyInvokable]
public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
{
    if (milliseconds < -62135596800000L || milliseconds > 253402300799999L)
    {
        throw new ArgumentOutOfRangeException("milliseconds", string.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"), -62135596800000L, 253402300799999L));
    }
    long ticks = milliseconds * 10000L + 621355968000000000L;
    return new DateTimeOffset(ticks, TimeSpan.Zero);
}
// System.DateTimeOffset
[__DynamicallyInvokable]
public static DateTimeOffset FromUnixTimeSeconds(long seconds)
{
	if (seconds < -62135596800L || seconds > 253402300799L)
	{
		throw new ArgumentOutOfRangeException("seconds", string.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"), -62135596800L, 253402300799L));
	}
	long ticks = seconds * 10000000L + 621355968000000000L;
	return new DateTimeOffset(ticks, TimeSpan.Zero);
}

  

// System.DateTimeOffset
[__DynamicallyInvokable]
public long ToUnixTimeMilliseconds()
{
    long num = this.UtcDateTime.Ticks / 10000L;
    return num - 62135596800000L;
}
// System.DateTimeOffset
[__DynamicallyInvokable]
public long ToUnixTimeSeconds()
{
	long num = this.UtcDateTime.Ticks / 10000000L;
	return num - 62135596800L;
}

  为了这四个方法特意装了VS2015,你没看错,是2015

posted @ 2014-11-14 22:56  BinSys  阅读(2553)  评论(0编辑  收藏  举报