Parse Date in Bash

前端 未结 10 1652
灰色年华
灰色年华 2020-11-29 02:17

How would you parse a date in bash, with separate fields (years, months, days, hours, minutes, seconds) into different variables?

The date format is: YYYY-MM-D

10条回答
  •  -上瘾入骨i
    2020-11-29 02:36

    I had a different input time format, so here is a more flexible solution.

    Convert dates in BSD/macOS

    date -jf in_format [+out_format] in_date
    

    where the formats use strftime (see man strftime).

    For the given input format YYYY-MM-DD hh:mm:ss:

    $ date -jf '%Y-%m-%d %H:%M:%S' '2017-05-10 13:40:01'
    Wed May 10 13:40:01 PDT 2017
    

    To read them into separate variables, I'm taking NVRAM's idea, but allowing you to use any strftime format:

    $ date_in='2017-05-10 13:40:01'
    
    $ format='%Y-%m-%d %H:%M:%S'
    
    $ read -r y m d H M S <<< "$(date -jf "$format" '+%Y %m %d %H %M %S' "$date_in")"
    
    $ for var in y m d H M S; do echo "$var=${!var}"; done
    y=2017
    m=05
    d=10
    H=13
    M=40
    S=01
    

    In scripts, always use read -r.

    In my case, I wanted to convert between timezones (see your /usr/share/zoneinfo directory for zone names):

    $ format=%Y-%m-%dT%H:%M:%S%z
    
    $ TZ=UTC date -jf $format +$format 2017-05-10T02:40:01+0200
    2017-05-10T00:40:01+0000
    
    $ TZ=America/Los_Angeles date -jf $format +$format 2017-05-10T02:40:01+0200
    2017-05-09T17:40:01-0700
    

    Convert dates in GNU/Linux

    On a Mac, you can install the GNU version of date as gdate with brew install coreutils.

    date [+out_format] -d in_date
    

    where the out_format uses strftime (see man strftime).

    In GNU coreutils' date command, there is no way to explicitly set an input format, since it tries to figure out the input format by itself, and stuff usually just works. (For detail, you can read the manual at coreutils: Date input formats.)

    For example:

    $ date '+%Y %m %d %H %M %S' -d '2017-05-10 13:40:01'
    2017 05 10 13 40 01
    

    To read them into separate variables:

    $ read -r y m d H M S <<< "$(date '+%Y %m %d %H %M %S' -d "$date_in")"
    

    To convert between timezones (see your /usr/share/zoneinfo directory for zone names), you can specify TZ="America/Los_Angeles" right in your input string. Note the literal " chars around the zone name, and the space character before in_date:

    TZ=out_tz date [+out_format] 'TZ="in_tz" in_date'
    

    For example:

    $ format='%Y-%m-%d %H:%M:%S%z'
    
    $ TZ=America/Los_Angeles date +"$format" -d 'TZ="UTC" 2017-05-10 02:40:01'
    2017-05-09 19:40:01-0700
    
    $ TZ=UTC date +"$format" -d 'TZ="America/Los_Angeles" 2017-05-09 19:40:01'
    2017-05-10 02:40:01+0000
    

    GNU date also understands hour offsets for the time zone:

    $ TZ=UTC date +"$format" -d '2017-05-09 19:40:01-0700'
    2017-05-10 02:40:01+0000
    

提交回复
热议问题