How can I parse relative dates with Perl?

前端 未结 4 1513
失恋的感觉
失恋的感觉 2021-01-02 05:34

I\'d love to know if there is a module to parse \"human formatted\" dates in Perl. I mean things like \"tomorrow\", \"Tuesday\", \"next week\", \"1 hour ago\".

My re

4条回答
  •  感情败类
    2021-01-02 05:48

    you may also find it interesting to look at the DateTime::Format family, specifically DateTime::Format::Natural. once you've parsed your date/time into a DateTime object, you can manipulate and evaluate it in a whole bunch of different ways.

    here's a sample program:

    use strict;
    use warnings;
    
    use DateTime::Format::Natural;
    
    my( $parser ) = DateTime::Format::Natural->new;
    
    while ( <> ) {
    
        chomp;
        my( $dt ) = $parser->parse_datetime( $_ );
    
        if ( $parser->success ) {
    
            print join( ' ', $dt->ymd, $dt->hms ) . "\n";
        }
        else {
    
            print $parser->error . "\n";
        }
    }
    

    output:

    tomorrow  
    2008-11-18 21:48:49  
    next Tuesday  
    2008-11-25 21:48:53  
    1 week from now  
    2008-11-24 21:48:57  
    1 hour ago  
    2008-11-17 20:48:59  
    

    TMTOWTDI :)

    -steve

提交回复
热议问题