解释UnixTime毫秒

2024-02-21

我正在尝试找到更好的方法在 C# 中将 DateTime 转换为 unix 时间戳

我发现有一个 DateTimeOffset.ToUnixTimeMilliseconds 方法:

public long ToUnixTimeMilliseconds()
{
   return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}

这个方法意味着什么?什么是常量被使用?

更新: 我猜 10000L 是从FRAME到毫秒。但是 62135596800000L 呢?


解释一下这个方法:

public long ToUnixTimeMilliseconds()
{
    return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}

DateTime.Ticks 单位是 100 纳秒间隔。

将其除以 10_000 得到毫秒,这解释了除以 10000L 的原因。

这是因为一纳秒是十亿分之一秒,或百万分之一毫秒。

要将纳秒转换为毫秒,您需要除以 1_000_000。

但是,刻度为 100 纳秒单位,因此您不必除以 1_000_000,而是必须除以 1_000_000/100 = 10_000。这就是为什么将 100 纳秒单位除以 10_000 以获得毫秒。

Unix 纪元(对应于 Unix 时间零)是 1970 年 1 月 1 日午夜。

DateTime 纪元(对应于 DateTime.Ticks 值为零)是 1 月 1 日 0001。

从 0001 年 1 月 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。

事实上,从检查源代码 https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,1b9a0703f21bda63我们可以找到以下内容:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解释UnixTime毫秒 的相关文章

随机推荐