简体   繁体   中英

Linux date objects behaving differently when hour/minute/second are specified

Why does adding one day work differently with the variables $d and $dt below?

Linux version: CentOS 6.8

If there is some kind of hidden timezone conversion going on with $dt but not with $d ? If so, how do I suppress it?

d="2019-04-01"
dt="2019-04-01 01:00:00"
date --date="$d +1 days" +'%Y-%m-%d'

Output: 2019-04-02

date --date="$dt +1 days" +'%Y-%m-%d %H:%M:%S'

Output: 2019-04-01 17:00:00

Just so this question is answered (I couldn't find a duplicate) it was pointed out in the comments that the +1 in +1 days was being interpreted as a UTC offset appended to the date.

The solution is to terminate the date string with a time zone specifier such as Z or UTC so it's recognized as a complete date.

See info '(coreutils) date invocation' for more details on date specifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM