From bug-octave-request at bevo dot che dot wisc dot edu Wed Jan 20 00:07:59 1999 Subject: localtime(t) and gmtime(t) get wrong answers if t's fraction is >= 0.5 From: Samsung Lim To: bug-octave at bevo dot che dot wisc dot edu Date: Wed, 20 Jan 1999 15:06:35 +0900 Bug report for Octave 2.0.13 configured for sparc-sun-solaris2.5.1 Description: ----------- In Octave, localtime(t) and gmtime(t) get wrong answers if t's fraction is greater than or equal to 0.5. In that case, "sec" is rounded up, while "usec" exactly corresponds to the fractional part. Consequently, mktime(localtime(t))==t turns out to be false (0). Some example is given at the end of the description. It sure is wrong, but this is a delicate problem because it may not be wrong for someone who needs only "sec" not "usec". Anyway, this error comes from the "src/time.cc". The following is a part of "localtime" definition. I think "NINT (tmp)" has to be "int (tmp)". ### BEGIN CODE ### DEFUN_DLD (localtime, args, , ... snipped ... if (! error_state) { time_t timeval = NINT (tmp); double ip; double fraction = modf (tmp, &ip); retval = octave_value (mk_tm_map (localtime (&timeval), fraction)); } ... snipped again ... ### END CODE ### octave:1> lt=localtime(t=916802834.5) lt = { usec = 500000 year = 99 mon = 0 mday = 20 sec = 15 <================ shouldn't it be 14? zone = KST min = 27 wday = 3 hour = 12 isdst = 0 yday = 19 } octave:2> mktime(lt)==t ans = 0 Fix: --- ### BEGIN CODE ### DEFUN_DLD (localtime, args, , ... snipped ... if (! error_state) { time_t timeval = int (tmp); double ip; double fraction = modf (tmp, &ip); retval = octave_value (mk_tm_map (localtime (&timeval), fraction)); } ... snipped again ... ### END CODE ###