in reply to changing a string to a useable date time

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

Replies are listed 'Best First'.
Re^2: changing a string to a useable date time
by cpiety (Novice) on Feb 01, 2005 at 16:28 UTC
    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

        thanks, it runs now. I guess Perl ignores white space but not "+r"!
        Thanks for your patience.
        I am not a programmer and decided to try and learn PERL to process some data.
        Frankly....It is kicking my butt.