R: Convert a Character to a timestamp - r

Currently we are implementing some sort of direct marketing
We send an SMS (timestamp) on a certain day from a certain country within Europe and then receive a response from a customer in the local country if they are interested in the product
I have a sample data set for two months, of SMS's sent and Responses received (if at all). The end goal is , I want to calculate the time difference in hours between the sent time and the response time factoring in Time Zones and Day Light Savings
The file i received has the time stamps in character format. I wish to convert them to dates of their local timezones, convert them to a standard zone and calculate the hours difference
I have tried the methods below to just create the conversion but it comes back with NA.
Any help would be greatly appreciated
as.POSIXlt("12/02/2015 11:23", tz = "Europe/London")
strptime("12/02/2015 11:23", "%d/%m/%Y %h:%m")
as.Date("12/02/2015 11:23","%d/%m/%y %h:%m")

Note that you do not say if you are using 24-hour or 12-hour clock, but given that you do not have AM/PM, I am going to assume the former:
strptime("12/02/2015 11:23 AM", "%d/%m/%Y %I:%M %p", tz = "Europe/London") #12-hour
strptime("12/02/2015 11:23", "%d/%m/%Y %H:%M", tz = "Europe/London") # 24-hour

Related

Convert time zone from EST to UTC using R base functions

I need to change my time zone from EST to UTC using the base R functions. I wrote this code but the problem is that although during the daylight saving time the time difference between these two time zones should be 4 hours, the function does not take it into account. Therefore, during the whole year the time difference equals to 5. My dataset is here.
data <- read.csv("data.csv")
data1 <- data
attributes(data1$time)$tzone
data1$time <- as.POSIXct(data1$time, format="%m/%d/%Y %H:%M:%S", tz="EST")
attributes(data1$time)$tzone
attr(data1$time, "tzone") <- "UTC"
attributes(data1$time)$tzone
Using tz = "EST" means you are specifying Eastern standard time, which is only active in autumn/winter. As indicated by #r2evans, tz = "US/Eastern" or tz = "America/New_York" specifies that the time should switch between EST and EDT (Eastern daylight time) on the appropriate dates.

SAS date and time to Normal date and time format (Y-M-D H:m:S)

I have obtained data from a remote tagging experiment and the dates are in SAS format (43255.940972). The last level of information I want to extract are seconds. I was trying this:
as.Date(data$Date, origin = "1960-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
But all I get in return is Year-Month-Day but no time details. How do I specify, using this command that I also want that information to be returned?
Thanks
Based on the answer provided by #Onyambu
as.POSIXct(a*86400,origin="1899-12-30")
Some documentation you can read on those functions and date time in general.

as.POSIXct assigns different timezones [duplicate]

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:
pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)
I would appreciate any help.
First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:
pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"
So that was midnight of the current date in Greenwich:
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"
When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.
The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:
> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"
(Notice the reversal of + with "behind" and - with "ahead".)
I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN).
Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work
mtn_ts = function(utcTime){
library(lubridate)
toTz = "us/mountain"
utcTime = force_tz(utcTime,tzone= 'GMT')
dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
dt = force_tz(dt,tzone= toTz)
return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))

Converting UTC time to local standard time in R

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:
pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)
I would appreciate any help.
First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:
pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"
So that was midnight of the current date in Greenwich:
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"
When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.
The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:
> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"
(Notice the reversal of + with "behind" and - with "ahead".)
I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN).
Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work
mtn_ts = function(utcTime){
library(lubridate)
toTz = "us/mountain"
utcTime = force_tz(utcTime,tzone= 'GMT')
dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
dt = force_tz(dt,tzone= toTz)
return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))

Results with difftime in R different from excel and timeanddate.com

t1 <- strptime("2015-02-17 12:20:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
t2 <- strptime("2015-03-13 15:00:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
as.numeric(abs(difftime(t1, t2, units = "mins", tz = "America/Chicago")))
The above returns 34,660 while Excel and http://www.timeanddate.com/date/duration.html both return 34,720. R recognizes t1 as CST and t2 as CDT due to daylight savings on 3/8/15. I wanted to confirm that R is correct, while these other two sources are not.
Depends if you want to take into account local daylight savings, or if these times are two points in time irrelevant of local shifts. Excel and that site are just taking the exact difference between those two times, discarding daylight savings - e.g. R can get the same result by changing the timezones to UTC, which doesn't observe daylight saving:
difftime(as.POSIXct("2015-03-13 15:00:00",tz="UTC"),
as.POSIXct("2015-02-17 12:20:00",tz="UTC"), units="mins")
# Time difference of 34720 mins
If I'm sitting in Chicago with a stopwatch and counting the minutes difference between when a local clock in the pub says "2015-02-17 12:20:00" and then hits "2015-03-13 15:00:00", I'd count 60 minutes less as the clock got wound forward an hour for daylight savings while I was counting.

Resources