[Rd] Bug with `[<-.POSIXlt` on specific OSes

Davis Vaughan d@v|@ @end|ng |rom r@tud|o@com
Thu Oct 6 15:33:27 CEST 2022


Hi all,

I have found another POSIXlt bug while I've been fiddling around with it.
This one only appears on specific OSes, because it has to do with the fact
that the `gmtoff` field is optional, and isn't always used on all OSes. It
also doesn't seem to be specific to r-devel, I think it has been there
awhile.

Here is the bug:

```
x <- as.POSIXlt(as.POSIXct("2013-01-31", tz = "America/Chicago"))

# Oh no!
x[1] <- NA
#> Error in x[[n]][i] <- value[[n]] : replacement has length zero
```

If you look at the objects, you can see that `x` has a `gmtoff` field, but
`NA` (when converted to POSIXlt, which is what `[<-.POSIXlt` does) does not:

```
unclass(x)
#> $sec
#> [1] 0
#>
#> $min
#> [1] 0
#>
#> $hour
#> [1] 0
#>
#> $mday
#> [1] 31
#>
#> $mon
#> [1] 0
#>
#> $year
#> [1] 113
#>
#> $wday
#> [1] 4
#>
#> $yday
#> [1] 30
#>
#> $isdst
#> [1] 0
#>
#> $zone
#> [1] "CST"
#>
#> $gmtoff
#> [1] -21600
#>
#> attr(,"tzone")
#> [1] "America/Chicago" "CST"             "CDT"

unclass(as.POSIXlt(NA))
#> $sec
#> [1] NA
#>
#> $min
#> [1] NA
#>
#> $hour
#> [1] NA
#>
#> $mday
#> [1] NA
#>
#> $mon
#> [1] NA
#>
#> $year
#> [1] NA
#>
#> $wday
#> [1] NA
#>
#> $yday
#> [1] NA
#>
#> $isdst
#> [1] -1
#>
#> attr(,"tzone")
#> [1] "UTC"
```

The problem seems to be that `[<-.POSIXlt` assumes that if the field was
there in `x` then it must also be there in `value`:
https://github.com/wch/r-source/blob/e10a971dee6a0ab851279c183cc21954d66b3be4/src/library/base/R/datetime.R#L1303-L1304

But this isn't the case for the `NA` value that was converted to POSIXlt.

I can't reproduce this on my personal Mac, but it affects the Linux, Mac,
and Windows machines we use for the lubridate CI checks through GitHub
Actions.

Thanks,
Davis

	[[alternative HTML version deleted]]



More information about the R-devel mailing list