[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] mktime works ?



Berndt Wischnewski wrote:
> 
> Hi ,
> 
> I try to calculate the time difference in seconds of some time data with
> first mktime and then difftime like

> #!/usr/bin/perl 
> use POSIX; 
> 
> $sec1 = 0; 
> $min1 = 11; 
> $hour1 = 10; 
> $day1 = 1; 
> $month1 = 0; 
> $year1 = 99; 
> $sec2 = 0; 
> $min2 = 9; 
> $hour2 = 11; 
> $day2 = 1; 
> $month2 = 0; 
> $year2 = 99; 
> 
> $secs1 = POSIX::mktime($sec1, $min1, $hour1, $day1, $year1); 
> $secs2 = POSIX::mktime($sec2, $min2, $hour2, $day2, $year2); 
> $result = POSIX::difftime($secs1, $secs2); 

How did you get this to run? You aren't supplying all the needed params
to mktime. Try running with the -w switch.

> but the result is always zero. Even the example from the POSIX.pod file
> #Calendar time for December 12, 1995, at 10:30 am.
> $time_t = POSIX::mktime( 0, 30, 10, 12, 11, 95 );
> print "Date = ", POSIX::ctime($time_t);
> returns:
> Date = Fri Jan 1 00:00:00 1904
> which seams not very correct.
> 
> Any idea?

If you look at the return value of $time_t, it is a negative number.
MacPerl is using signed integers where unix perl would be using unsigned
integers. 

I don't know if the following will work universally, but it works on the
example and your code.

#Calendar time for December 12, 1995, at 10:30 am.
$time_t = POSIX::mktime( 0, 30, 10, 12, 11, 95 );
print "Date = ", POSIX::ctime($time_t);
#try it another way below
$newtime = $time_t & -1 #bitwise AND with negative 1
print "Date = ", POSIX::ctime($newtime);

This yields Tue Dec 12 10:30:00 1995

Here is your code, modified to yield a result: 
#!/usr/bin/perl -w
use POSIX; 

$sec1 = 0; 
$min1 = 11; 
$hour1 = 10; 
$day1 = 1; 
$month1 = 0; 
$year1 = 99; 
$sec2 = 0; 
$min2 = 9;
$hour2 = 11; 
$day2 = 1;
$month2 = 0; 
$year2 = 99; 
$secs1 = POSIX::mktime($sec1, $min1, $hour1, $day1, $month1, $year1); 
$secs2 = POSIX::mktime($sec2, $min2, $hour2, $day2, $month2, $year2);
$secs1 = $secs1 & -1;
$secs2 = $secs2 & -1;

print "Date = ", POSIX::ctime($secs1);
print "Date = ", POSIX::ctime($secs2);

$result = POSIX::difftime($secs1, $secs2);
print $result;
__END__

Yields this:
Date = Fri Jan  1 10:11:00 1999
Date = Fri Jan  1 11:09:00 1999
-3480

That's about the number of seconds difference in an hour, right?

Hope that helps,
Geoff

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org