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), convert_to_seconds($endtime), $interval * 60); foreach (@intervals){ print Dumper(convert_to_hh24mm($_) ); }