This is the mail archive of the newlib@sourceware.org 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]
Other format: [Raw text]

strptime fix for first_day


Hi!

While trying to fix [1] and before I realized that Cygwin had its own
strptime implementation, I found bugs in the newlib version...

First, the sub-expression "ret + 365 + is_leap_year (year) ? 1 : 0"
will always return 1 for anything later that 1970 (since is_leap_year
only ever returns 0 or 1, and ?: has lower precedence than +.)

Second, the old version, when called with e.g. 1972 basically
intended to do:
	(4 + 366) % 7 -> 6       (for the 366 days of 1972)
	(6 + 365) % 7 -> 0       (for the 365 days of 1971)
But that is bogus, it should be:
	(4 + 365) % 7 -> 5       (for the 365 days of 1971)
	(5 + 365) % 7 -> 6       (for the 365 days of 1970)

This patch fixes both problems.

Cheers,
Peter

[1] http://cygwin.com/ml/cygwin/2011-05/msg00159.html

2011-05-12  Peter Rosin  <peda@lysator.liu.se>

	* libc/time/strptime.c (first_day): Actually return the wday
	of the first day of the year.

Index: libc/time/strptime.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/time/strptime.c,v
retrieving revision 1.5
diff -u -p -r1.5 strptime.c
--- libc/time/strptime.c        22 Jan 2010 13:03:42 -0000      1.5
+++ libc/time/strptime.c        12 May 2011 07:17:35 -0000
@@ -78,8 +78,8 @@ first_day (int year)
 {
     int ret = 4;

-    for (; year > 1970; --year)
-       ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
+    while (--year >= 1970)
+       ret = (ret + 365 + is_leap_year (year)) % 7;
     return ret;
 }


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