getting the current time at specific timezone

只谈情不闲聊 提交于 2019-12-11 02:18:51

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/45169693/getting-the-current-time-at-specific-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!