note
salva
without using any module...
<c>
#!/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, $seconds);
}
__DATA__
# YYYYMMDDhhmmss seconds
20060413020000 900
20060413024500 900
20061231235700 900
</c>
543058
543058