Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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.
10.1.10.178 - - [15/Jun/2010:23:30:34 +0000] - 10.1.10.178 - - [16/Jun/2010:23:30:34 +0000] -

Comment on GMT to PST format
Download Code
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 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; }

    Untested.

Re: GMT to PST format
by Krambambuli (Deacon) 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__ 10.1.10.178 - - [15/Jun/2010:23:30:34 +0000] - 10.1.10.178 - - [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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://845350]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-21 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls