gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:24:35 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com