bash 以秒为单位获取 UTC 时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24547655/
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-09-10 00:54:23  来源:igfitidea点击:

Get UTC time in seconds

bashdatedatetime

提问by Romanzo Criminale

It looks like I can't manage to get the bash UTC date in second. I'm in Sydney so + 10hours UTC time

看起来我无法在第二秒内获得 bash UTC 日期。我在悉尼 + 10 小时 UTC 时间

 date
 Thu Jul 3 17:28:19 WST 2014

 date -u
 Thu Jul 3 07:28:20 UTC 2014

But when I tried to convert it, I'm getting the same result which is not UTC time

但是当我尝试转换它时,我得到了相同的结果,这不是 UTC 时间

 date +%s
 1404372514

 date -u +%s
 1404372515

What am I missing here?

我在这里缺少什么?



After getting an answer saying date +%swas returning UTC time, here are more details about the problem I'm facing now.

在得到答复说date +%s返回 UTC 时间后,这里是有关我现在面临的问题的更多详细信息。

I'm trying to compare a date written in a file with python. This date is written in seconds in UTC time. And the bash date +%sdoesn't give me the same one. Actually if I'm doing in python time.asctime(time.localtime(date_in_seconds_from_bash)), I get the current time of Sydney, not UTC. I don't understand.

我正在尝试将文件中写入的日期与 python 进行比较。此日期以 UTC 时间的秒数表示。bashdate +%s并没有给我同样的结果。实际上,如果我在 python 中做time.asctime(time.localtime(date_in_seconds_from_bash)),我会得到悉尼的当前时间,而不是 UTC。我不明白。

回答by JamesNoonan33

I believe +%sis seconds since epoch. It's timezone invariant.

我相信+%s是自纪元以来的几秒钟。它是时区不变的。

回答by Derek Boonstra

I bet this is what was intended as a result.

我敢打赌,这就是预期的结果。

$ date -u --date=@1404372514
Thu Jul  3 07:28:34 UTC 2014

回答by Keith Thompson

You say you're using:

你说你正在使用:

time.asctime(time.localtime(date_in_seconds_from_bash))

where date_in_seconds_from_bashis presumably the output of date +%s.

其中date_in_seconds_from_bash是推测的输出date +%s

The time.localtimefunction, as the name implies, gives you local time.

time.localtime顾名思义,该函数为您提供本地时间

If you want UTC, use time.gmtime()rather than time.localtime().

如果您想要 UTC,请使用time.gmtime()而不是time.localtime().

As JamesNoonan33's answersays, the output of date +%sis timezone invariant, so date +%sis exactly equivalent to date -u %s. It prints the number of seconds since the "epoch", which is 1970-01-01 00:00:00 UTC. The output you show in your question is entirely consistent with that:

正如JamesNoonan33 的回答所说, 的输出date +%s是时区不变的,因此date +%s完全等同于date -u %s. 它打印自“纪元”以来的秒数,即1970-01-01 00:00:00 UTC. 您在问题中显示的输出与此完全一致:

date -u
Thu Jul 3 07:28:20 UTC 2014

date +%s
1404372514   # 14 seconds after "date -u" command

date -u +%s
1404372515   # 15 seconds after "date -u" command

回答by Adam

One might consider adding this line to ~/.bash_profile(or similar) in order to can quickly get the current UTC both as current time and as seconds since the epoch.

人们可能会考虑将此行添加到~/.bash_profile(或类似的)中,以便可以快速获取当前 UTC 作为当前时间和自纪元以来的秒数。

alias utc='date -u && date -u +%s'

alias utc='date -u && date -u +%s'