R strptime (version R 3.2.2)
Using strptime for text strings with "AM or PM" options works fine in R version 3.0.2
> strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p") [1] "2015-08-25 18:38:41" I recently upgraded to R 3.2.2 and now found that this commend returns NA:
>strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p") [1] NA Something seems to be related to "PM." If I remove "PM" and use the command as follows, it works (but, of course, it is interpreted as AM not PM):
>strptime("8/25/2015 6:38:41", "%m/%d/%Y %H:%M:%S") [1] "2015-08-25 06:38:41 NZST" What am I missing here?
Update:
Thank you all for your comments:
I reinstalled R 3.2.2 but got the same error
> strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p") [1] NA and here is the session information on request
> sessionInfo() R version 3.2.2 (2015-08-14) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252 [3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C [5] LC_TIME=English_New Zealand.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base Then I changed the locale as suggested in the corresponding post
> Sys.setlocale(category="LC_TIME","C") [1] "C" > strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p") [1] "2015-08-25 18:38:41 NZST" And it worked - but does this mean that I have to change the locale every time I want to convert text time to a POSIXct class?
Try lubridate from hadleyverse
library(lubridate) arrive <- ymd_hms("2011-06-04 12:00:00", tz = "Pacific/Auckland") arrive ## [1] "2011-06-04 12:00:00 NZST" https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html