Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: getting Date calculation in simpler way

by salva (Abbot)
on Apr 13, 2006 at 14:39 UTC ( #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
Node Status?
node history
Node Type: note [id://543102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2020-05-27 00:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (152 votes). Check out past polls.

    Notices?