skip to Main Content

If I enter date +"%Y/%m/%d %H:%M" -d "20200819T1234" the expected output is 2020/08/19 12:34 when in I actually get 2020/08/19 02:34 that is, exactly 10 hours less. Could someone explain to me why this happens?
At the moment I manage with + 10 hours at the end of the command, but I don’t think this is normal. The time zone set on the server is -03 so I am very confused.
This happens in both Ubuntu 16 and Debian 10

2

Answers


  1. Your format is mismatch with the time string: try

    date +"%Y/%m/%d %H:%M" -d "2020/08/19 12:34"
    

    2020/08/19 12:34

    Login or Signup to reply.
  2. The GNU code for parsing dates (parse_datetime2) has this table embedded within it that provides the reason why you’re getting the incorrect time (trimmed for conciseness):

    /* Military time zone table.
    
        Note 'T' is a special case, as it is used as the separator in ISO
              8601 date and time of day representation. */
    
    static table const military_table[] =
    {
        { "A", tZONE, -HOUR ( 1) },
        { "B", tZONE, -HOUR ( 2) },
        { "C", tZONE, -HOUR ( 3) },
                  :  :  :
        { "Y", tZONE,  HOUR (12) },
        { "Z", tZONE,  HOUR ( 0) },
        { NULL, 0, 0 }
    };
    

    And, indeed, if you run date in debug mode, you can see what’s happening (output trimmed for conciseness):

    pax> for i in {A..I} {K..Z} ; do
    ...>     date -d $i --debug 2>&1 | awk -vZ=$i '/parsed zone/{print "  "Z":"$5}'
    ...> done
      A:UTC-01
      B:UTC-02
        :  :
      T:UTC+07
        :  :
      Z:UTC+00
    

    Most letters apply a timezone offset based on military timezones, hence the T that would normally be used to separate date and time in ISO format is being handled differently here, as specifying UTC+7, explaining the ten-hour difference between that and your server at UTC-3.

    The bottom line is that you need to use an input format that’s acceptable to date. That’s as simple as (in this case) replacing the T with a space, as per the following transcript:

    pax:~> ### First, the problematic one.
    pax:~> date +"%Y/%m/%d %H:%M" -d "20200819T1234" --debug
    date: parsed number part: (Y-M-D) 2020-08-19
    date: parsed zone part: UTC+07
    date: parsed number part: 12:34:00
    date: input timezone: parsed date/time string (+07)
    date: using specified time as starting value: '12:34:00'
    date: starting date/time: '(Y-M-D) 2020-08-19 12:34:00 TZ=+07'
    date: '(Y-M-D) 2020-08-19 12:34:00 TZ=+07' = 1597815240 epoch-seconds
    date: timezone: system default
    date: final: 1597815240.000000000 (epoch-seconds)
    date: final: (Y-M-D) 2020-08-19 05:34:00 (UTC)
    date: final: (Y-M-D) 2020-08-19 13:34:00 (UTC+08)
    2020/08/19 13:34
    
    pax:~> ### Then, the correct one.
    pax:~> date +"%Y/%m/%d %H:%M" -d "20200819 1234" --debug
    date: parsed number part: (Y-M-D) 2020-08-19
    date: parsed number part: 12:34:00
    date: input timezone: system default
    date: using specified time as starting value: '12:34:00'
    date: starting date/time: '(Y-M-D) 2020-08-19 12:34:00'
    date: '(Y-M-D) 2020-08-19 12:34:00' = 1597811640 epoch-seconds
    date: timezone: system default
    date: final: 1597811640.000000000 (epoch-seconds)
    date: final: (Y-M-D) 2020-08-19 04:34:00 (UTC)
    date: final: (Y-M-D) 2020-08-19 12:34:00 (UTC+08)
    2020/08/19 12:34
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search