#--------------------------------------------------------------- # Time Intervals, used by 'parseInterval2()' # what everything is worth in seconds #--------------------------------------------------------------- my %time_intervals = ( 'days' => (60 * 60 * 24), 'hours' => (60 * 60), 'minutes' => 60, 'seconds' => 1 ); #--------------------------------------------------------------- # # parseInterval2 - Subset of Time::Interval functionality # Only parses 'seconds' intervals. # #--------------------------------------------------------------- =pod =item parseInterval2 ( I, I ) Return a hash describing how many 'hours', 'minutes', 'seconds' are represented by the I argument. If I is 1, then the time is returned in a wordy manner similar to Time::Interval. If I is 2, then the time is returned in a DD:HH:MM:SS format. If I is 3, then the time is returned in an HH:MM:SS format where the days value is added into the hours position. Otherwise, return a reference to a hash. =cut #--------------------------------------------------------------- sub parseInterval2 { my ($seconds, $String) = @_; $String = 0 if !defined $String; #do the thang my %time = ( 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0 ); foreach ("days","hours","minutes","seconds") { while ($seconds >= $time_intervals{$_}) { $time{$_} ++; $seconds -= $time_intervals{$_}; } } if ($String == 1) { # [[[DD days,] HH hours,] MM minutes,] SS seconds my @temp; my $show = 0; foreach ("days","hours","minutes","seconds") { $show = 1 if $time{$_} > 0; next if !$show; push (@temp, "$time{$_} $_"); } return (join (", ", @temp)); } elsif ($String == 2) { # [DD:]?HH:MM:SS my $out = ""; $out .= sprintf("%02d:", $time{'days'}) if $time{'days'}; $out .= sprintf("%02d:%02d:%02d", @time{qw(hours minutes seconds)}); return $out; } elsif ($String == 3) { # [H]*HH:MM:SS return sprintf("%02d:%02d:%02d", (24 * $time{'days'}) + $time{'hours'}, @time{qw(minutes seconds)}); } else { #return a data structure return (\%time); } }