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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.