http://qs1969.pair.com?node_id=543096


in reply to getting Date calculation in simpler way

I would be amazed if your bosses disallow use of modules that come as part of the standard Perl distribution. You need to use Time::Local to turn a date into seconds since the epoch. Other than using this module, the following script does pretty much as ff suggests.

use strict; use warnings; # Need this to turn date into seconds since epoch. Standard # part of Perl distribution. # use Time::Local; # Initialise date string, print it and then increment by # fifteen minutes and print ten times just to show it works. # our $dateStr = "200604132341"; print "$dateStr\n"; $dateStr = plus15($dateStr), print "$dateStr\n" for 1 .. 10; sub plus15 { # Get date string, set seconds to zero, pull the rest of # the date from the string with a match; # my $dateStr = shift; my $sec = 0; my ($year, $mth, $day, $hr, $min) = $dateStr =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)$/; # Get seconds since the epoch, note months start from zero for # January to 11 for December. Add 15 minutes-worth of seconds. # my $timeVal = timelocal($sec, $min, $hr, $day, $mth - 1, $year); $timeVal += 15 * 60; # Turn back in to separate parts, year, month etc. Note that # localtime() returns years since 1900. Construct a new date # string and return it. # my @timeParts = localtime($timeVal); my $newStr = $timeParts[5] + 1900 . sprintf("%02d", $timeParts[4] + 1) . sprintf("%02d", $timeParts[3]) . sprintf("%02d", $timeParts[2]) . sprintf("%02d", $timeParts[1]); return $newStr; }

I you can't even use this module then you are forced to increment the minutes, roll the hour and set back the minutes if they go over 59, etc., etc. Painful, but character building.

Best of luck,

JohnGG