I've a data frame with date and time format of different time zones, I want to compare this with the current time at that timezone. so I want to add 1 hr to the below "Date and time column and then compare that with the current time in that time zone like for the first one (timezone is EDT and the current time is 2017-07-18 10:20 in EDT)
Date and time TZ
2017-07-08 16:00 EDT
2017-07-17 15:30 PDT
2017-07-17 11:00 EDT
2017-07-17 20:00 EDT
2017-07-17 10:00 EDT
2017-07-13 15:00 PDT
where EDT is "America/New_York" and PDT is pacific time zone. I just has the time in the raw data and later with city names I created a column to know if it is "EDT OR PDT" not sure how to proceed from here, I tried something on this time zone not changing when specified
It's really tricky with timezone and my system has a default if "America/New_York" time zone and I'm not sure if whatever I tried was wrong.
Can anyone give some idea how to get the local time in a column ?
my desired output is:
Date and time | TZ | Localtime(current)
2017-07-08 16:00 | EDT | 2017-07-17 2017-07-18 10:24:19 EDT
2017-07-17 15:30 | PDT | 2017-07-17 2017-07-18 09:25:19 PDT
2017-07-17 11:00 | CDT | 2017-07-17 2017-07-18 09:25:19 CDT
2017-07-17 20:00 | EDT | 2017-07-17 2017-07-18 23:02:19 EDT
2017-07-17 10:00 | EDT | 2017-07-17 2017-07-18 10:24:19 EDT
2017-07-13 15:00 | PDT | 2017-07-17 2017-07-18 09:25:19 PDT
library(lubridate)
currentTime <- Sys.time()
tzs <- c("America/Los_Angeles", "America/Chicago", "America/New_York")
names(tzs) <- c("PDT", "CDT", "EDT")
lapply(tzs[data$TZ], with_tz, time = currentTime)
As suggested use with_tz
from lubridate
, then loop through.
If you don't want/need to use lubridate, then this will give the same result, using the tzs
and currentTime
objects from @troh's answer:
Map(`attr<-`, currentTime, "tzone", tzs[dat$TZ])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With