My first impression is that sec2date is nothing more than POSIX::strftime, but with nonstandard string escapes (like %E and your definitions of %S and %s). Because of this, your code is going to be very confusing for anyone familiar with POSIX::strftime.

If all you wanted to do was get strftime-like behavior while also supporting milliseconds, it would be much easier to simply write a wrapper around stftime, something like this (untested)

use POSIX 'strftime'; ## similar to strftime but with milliseconds via "%q" sub sec2date { my ($fmt, $sec) = @_; ## fractional part of $sec, rounded to 3 digits my $milli = sprintf "%03.0f", 1000 * ($sec - int $sec); ## %q becomes our milliseconds $fmt =~ s/%q/$milli/g; strftime $fmt, localtime(int $sec); }
The same could be said for your date2sec code. Epoch calculation is not something I think you really want to do. I don't mind you writing your own regexes to extract the appropriate parts from your format, but I would be really suspicious of anyone's hand-rolled epoch second converter. Especially when you have a comment like this:
# convert data2seconds, should be good until the year 2100
"Should be good" ?? Yikes! Instead, I would use Time::Local to do the epoch-seconds conversion part. In your regex, extract the fractional seconds to another variable, send everything to timelocal, and then add on the fractional seconds at the end. Here is how you can use timelocal, even only having year+Julian. You see, it handles all the leap year calculation for you.
use Time::Local 'timelocal_nocheck'; ## given year+julian date, my $epoch = timelocal_nocheck $sec, $min, $hour, $julian, 0, $year-190 +0; ## or given y/m/d (could use normal "timelocal" here) my $epoch = timelocal_nocheck $sec, $min, $hour, $day, $mon-1, $year-1 +900; ## add milliseconds on at the end: $epoch .= ".$milli";
Those are my meta-comments about what you're trying to accomplish. As for your code specifically, there are a few things going on that seemed strange to me..

You have this kind of construct several times:

$sec = "0" x (2 - length($time)).$time ; $msec = "0" x (3 - length($msec)).$msec ;
This is written a little more clearly as:
$sec = sprintf "%02d", $time; $msec = sprintf "%03d", $msec;
You have a zillion lines like this:
($format = $format ) =~ s/\%Y/$year/g ; # xxxx ($format = $format ) =~ s/\%m/$month/g ; # 1-12 ($format = $format ) =~ s/\%d/$day/g ; # 01-31
Why $format = $format ? Looks like you saw a snippet somewhere that said ($new = $old) =~ s///, but that's completely redundant here as you aren't saving the old value. Better would be:
for ($format) { s/%Y/$year/g; s/%m/$month/g; ... }
But even better yet would be to put all your date information in a hash, and you can replace all those s/// commands with just one:
my %data = ( Y => $year, m => $month, d => $day, ... ); $format =~ s/\%([A-Za-z])/ exists $data{$1} ? $data{$1} : "%$1" /ge;
Update: fixed sprintf format in first code snippet

blokhead


In reply to Re: Wrote my own date parser by blokhead
in thread Wrote my own date parser by jeanluca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.