6

Using strptime for text strings with a "AM or PM" worked 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 find this commend returns a NA:

>strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p")
[1] NA

It appears to be something to do with the "PM". If I remove the "PM" and use the command as follows, it works (but of course it interprets 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:

Thanks 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 info as requested

>  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 a related 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 I have to change the locale each time I want to convert a text time into a POSIXct class?

a_mcm
  • 61
  • 3
  • strptime uses the locale of the system - what is the output of `Sys.getlocale(category = "LC_ALL")`? – jeremycg Aug 26 '15 at 23:18
  • > Sys.getlocale(category = "LC_ALL") [1] "LC_COLLATE=English_New Zealand.1252;LC_CTYPE=English_New Zealand.1252;LC_MONETARY=English_New Zealand.1252;LC_NUMERIC=C;LC_TIME=English_New Zealand.1252" – a_mcm Aug 26 '15 at 23:23
  • 1
    weird, even with that locale set, I can't reproduce the problem. – jeremycg Aug 26 '15 at 23:35
  • Just tried it on my old version of R (version 3.0.2: Here is what I got: > strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p") [1] "2015-08-25 18:38:41" – a_mcm Aug 26 '15 at 23:41
  • 1
    Either try running with --vanilla or reinstall. If it persists, then update your post with output from sessionInfo(). (I cannot reproduce on R vers 3.2.2 running on OSX-Yosemite in a US-locale. ) – IRTFM Aug 27 '15 at 00:29
  • I reinstalled R 3.2.2 and got the same problem. However – a_mcm Aug 27 '15 at 02:41
  • `Sys.setlocale(category="LC_TIME","en_US.UTF-8")` saved my day! Thanks – nodakai Jan 26 '16 at 07:40

1 Answers1

1

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

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
  • This is a nice solution, but I still don't understand why POSIXct is not working in some cases with am/pm times. – Nakx Apr 28 '20 at 03:36