add time units to a date without timezone

佐手、 提交于 2021-02-05 08:36:06

问题


I write these in Linux bash

date -d "2018-08-21 02:00:00" +'%y-%m-%d %T'

and it prints

2018-08-21 02:00:00

But when I write these

date -d "2018-08-21 02:00:00 +1 hour" +'%y-%m-%d %T'

it prints

2018-08-21 07:30:00 instead of 2018-08-21 03:00:00

It adds my timezone to the date. How can I ignore timezone when I'm adding time units?


回答1:


What is happening is that the +1 is interpreted as the timezone UTC+1h. So it will convert your input date from UTC+1 to your local time-zone and then still add an extra hour to it due to the statement hour.

To solve this, you have to get rid of the + sign. Here are some possibilities:

date -d "2018-08-21 02:00:00 next hour" "+%F %T"
date -d "2018-08-21 02:00:00 hour" "+%F %T"

Or use float numbers:

date -d "2018-08-21 02:00:00 + 1.0 hour" "+%F %T"

For more information on why this is the case, have a look at: How to add an interval to a date/time stored in a variable



来源:https://stackoverflow.com/questions/51948822/add-time-units-to-a-date-without-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!