在 C# 中计算未来纪元时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/906034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:31:01  来源:igfitidea点击:

Calculating Future Epoch Time in C#

c#timeepoch

提问by UnkwnTech

I was able to find example code to get the current timestamp in Linux Epoch (Seconds since Midnight Jan 1st 1970), however I am having trouble finding an example as to how to calculate what the Epoch will be in the future, say for example 10 minutes from now, so how can I calculate a future time in Linux Epoch?

我能够找到示例代码来获取 Linux Epoch 中的当前时间戳(自 1970 年 1 月 1 日午夜以来的秒数),但是我无法找到有关如何计算未来 Epoch 的示例,例如 10几分钟后,那么如何计算 Linux Epoch 中的未来时间?

采纳答案by Noldorin

This extension method should do the job:

这个扩展方法应该可以完成这项工作:

private static double GetUnixEpoch(this DateTime dateTime)
{
    var unixTime = dateTime.ToUniversalTime() - 
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    return unixTime.TotalSeconds;
}

And you can use it as such:

你可以这样使用它:

var unixTime1 = DateTime.Now.GetUnixEpoch(); // precisely now
var unixTime2 = (DateTime.Now + new TimeSpan(0, 10, 0)).GetUnixEpoch(); // 10 minutes in future

Note that you need to deal with all date-times in UTC(Universal Time), since that's how the start of the Unix Epoch is defined.

请注意,您需要处理UTC(世界时)中的所有日期时间,因为这就是 Unix 纪元的开始定义方式。

回答by Nick

The basic solution:

基本解决方案:

  1. Take Current Time
  2. Add Offset to Future (10 Mins, in your example)
  3. Subtract Midnight of Jan 1st 1970
  4. Get the total number of seconds between the two
  1. 取当前时间
  2. 向未来添加偏移量(在您的示例中为 10 分钟)
  3. 减去 1970 年 1 月 1 日午夜
  4. 获取两者之间的总秒数

In c# (off the top of my head, untested):

在 c# 中(在我的头顶上,未经测试):

DateTime myEpoch = DateTime.Now.Add( new TimeSpan(...) );
return myEpoch.Subtract( new DateTime(1970, 1, 1, 0, 0, 0) ).TotalSeconds;

回答by Eoin Campbell

Whats the function you use to get the current time ?

你用来获取当前时间的函数是什么?

Sure that takes a .NET DateTime parameter...

当然需要一个 .NET DateTime 参数......

Surely it's just a simple matter of passing in a future DateTime to the function.

当然,这只是将未来的 DateTime 传递给函数的简单问题。

or doing a DateDiff in seconds between the current time and the future time and adding that on.

或者在当前时间和未来时间之间以秒为单位执行 DateDiff 并添加它。

var dt = new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();

var now = System.DateTime.Now.ToUniversalTime();
var future = new DateTime(2010, 1, 1).ToUniversalTime();

Console.WriteLine((now - dt).TotalSeconds);
Console.WriteLine((future - dt).TotalSeconds);

回答by Peter Stuer

There is an interesting twist when you want to know the Unix Epoch time in .Net on a Windows system.

当您想知道 Windows 系统上 .Net 中的 Unix Epoch 时间时,有一个有趣的转折。

For nearly all practical cases and assuming the current time is past the Unix Epoch you could indeed take

对于几乎所有实际情况并假设当前时间已经超过 Unix Epoch,您确实可以采用

System.TimeSpan timeDifference = DateTime.UTCNow - 
            new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long unixEpochTime = System.Convert.ToInt64(timeDifference.TotalSeconds);

But,

但,

Unix Epoch Time is defined as "... a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds." (1)

Unix 纪元时间被定义为“……一个描述时间点的系统,定义为自 1970 年 1 月 1 日协调世界时 (UTC) 午夜以来经过的秒数,不计算闰秒。” (1)

Since 1972, UTC has included "leap seconds", and we have had a total of 25 of them so far. (2)

自 1972 年以来,UTC 就包含了“闰秒”,到目前为止我们总共有 25 个。(2)

The .Net DateTime has no provisions for Leap Seconds, but will simply rely on the OS time. Windows is blissfully unaware of Leap Seconds (3)(4), and so will just have the notion of time as it receives it from its NTP master (I believe the default for a non-domain connected machine is time.windows.com ), which is probably serving up UTC including leap seconds.

.Net DateTime 没有闰秒的规定,而只是依赖于操作系统时间。Windows 完全不知道闰秒 (3)(4),因此当它从它的 NTP 主机接收它时只会有时间的概念(我相信非域连接机器的默认值是 time.windows.com ) ,这可能提供 UTC 包括闰秒。

This means that in order to be pedantically correct about the real number of seconds passed since the Unix epoch, you should probably add the leap seconds to the result obtained above for applications that rely on this. You would have to track the number of seconds to add at each time since leap seconds are not announced far in advance (2). However, as the definition of Unix Epoch Time explicitly excludes leap seconds, you can safely ignore this and simply recalculate seconds from the current UTC time.

这意味着,为了对自 Unix 纪元以来经过的实际秒数进行迂回的正确处理,您可能应该将闰秒添加到上面为依赖于此的应用程序获得的结果中。您必须跟踪每次添加的秒数,因为闰秒不会提前很长时间 (2)。但是,由于 Unix Epoch Time 的定义明确排除了闰秒,您可以放心地忽略这一点,只需从当前 UTC 时间重新计算秒数。

Sometimes, leap seconds do cause software mayhem (5). The debate over whether to keep or eliminate the practice is ongoing (6)(7)(8).

有时,闰秒确实会导致软件混乱 (5)。关于是保留还是取消这种做法的争论仍在继续 (6)(7)(8)。

The last leap second at the time of the answer occurred on the 1st of July 2012 (9) and caused problems for various sites and applications (10)

回答时的最后一个闰秒发生在 2012 年 7 月 1 日 (9) 并导致各种站点和应用程序出现问题 (10)

(1) http://en.wikipedia.org/wiki/Unix_time

(1) http://en.wikipedia.org/wiki/Unix_time

(2) http://en.wikipedia.org/wiki/Leap_second

(2) http://en.wikipedia.org/wiki/Leap_second

(3) http://support.microsoft.com/kb/909614

(3) http://support.microsoft.com/kb/909614

(4) http://www.meinberg.de/english/info/leap-second.htm

(4) http://www.meinberg.de/english/info/leap-second.htm

(5) http://www.networkworld.com/news/2009/010609-leap-second-snafu-affects-oracle.html

(5) http://www.networkworld.com/news/2009/010609-leap-second-snafu-affects-oracle.html

(6) http://www.pcworld.idg.com.au/article/358024/time_waits_no_one_leap_seconds_may_cut/

(6) http://www.pcworld.idg.com.au/article/358024/time_waits_no_one_leap_seconds_may_cut/

(7) http://queue.acm.org/detail.cfm?id=1967009

(7) http://queue.acm.org/detail.cfm?id=1967009

(8) http://arxiv.org/abs/1106.3141

(8) http://arxiv.org/abs/1106.3141

(9) http://hpiers.obspm.fr/iers/bul/bulc/bulletinc.dat

(9) http://hpiers.obspm.fr/iers/bul/bulc/bulletinc.dat

(10) http://arstechnica.com/business/2012/07/one-day-later-the-leap-second-v-the-internet-scorecard/

(10) http://arstechnica.com/business/2012/07/one-day-later-the-leap-second-v-the-internet-scorecard/

(The original answer had a mistake, which was thankfully caught by the commenters Edward Brey and Mormegil below)

(原来的回答有一个错误,幸好被下面的评论者 Edward Brey 和 Mormegil 发现了)