[Rd] A potential POSIXlt->Date bug introduced in r-devel

Davis Vaughan d@v|@ @end|ng |rom r@tud|o@com
Wed Oct 5 23:04:11 CEST 2022


Hi all,

I think I have discovered a bug in the conversion from POSIXlt to Date that
has been introduced in r-devel.

It affects lubridate, but surprisingly didn't cause test failures there.
Instead it caused test failures in users of lubridate, like slider, arrow,
and admiral (see https://github.com/tidyverse/lubridate/issues/1069), and
at least in slider I have been asked by CRAN to correct this issue before
2022-10-16.

In r-devel we get the following:

```
data <- list(
  sec = 0,
  min = 0L,
  hour = 0L,
  mday = 31L,
  mon = c(0L, NA, 2L),
  year = 113L,
  wday = 4L,
  yday = 30L,
  isdst = 0L
)

x <- .POSIXlt(xx = data, tz = "UTC")
x
#> [1] "2013-01-31 UTC" NA               "2013-03-31 UTC"

# Looks right
as.POSIXct(x)
#> [1] "2013-01-31 UTC" NA               "2013-03-31 UTC"

# Weird, where is the `NA`?
as.Date(x)
#> [1] "2013-01-31" "1970-01-01" "2013-03-31"
```

The POSIXlt object is length 3, but is only partially filled out. The other
elements are all recycled to length 3 upon conversion to POSIXct or Date.
But when converting to Date, we lose the `NA` value. I think the
`as.Date()` conversion seems inconsistent with the `as.POSIXct()`
conversion.

It looks like this comes up because the conversion to Date now defaults to
using `sec` if any of the date-like fields are `NA_INTEGER`, but this means
the `NA` in the `mon` field is ignored.
https://github.com/wch/r-source/blob/e10a971dee6a0ab851279c183cc21954d66b3be4/src/main/datetime.c#L1293-L1295

Thanks all,
Davis Vaughan

	[[alternative HTML version deleted]]




More information about the R-devel mailing list