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

In reply to Re: Generating basic time intervals between two times by jorg
in thread Generating basic time intervals between two times by ryan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.