我正在寻找更好的方法将DateTime转换为C#中的unix时间戳
我发现有一个DateTimeOffset.ToUnixTimeMilliseconds方法:
public long ToUnixTimeMilliseconds()
{
return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
这种方法意味着什么?使用了什么常量?
UPD:我想10000升正在从帧转换到毫秒。那62135596800000升呢?
发布于 2019-10-15 09:17:24
为了解释这种方法:
public long ToUnixTimeMilliseconds()
{
return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
DateTime.Ticks单位是100纳秒间隔。
除以10_000产生毫秒,这解释了除以10000 L。
这是因为一纳秒等于十亿分之一秒,或者一百万毫秒。
因此,要将纳秒转换为毫秒,需要除以1_000_000。
然而,滴答是100纳秒单位,所以不是除以1_000_000,你将不得不除以1_000_000/100 = 10_000。这就是为什么你用10_000除以100纳秒单位来获得毫秒的原因。
Unix时代(相当于一个Unix时间为零)是1970年1月1日午夜。
DateTime时代(对应于DateTime.Ticks值为零)为0001年1月1日。
从01月1日到1970年1月1日之间的毫秒数是62135596800000秒。这解释了62135596800000的减法。
就在这里!
注意:您可以计算毫秒数的近似值如下:
Approximate number of days per year = 365.24219
Number of years between 0001 and 1970 = 1969
Thus, total approx milliseconds = 1969 * 365.24219 * 24 * 60 * 60 * 1000
= 62135585750000
确切的数字要难得多,但正如上面公式中所使用的那样,它得到了62135596800000。
实际上,通过对源代码的检查,我们可以发现以下内容:
public long ToUnixTimeSeconds() {
// Truncate sub-second precision before offsetting by the Unix Epoch to avoid
// the last digit being off by one for dates that result in negative Unix times.
//
// For example, consider the DateTimeOffset 12/31/1969 12:59:59.001 +0
// ticks = 621355967990010000
// ticksFromEpoch = ticks - UnixEpochTicks = -9990000
// secondsFromEpoch = ticksFromEpoch / TimeSpan.TicksPerSecond = 0
//
// Notice that secondsFromEpoch is rounded *up* by the truncation induced by integer division,
// whereas we actually always want to round *down* when converting to Unix time. This happens
// automatically for positive Unix time values. Now the example becomes:
// seconds = ticks / TimeSpan.TicksPerSecond = 62135596799
// secondsFromEpoch = seconds - UnixEpochSeconds = -1
//
// In other words, we want to consistently round toward the time 1/1/0001 00:00:00,
// rather than toward the Unix Epoch (1/1/1970 00:00:00).
long seconds = UtcDateTime.Ticks / TimeSpan.TicksPerSecond;
return seconds - UnixEpochSeconds;
}
// Number of days in a non-leap year
private const int DaysPerYear = 365;
// Number of days in 4 years
private const int DaysPer4Years = DaysPerYear * 4 + 1; // 1461
// Number of days in 100 years
private const int DaysPer100Years = DaysPer4Years * 25 - 1; // 36524
// Number of days in 400 years
private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097
// Number of days from 1/1/0001 to 12/31/1600
private const int DaysTo1601 = DaysPer400Years * 4; // 584388
// Number of days from 1/1/0001 to 12/30/1899
private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
// Number of days from 1/1/0001 to 12/31/1969
internal const int DaysTo1970 = DaysPer400Years * 4 + DaysPer100Years * 3 + DaysPer4Years * 17 + DaysPerYear; // 719,162
我们现在可以使用它来计算到1970年的毫秒数:
719162 (DaysTo1970) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds)
= 62135596800000
发布于 2022-06-09 23:34:34
621355968000000000
是DateTime
定义的Ticks
的正确数量。
接受的答案使人觉得DateTime
可能产生与实际使用它不同的未定义输出。
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks
// 621355968000000000
自己在线运行:https://dotnetfiddle.net/jU0SIp
/// <summary>
/// Convert Ticks to Unix Timestamp
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static long ToUnixTimestamp(long time)
{
return (time - 621355968000000000) / TimeSpan.TicksPerMillisecond;
}
如果您愿意,可以用TimeSpan.TicksPerMillisecond
替换10000
(如您的例子),它只是There is 10,000 ticks in a millisecond
的常量
https://stackoverflow.com/questions/58391011
复制相似问题