Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl Monk, Perl Meditation
 
PerlMonks  

Generating basic time intervals between two times

by ryan (Pilgrim)
on May 20, 2001 at 14:36 UTC ( #81816=perlquestion: print w/ replies, xml ) Need Help??
ryan has asked for the wisdom of the Perl Monks concerning the following question:

I needed to get a list of strings representing time between two given PM times in specific intervals. So between 7pm and 8:20pm inclusive in 20 minute intervals you get: 7:00 7:20 7:40 8:00 8:20

I didn't use any Time libs becuase I found them slow for repetitive parsing plus i wanted to nut it out myself.

Just wondering what other ways people may go about this task while keeping the code fairly readable and not overly nested?

#!/usr/bin/perl $t1 = '7:30'; #minimum time $t2 = '11:45'; #maximum time @t1 = split(':', $t1); @t2 = split(':', $t2); $interval = 15; #may be (10,15,20,30) #create an array of intervals for (0..(60/$interval)-1) { $mins[@mins]=(($_*$interval) ? $_*$interval : '00'); } #loop for number of hours requested for ($t1[0]..$t2[0]) { #loop for number of intervals in array foreach $mins (@mins) { #print if min <= result <= max if ($_<=$t2[0]&&$mins<=$t2[1]) { print "$_:$mins\n" if (!($_==$t1[0]&&$mins<$t1[1])) } } }

Comment on Generating basic time intervals between two times
Download Code
Re: Generating basic time intervals between two times
by jorg (Friar) on May 20, 2001 at 15:26 UTC
    Converting the hh24:mm format into seconds since midnight allows for greater simplification and modularisationg of the code.
    use strict; use Data::Dumper; ################################################# #converts a string hh24:mm into seconds equivalent since midnight sub convert_to_seconds { my $time = shift; my ($hours, $minutes) = split ':', $time; return $hours * 3600 + $minutes * 60; } ################################################# #converts a seconds since midnight into hh24:mm format sub convert_to_hh24mm { my $time = shift; my $hours = int ($time / 3600); my $minutes = ($time % 3600) / 60; return sprintf("%02d:%02d", $hours, $minutes); } ################################################# #takes a begin and end time (in seconds since midnight) #and an interval (in seconds as well) as arguments sub generate_intervals { my ($begin, $end, $interval) = @_; my @intervals; while ($end > $begin) { push @intervals, $begin; $begin += $interval; } return @intervals; } ################################################# # M A I N # my $begintime = '18:20'; my $endtime = '22:30'; my $interval = 15; my @intervals = generate_intervals( convert_to_seconds($begintime), co +nvert_to_seconds($endtime), $interval * 60); foreach (@intervals){ print Dumper(convert_to_hh24mm($_) ); }
    This outputs :

    E:\>perl -w test.pl
    $VAR1 = '18:20';
    $VAR1 = '18:35';
    $VAR1 = '18:50';
    $VAR1 = '19:05';
    $VAR1 = '19:20';
    $VAR1 = '19:35';
    $VAR1 = '19:50';
    $VAR1 = '20:05';
    $VAR1 = '20:20';
    $VAR1 = '20:35';
    $VAR1 = '20:50';
    $VAR1 = '21:05';
    $VAR1 = '21:20';
    $VAR1 = '21:35';
    $VAR1 = '21:50';
    $VAR1 = '22:05';
    $VAR1 = '22:20';

    Jorg

    "Do or do not, there is no try" -- Yoda
Re: Generating basic time intervals between two times
by mikfire (Deacon) on May 20, 2001 at 20:38 UTC
    Just for grins, you could use something like this. Time::Local is a core module that translates a string representation of time into seconds - think of it as a reverse of localtime.
    #!/usr/bin/perl -w use strict; use Time::Local; my $start = "7:30"; my $end = "11:45"; my $interval = 15; # Assuming time is in a 24-hour format and adjusting for my timezone : +) my ($start_sec,$end_sec) = map { /(\d+):(\d+)/;timelocal( 0, $2, $1, 1 +, 1,1970 ) } $start, $end; # In case the times cross the midnight boundary $end_sec += 86400 if ( $start_sec > $end_sec ); my $int_sec = $interval * 60; while ( $start_sec <= $end_sec ) { printf "%d:%02d\n", (localtime($start_sec))[2,1]; $start_sec += $int_sec; }
    I use timelocal to translate the given times into the correct seconds - including timezone adjustments. I chose the Epoch for no particular reason. After that, I simply let localtime do its thing.
    mikfire
Re: Generating basic time intervals between two times
by ryan (Pilgrim) on May 21, 2001 at 08:41 UTC
    Thanks for your ideas. I noticed later on a flaw in my code, i left out an extra check, making it one if statement longer.

    I know it is a common comment on here to use what already exists, which in my case is the Date and Time libs. They certainly make my problem easier :) They just proved very slow comparatively for this particular problem when it is scaled higher. I think for the sake of avoiding omitting more logical flaws the Libs may be more the go however.

    Working out epoch seconds manually without the Libs seems to avoid some of the processing overhead, thanks jorg.

    It's 174 of one and 14 1/2 dozen of the other really.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://81816]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-04-18 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls