7

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
KKY
  • 95
  • 1
  • 8

2 Answers2

2
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.

troh
  • 1,354
  • 10
  • 19
  • thank you, this does gives the current time zone of different cities. Sorry to ask but where would I input my data? – KKY Jul 18 '17 at 16:27
  • 1
    You can use `OlsonNames()` to look up the matching betwen timezones. I've update my answer above. – troh Jul 18 '17 at 16:40
1

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])
thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • @thelateemail, this script gives me some output like "1500447303.06667" which is not in time format, I tried to do it using `anytime ` but it ain't working, tried to change the type using `as.character' it is giving the same output. – KKY Jul 19 '17 at 08:59
  • @kky - just checked again, it doesn't. It gives a `list` of `POSIXct` times in various timezones, identical to the accepted answer. If you try to `unlist` it, it will fall back to the numeric representation of seconds since 1970-01-01, which is what a `POSIXct` is. You can only have one timezone associated with a vector, so you might be better off formatting your times like - `sapply(Map(\`attr<-\`, currentTime, "tzone", tzs[dat$TZ]), format)` – thelatemail Jul 19 '17 at 09:58
  • @thelateemail, it is giving me an error `Error: unexpected input in "fgfg1$h<-sapply(Map(`attr<-`, currentTime, "tzone", tzs[df$col]) %b %d %Y %H:%M)" In addition: There were 50 or more warnings (use warnings() to see the first 50)`where I'm going wrong, please correct me – KKY Jul 19 '17 at 12:02
  • @kky - that unquoted `%b %d %Y %H:%M` you've added is the issue. That wasn't in the code example I provided. You need to make your datetime variable a proper datetime first, which you obviously had previously if you were getting output like `1500447303.06667` first time around. Step through each part of the code in @troh's answer, just replacing his last line with my above line. – thelatemail Jul 19 '17 at 21:53
  • yes I tried that but it gives me all the rows with the same time eg: "2017-07-20 01:37:55" – KKY Jul 20 '17 at 02:39