Invalid %%A auto parser - r

Can anybody tell me what is wrong with this command in R?
I literally have tried everything:
d0 <- "domingo 04 febrero 2018"
parse_date(d0, "%A %d %B %Y", locale = locale("es"))
When I execute the above code, I get an error that says
"invalid %%A auto parser"

I think that the problem is that the "%A" format specification is not defined in the parse_date function.
d0 <- "04 febrero 2018"
parse_date(d0, "%d %B %Y", locale = locale("es"))
[1] "2018-02-04"

The %A is a valid format per ISO8601 specification.
The actual problem is that parse_date (a function from readr package) doesnot provide support for all of ISO8601 specifications. Missing features include Week and Weekday specifications.
But we have an alternate solution in base r itself. Let me provide you few examples:
# %A is supported is base r
> as.character(Sys.Date(), "%A %d %B %Y")
[1] "Sunday 04 February 2018"
# Try to parse character string to date now
> as.POSIXct("Sunday 04 February 2018", format = "%A %d %B %Y")
[1] "2018-02-04 GMT"
# Lets execute the same code (converted to English) from OP
d0 <- "Sunday 04 February 2018"
as.POSIXct(d0, format = "%A %d %B %Y", tz = "GMT")
# Result :
[1] "2018-02-04 GMT"

Related

Convert Uncommon Date Format

Is there an easy way to transform this string into a meaningful date format?
date <- "Tue Apr 04 10:18:33 +0000 2017"
Right now I would use some regex and then the lubridate package. But I guess there is a less complicated way.
Try this:
as.POSIXct(date, format = "%a %b %d %H:%M:%S %z %Y")

strptime returning NA

I am getting NA value for my date string using strptime in R.
I looked at the various answers, but it didn't work.
Here is my code
startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)
Any help would be appreciated.
"%H:%m:%S" should be "%H:%M:%S". Once you change that, you'll get an error because %Z is not valid for input.
If all the datetime strings have UTC timezone, this will work:
R> strptime("Wed May 25 01:51:32 UTC 2016", "%a %B %d %H:%M:%S UTC %Y", "UTC")
[1] "2016-05-25 01:51:32 UTC"
If not, then you can extract the year and prepend it to the string, because strptime will ignore all characters after those specified by the format string.
R> dts <- "Wed May 25 01:51:32 UTC 2016"
R> dtf <- "%Y %a %B %d %H:%M:%S"
R> strptime(paste(substring(dts, nchar(dts)-3), dts), dtf, "UTC")
[1] "2016-05-25 01:51:32 UTC"

R convert string in date format

I'm having some problems converting string vectors into date format. I have found a lot of information here and here but they haven't worked in my case (R 3.2.3).
> strptime("Fri Feb 05 14:10:10 +0000 2016",
format="%a %b %d %H:%M:%S %z %Y", tz="GMT")
[1] NA

How can I parse date in another language?

My local environment is French :
> Sys.getlocale()
[1] "fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8"
I would like to parse date in English but I don't know how to enter this parameter to my function.
If the date is in French, everything works :
> as.Date("15 mai 2004", "%d %B %Y")
[1] "2004-05-15"
If I have a date in English, it doesn't work :
> as.Date("15 mai 2004", "%d %B %Y")
[1] "2004-05-15"
as.Date("15 may 2004", "%d %B %Y")
[1] NA
Ok, here is the solution :
Sys.setlocale(category = "LC_TIME", locale = "en_GB.UTF-8")
as.Date("15 may 2004", "%d %B %Y")

converting utc format to required format using python

I am trying to convert UTC format date format to required format using python. Simply I have the datetime in the format(Fri Dec 07 19:06:06 +0000 2012), I need to convert this into my format(2012-12-07 19:06:06:546 +0000)
Code:
created_at = "Fri Dec 07 19:06:06 +0000 2012"
d = datetime.strptime(created_at, '%a %b %d %H:%M:%S %z %Y')
date_object = d.strftime('%y-%m-%d %H:%M:%S')
result:
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'
The below link from python bugs says its fixed but i didn understand what is fixed and in which version i can use %z
http://bugs.python.org/issue6641
I am not able to use %z . Is there any other way to handle this??
Do it like that:
from datetime import datetime
TIMESTAMP = datetime.utcnow().strftime('%d/%m/%Y %H:%M:%S')

Resources