My task: At the entrance I get a date with the time in the form of "20170101 14:00:00". I need to take an hour and save to a variable for future use.

What I did: After a few hours of google and forums, I figured out some shell basics. I read in several articles about date. But unfortunately the script does not what I expect from it.

The script recognizes the date and formats the correct view. But very unpredictable behaves "-1hour". I get the correct date at "+ 3hours" and so far I have adopted it for myself in the script. Could any unexpected results occur in the future? Is it connected in some way with time zones?

Question: Why does “-” add time, and “+” take away? And why suddenly time starts to be calculated from "4:00:00" when you enter "2:00:00"? I would be very grateful for podskaskam or links where I can learn my knowledge in shell scripts.

DATE="20170101 14:00:00" dat2=$(date -d "$DATE" +'%Y%m%d %H%M%S') ECHO $dat2 ECHO date -d "$DATE +0hour" +'%Y%m%d %H%M%S' date -d "$DATE +1hour" +'%Y%m%d %H%M%S' date -d "$DATE +2hour" +'%Y%m%d %H%M%S' date -d "$DATE +3hour" +'%Y%m%d %H%M%S' date -d "$DATE +4hour" +'%Y%m%d %H%M%S' ECHO date -d "$DATE -1hour" +'%Y%m%d %H%M%S' date -d "$DATE -2hour" +'%Y%m%d %H%M%S' date -d "$DATE -3hour" +'%Y%m%d %H%M%S 

output:

 20170101 140000 20170101 160000 20170101 150000 20170101 140000 20170101 130000 20170101 120000 20170101 170000 20170101 180000 20170101 190000 

2 answers 2

GNU date understands the format x hour ago

 date -d '2017-01-01 15:22:00 2 hour ago' "+%m/%d/%Y %H:%M:%S" 01/01/2017 13:22:00 

Or closer to your:

 date -d '20170101 15:22:00 1 hour ago' "+%Y%m%d %H%M%S" 20170101 142200 date -d '20170101 15:22:00 2 hour ago' "+%Y%m%d %H%M%S" 20170101 132200 

    in a line like 20170101 14:00:00 +x hour you are probably “confused” by the algorithm laid down in gnu / date , which it tries to figure out based on the iso 8601 standard, the system time zone and its own additions of type x hour [ago] what exactly do you mean.


    in order to give the algorithm more unambiguity, it is better to use add-ons exactly as they are described in the help:

    • дата время 1 hour - "plus one hour to the specified time"
    • дата время 1 hour ago - "minus one hour from the specified time"

    but if for some reason you do not need to specify 1 hour [ago] , but +1 hour / -1 hour , then give the algorithm a “hint” - put this line before the date:

    • -1 hour дата время
    • +1 hour дата время

    ps The specific time of the clock, issued by the program for the line of the type 20170101 14:00:00 +x hour , is also influenced by the time zone selected in the system (or its overlapping value of the TZ environment variable). I managed to repeat your number with TZ=Europe/Paris ( UTC+1 ):

     $ TZ=Europe/Paris date -d "20170101 14:00:00 +0hour" +'%Y%m%d %H%M%S' 20170101 160000 

    at TZ , equal to, for example, Europe/Moscow , the figure will be quite different:

     $ TZ=Europe/Moscow date -d "20170101 14:00:00 +0hour" +'%Y%m%d %H%M%S' 20170101 180000