Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `as.POSIXlt("1994-01-01")` return an error?

Tags:

r

posixlt

Why does as.POSIXlt("1994-01-01") return the following error?

Error in as.POSIXlt.character("1994-01-01"):
  character string is not in a standard unambiguous format

I got the same error in R 4.5.1 and 4.3.2, Windows 11 x64.

This is very strange since as.POSIXlt("1993-12-31") and as.POSIXlt("1994-01-02") do not return an error, but rather the expected result (an object of class "POSIXlt" "POSIXt").

> charToRaw("1994-01-01")
 [1] 31 39 39 34 2d 30 31 2d 30 31

> as.POSIXct("1994-01-01")
Error en as.POSIXlt.character(x, tz, ...): 
 la cadena de caracteres no está en un formato estándar inequívoco

> as.POSIXct("1994-01-01", tz="UTC")
 [1] "1994-01-01 UTC"

I have tried in fresh sessions on two different PCs. I have tried doing it in Terminal (outside of RStudio/Positron) and get the same results. And no, it wasn't a character issue, because it appears when I type the text directly. It is very strange.

Results for sessionInfo():

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=Spanish_Mexico.utf8  LC_CTYPE=Spanish_Mexico.utf8    LC_MONETARY=Spanish_Mexico.utf8
[4] LC_NUMERIC=C                    LC_TIME=Spanish_Mexico.utf8

time zone: America/Lima
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] ggplot2_3.5.2 tidyr_1.3.1   dplyr_1.1.4

loaded via a namespace (and not attached):
 [1] labeling_0.4.3     RColorBrewer_1.1-3 utf8_1.2.6         R6_2.6.1           tidyselect_1.2.1
 [6] farver_2.1.2       magrittr_2.0.3     gtable_0.3.6       glue_1.8.0         tibble_3.3.0
[11] pkgconfig_2.0.3    generics_0.1.4     lifecycle_1.0.4    cli_3.6.5          scales_1.4.0
[16] grid_4.5.1         vctrs_0.6.5        withr_3.0.2        compiler_4.5.1     purrr_1.1.0
[21] rstudioapi_0.17.1  tools_4.5.1        pillar_1.11.0      crayon_1.5.3       rlang_1.1.6
like image 953
Wencheng Lau-Medrano Avatar asked Dec 02 '25 03:12

Wencheng Lau-Medrano


1 Answers

I can reproduce the error on my machine with

as.POSIXct("1994-01-01", tz="America/Lima")
# Error in as.POSIXlt.character(x, tz, ...) : 
#   character string is not in a standard unambiguous format

If we check timezonedb for the America/Lima time zone we can see several adjustments made to that timezone over the years, including this one

After Friday, 31 December, 1993 11:59:59 PM: Clocks were moved forward to become Saturday, 01 January, 1994 01:00:00 AM

This basically means that midnight did not exist on Jan 1, 1994 in that timezone. That day began at 1am. When R is parsing date/times values like "1994-01-01", it implicitly assumes that you are using midnight as the time for that date.

Because humans can change how time zones work, there are just some date/time combinations that aren't valid. And you found one of the few where that's the case. It doesn't look like any change have been made to the timezone since 1994 so hopefully things won't be as messy in the future.

If you don't need time information, you can parse the value as just a date with

as.Date("1994-01-01")
like image 75
MrFlick Avatar answered Dec 04 '25 21:12

MrFlick