0

I had been trying to parse the code below with not good results. The function doesn´t recognize am/pm.

 library (tidyverse) 
 library (lubridate)
 c = c("28 de julio de 2020 6:02 PM Hora de Montevideo")
 dmy_hm (c)

#with this unsatisfactory result

[1] "2020-07-28 06:02:00 UTC"

The time must be 18:02:00. Clearly I'm doing something wrong with LOCALE. Could someone helpme?

Thanks

mpo
  • 5
  • 1
  • What locale are you currently using? i.e. what are `system("locale -a")` and `Sys.getlocale("LC_TIME")`? – Calum You Aug 20 '20 at 01:44
  • Thank you Calumn. I'm using ** > Sys.getlocale("LC_TIME") [1] "Spanish_Spain.1252"** – mpo Aug 20 '20 at 09:28

1 Answers1

0

This works for me :

x = c("28 de julio de 2020 6:02 PM Hora de Montevideo")

#Set locale to spanish
Sys.setlocale("LC_TIME", "es_ES")
as.POSIXct(x, format = '%d de %B de %Y %I:%M %p', tz = 'UTC')
#[1] "2020-07-28 18:02:00 UTC"

lubridate::dmy_hm(x)
#[1] "2020-07-28 18:02:00 UTC"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you Ronak, but it doesn't work in my system. I dón't know why. When I ask to change Locale setting using> **Sys.setlocale("LC_TIME", "es_ES")**, the System retieves me with Warning message: **In Sys.setlocale("LC_TIME", "es_ES") : OS reports request to set locale to "es_ES" cannot be honored** Any idea? Thanks – mpo Aug 20 '20 at 09:08
  • I'm using Windows – mpo Aug 20 '20 at 09:16
  • Can you try `Sys.setlocale("LC_ALL","Spanish")`. See various options here - https://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r – Ronak Shah Aug 20 '20 at 09:20
  • @mpo Hi, were you able to resolve this? Did you get the answer that you were looking for? – Ronak Shah Aug 21 '20 at 12:46
  • Hi Ronak, I can't. What I'm doing is editing the data, on excel. Probably the problem is that my system. I tried using "Sys.setlocale ("LC_ALL", "Spanish")" But it doesn`t work. – mpo Aug 27 '20 at 19:41
  • Hí, I could finally. Probably the problem was that I´m using Windows. My code ` Sys.setlocale("LC_TIME", "Spanish") [1] "Spanish_Spain.1252" > x= c("27 de julio de 2020 3:43 PM Hora de Montevideo") > as.POSIXct(x, format = '%d de %B de %Y %I:%M %p', tz = 'UTC') [1] "2020-07-27 15:43:00 UTC" ` Thank you very much. – mpo Sep 01 '20 at 20:52