Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is the simplest way to get unix time:</p> <pre><code>use Time::Local; timelocal($second,$minute,$hour,$day,$month-1,$year); </code></pre> <p>Note the reverse order of the arguments and that January is month 0. For many more options, see the <a href="https://metacpan.org/pod/DateTime" rel="noreferrer">DateTime</a> module from CPAN. </p> <p>As for parsing, see the <a href="https://metacpan.org/pod/Date::Parse" rel="noreferrer">Date::Parse</a> module from CPAN. If you really need to get fancy with date parsing, the <a href="https://metacpan.org/pod/Date::Manip" rel="noreferrer">Date::Manip</a> may be helpful, though its own documentation warns you away from it since it carries a lot of baggage (it knows things like common business holidays, for example) and other solutions are much faster.</p> <p>If you happen to know something about the format of the date/times you'll be parsing then a simple regular expression may suffice but you're probably better off using an appropriate CPAN module. For example, if you know the dates will always be in YMDHMS order, use the CPAN module <a href="https://metacpan.org/pod/DateTime::Format::ISO8601" rel="noreferrer">DateTime::Format::ISO8601</a>.</p> <hr> <p>For my own reference, if nothing else, below is a function I use for an application where I know the dates will always be in YMDHMS order with all or part of the "HMS" part optional. It accepts any delimiters (eg, "2009-02-15" or "2009.02.15"). It returns the corresponding unix time (seconds since 1970-01-01 00:00:00 GMT) or -1 if it couldn't parse it (which means you better be sure you'll never legitimately need to parse the date 1969-12-31 23:59:59). It also presumes two-digit years XX up to "69" refer to "20XX", otherwise "19XX" (eg, "50-02-15" means 2050-02-15 but "75-02-15" means 1975-02-15).</p> <pre><code>use Time::Local; sub parsedate { my($s) = @_; my($year, $month, $day, $hour, $minute, $second); if($s =~ m{^\s*(\d{1,4})\W*0*(\d{1,2})\W*0*(\d{1,2})\W*0* (\d{0,2})\W*0*(\d{0,2})\W*0*(\d{0,2})}x) { $year = $1; $month = $2; $day = $3; $hour = $4; $minute = $5; $second = $6; $hour |= 0; $minute |= 0; $second |= 0; # defaults. $year = ($year&lt;100 ? ($year&lt;70 ? 2000+$year : 1900+$year) : $year); return timelocal($second,$minute,$hour,$day,$month-1,$year); } return -1; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload