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

Hi,
I have a start time and end time and also the current time.
(i.e.) starttime = '9:01PM'
endtime='3:30AM';
currenttime='1:10AM';
My issue is i have to check whether the currenttime is between the start and end time.
Any help is much appreciated.

Replies are listed 'Best First'.
Re: current time between two time
by davido (Cardinal) on Mar 04, 2014 at 17:50 UTC

    Perl ships with Time::Piece as part of its core distribution. The documentation for that module describes date calculations and comparisons.

    It's not the shiniest new module for date/time manipulation, but it comes with Perl (so you can't use the no external modules excuse), it will work, and its documentation is adequate for your purposes.

    Update: One problem you may run into is that your times are ambiguous because they're not associated with a date. Is "endtime" always after "starttime"? Should "currenttime" be coerced to the next day if it's less than "starttime"? What rules should be followed for the coersion?

    use Time::Piece; # Core module use Time::Seconds; # Core module use constant TIME_FORMAT => '%I:%M%p'; my( $starttime, $endtime, $currenttime ) = ( '9:01PM', '3:30AM', '1:10AM' ); # Create Time::Piece objects my( $st, $et, $ct ) = map { Time::Piece->strptime( $_, TIME_FORMAT ); } ( $starttime, $endtime, $currenttime ); # Coerce end time to always be after start time. # Coerce current time to always fall after start time (because time wi +thout # date is ambiguous) ( $et, $ct ) = map { $_ < $st ? $_ + ONE_DAY : $_ } ( $et, $ct ); # Now we don't technically need to verify that $ct >= $st, but semanti +cally it # doesn't hurt, and probably aids understanding of the algorithm, in c +ase # one wants to deal with the ambiguity of a missing date differently. print $ct->strftime(TIME_FORMAT), " is ", ( $ct >= $st && $ct <= $et ) ? "in" : "out of", " range\n";

    This reminds me of a bug that used to exist in the Buffalo LinkStation NAS firmware. The device could be set to turn off at a given time, and back on again at a given time. This seemed like a great idea to save power; have it do an incremental backup at 10:00pm, shut off at 01:00, and back on again at 08:00. But the bug was that setting a sleep time of 01:00 was seen by the firmware to be before the wakeup time of 08:00, and it would be rejected as soon as one clicked, "apply" because it didn't make sense to sleep a device that hadn't been awakened yet. Their bug fix was to allow sleep times of (for example) 27:30. Apparently testing hadn't considered this scenario, which is strange because it seemed like the most obvious use for the feature.


    Dave

Re: current time between two time
by karlgoethebier (Abbot) on Mar 04, 2014 at 17:56 UTC
Re: current time between two time
by vinoth.ree (Monsignor) on Mar 04, 2014 at 18:31 UTC

    Hi,

    Time::Period also helpful in your case,

    As document in CPAN, The inPeriod function determines if a given time falls within a given period. inPeriod returns 1 if the time does fall within the given period, 0 if not, and -1 if inPeriod detects a malformed time or period.


    All is well
Re: current time between two time
by Anonymous Monk on Mar 05, 2014 at 00:27 UTC
    Given that you have at your disposal a full-featured date/time manipulation library that is known to pass all of the great-many self tests that its author has devised to throw at it ... and furthermore given that this library has been deemed good-enough to be accepted into the Perl Core Distribution ... I cordially suggest that one would be rather a Fool not to use it. Pick an arbitrary date, then use this known-good library to calculate three date+time values (start, end, current ...), then simply "do the math." Done!!™

    You have plenty of headaches that are worthy of your one-of-a-kind creativity. This is not one of them.
Re: current time between two time
by clueless newbie (Curate) on Mar 04, 2014 at 19:27 UTC
    If they're always in the same day you could just convert to minutes.
    #!/usr/bin/perl for (qw(9:01PM 3:30AM 1:10AM)) { if (m{^(\d+):(\d\d)(AM|PM)}) { print "$_ is @{[$1*60+$2+($3 eq 'PM' ? 12*60 : 0)]} minutes\n" +; }; };

      I think that even his example times are not in the same day. Start time 9:01PM, end time 3:30AM? Either the end time is the next day, or it falls before the start time, which doesn't make much sense.


      Dave

        I still like the approach to just compare minutes. If the end-time is before the start-time, you could always correct it by adding 24 hours.