Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

formatting datetime with strftime

by mreaves (Initiate)
on Jul 22, 2013 at 14:07 UTC ( #1045652=perlquestion: print w/ replies, xml ) Need Help??
mreaves has asked for the wisdom of the Perl Monks concerning the following question:

I need to covert the format of a datetime variable found in a log file from format: "05/1/13 16:45" to format: "May 01 2013 16:45:00 GMT". I presume the approach would be to parse the text datetime into a Perl datetime variable and format the results appropriately. I can parse & load the variable, however I having trouble formatting it. Any suggestions would be greatly appreciated.
#!perl -w use POSIX 'strftime'; use Date::Calc qw(:all); $datetimestring = "4/5/13 16:09"; #split datetimestring into date & time ($date_raw, $time_raw) = split(/ /, $datetimestring); print "the date_raw is: $date_raw \n"; #split the date into MM DD YY ($m0_raw, $d0_raw, $y0_raw) = split(/\//, $date_raw); $m_raw = sprintf ("%02d", $m0_raw); print "the m_raw is: $m_raw \n"; $d_raw = sprintf ("%02d", $d0_raw); print "the d_raw is: $d_raw \n"; $y_raw = 2000 + $y0_raw; print "the y_raw is: $y_raw \n\n"; print "the time_raw is: $time_raw \n"; #split the time_raw into HH MM ($hh_raw, $mm_raw) = split(/:/, $time_raw); print "the hh_raw is: $hh_raw \n"; print "the mm_raw is: $mm_raw \n\n"; $s0_raw = 0; $ss_raw = sprintf ("%02d", $s0_raw); #load a Perl datetime variable using mktime $datetime1 = Mktime($y_raw,$m_raw,$d_raw, $hh_raw, $mm_raw, $ss_ra +w); print "the loaded datetime variable is: $datetime1 \n"; #format the datetime variable to match the require format MON dd yyyy +HH:MM:SS GMT $time2 = strftime(%b %d, %y %T, $m_raw, $d0_raw, $y_raw, $hh_raw, +$mm_raw, $ss_raw); print "the formatted date time is: $time2 \n";
The error result I get is:
Argument "d" isn't numeric in modulus (%) at playdate.pl line 36. Illegal modulus zero at playdate.pl line 36.

Comment on formatting datetime with strftime
Select or Download Code
Re: formatting datetime with strftime
by ramlight (Friar) on Jul 22, 2013 at 14:23 UTC
    You forgot to put quotes around the format string in strftime:
    $time2 = strftime("%b %d, %y %T", $m_raw, $d0_raw, $y_raw, $hh_raw, $m +m_raw, $ss_raw);
    It works for me with the quotes added:
    the date_raw is: 4/5/13 the m_raw is: 04 the d_raw is: 05 the y_raw is: 2013 the time_raw is: 16:09 the hh_raw is: 16 the mm_raw is: 09 the loaded datetime variable is: 1365192540 the formatted date time is: Jan 07, 01 21:05:04
      Thanks very much ramlight... I'm on a Win8 machine... even with the quotes, I'm getting....
      the date_raw is: 4/5/13 the m_raw is: 04 the d_raw is: 05 the y_raw is: 2013 the time_raw is: 16:09 the hh_raw is: 16 the mm_raw is: 09 the loaded datetime variable is: 1365192540 the formatted date time is: %b %d, %y %T
      is there a switch I missed or something else ???
        Show your exact code. It looks like you are using:
        $time2 = "%b %d, %y %T"; print "the formatted date time is: $time2 \n";
Re: formatting datetime with strftime
by mtmcc (Hermit) on Jul 22, 2013 at 14:38 UTC
    When in doubt, check this out!
Re: formatting datetime with strftime
by Loops (Curate) on Jul 22, 2013 at 14:53 UTC

    Perhaps you could employ Date::Manip instead of doing it all by hand

    use Date::Manip; print UnixDate('05/1/13 16:45 GMT', '%b %d %Y %T %Z');
    Output:
    May 01 2013 16:45:00 GMT
Re: formatting datetime with strftime
by GeneralElektrix (Acolyte) on Jul 22, 2013 at 15:40 UTC
    #!perl -w use strict; use DateTime::Format::CLDR; my $datetimestring = "4/5/13 16:09"; my $cldr1 = new DateTime::Format::CLDR( pattern => 'M/d/yy HH:mm', ); my $dt1 = $cldr1->parse_datetime($datetimestring); print $cldr1->format_datetime($dt1), "\n"; my $cldr2 = new DateTime::Format::CLDR( pattern => 'MMMM dd yyyy HH:mm:ss', locale => 'en_US', time_zone => 'GMT', ); print $cldr2->format_datetime($dt1), " GMT\n";
      I think I improved on this version of the code. Let's suppose the original time is in the 'America/New-York' time zone and you want it in GMT, here's the new recipe:
      #!perl -w use strict; use DateTime::Format::CLDR; my $datetimestring = "4/5/13 16:09"; my $cldr1 = new DateTime::Format::CLDR( pattern => 'M/d/yy HH:mm', time_zone => 'America/New_York', ); my $dt1 = $cldr1->parse_datetime($datetimestring); print $cldr1->format_datetime($dt1), "\n"; my $cldr2 = new DateTime::Format::CLDR( pattern => 'MMMM dd yyyy HH:mm:ss', locale => 'en_US', time_zone => 'America/New_York', ); $dt1->set_time_zone('GMT'); print $cldr2->format_datetime($dt1), " GMT\n";
      Result:
      4/5/13 16:09 April 05 2013 20:09:00 GMT
Re: formatting datetime with strftime
by Cristoforo (Deacon) on Jul 22, 2013 at 15:51 UTC
    Time::Piece, (was first released with perl v5.9.5), would solve this simply.
    #!/usr/bin/perl use strict; use warnings; use Time::Piece; my $datetimestring = "4/5/13 16:09"; my $t = Time::Piece->strptime($datetimestring, "%m/%d/%y %H:%M"); print $t->strftime("%b %d, %Y %H:%M:%S");
    Prints Apr 05, 2013 16:09:00

    Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045652]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-12-22 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (133 votes), past polls