向Unix时间解释毫秒

Tim*_*hko 2 c# datetime unix-timestamp

我试图找到更好的方法将C#中的DateTime转换为Unix时间戳

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

public long ToUnixTimeMilliseconds()
{
   return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
Run Code Online (Sandbox Code Playgroud)

这个方法是什么意思?使用了哪些常量

UPD:我想10000L从FRAME转换为毫秒。但是62135596800000L呢?

Mat*_*son 5

解释此方法:

public long ToUnixTimeMilliseconds()
{
    return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
Run Code Online (Sandbox Code Playgroud)

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

将其除以10_000会产生毫秒,这说明了将其除以10000L。

这是因为1纳秒等于十亿分之一秒,即一百万分之一毫秒。

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

但是,刻度是100纳秒单位,因此必须除以1_000_000 / 100 = 10_000才能除以1_000_000。这就是为什么将100纳秒单位除以10_000得出毫秒的原因。

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

DateTime时期(对应于DateTime.Ticks值为零)是0001年1月1日。

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
Run Code Online (Sandbox Code Playgroud)

确切的数字很难计算,但如上式所示,得出的数字为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        
Run Code Online (Sandbox Code Playgroud)

现在我们可以用来计算到1970年的毫秒数:

719162 (DaysTo1970) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds) 
= 621355967990000
Run Code Online (Sandbox Code Playgroud)