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 without # date is ambiguous) ( $et, $ct ) = map { $_ < $st ? $_ + ONE_DAY : $_ } ( $et, $ct ); # Now we don't technically need to verify that $ct >= $st, but semantically it # doesn't hurt, and probably aids understanding of the algorithm, in case # 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";