Adding minutes to given time Rstudio - r

I have a dataframe where time is given in the format: Tue Apr 03 18:00:06 +0000 2012
And timezone offset is given in minutes in the format: 240
How do I add 240 minutes to the time above so I can get local time from UTC time?

We could convert to POSIXct and then add the minutes
library(lubridate)
out <- as.POSIXct(str1, format = '%a %b %d %H:%M:%S +0000 %Y') %m+% minutes(240)
tz(out) <- "UTC"
out
#[1] "2012-04-03 22:00:06 UTC"
data
str1 <- 'Tue Apr 03 18:00:06 +0000 2012'

Related

Splunk convert Wed Sep 23 08:00:00 PDT 2020 to _time and epoch time in splunk

Splunk convert Wed Sep 23 08:00:00 PDT 2020 to _time and epoch time in splunk . What is the splunk query to convert java date format to yyyy-MM-dd
From https://docs.splunk.com/Documentation/Splunk/8.0.6/SearchReference/DateandTimeFunctions#strptime.28X.2CY.29
| eval date_unix_timestamp=strptime(date, "%Y-%m-%d")
To convert time strings from one format to another you must strptime() convert to epoch form and then use strftime() to convert to the desired result format.
... | eval newTS=strftime(strptime("Wed Sep 23 08:00:00 PDT 2020", "%a %b %d %H:%M:%:S %Z %Y"), "%Y-%m-%d")

How to Parse string of the format 04 Nov 2018 07:39 AM

I have data imported from an external report which contains a column of dates in the following format : 04 Nov 2018 07:39 AM
I would like to parse that string into a date but am unable to find an orders string that can handle that format
library(lubridate)
Vec1 <- ("04 Nov 2018 07:39 AM")
parse_date_time(Vec1, orders = 'dmy')
[1] NA
Warning message:
All formats failed to parse. No formats found.
Read ?strptime
Vec1 <- ("04 Nov 2018 07:39 AM")
as.POSIXct(Vec1, format = "%d %b %Y %H:%M %p")
#[1] "2018-11-04 07:39:00 GMT"
With lubridate you actually needed the hour and minute component
library(lubridate)
parse_date_time(Vec1, orders = "dmyhm")
Or as #ed_sans mentioned
dmy_hm(Vec1)

Extract time stamps from string and convert to R POSIXct object

Currently, my dataset has a time variable (factor) in the following format:
weekday month day hour min seconds +0000 year
I don't know what the "+0000" field is but all observations have this. For example:
"Tues Feb 02 11:05:21 +0000 2018"
"Mon Jun 12 06:21:50 +0000 2017"
"Wed Aug 01 11:24:08 +0000 2018"
I want to convert these values to POSIXlt or POSIXct objects(year-month-day hour:min:sec) and make them numeric. Currently, using as.numeric(as.character(time-variable)) outputs incorrect values.
Thank you for the great responses! I really appreciate a lot.
Not sure how to reproduce the transition from factor to char, but starting from that this code should work:
t <- unlist(strsplit(as.character("Tues Feb 02 11:05:21 +0000 2018")," "))
strptime(paste(t[6],t[2],t[3], t[4]),format='%Y %b %d %H:%M:%S')
PS: More on date formats and conversion: https://www.stat.berkeley.edu/~s133/dates.html
For this problem you can get by without using lubridate. First, to extract individual dates we can use regmatches and gregexpr:
date_char <- 'Tue Feb 02 11:05:21 +0000 2018 Mon Jun 12 06:21:50 +0000 2017'
ptrn <- '([[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}\\:[[:digit:]]{2}\\:[[:digit:]]{2} \\+[[:digit:]]{4} [[:digit:]]{4})'
date_vec <- unlist( regmatches(date_char, gregexpr(ptrn, date_char)))
> date_vec
[1] "Tue Feb 02 11:05:21 +0000 2018" "Mon Jun 12 06:21:50 +0000 2017"
You can learn more about regular expressions here.
In the above example +0000 field is the UTC offset in hours e.g. it would be -0500 for EST timezone. To convert to R date-time object:
> as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC')
[1] "2018-02-02 11:05:21 UTC" "2017-06-12 06:21:50 UTC"
which is the desired output. The formats can be found here or you can use lubridate::guess_formats(). If you don't specify the tz, you'll get the output in your system's time zone (e.g. for me that would be EST). Since the offset is specified in the format, R correctly carries out the conversion.
To get numeric values, the following works:
> as.numeric(as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC'))
[1] 1517569521 1497248510
Note: this is based on uniform string structure. In the OP there was Tues instead of Tue which wouldn't work. The above example is based on the three-letter abbreviation which is the standard reporting format.
If however, your data is a mix of different formats, you'd have to extract individual time strings (customized regexes, of course), then use lubridate::guess_formats() to get the formats and then use those to carry out the conversion.
Hope this is helpful!!

How to convert a character timestamp into a date-time object in R

There is a timestamp variable (i.e. UTC) in my data frame which is a character / string and the date-time format is as follows:-
Fri Aug 10 04:42:47 +0000 2012
How to convert it into a date-time object in R? I tried using the following but it is giving me NAs.
data1$datetime <- as.POSIXct(as.numeric(data1$UTC),origin="1970-01-01",tz="GMT")
This works for your example. See ?strptime for the format codes.
as.POSIXct("Fri Aug 10 04:42:47 +0000 2012",format="%a %b %d %H:%M:%S %z %Y",tz="GMT")
[1] "2012-08-10 04:42:47 GMT"
You can also use parse_date_time from lubridate, which saves you the typing of spaces and % signs:
date_string = "Fri Aug 10 04:42:47 +0000 2012"
library(lubridate)
parse_date_time(date_string, "abdHMSzY", tz = "GMT")
# [1] "2012-08-10 04:42:47 GMT"

How to convert string to date format in R

I have a column of strings in the following format:
Wed, 6 Dec 2000 08:47:00 -0800 (PST)
How can I convert this into date format using lubridate or another package? I have done this before, but there was no -0800 (PST) at the end.
Thank you.
I was able to get a result using strptime() without even worrying about the timezone name at the end:
> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x, "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
However, if you want to remove the timezone name, you can use substr() to do this:
> strptime(substr(x, 1, nchar(x)-6), "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
We can also use parse_date_time
library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

Resources