Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

GMT to PST format

by Anonymous Monk
on Jun 18, 2010 at 11:34 UTC ( #845350=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a file which is tab delimited, it contains the GMT date.
Please tell me how to convert from GMT to PST time and if the date and time is of 2 days ago from the current date store the lines in a file or else remove the line. - - [15/Jun/2010:23:30:34 +0000] - - - [16/Jun/2010:23:30:34 +0000] -

Replies are listed 'Best First'.
Re: GMT to PST format
by ikegami (Pope) on Jun 18, 2010 at 16:12 UTC
    use DateTime qw( ); my @months = qw( ... ); my $month_lkup = map { $months[$_] => $_+1 } 0..$#months; my $ref_dt = DateTime->today()-subtract( days => 2 ); while (<>) { my ($date) = /^(?:\S+\s+){3}\[([^\]]*)\]/ or next; my ($d,$m,$y,$H,$M,$S) = $date =~ /\w+/g; $m = $month_lkup{$m}; my $dt = DateTime->new( year => $y, month => $m, day => $d, hour => $H, minute => $M, second => $S, time_zone => 'UTC', ); $dt->set_time_zone('local'); $dt->truncate( to => 'day' ); print if $dt == $ref_dt; }


Re: GMT to PST format
by Anonymous Monk on Jun 18, 2010 at 12:01 UTC
      I ran into a similar issue. The problem could be that you are using Date::Manip version 6 syntax, and that you are using a perl older than 5.10. In this case, only Date::Manip version 5 syntax is allowed. So the solution is to upgrade to using a newer perl, such as 5.10, or change the syntax of your code to use version 5 of Date::Manip. See the man pages such as: html/site/lib/Date/Manip/DM5.html html/site/lib/Date/Manip.html gmoore777
Re: GMT to PST format
by Krambambuli (Curate) on Jun 19, 2010 at 10:18 UTC
    Using Date::Manip and ssuming your local time is PST, you could use something like
    #!/usr/bin/perl use strict; use warnings; use Date::Manip; my $date_manip_object = new Date::Manip::Date; $date_manip_object->parse( '2 days ago' ); $date_manip_object->convert( 'GMT' ); my $gmt_ref = $date_manip_object->printf( '%Y%m%d') ; while (<DATA>) { my $err = $date_manip_object->parse_format('.*?\\[%d/%b/%Y:%T\s+%z +\\].*', $_); next if $err; # skip (?) lines that do not match the date format my $input_line_date = $date_manip_object->printf( '%Y%m%d') ; if ($input_line_date eq $gmt_ref) { print; } } __DATA__ - - [15/Jun/2010:23:30:34 +0000] - - - [16/Jun/2010:23:30:34 +0000] -
      Unfortunately we don't have installed this module.
      Can't locate object method "new" via package "Date::Manip::Date" (perh +aps you forgot to load "Date::Manip::Date"?)
      I am getting the error as above. Please tell me is there any other way to do this.
        If you can, install Date::Manip from CPAN. Otherwise, try a solution with one of the modules you have installed, maybe DateTime.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://845350]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2017-04-24 20:33 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (445 votes). Check out past polls.