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

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.

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.

Interconverting POSIXct and numeric in R

I'm importing data from Excel and then trying to manipulate dates and times in R and it's giving me SUCH A HEADACHE. In the Excel file, one column contained a date and time, and another column contained a different time for that same day. The data in R looks basically like this example:
mydata <- data.frame(DateTime1 = as.POSIXct(c("2014-12-13 04:56:00",
"2014-12-13 09:30:00",
"2014-12-13 11:30:00",
"2014-12-13 13:30:00"),
origin = "1970-01-01", tz = "GMT"),
Time2 = c(0.209, 0.209, 0.715, 0.715))
I'd like to have a new column in POSIXct format with the date and the 2nd time, and I can't get that to work. I've tried:
mydata$DateTime2 <- as.POSIXct(as.numeric(as.Date(mydata$DateTime1)
+ mydata$Time2), origin = "1970-01-01",
tz = "GMT")
but that gives me dates and times close to 1/1/1970.
This is more convoluted, but one thing that has worked in other similiar situations that I've also tried is:
library(lubridate)
mydata$DateTime2 <- ymd_hms(format(as.POSIXct(as.Date(mydata$DateTime1) +
mydata$Time2,
origin = "1899-12-30", tz = "GMT")))
but that gives me dates and times that are off by 8 hours. That time difference makes me think that the problem is the time zone since I'm on Pacific Standard Time, but I set it to GMT both in the input data and when trying to convert! What gives? I'm hesitant to just add 8 hours to everything because of daylight savings time complications.
Really, both of the attempts I'm listing here seem to have problems with interconversion, i.e., if you start with a POSIXct object and convert it to numeric and then convert it back again to POSIXct, you should end up back where you started and you don't. Similarly, if you start with the time zone being GMT and then you add something that also is set to have the time zone as GMT, then you shouldn't have a problem with things somewhere mysteriously getting converted to the system time zone.
Advice?
I found an answer based on chris holbrook's answer here: How do you convert dates/times from one time zone to another in R?
This worked:
mydata$DateTime2 <- as.POSIXct(as.Date(mydata$DateTime1) +
mydata$Time2)
attributes(mydata$DateTime2)$tzone <- "GMT"
#MichaelChirico and I were correct that the time zone was the problem. I'm still not sure why, but the time zone for DateTime2 was apparently PST. It didn't list "PST" when I checked str(mydata$DateTime2), but based on the time difference, it must have been, in fact, PST until I set the attributes. Crazy. It did that even though DateTime1 was GMT.

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"))

R: Convert a Character to a timestamp

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

Resources