Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

POSIX::mktime() or Time::Local::timelocal()

by japhy (Canon)
on Nov 01, 2001 at 04:20 UTC ( [id://122464]=perlmeditation: print w/replies, xml ) Need Help??

Hmm... is there a difference between the two functions? I ask because I recently saw blakem using the POSIX function instead of the Time::Local function.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

  • Comment on POSIX::mktime() or Time::Local::timelocal()

Replies are listed 'Best First'.
Re: POSIX::mktime() or Time::Local::timelocal()
by broquaint (Abbot) on Nov 01, 2001 at 16:16 UTC
    I think functionally they both do the same thing. The only difference that I can see is that one is writen in C (POSIX::mktime()) and is fully POSIX compliant, and the other is written in Perl (Time::Local::timelocal()), and possibly not. So I'm guessing that timelocal() is more portable, but mktime() is more standard (but that is a guess ;o)
    HTH

    broquaint

      Aren't both POSIX::* and Time::Local in the core?

Re: POSIX::mktime() or Time::Local::timelocal()
by Anonymous Monk on Jan 05, 2002 at 00:03 UTC
    timelocal cannot handle the ambiguous hour (from 1 to 2am at the end of daylight savings time), because it has no $isdst parameter. Example:
    use strict; use Time::Local; use POSIX; my ($time,$time2,$sec,$min,$hour,$mday,$mon,$year,$isdst); my ($wday, $yday, $hm); $sec = 59; $min = 59; $hour = 0; $mon = 9; $mday = 28; $year = 2001; $time = timelocal($sec,$min,$hour,$mday,$mon,$year); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tim +e); $hm = sprintf("%02d/%02d/%04d %02d:%02d:%02d",$mon+1,$mday,$year+1900, +$hour,$min,$sec); print "Local time: $hm and DST is $isdst\n"; # Skip ahead one hour. $time += 3600; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tim +e); $hm = sprintf("%02d/%02d/%04d %02d:%02d:%02d",$mon+1,$mday,$year+1900, +$hour,$min,$sec); print "Local time: $hm and DST is $isdst\n"; $time2 = timelocal($sec,$min,$hour,$mday,$mon,$year); if ($time == $time2) { print "timelocal conversion correct -- timestamp is $time\n"; } else { print "timelocal conversion error -- $time converted to $time2\n"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t +ime2); $hm = sprintf("%02d/%02d/%04d %02d:%02d:%02d",$mon+1,$mday,$year+19 +00,$hour,$min,$sec); print "(Local time: $hm and DST is $isdst)\n"; } $time2 = POSIX::mktime($sec,$min,$hour,$mday,$mon,$year,0,0,1); if ($time == $time2) { print "mktime conversion correct -- timestamp is $time\n"; } else { print "mktime conversion error -- $time converted to $time2\n"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t +ime2); $hm = sprintf("%02d/%02d/%04d %02d:%02d:%02d",$mon+1,$mday,$year+19 +00,$hour,$min,$sec); print "(Local time: $hm and DST is $isdst)\n"; } # -------- end of example -----------
    This prints:
    Local time: 10/28/2001 00:59:59 and DST is 1 Local time: 10/28/2001 01:59:59 and DST is 1 timelocal conversion error -- 1004255999 converted to 1004259599 (Local time: 10/28/2001 01:59:59 and DST is 0) mktime conversion correct -- timestamp is 1004255999

      Please note that timelocal() range-checks its parameters, and cannot be used to 'normalise' a date which mktime() can do.

      For example, mktime() can be passed the month=14. This means March in the following year.

      I.e., 11=Dec, 12=Jan next year, 13=Feb next year, 14=Mar next year.

      Another example is you have today's date, then you can add 18 to months to get the epoch-seconds for the year, month & day in 18 month's time.

      Negative values can also be used, for example, -1 for the day, which is effectively the last day of the previous month.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://122464]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found