Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: getting Date calculation in simpler way

by johngg (Canon)
on Apr 13, 2006 at 14:25 UTC ( #543096=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2020-05-30 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?