在某些情况下,shell 日期 "-n hours"与 "n hours ago"不同

标签 shell date

在 shell 中,当我打印这个时

date -d "2016-11-23 13:05 -1 hours "  "+%Y-%m-%d %H:00:00"

我得到 2016-11-23 23:00:00.奇怪的!

当我打印这个
date -d "2016-11-23 13:05 1 hours ago"  "+%Y-%m-%d %H:00:00"

我得到 2016-11-23 12:00:00 .

为什么它们不同?我认为他们都是2016-11-23 12:00:00 .

最佳答案

这是因为负数被视为时区的偏移量,而不是 13:05 .在我的时区 MET(格林威治标准时间以东一小时),这是我得到的:

$ date -d "2016-11-23 13:05 -1 hours "  "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT date -d "2016-11-23 13:05 -1 hours "  "+%Y-%m-%d %H:00:00"
2016-11-23 15:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -1 hours "  "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -2 hours "  "+%Y-%m-%d %H:00:00"
2016-11-23 17:00:00

时区偏移量通常指定为四位数字,如
Sun, 29  Feb 2004  16:21:42 -0800

但显然 date(1) 也对 -1 感到满意。

从手册页:

DATE STRING

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.

关于在某些情况下,shell 日期 "-n hours"与 "n hours ago"不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40757340/

相关文章:

java - 如何用java解析这个文本文件?

linux - 通过shell脚本更改文件内容

linux - 如何测试 File2 中是否存在 File1 每一行的子字符串

linux - CURL 进度条 : How to pipe and extract numbers only using grep?

Ruby Date.strptime 不强制使用 4 位数年份

string - 需要有效的方法来对 Scala 中的字符串(日期)列表进行排序。 (避开Java的黑暗。)

R以yyyymmdd格式读取日期

linux - 两个文件的嵌套循环

css - 如何将 `diff`文件创建一个 "common"文件?

Bash 将纪元转换为日期,显示错误的时间