What time format is the following? - r

I have the following datetime string:
mytimestr <- "2014-04-15T14:40:00.000000000+00:00"
I assumed it was an ISO3339 format, but the following format string, which should describe that:
> as.POSIXct(mytimestr, format="%Y-%m-%dT%H:%M:%E9S%Ez")
[1] NA
Produces NA as you can see. Questions are either:
Can you identify a format that decodes this time? Or
Is there a common resource or function that can look at a string time and tell me the time format?

We could use ymd_hms from lubridate
library(lubridate)
ymd_hms(mytimestr)
#[1] "2014-04-15 14:40:00 UTC"

Related

reformatting datetime in R

I have data "A" in the format chr "5/7/2021 15:15". I would like to convert it to a format which R will recognize. (It is giving me errors when I try to plot, for instance, which leads me to believe it needs to be reformatted.)
Here is the format "B" I would like to achieve. R seems to like this ok, so I might as well match it (?):
POSIXct, format: "2021-8-11 16:00:00". I am not sure if the seconds are needed, and they do not exist in data "A" so the seconds could be omitted. If R doesn't care then I don't either. The timezone is UTC.
How do I do it? I have tried a couple things, including:
CTD_datetime_UTC <- as.POSIXct(CTD$Date.and.Time, tz = "UTC").
You can use strptime from base R. But there are many parsers for dates...
Assuming the format is "day/month/year" (example is not unambiguous, could also be "month/day/year")
strptime("5/7/2021 15:15", "%d/%m/%Y %H:%M", tz = "UTC")
Returns:
[1] "2021-07-05 15:15:00 UTC"
Using parsedate
library(parsedate)
parse_date("5/7/2021 15:15")
[1] "2021-05-07 15:15:00 UTC"

Convert UNIX time string to date in R

I have the following UNIX time string:
"1575824800.169"
If you convert this time you will get: 12/08/2019 17:06
on an online unix converter.
However when trying to convert this in R using the following code:
as.POSIXct("1575824800.169", format='%d/%m/%Y %H:%M', origin = "1970-01-01")
I am returned with the value NA
i'm struggling to see why the above code does not work - i have looked into different answers on here, but have not found one where the unix time string has 3 digits after the period (dot) in the string. Maybe this is the problem?
You don't have a string encoding a date (as implied by using the format argument of as.POSIXct) but a number. If we re-cast the string as a numeric and get rid of the format argument we get the expected result (although we might need to use the tz argument to specify a timezone)
as.POSIXct(1575824800.169, origin = "1970-01-01")
Returns:
[1] "2019-12-08 18:06:40 CET"
Edit:
Adding timezone argument
as.POSIXct(1575824800.169, origin = "1970-01-01", tz = "UCT")
Returns:
[1] "2019-12-08 17:06:40 UTC"
Edit 2:
Regarding converting the string to numeric with as.numeric: As #IceCreamToucan pointed out, it does not matter. Only the "printed" value changes, the internal representation stays the same and therefore the result is still correct
as.POSIXct(as.numeric("1575824800.169"), origin = "1970-01-01", tz = "UCT")
Returns the same:
[1] "2019-12-08 17:06:40 UTC"
The anytime package aims to help here with some built-in heuristics. So numeric data in that range is automagically taken as (fractional) seconds since the epoch:
R> anytime::anytime(1575824800.169)
[1] "2019-12-08 11:06:40.168 CST"
R>
There is also a wrapper for UTC and some other options should you need them:
R> anytime::utctime(1575824800.169)
[1] "2019-12-08 17:06:40.168 UTC"
R>

Change string including "T07:57:00Z" to a datetime type?

I have a list of strings which take the following form: "2019-03-05T07:57:00Z" and I need to convert them to a Date data type so that I can do calculations with them, however I can't get R to recognize the "T07:57:00Z" as a time. The format itself doesn't matter as long as it is in a form where I can do calculations, does R have a way to handle this form of date?
Use lubridate for datetime use cases:
lubridate::ymd_hms("2019-03-05T07:57:00Z")
[1] "2019-03-05 07:57:00 UTC"
Or with base R
(res <- as.POSIXct("2019-03-05T07:57:00Z", format = "%Y-%m-%dT%H:%M:%SZ"))
# [1] "2019-03-05 07:57:00 CET"

Extracting time in R

I have character field in the following format
df
sd
10/12/2017 6:12
10/12/2017 6:14
I want to convert it into date format so that I can extract time from it.
Now having read this, I don't want to use regex as I want to keep it more generic as the formats might change. So I wanted to convert it to a date format and use lubridate to extract required fields.
So i used the following :
d1 <- strptime(df$sd[1], "%m/%d/%Y%H:%M")
and it gives me the following result
d1
[1] "2017-10-12 IST"
whereas i was expecting the hours and mins to be included as well.
Also on trying to use
format(dmy_hms(df$sd), "%H:%M:%S")
I get that "All formats failed to parse. No formats found"
Any suggestions on this?
Here's the lubridate solution:
library(lubridate)
df <- data.frame(sd=c('10/12/2017 6:12','10/12/2017 6:14'),stringsAsFactors = F)
dmy_hm(df$sd)
#[1] "2017-12-10 06:12:00 UTC" "2017-12-10 06:14:00 UTC"

Parse datetime with lubridate

I am trying to parse the following datetime with the following format:
library(lubridate)
a <- "2004-05-07 18:24:58.666424"
I tried the following, but returned NAs
b <- lubridate::mdy_hms(a)
c <- lubridate::mdy(a)
Could anyone please explain how to parse this. I am also fine if lubridate is not used.
With lubridate, you can specify that your seconds have a decimal with the special S! or OS formats; see ?parse_date_time for more parsing options.
> parse_date_time("2004-05-07 18:24:58.666424", 'ymd HMS!')
[1] "2004-05-07 18:24:58 UTC"
Alternately, it seems to parse fine with just the usual default "ymd HMS":
parse_date_time("2004-05-07 18:24:58.666424", 'ymd HMS')
or the shorthand
ymd_hms("2004-05-07 18:24:58.666424")
Try
options(digits.secs=6)
as.POSIXct(a,"%Y-%m-%d %H:%M:%S.%OS")
#[1] "2004-05-07 18:24:58.666424"
mdy=Month day year, your data is setup as ymd
Try ymd_hms or ymd

Resources