Problem overview
In R, calling as.POSIXlt("1994-01-01")
sometimes produces the error:
"character string is not in a standard unambiguous format"
Meanwhile, as.POSIXlt("1993-12-31")
and as.POSIXlt("1994-01-02")
work fine. Developers wondering why a single date fails discovered this happens in certain time zones. For example, in the America/Lima time zone, there was a one‑hour clock change at midnight on 1 January 1994: clocks jumped from 23:59:59 on 31 December 1993 directly to 01:00:00 on 1 January 1994. In other words, midnight on that day never existed, so R can’t parse it.
Why this happens
R’s as.POSIXlt
assumes midnight (00:00:00) when you supply a date without a time. If your system’s default time zone is set to America/Lima
, the date “1994-01-01” corresponds to a non‑existent local time because the clocks jumped forward one hour at 00:00. R therefore throws an error since the string is ambiguous. Other dates before and after are valid because those midnights existed.
Solution
There are two ways to resolve this error:
- Specify a time zone where midnight existed: Passing a
tz
argument forces R to interpret the date in a specific time zone. For example:
as.POSIXlt("1994-01-01", tz = "UTC")
# [1] "1994-01-01 UTC"
UTC
has no daylight‑saving shifts on that date, so the date parses normally. You can also choose any other time zone without a gap at midnight.
- Convert to a
Date
and then to POSIX: If you don’t care about time zones, convert to aDate
and then to POSIX. Usingas.Date()
avoids time‑zone interpretation. When you later need POSIX, supply an explicit time zone:
d <- as.Date("1994-01-01")
# Convert to POSIX with a safe timezone
as.POSIXlt(d, tz = "UTC")
This two‑step approach bypasses the ambiguous midnight altogether.
Conclusion
The ambiguous-date error occurs because R cannot represent a date at a time that never existed in the local time zone. Specifying a different tz
or converting via as.Date
are simple ways to avoid the error. Always be mindful of daylight‑saving or historical time‑zone changes when working with dates and times