PRS has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to convert the time from eg:'Fri Jun 16 18:04:03 2012' to '06 16 18:04:03 2012' format. Basically i dont want the day, i want the date time year i.e,.everything in numerics only.'Fri Jun 16 18:04:03' is the output of localtime im using in my script.

Suggest any command or anything

Replies are listed 'Best First'.
Re: Conversion of Time
by daxim (Curate) on Jun 20, 2012 at 14:36 UTC
    See DateTime and DateTime::Format::Strptime.
    use DateTime::Format::Strptime qw(); print DateTime::Format::Strptime ->new( locale => 'en', pattern => '%a %b %d %T %Y', on_error => 'croak', )->parse_datetime('Fri Jun 16 18:04:03 2012') ->strftime('%m %d %T %Y'); # '06 16 18:04:03 2012'
    2012-06-16 was a Saturday, not a Friday. If you regularly get wrong weekdays in your input datetime stamps, remove the pattern %a, it will work fine without it. If you can change the output format, please use a standard format such as DateTime::Format::RFC3339 instead! This will increase interoperability.
    use DateTime::Format::RFC3339 qw(); use DateTime::Format::Strptime qw(); print DateTime::Format::RFC3339 ->new ->format_datetime(DateTime::Format::Strptime ->new( locale => 'en', pattern => '%a %b %d %T %Y', on_error => 'croak', )->parse_datetime('Fri Jun 16 18:04:03 2012') ); # '2012-06-16T18:04:03+00:00'
Re: Conversion of Time
by stevieb (Canon) on Jun 20, 2012 at 14:21 UTC

    Although someone may have a more direct approach, I do know that DateTime can help you with this.

    Instead of using localtime, use my $time = DateTime->now(); and then render the output however you want with the object.

Re: Conversion of Time
by Ratazong (Monsignor) on Jun 20, 2012 at 14:35 UTC

    You could also do it by hand, using a regex to extract the relevant info from your input-string and some hash-lookup to translate the month to a number:

    my %convert = ( "Jan" => "01", "Feb" => "02", "Mar" => "03", "Jun" => +"06" ,); # ... you get the idea my $in = "Fri Jun 16 18:04:03 2012"; $in =~ /.*?\s(\w\w\w)\s(.*)$/; my $out = $convert{$1} . " " . $2;

    HTH, Rata
Re: Conversion of Time
by frozenwithjoy (Priest) on Jun 20, 2012 at 14:41 UTC
    Besides DateTime, I also recommend taking a look at localtime. The first example shows you how to break the output into its components. Also, search for strftime in POSIX to see how to format date/time strings.

      LOL, I was just revisiting this very same issue only two days ago, when I decided I wanted to whip up a handful of scripts I could call from web pages to print my preferred date format(s). :)

      #!/usr/bin/perl -T print "Content-type: text/html\n\n"; use POSIX qw(strftime); # see man strftime(3) for format options use strict; my $gmtstring = strftime "%F %H:%I:%S %z", localtime; print $gmtstring; # This will produce: 2012-06-20 09:09:27 -0700 (your GMT may be differ +ent) # The following appends TZ, instead of GMT #!/usr/bin/perl -T print "Content-type: text/html\n\n"; use POSIX qw(strftime); # see man strftime(3) for format options use strict; my $tzstring = strftime "%F %H:%I:%S %Z", localtime; print $tzstring; # Which produces: 2012-06-20 09:09:57 PDT (again, your TZ may be diffe +rent)

      Short, & sweet -- just the way I like 'em. Maybe you will too. :)
      Hope this helps!

      #!/usr/bin/perl -Tw
      
      use strict;
      use perl::always;
      
      my $perl_version( 5.12.4 );
      
      print $perl_version;
Re: Conversion of Time
by Anonymous Monk on Jun 20, 2012 at 14:28 UTC