Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R converting datetime format in foreign language not working

Tags:

date

datetime

r

I am trying to use the package parsedate to parse/convert several different datetime formats into a uniform/homogeneous format the issue is that some dates will be in English (my machine language) and some will be in Spanish allow me to illustrate:

I have two vectors:

#English dates
dates<-c("2016 jun 15 8:39 p.m","2016 apr 2 8:39 a.m","2016 dec 2 8:39 a.m")
#Spanish dates
fechas<-c("2016 junio 15 8:39 p.m","2016 abril 2 8:39 a.m","2016 diciembre 2 8:39 a.m")

I noticed that the function parse_date() correctly converts the vector dates into the desired output format, but when trying to parse the vector with Spanish dates it does not work even when changing local time to "Spanish" as is shown below:

#Parsing english dates
parsedate::parse_date(dates)

> parsedate::parse_date(dates)
[1] "2016-06-15 08:39:00 UTC" "2016-04-02 08:39:00 UTC" "2016-12-02 08:39:00 UTC"


#Parsing spanish dates
Sys.setlocale("LC_TIME", "Spanish")
parsedate::parse_date(fechas)

> Sys.setlocale("LC_TIME", "Spanish")
[1] "Spanish_Spain.1252"
> parsedate::parse_date(fechas)
[1] "2016-01-15 08:39:00 UTC" "2016-01-02 08:39:00 UTC" "2016-01-02 08:39:00 UTC"

The Spanish output is wrong because it should return the same output in the English dates, I have tried several ways to properly change the local time of my machine to Spanish with no luck. What can I try next?

like image 823
R_Student Avatar asked Oct 21 '25 08:10

R_Student


1 Answers

See here https://github.com/tidyverse/lubridate/issues/781

Sys.setlocale("LC_TIME", "Spanish_Spain.1252")
format <- "%a@%A@%b@%B@%p@"
enc2utf8(unique(format(lubridate:::.date_template, format = format)))
str(lubridate:::.get_locale_regs("Spanish_Spain.1252"))
library(lubridate)

Sys.getlocale("LC_TIME")
[1] "Spanish_Spain.1252"

parse_date_time(fechas, 'ymd HM')
[1] "2016-06-15 08:39:00 UTC" "2016-04-02 08:39:00 UTC" "2016-12-02 08:39:00 UTC"
like image 172
TarJae Avatar answered Oct 22 '25 23:10

TarJae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!