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

Thanks to anyone that helped me with this question!
I was able to convert the string 199812312230 to 12/31/1998/22/30 using the script below.
#!/usr/bin/perl use strict; use warnings; use Time::Local; use Date::Manip qw(ParseDate UnixDate); while (<IN>) { $date = ParseDate($_); if (!$date) { warn "Bad date string: $_\n"; next; } else { ($year, $month, $day, $hour, $min ) = UnixDate($date, "%Y", "% +m", "%d", "%H", "%M"); print "Date was $month/$day/$year/$hour/$min\n"; } }

Replies are listed 'Best First'.
Re: changing a string to a useable date time
by Limbic~Region (Chancellor) on Feb 01, 2005 at 15:16 UTC
    cpiety,
    ...yyyymmddhhmmss or any other useable date time format

    You are going to need to define what you mean since it already appears to be in the first format. Likely, you probably want to use Time::Local, and then use strftime from POSIX to format however you want. Both modules are part of the core.

    #!/usr/bin/perl use strict; use warnings; use Time::Local; use POSIX 'strftime'; my $str = '19980730010031'; my $epoch = get_epoch( $str ); print strftime("%m/%d/%Y", localtime($epoch)); sub get_epoch { my $str = shift; my ($yr, $mon, $day, $hr, $min, $sec) = unpack('A4A2A2A2A2A2', $st +r); $mon--; return timelocal($sec, $min, $hr, $day, $mon, $yr); }
    Incidently, $epoch is number of seconds since epoch so it is easy to calculate the difference between two stamps just by subtracting and dividing by the appropriate number of seconds (86_400 if you want the difference in days).

    Cheers - L~R

      Hi L-R, I get the following message when I run your code:

      Perl_programs/2005_perl_programs> perl LR_date_time_convert.pl
      Global symbol "$st" requires explicit package name at LR_date_time_convert.pl line 22.
      Execution of LR_date_time_convert.pl aborted due to compilation errors.
      ## OPEN input file for reading open (IN, "1998_wind_date_time.txt") or die "Couldn't open 1998_wind_d +ate_time.txt "; ## OPEN input file for reading open (OUT, ">1998_wind_formatted_data2.out") or die "Couldn't open 199 +8_wind_formatted_data2.out"; #!/usr/bin/perl use strict; use warnings; use Time::Local; use POSIX 'strftime'; my $str = '19980730010031'; my $epoch = get_epoch( $str ); print strftime("%m/%d/%Y", localtime($epoch)); sub get_epoch { my $str = shift; my ($yr, $mon, $day, $hr, $min, $sec) = unpack('A4A2A2A2A2A2', $st +r); $mon--; return timelocal($sec, $min, $hr, $day, $mon, $yr); }
        cpiety,
        That is a copy/paste error on your part. You should use the 'download code' link instead. As you can see
        my ($yr, $mon, $day, $hr, $min, $sec) = unpack('A4A2A2A2A2A2', $st +r);
        Should be
        my ($yr, $mon, $day, $hr, $min, $sec) = unpack('A4A2A2A2A2A2', $str);

        Cheers - L~R

Re: changing a string to a useable date time
by demerphq (Chancellor) on Feb 01, 2005 at 15:34 UTC
    s/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/$1-$2-$3 $4:$5:$6/ or die "Unknown date format '$_'" for @dates;
    ---
    demerphq

Re: changing a string to a useable date time
by friedo (Prior) on Feb 01, 2005 at 15:03 UTC
    I'm not sure I understand your question. The string you show, "1998073001003", already seems to be in YYYYMMDDhhmmss format.

    If you want to extract the parts of that date, I reccomend simply using substr or unpack. Unpack would be faster but only works of the date fields are of constant width, which they appear to be.

      Hi, If I try say to plot this in Excel I can't change the format to recognize it as a datetime. I would also like to keep track of changing times (i.e. how many consectutive hours do I have) and I think I will have trouble doing comparisons when I change days in the current format. Its somewhat hard to read now too for manually scanning the data. Thanks for the help friedo, charles