This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

strftime %W patch


[Please CC me on replies since I am not subscribed to this list.]

While tracking down why Cygwin Python's strftime regression test
failed, I uncovered a problem with the current newlib strftime() "%W"
implementation.  It seems just like people, newlib's strftime() does
not like Mondays. :,)

The first attachment is a small test program that demonstrates the
problem.

When run under Cygwin, we get the following:

    $ stest2
    37
    Mon Sep 17 16:13:02 2001

but when run under Red Hat 7.1, we get the following:

    $ stest2
    38
    Mon Sep 17 16:13:02 2001

as expected.

The second and third attachments are a patch and corresponding ChangeLog
that correct this problem.  Since newlib's strftime() "%U" behaved
correctly, I modeled the "%W" implementation after it.

After applying this patch, both the test program and Python's strftime
regression test ran correctly.

Thanks,
Jason
#include <time.h>
#include <stdio.h>

int main(void)
{
  struct tm time;
  char buf[256];
  time_t time2;

  time.tm_sec = 2;     /* seconds after the minute - [0,59] */
  time.tm_min = 13;    /* minutes after the hour - [0,59] */
  time.tm_hour = 16;   /* hours since midnight - [0,23] */
  time.tm_mday = 17;   /* day of the month - [1,31] */
  time.tm_mon = 8;     /* months since January - [0,11] */
  time.tm_year = 101;  /* years since 1900 */
  time.tm_wday = 1;    /* days since Sunday - [0,6] */
  time.tm_yday = 260;  /* days since January 1 - [0,365] */
  time.tm_isdst = 1;   /* daylight savings time flag */

  strftime(buf, sizeof(buf), "%W", &time);
  printf("%s\n", buf);

  time2 = mktime(&time);
  printf("%s", ctime(&time2));

  return(0);
}
Index: strftime.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/time/strftime.c,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 strftime.c
--- strftime.c	2000/02/17 19:39:51	1.1.1.1
+++ strftime.c	2001/09/13 03:22:22
@@ -362,9 +362,10 @@ _DEFUN (strftime, (s, maxsize, format, t
 	case 'W':
 	  if (count < maxsize - 2)
 	    {
+	      int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
 	      sprintf (&s[count], "%2.2d",
-		       (tim_p->tm_yday + ((8 -
-					   tim_p->tm_wday) % 7)) / 7);
+		       (tim_p->tm_yday + 7 -
+			wday) / 7);
 	      count += 2;
 	    }
 	  else
Thu Sep 13 08:49:49 2001  Jason Tishler <jason@tishler.net>

	* strftime.c (strftime): Fix "%W" implementation to properly handle
	Mondays too.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]