1# we need to comment this out or PAUSE might index it 2# pack age DateTime::Format::Apache; 3 4use DateTime::Format::Builder ( 5 parsers => { 6 parse_datetime => { 7 strptime => '%e/%b/%Y:%H:%M:%S %z', 8 9 # params => [qw( day month year hour minute second time_zone )], 10 # regex => qr{ ^ 11 # (\d+)/(\w{3})/(\d{4}) 12 # : 13 # (\d\d):(\d\d):(\d\d) 14 # \s 15 # ([+-]\d{4}) 16 # $ }x, 17 # postprocess => sub { 18 # my %args = @_; 19 # $args{parsed}{month} = month_to_num( $args{parsed}{month} ); 20 # 1; 21 # }, 22 }, 23 }, 24); 25 26sub month_to_num { 27 my $wanted = shift; 28 my %months; 29 my $lang = DateTime::Language->new( language => 'en' ); 30 my $i; 31 $months{$_} = ++$i for @{ $lang->month_abbreviations }; 32 return $months{$wanted}; 33} 34 35sub format_datetime { 36 my ( $self, $dt ) = @_; 37 return $dt->strftime("%e/%b/%Y:%H:%M:%S %z"); 38} 39 40package main; 41 42my $parser = DateTime::Format::Apache->new(); 43 44my @dates = ( '27/Feb/2003:19:45:11 -0400', '27/Apr/2003:19:45:11 -0400' ); 45 46for my $date (@dates) { 47 my $dt 48 = $parser->parse_datetime($date)->set_time_zone('Australia/Sydney'); 49 print "$date => ", $dt->datetime, " => ", $parser->format_datetime($dt), 50 "\n"; 51} 52