I was playing around with date to try to convert expressions such as "2 hours" to a number of seconds like 7200. I thought I could perform this with invocations such as:
date -d "1970-01-01 00:00:00 + 2 hours" +"%s"
Run Code Online (Sandbox Code Playgroud)
Yet, I noticed extremely strange results while so doing. (My /etc/timezone contains "Europe/Paris", hence the use of TZ to force UTC times.)
omega:~$ TZ=utc date -d "1970-01-01 00:00:00" +"%s"
0
omega:~$ TZ=utc date -d "1970-01-01 00:00:00 + 0 minutes" +"%s"
60
omega:~$ TZ=utc date -d "1970-01-01 00:00:00 + -1 minutes" +"%s"
3660
omega:~$ TZ=utc date -d "1970-01-01 00:00:00 + 1 minutes" +"%s"
-3540
Run Code Online (Sandbox Code Playgroud)
The first result is reasonable enough, but I'm really totally puzzled about how date might have come up with the three last results. Does anyone understand the logic between those results?
der*_*ert 11
我认为它将您+ x作为时区说明符(例如,考虑2013-04-25 19:52:36 +4是一个有效的时间戳,在 UTC 前 4 小时的时区中)。
然后它看到“分钟”这个词,把它当作分钟的同义词,所以一分钟后给你。
如果你输入一个明确的时区说明符,它会起作用:
anthony@Zia:~$ TZ=utc date -d "1970-01-01 00:00:00 UTC" +"%s"
0
anthony@Zia:~$ TZ=utc date -d "1970-01-01 00:00:00 UTC + 0 minutes" +"%s"
0
anthony@Zia:~$ TZ=utc date -d "1970-01-01 00:00:00 UTC + -1 minutes" +"%s"
-60
anthony@Zia:~$ TZ=utc date -d "1970-01-01 00:00:00 UTC + 1 minutes" +"%s"
60
Run Code Online (Sandbox Code Playgroud)
请注意UTC在秒字段之后。您还可以输入第二个+0:
anthony@Zia:~$ TZ=utc date -d "1970-01-01 00:00:00 +0 + -1 minutes" +"%s"
-60
Run Code Online (Sandbox Code Playgroud)
...但至少就个人而言,这更难阅读。
或者您可以Z在秒后放置 a ,等等。TZ在上面的示例中,您实际上不需要将环境变量设置为 UTC。
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |