向Unix时间解释毫秒 [英] Explain ToUnixTimeMilliseconds

查看:163
本文介绍了向Unix时间解释毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 公共长ToUnixTimeMilliseconds()
{
返回this.UtcDateTime.Ticks / 10000L-62135596800000L;
}

此方法是什么意思?使用什么常量



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

解决方案

要解释这种方法:

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

DateTime。刻度单位为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日午夜。 / p>

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



0001年1月1日到1970年1月1日之间的毫秒数为62135596800000。这就是减去62135596800000的原因。



然后就可以了!



注意:您可以如下计算毫秒数的近似值

 每年大约的天数= 365.24219 
0001至1970年之间的年数= 1969
因此,总计约毫秒= 1969 * 365.24219 * 24 * 60 * 60 * 1000
= 62135585750000

确切的数字很难计算,但得出的数字为62135596800000



实际上,通过检查源代码,我们可以找到以下内容:

  public long ToUnixTimeSeconds(){
//在被Unix抵消之前截断亚秒精度避免
的时代//对于导致负Unix时间的日期,最后一位偏移1。
//
//例如,考虑DateTimeOffset 12/31/1969 12:59:59.001 +0
// ticks = 621355967990010000
// ticksFromEpoch = ticks-UnixEpochTicks = -9990000
// secondsFromEpoch = ticksFromEpoch / TimeSpan.TicksPerSecond = 0
//
//请注意,secondsFromEpoch由整数除法
截断而向上取整//,而实际上我们总是希望在转换为Unix时间时将*舍入*。
//对于Unix时间正值会自动发生。现在该示例变为:
//秒= ticks / TimeSpan.TicksPerSecond = 62135596799
// secondsFromEpoch =秒-UnixEpochSeconds = -1
//
//换句话说,我们要始终朝着时间1/1/0001 00:00:00,
//舍入,而不是朝着Unix时代(1/1/1970 00:00:00)进行取整。
长秒= UtcDateTime.Ticks / TimeSpan.TicksPerSecond;
返回秒-UnixEpochSeconds;
}

//非-年的天数
private const int DaysPerYear = 365;
// 4年中的天数
private const int DaysPer4Years = DaysPerYear * 4 +1; // 1461
// 100年的天数
private const int DaysPer100Years = DaysPer4Years * 25-1; // 36524
// 400年的天数
private const int DaysPer400Years = DaysPer100Years * 4 +1; // 146097

//从1/1/0001到12/31/1600的天数
private const int DaysTo1601 = DaysPer400Years * 4; // 584388
//从1/1/0001到12/30/1899的天数
private const int DaysTo1899 = DaysPer400年* 4 + DaysPer100年* 3-367;
//从1/1/0001到1969年12月31日的天数
internal const int DaysTo1970 = DaysPer400年* 4 + DaysPer100年* 3 + DaysPer4Year * 17 + DaysPerYear; // 719,162

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

  719162(DaysTo1970)* 24(小时)* 60(分钟)* 60(秒)* 1000(毫秒)
= 621355967990000


I'm trying to find better way to convert DateTime to unix timestamp in C#

I find out that there is a DateTimeOffset.ToUnixTimeMilliseconds method:

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

What this method means? What the constants are used?

UPD: I guess 10000L is converting from FRAME to Milliseconds. But what about 62135596800000L?

解决方案

To explain this method:

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

DateTime.Ticks units are 100 nanosecond intervals.

Dividing this by 10_000 yields milliseconds, which explains the division by 10000L.

This is because one nanosecond is one billionth of a second, or one millionth of a millisecond.

To convert a nanosecond to a millisecond you would therefore divide by 1_000_000.

However, the ticks are 100 nanosecond units, so instead of dividing by 1_000_000 you would have to divide by 1_000_000/100 = 10_000. That's why you divide the 100 nanosecond units by 10_000 to get milliseconds.

The Unix epoch (which corresponds to a Unix time of zero) is midnight on 1st January 1970.

The DateTime epoch (which corresponds to a DateTime.Ticks value of zero) is 1st January 0001.

The number of milliseconds between 1st January 0001 and 1st January 1970 is 62135596800000. This explains the subtraction of 62135596800000.

And there you have it!

Note: You can compute an approximate value for the number of milliseconds as follows:

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

The exact figure is much harder to calculate, but it comes out to 62135596800000 as used in the formula above.

In fact, from inspection of the source code we can find the following:

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        

Which we can now use to calculate the number of milliseconds to 1970:

719162 (DaysTo1970) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds) 
= 621355967990000

这篇关于向Unix时间解释毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆