2

Edit: using Amazon Linux:

Linux ip-xx-xx-xx-xxx 3.14.44-32.39.amzn1.x86_64 #1 SMP Thu Jun 11 20:33:38 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


Output of this command:

date +"%Y-%m-%d %H:%M:%S" --date="2015-07-27 00:11:22"

2015-07-27 00:11:22

Output of this command:

date +"%Y-%m-%d %H:%M:%S" --date="2015-07-27 00:11:22 + 00:05"

2015-07-27 00:06:22

Output of this command:

date +"%Y-%m-%d %H:%M:%S" --date="2015-07-27 00:11:22 - 00:05"

2015-07-27 00:16:22

OMFG is the time flowing backwards? Or an 8th layer issue?

Edit: My Amazon Linux is at UTC+00:00 while my Ubuntu local machine is at UTC-05:00. The same commands in Ubuntu show:

2015-07-26 19:11:22

2015-07-26 19:06:22

2015-07-26 19:16:22

respectively, so + 00:05 does not seem to be the time zone.

  • 1
    Got the same issue on my gentoo.. I'm checking the "coreutils" sources for more details. – Adrien M. Jul 27 '15 at 21:42
  • 1
    The numbers after after +/- mean time zone, so --date="2015-07-27 00:11:22 + 00:05" means print date 2015-07-27 00:11:22 in a time zone that is 5 minutes west of UTC, i.e. 5 minutes earlier: 00:06:22. – jimmij Jul 27 '15 at 21:59
  • I want to perform date arithmetic. How do I do it? – Luis Masuelli Jul 27 '15 at 22:01
  • Wait! in my ubuntu machine I get "2015-07-26 19:16:22" as result. This means that 00:05 is not the timezone, but the timezone is 05:00 and both things seem to be applied – Luis Masuelli Jul 27 '15 at 22:03
  • @LuisMasuelli print date as a number of seconds since unix epoch with '+%s', grab it in variable with command substitution, and do arithmetic in normal way with (()) (double parenthesis) in bash. – jimmij Jul 27 '15 at 22:03
2

According to the source of parse-datetime.y of gnulib, it seems to be a feature related to the function time_zone_hhmm.

First, we cannot use your format to add seconds : date +"%Y-%m-%d %H:%M:%S" --date="2015-07-27 00:11:22 - 00:05:01", I'm having a parse error :

date: invalid date ‘2015-07-27 00:11:22 - 00:05:01’

Then, according to the header of the time_zone_hhmm function :

/* Convert a time zone expressed as HH:MM into an integer count of
minutes.  If MM is negative, then S is of the form HHMM and needs
to be picked apart; otherwise, S is of the form HH.  As specified in
http://www.opengroup.org/susv3xbd/xbd_chap08.html#tag_08_03, allow
only valid TZ range, and consider first two digits as hours, if no
minutes specified.  */

So, looks like you're modifing the timezone, which match the behavior of inverted add/remove time.

Should you want to perform arthmetics on time, you'd better use Epoch timestamps (date +'%s') which are easier to perform calculations on. Moreover, they are based on UTC time, which does not take local timezone of the server you're working on into account.

A timestamp will thus return the same result on USA, Asia or Europe. Then you use this timestamp to display human readable time, which takes the Timezone into account.

Anyway, nice catch. I would never have dug in gnulib source without this nice trick !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.