Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the current time at specific timezone

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
like image 523
KKY Avatar asked Jul 18 '17 14:07

KKY


2 Answers

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.

like image 104
troh Avatar answered Oct 13 '22 01:10

troh


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])
like image 34
thelatemail Avatar answered Oct 13 '22 00:10

thelatemail