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

Hi,
I am trying to find and easier way (and one that works) of converting a time string like "08:30:12" to a time in seconds (for the current day) since 1,Jan 1970.
Here is the code I have so far:
Note: for some reason the hour is off by one. I dont know why.
#!/usr/bin/perl use strict; use warnings; use POSIX; my $time = "08:30:12"; my (@time) = split(/:/, $time); my (@lt) = localtime(); my $secs = mktime($time[2], $time[1], $time[0], $lt[3], $lt[4], $lt[5] +, $lt[6]. $lt[7], $lt[8]); # Lets check if the $secs return the correct time... my $today = strftime "%D, %T", localtime($secs); print $today , "\n"; exit;
Does anyone know a better way to do this?

Cheers,
Reagen

Replies are listed 'Best First'.
Re: Convert time to todays time
by graff (Chancellor) on Jun 08, 2004 at 09:09 UTC
    You could try Time::Local (which I believe is part of the core distribution). It would go something like this:
    use strict; use Time::Local; my $time = "08:30:10"; my ($dy,$mo,$yr) = (localtime)[3..5]; my ($hr,$mi,$se) = split(/:/,$time); my $utime = timelocal( $se, $mi, $hr, $dy, $mo, $yr ); print $utime,$/; # update: forgot to add the confirmation: print scalar localtime( $utime ),$/;
Re: Convert time to todays time
by Jaap (Curate) on Jun 08, 2004 at 11:30 UTC
    Try DateTime. It is new and it's the swiss army knife of date/time conversion:
    use DateTime; $dt = DateTime->new( year => 1964, month => 10, day => 16, hour => 16, minute => 12, second => 47, nanosecond => 500000000, time_zone => 'Asia/Taipei', ); $dt = DateTime->from_epoch( epoch => $epoch ); $dt = DateTime->now; # same as ( epoch => time() ) $year = $dt->year; $month = $dt->month; # 1-12 - also mon $day = $dt->day; # 1-31 - also day_of_month, mday $dow = $dt->day_of_week; # 1-7 (Monday is 1) - also dow, wday $hour = $dt->hour; # 0-23 $minute = $dt->minute; # 0-59 - also min $second = $dt->second; # 0-61 (leap seconds!) - also sec
Re: Convert time to todays time
by ysth (Canon) on Jun 08, 2004 at 09:55 UTC
    It's a really good idea to specify which things you actually want to import:
    use POSIX qw/mktime strftile/;
    POSIX has a lot of symbols that take a fair amount of time and memory (as well as potentially conflicting with other stuff you have).

      And if you don't have POSIX installed, you can try Time::Piece instead:

      use Time::Piece; my $today = localtime->strptime(localtime->strftime('%F'), '%F'); my $hours = localtime->strptime('08:30:12','%I:%M:%S'); print $today->epoch + $hours->epoch;

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Convert time to todays time
by gnork (Scribe) on Jun 08, 2004 at 10:44 UTC
    The off by one could be daylight saving related.
    @t = localtime(time); print "Daylight saving active" if $t[8] eq 1;
    Gnork

    cat /dev/world | perl -e "(/(^.*? \?) 42\!/) && (print $1))"
    errors->(c)
Re: Convert time to todays time
by Anonymous Monk on Jun 08, 2004 at 22:19 UTC
    Time::ParseDate is great #!/usr/bin/perl use Time::ParseDate; my $time = "08:30:12"; my $seconds = parsedate("today $time");