错误消息“日期:无效日期'2016-10-16'”

Ter*_*ior 35 date timezone

今天,我的时钟自动调整为夏令时,来自 crontab 的脚本开始失败。我查看了发生了什么,并显示了以下错误LC_ALL=C:

日期:无效日期“2016-10-16”

我认为最好只是重新启动系统,但现在我已经重新启动,错误仍然出现:

$ date -d '2016-10-15'
Sat Oct 15 00:00:00 BRT 2016
$ date -d '2016-10-16'
date: data inválida “2016-10-16”
$ date -d '2016-10-17'
Mon Oct 17 00:00:00 BRST 2016
Run Code Online (Sandbox Code Playgroud)

什么可能导致这种情况?

cuo*_*glm 57

问题是夏令时在 2016 年 10 月 16 日在您的时区更改并转发了 1 小时:

$ zdump -v America/Sao_Paulo | awk '/Oct 16/ && /2016/'
America/Sao_Paulo  Sun Oct 16 02:59:59 2016 UTC = Sat Oct 15 23:59:59 2016 BRT isdst=0
America/Sao_Paulo  Sun Oct 16 03:00:00 2016 UTC = Sun Oct 16 01:00:00 2016 BRST isdst=1
Run Code Online (Sandbox Code Playgroud)

因此,当天00:00到00:59当天之间的任何时间在您的时区都被视为无效(但在其他时区可能有效):

$ TZ=America/Sao_Paulo gdate -d '2016-10-16 0:59'
gdate: invalid date ‘2016-10-16 0:59’

$ TZ=Asia/Ho_Chi_Minh gdate -d '2016-10-16 0:59'
Sun Oct 16 00:59:00 ICT 2016
Run Code Online (Sandbox Code Playgroud)

您可以设置不在该范围内的额外时间:

$ TZ=America/Sao_Paulo gdate -d '2016-10-16 1:00'
Sun Oct 16 01:00:00 BRST 2016
Run Code Online (Sandbox Code Playgroud)

以上是 GNU 日期行为。

BSD日期没有这个问题。如果输入的日期在时区中无效,则会静默向前调整 1 小时,直到达到有效时间:

$ TZ=America/Sao_Paulo date -j -f '%Y%m%d%H%M' 201610160000
Sun Oct 16 01:00:53 BRST 2016
Run Code Online (Sandbox Code Playgroud)