Bug 14813

Summary: Perl POSIX::strftime bugfeature
Product: Base System Reporter: Valentin Nechayev <netch>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Valentin Nechayev 1999-11-10 19:20:00 UTC
GMTime of unixtime 941284799 is 30th of October, 1999, 11:59:59.

The following code:

#!/usr/bin/perl
use POSIX;
print strftime("%Y %m %d %H %M %S", gmtime(941284799)), "\n";

prints

1999 10 30 12 59 59

(note environment information: timezone is Europe/Kiev)

This effect (adding of 1 hour) appeared in perl 5.00503 and did not exist in
perl 5.00502.

Diff of ${perl}/ext/POSIX/POSIX.xs between 5.00502 and 5.00503
contains following:

==={
@@ -3591,7 +3603,7 @@
        RETVAL

 char *
-strftime(fmt, sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
+strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1
        char *          fmt
        int             sec
        int             min
@@ -3617,8 +3629,45 @@
            mytm.tm_wday = wday;
            mytm.tm_yday = yday;
            mytm.tm_isdst = isdst;
+           (void) mktime(&mytm);
            len = strftime(tmpbuf, sizeof tmpbuf, fmt, &mytm);
===}

Well, test it and see that mktime() normalizes time according to
local time zone (Europe/Kiev in our case):

==={
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

void f( int x )
{
   char buf[ 200 ];
   struct tm stm;
   bzero( &stm, sizeof stm );
   stm.tm_year = 99; stm.tm_mon = 9; stm.tm_mday = 30; stm.tm_hour = 11;
   stm.tm_min = 59; stm.tm_sec = 59; stm.tm_isdst = 0;
   if( x )
      mktime( &stm );
   bzero( buf, sizeof buf );
   strftime( buf, sizeof buf, "%Y %m %d %H %M %S", &stm );
   puts( buf );
   printf( "%d %d %d %d %d %d\n", stm.tm_year, stm.tm_mon, stm.tm_mday,
         stm.tm_hour, stm.tm_min, stm.tm_sec, stm.tm_isdst );
}

int main() {
   f( 0 ); f( 1 ); return 1;
}
===}

output is:

==={
netch@burka:~/prog/tiny/2>./3
1999 10 30 11 59 59
99 9 30 11 59 59 0
1999 10 30 12 59 59
99 9 30 12 59 59 1
===}

This mktime() behavior possibly correct and in any case accords to its man page:

==={
     On successful completion, the values of the tm_wday and tm_yday compo-
     nents of the structure are set appropriately, and the other components
     are set to represent the specified calendar time, but with their values
     forced to their normal ranges; the final value of tm_mday is not set un-
     til tm_mon and tm_year are determined.  Mktime() returns the specified
     calendar time; if the calendar time cannot be represented, it returns -1;
===}

But, the time in question was GMT time, not local time.

Thus, perl MUST NOT call mktime() in strftime() because strftime MUST
ONLY PRINT its data and MUST NOT have any opinion of its content because
it cannot know real time zone of the data.

Fix: Disable the mktime() call in POSIX::strftime. Also disable init_tm(),
whis is really localtime(time()) - IMHO the better solution for FreeBSD
in case of tm_gmtoff & tm_zone patameters is to set them to most safe value,
i.e. 0.

(note: spaces/tabs are incorrect in this diff)
==={
How-To-Repeat: 
See above.
Comment 1 Andrey A. Chernov freebsd_committer freebsd_triage 1999-11-11 23:04:23 UTC
On Wed, Nov 10, 1999 at 09:12:44PM +0200, Valentin Nechayev wrote:
> Disable the mktime() call in POSIX::strftime. Also disable init_tm(),
> whis is really localtime(time()) - IMHO the better solution for FreeBSD
> in case of tm_gmtoff & tm_zone patameters is to set them to most safe value,
> i.e. 0.

What about just replacing mktime() with timegm()? It seems to produce correct 
results, but I not check it.

-- 
Andrey A. Chernov
http://nagual.pp.ru/~ache/
MTH/SH/HE S-- W-- N+ PEC>+ D A a++ C G>+ QH+(++) 666+>++ Y
Comment 2 Valentin Nechayev 1999-11-12 06:12:03 UTC
Hello Andrey A. Chernov! 

 Thu, Nov 11, 1999 at 15:04:23, ache wrote about "Re: bin/14813: Perl POSIX::strftime bugfeature": 

> On Wed, Nov 10, 1999 at 09:12:44PM +0200, Valentin Nechayev wrote:
> > Disable the mktime() call in POSIX::strftime. Also disable init_tm(),
> > whis is really localtime(time()) - IMHO the better solution for FreeBSD
> > in case of tm_gmtoff & tm_zone patameters is to set them to most safe value,
> > i.e. 0.
> 
> What about just replacing mktime() with timegm()? It seems to produce correct 
> results, but I not check it.

No, it is bad also. In that variant, it will print correctly GMT time,
but incorrectly local time, possibly ;( (Consider variant, where timegm()
normalizes time with tm_isdst==1. What shall happen? If you know current
timegm() behavior, it can change.) Also, timegm() AFAIR exists not in all
unices.

IMHO, the only normal variant is to disable this call. There is another,
ideological basis of disabling: every subroutine must do its own work and
must produce minimum of side effects. Goal of strftime() is to print time
and date according to given format, it must not normalize or convert time.
If I ask it to print 31th of February, it must print this date literally.
If I want normalize data, I can do it by special normalizing call, i.e.,
mktime() or timegm() accordingly to my knowledge of this date origin.

--
NVA
Comment 3 Andrey A. Chernov freebsd_committer freebsd_triage 1999-11-13 20:09:37 UTC
State Changed
From-To: open->closed

Sligtly different fix applied