linux bash - Parse date in custom format

后端 未结 4 909
挽巷
挽巷 2020-12-07 01:19

I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the G

4条回答
  •  天涯浪人
    2020-12-07 01:59

    You may use libdatetime-format-flexible-perl.

    #!/usr/bin/perl
    use DateTime::Format::Flexible;
    my $date_str = "So 22 Dez 2013 07:29:35 CET";
    $parser = DateTime::Format::Flexible->new;
    my $date = $parser->parse_datetime($date_str);
    print $date
    

    Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:

    printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
    

    Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.

    $ export LC_TIME=ru_RU.UTF-8
    $ NOW="$(date "+%c")"
    $ date --date="$NOW" '+%d.%m.%Y'
    22.12.2013
    

提交回复
热议问题