No timezones;
This much is (sort of) trivial to fix. Add ([+-]\d{4}) to the end of your regex, and then include - $tz/100*3600 - $tz%100*60 at the end of the expression.
sub str2epoch {
my( $d, $m, $y, $H, $M, $S, $tz ) = $_[0] =~
m/^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d) ([+-]\d{4
+})/
or die "Bad format $_[0]";
my $leaps = int( ($y - 1970) / 4 + 0.5 );
(((($y-1970)*365 +$leaps+MONTHS->{$m}+($d-1))*24 +$H)*60 +$M)*60 +
+$S
- ($tz < 0 ? -1 : 1)*(substr($tz,1,2)*3600 + substr($tz,
+3)*60);
}
- No leap seconds;
- No daylight savings;
- Only works for another 87 years.
See my answer for timegm, which does slow things down, but is a bit more robust. ++ for pure-Perl that out-performs the "efficient" Time::Local routines and works in most all cases, though!
Edit: Updated $tz calc for fractional hours.
-
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.
|