Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: getting Date calculation in simpler way

by johngg (Canon)
on Apr 13, 2006 at 14:25 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://543096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found