Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comparing time strings from a list of HH:MM:SS times

by slugger415 (Monk)
on Aug 10, 2022 at 18:38 UTC ( [id://11146084]=perlquestion: print w/replies, xml ) Need Help??

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

Hello esteemed PerlMonks,

I have a list of time strings in the HH:MM:SS format. I want to add each to a variable set with localtime and compare it to a timestamp later in the script. I'm having trouble understanding how to add that HH:MM:SS string to the localtime variable.

#! /usr/bin/perl use Time::Piece; use strict; my(@times) = ("00:05:21","00:08:05","00:10:33"); my $startTime = localtime(); print "Start: ", $startTime, $/; foreach my $t (@times) { sleep 2; my $newTime = localtime(); my $ss = $startTime + $t; ### this is where I need advice if($ss > $newTime){ print "\$ss is greater.\n"; ### execute some functions here } print "Newtime: ", $newTime, $/; my $diff = $newTime - $startTime; print $diff, $/;

The $diff part works but not the addition of $t, how do I add that time to it? Obviously I need to convert it to something Time::Piece understands.

Thank you.

Replies are listed 'Best First'.
Re: Comparing time strings from a list of HH:MM:SS times
by hippo (Bishop) on Aug 10, 2022 at 20:23 UTC
    The $diff part works but not the addition of $t, how do I add that time to it? Obviously I need to convert it to something Time::Piece understands.

    You cannot add a timestamp to another timestamp, that makes no logical sense. I'll therefore assume that what you have in this line:

    my(@times) = ("00:05:21","00:08:05","00:10:33");

    are not timestamps but durations. Therefore all you need to do is to convert these durations into seconds which you can then add to a Time::Piece object. You can either parse this yourself or else use something like Time::Duration::Parse.

    #!/usr/bin/env perl use strict; use warnings; use Time::Piece; use Time::Seconds; my @durations = (qw/00:05:21 00:08:05 00:10:33/); my $start = localtime; for my $d (@durations) { print "$start plus $d = "; my ($hh, $mm, $ss) = split /:/, $d; my $t = $start + $hh * ONE_HOUR + $mm * ONE_MINUTE + $ss; print "$t\n"; }

    🦛

      that worked perfectly, thank you!

Re: Comparing time strings from a list of HH:MM:SS times
by Your Mother (Archbishop) on Aug 10, 2022 at 19:51 UTC

    Your trouble is double-headed. There is no automatic conversion of the “%H:%M:%S” you’re using to objects. And you’re using times that are free of any dates (and timezones) so even if they were objects, the convention would turn your times into dates based on the zero epoch, like so: Thu Jan  1 00:05:21 1970.

    For this to work and make sense you will have to add date information to your @times. If you can’t you might be able to find a heuristic to add the date based on the limits of how far it can be from “now.” But that sounds like a horrible approach.

    If you can get the dates, it’s easy to turn them into Time::Piece objects and then do your math on them.

    my $time = Time::Piece->strptime("Tue Aug 9 15:39:01 GMT 2022", "%a %b %e %H:%M:%S %Z %Y");

      thank you that makes sense!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found