Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: getting Date calculation in simpler way

by salva (Canon)
on Apr 13, 2006 at 14:39 UTC ( [id://543102]=note: print w/replies, xml ) Need Help??


in reply to getting Date calculation in simpler way

without using any module...
#!/usr/bin/perl use strict; use warnings; sub time_to_str { my ($sec, $min, $hour, $mday, $mon, $year) = localtime(shift); return sprintf "%04d%02d%02d%02d%02d%02d", $year+1900, $mon+1, $mday +, $hour, $min, $sec; } sub find_time { my $tstr = shift; my $max = 2**31; my $min = 0; while ($max != $min) { my $mid = int (($max + $min) / 2); my $midstr = time_to_str $mid; if ($tstr gt $midstr) { $min = $mid + 1; } else { $max = $mid; } } return $max; } sub add_seconds { my ($timestr, $seconds) = @_; my $time = find_time $timestr; return time_to_str($time + $seconds) } while (<DATA>) { next if /^\s*(?:#.*)?$/; my ($date, $seconds) = split; printf "%s + %ds = %s\n", $date, $seconds, add_seconds($date, $secon +ds); } __DATA__ # YYYYMMDDhhmmss seconds 20060413020000 900 20060413024500 900 20061231235700 900

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-16 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found