Error using POSIXlt in R: Character string is not in a standard unambiguous format

55 views Asked by At

I am attempting the run the following line of code in R:

    as.POSIXlt(c("28/7/2022  17:56:20", "28/3/2021  1:10:41 PM"), tryFormats = c("%d/%m/%Y %I:%M:%OS %p",
                                                               "%d/%m/%Y %H:%M:%OS"), optional = F)

This is giving me the following error:

    Error in as.POSIXlt.character(c("28/7/2022  17:56:20", "28/3/2021  1:10:41 PM"),  : 
      character string is not in a standard unambiguous format

I've already ran this code on a similar vector of time/dates where it worked with no issues. The code also works when I run it on each of the date/time strings individually. The error only occurs when I try to convert both these strings at the same time.

Update: Following the guidance from Ritchie in the comments, I've found a similar Daylight savings time issue which suggested setting the tz = "UTC". This resolved the error I was getting but now "28/3/2021 1:10:41 PM" is being converted to "2021-03-28 01:10:41" rather than "2021-03-28 13:10:41".

Note: This conversion works fine when the tz parameter isn't set

0

There are 0 answers