localtime and mktime

  • Thread starter mhearne808[insert-at-sign-here]gmail[insert-dot-he
  • Start date
M

mhearne808[insert-at-sign-here]gmail[insert-dot-he

I think have a fundamental misunderstanding of one of either
localtime() or mktime(). Here's a snippet of code that illustrates
what I find confusing:

#############################
use Time::Local;
use POSIX;

$unixtime4 = time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($unixtime4);
$unixtime5 = mktime($sec,$min,$hour,$mday,$mon,$year);

print STDERR "Time4: $unixtime4\n";
print STDERR "Time5: $unixtime5\n";
#############################

I would expect that $unixtime4 and $unixtime5 would be identical -
however, they are different by exactly 1 hour (on Mac OS X and Linux
RHEL).

Can anyone explain to me why this is?

--Mike
 
C

chanio

mhearne808[insert-at-sign-here]gmail[insert-dot-here]com ha escrito:
I think have a fundamental misunderstanding of one of either
localtime() or mktime(). Here's a snippet of code that illustrates
what I find confusing:

#############################
use Time::Local;
use POSIX;

$unixtime4 = time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($unixtime4);
$unixtime5 = mktime($sec,$min,$hour,$mday,$mon,$year);

print STDERR "Time4: $unixtime4\n";
print STDERR "Time5: $unixtime5\n";
#############################

I would expect that $unixtime4 and $unixtime5 would be identical -
however, they are different by exactly 1 hour (on Mac OS X and Linux
RHEL).

Can anyone explain to me why this is?

--Mike
Right!
But please, check your system time configuration.
Your script works well.
I get the following result...
Time4: 1183067516
Time5: 1183067516
See?
Find out your locale with POSIX
here, everything Ok (and I live in Argentina, TZ -03)
alberto
 
P

Peter J. Holzer

mhearne808[insert-at-sign-here]gmail[insert-dot-here]com ha escrito:
I think have a fundamental misunderstanding of one of either
localtime() or mktime(). Here's a snippet of code that illustrates
what I find confusing:

$unixtime4 = time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($unixtime4);
$unixtime5 = mktime($sec,$min,$hour,$mday,$mon,$year); [...]
I would expect that $unixtime4 and $unixtime5 would be identical -
however, they are different by exactly 1 hour (on Mac OS X and Linux
RHEL).

Can anyone explain to me why this is?

--Mike
Right!
But please, check your system time configuration.
Your script works well. [...]
here, everything Ok (and I live in Argentina, TZ -03)

So you currently don't have DST - try it again in six months.
I assume that Mike lives on the northern hemisphere so he currently has
DST.

The problem is that mktime takes 9 arguments, not 6:

mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)

The last 3 are optional, but the default value of 0 for isdst is not
particularly useful. Either you know whether you want DST or not, then
you should explicitely specify it. Or you want mktime to guess, then you
have to specify isdst as -1:

unixtime5 = mktime($sec, $min, $hour, $mday, $mon, $year, 0, 0, -1);

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top