Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

Marcus

If I subtract a time amount from the current date, GNU date works intuitively:

date '+%F %R'; date '+%F %R' --date='- 1 hour'
2021-04-19 15:35
2021-04-19 14:35

However, when I use a date as operand, the result is unexpected:

$ date '+%F %R' --date='2000/1/2 03:04:05 - 1 hour'
2000-01-02 06:04

$ date '+%F %R' --date='2000/1/2 03:04:05 + 1 hour ago'
2000-01-02 02:04

How is date intepreting the $date - 1 hour expression?

ilkkachu

In short: The date you give with --date is taken in local time, unless you specify a time zone, and something like +/- NNN is taken as one. Only anything after that, even if it's just hour is taken as the relative modifier. So - 1 hour doesn't mean to subtract one hour from the given time, but to specify that the time is in the time zone UTC-01, and then to add one hour to it.


What I think should work for what you're trying, would be to either explicitly give the timezone before the offset, or put the offset first so it can't be confused with a timezone.

Here, using the Central European Summer Time timezone (CEST), and today's date, with %Z added to the output to show the timezone. (You could also use %z to output the numeric timezone, or +0200 here.)

$ date +'%F %T %Z' -d '2021-04-19 12:00:00 CEST + 5 hours'
2021-04-19 17:00:00 CEST
$ date +'%F %T %Z' -d '+ 5 hours 2021-04-19 12:00:00'
2021-04-19 17:00:00 CEST

Though of course for a January date like in the question, a summer-time time zone like CEST would not be a valid one. But rearranging the two still works, the time you give is just taken as the local time at that time.

$ date +'%F %T %Z' -d '+ 5 hours 2021-01-01 12:00:00'
2021-01-01 17:00:00 CET

(And for 2021-10-31 02:30:00 I get CET, even though that time also exists in CEST...)

(See older revisions of this answer for more examples on how it interprets various inputs.)


As per @muru's answer on another question, we can also use the --debug option to have the program actually tell us what it did. Note the second and third lines:

$ date --debug +'%F %T %Z' -d '2021-04-19 12:00:00 - 1 hour'
date: parsed date part: (Y-M-D) 2021-04-19
date: parsed time part: 12:00:00 TZ=-01:00
date: parsed relative part: +1 hour(s)
date: input timezone: -01:00 (set from parsed date/time string)
date: using specified time as starting value: '12:00:00'
date: starting date/time: '(Y-M-D) 2021-04-19 12:00:00 TZ=-01:00'
date: '(Y-M-D) 2021-04-19 12:00:00 TZ=-01:00' = 1618837200 epoch-seconds
date: after time adjustment (+1 hours, +0 minutes, +0 seconds, +0 ns),
date:     new time = 1618840800 epoch-seconds
date: output timezone: +01:00 (set from TZ="Europe/Berlin" environment value)
date: final: 1618840800.000000000 (epoch-seconds)
date: final: (Y-M-D) 2021-04-19 14:00:00 (UTC0)
date: final: (Y-M-D) 2021-04-19 16:00:00 (output timezone TZ=+01:00)
2021-04-19 16:00:00 CEST

The man page says:

The date string format is more complex than is easily documented here [...]

Which indeed seems quite apt. The more comprehensive documentation is in the info pages, or online: https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Why does new.Date() differs 1 hour from new.Date().toISOString()?

From Dev

Why SimpleDateFormat does not apply specified date pattern?

From Dev

Why SimpleDateFormat does not apply specified date pattern?

From Dev

Date validation - Date object differs from input

From Dev

JavaScript Date() differs when timezone changes

From Dev

Why does having the time in GNU date break "- 1 day"?

From Dev

javascript creates new date minus one day

From Dev

Date minus date query

From Dev

Date minus date query

From Dev

Copy files from a directory to another one with date in a specified range

From Dev

Minus Year from Date original: 20140320 Android

From Dev

Where is the date format specified when reading a date as a string from JDBC ResultSet

From Dev

Where is the date format specified when reading a date as a string from JDBC ResultSet

From Dev

how does the gnu coreutils `date` work?

From Dev

Minus date calculation with GWT

From Dev

Get today's date minus one Year in Unix (AIX)

From Dev

How to get the date one month before from current date, when current date is January?

From Dev

VBA interpretation of input date

From Dev

How to override GetHashCode when a Date matches if plus or minus seven days

From Dev

Why does javascript new Date add one hour depending on the format of date

From Dev

Why does the query by date range not work when different years are involved?

From Dev

Python: Date difference when one date is NAT

From Dev

JavaScript prototypal inheritance does this code differs one from the other?

From Dev

Moment JS Date format to remove minus sign from iso format

From Dev

why does ":type null" differs in Frege from Haskell?

From Dev

Why does FROM_UNIXTIME is returning date back in 1970?

From Dev

Why does js subtract a day from a Date object with a certain format?

From Dev

Why size of 'BigInteger' differs when saved from what 'bitLength' reports?

Related Related

  1. 1

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  2. 2

    Why does new.Date() differs 1 hour from new.Date().toISOString()?

  3. 3

    Why SimpleDateFormat does not apply specified date pattern?

  4. 4

    Why SimpleDateFormat does not apply specified date pattern?

  5. 5

    Date validation - Date object differs from input

  6. 6

    JavaScript Date() differs when timezone changes

  7. 7

    Why does having the time in GNU date break "- 1 day"?

  8. 8

    javascript creates new date minus one day

  9. 9

    Date minus date query

  10. 10

    Date minus date query

  11. 11

    Copy files from a directory to another one with date in a specified range

  12. 12

    Minus Year from Date original: 20140320 Android

  13. 13

    Where is the date format specified when reading a date as a string from JDBC ResultSet

  14. 14

    Where is the date format specified when reading a date as a string from JDBC ResultSet

  15. 15

    how does the gnu coreutils `date` work?

  16. 16

    Minus date calculation with GWT

  17. 17

    Get today's date minus one Year in Unix (AIX)

  18. 18

    How to get the date one month before from current date, when current date is January?

  19. 19

    VBA interpretation of input date

  20. 20

    How to override GetHashCode when a Date matches if plus or minus seven days

  21. 21

    Why does javascript new Date add one hour depending on the format of date

  22. 22

    Why does the query by date range not work when different years are involved?

  23. 23

    Python: Date difference when one date is NAT

  24. 24

    JavaScript prototypal inheritance does this code differs one from the other?

  25. 25

    Moment JS Date format to remove minus sign from iso format

  26. 26

    why does ":type null" differs in Frege from Haskell?

  27. 27

    Why does FROM_UNIXTIME is returning date back in 1970?

  28. 28

    Why does js subtract a day from a Date object with a certain format?

  29. 29

    Why size of 'BigInteger' differs when saved from what 'bitLength' reports?

HotTag

Archive