[Rd] bug and patch: strptime first-of-month error in (possibly unsupported use of) "%j" format (PR#9577)

jbrzusto at fastmail.fm jbrzusto at fastmail.fm
Wed Mar 21 15:58:35 CET 2007


Full_Name: John Brzustowski
Version: R-devel-trunk
OS: linux (problem under Windows too)
Submission from: (NULL) (74.101.124.238)


(This bug was discovered by Phil Taylor, Acadia University.)
I'm not sure from reading the documentation whether strptime(x, "%j") is meant
to be supported, but if so, there is a bug which prevents it from working on the
first day of months after January:

> strptime(31:33, "%j")
[1] "2007-01-31" NA           "2007-02-02"

# the full extent of R's taunting
strptime(1 + cumsum(c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30)), "%j")
 [1] NA NA NA NA NA NA NA NA NA NA NA
>

The problem is an edge-case comparison in datetime.c:glibc_fix().  This
generates a date like "Jan 32", which validate_tm() catches and NAs.
(Values of field tm->tm_yday start at 0, not 1.)
=================================================================================
PATCH:

--- R/src/main/datetime.c	(revision 40860)
+++ R/src/main/datetime.c	(working copy)
@@ -796,7 +796,7 @@
     if(tm->tm_yday != NA_INTEGER) {
 	/* since we have yday, let that take precedence over mon/mday */
 	int yday = tm->tm_yday, mon = 0;
-	while(yday > (tmp = days_in_month[mon] +
+	while(yday >= (tmp = days_in_month[mon] +
 		      ((mon==1 && isleap(1900+tm->tm_year))? 1 : 0))) {
 	    yday -= tmp;
 	    mon++;



More information about the R-devel mailing list