rdatetimeposixctposixlt

Convert EST/EDT data to GMT in R


I have a dataset that contain EST/EST date like this.

c(
"11/03/2007 18:00 EDT", "11/03/2007 19:00 EDT", "11/03/2007 20:00 EDT",
"11/03/2007 21:00 EDT", "11/03/2007 22:00 EDT", "11/03/2007 23:00 EDT",
"11/04/2007 00:00 EDT", "11/04/2007 01:00 EDT", "11/04/2007 01:00 EST",
"11/04/2007 02:00 EST", "11/04/2007 03:00 EST", "11/04/2007 04:00 EST",
"11/04/2007 05:00 EST", "11/04/2007 06:00 EST", "11/04/2007 07:00 EST",
"11/04/2007 08:00 EST", "11/04/2007 09:00 EST", "11/04/2007 10:00 EST",
"11/04/2007 11:00 EST")

I want to convert this data in to GMT. I tried the follwing code

Data$Time=as.POSIXct(Data$Time, '%m/%d/%Y %H:%M',tz='EST5EDT')
attributes(Data$Time)$tzone <- "GMT"

and got the following output.

[1] "2007-11-03 22:00:00 GMT" "2007-11-03 23:00:00 GMT"
 [3] "2007-11-04 00:00:00 GMT" "2007-11-04 01:00:00 GMT"
 [5] "2007-11-04 02:00:00 GMT" "2007-11-04 03:00:00 GMT"
 [7] "2007-11-04 04:00:00 GMT" "2007-11-04 05:00:00 GMT"
 [9] "2007-11-04 05:00:00 GMT" "2007-11-04 07:00:00 GMT"
[11] "2007-11-04 08:00:00 GMT" "2007-11-04 09:00:00 GMT"
[13] "2007-11-04 10:00:00 GMT" "2007-11-04 11:00:00 GMT"
[15] "2007-11-04 12:00:00 GMT" "2007-11-04 13:00:00 GMT"
[17] "2007-11-04 14:00:00 GMT" "2007-11-04 15:00:00 GMT"
[19] "2007-11-04 16:00:00 GMT"

which is not correct for 01:00 EST. How to solve that issue ?


Solution

  • You may have to provide the offset yourself and then specify that using %z when converting to POSIXct.

    d2 = as.POSIXct(x = gsub("EDT", "-0400", gsub("EST", "-0500", d)),
                    "%m/%d/%Y %H:%M %z",
                    tz = "US/Eastern")
    attr(d2, "tzone") = "GMT"
    d2
    

    DATA

    d = c("11/03/2007 18:00 EDT", "11/03/2007 19:00 EDT", "11/03/2007 20:00 EDT", 
    "11/03/2007 21:00 EDT", "11/03/2007 22:00 EDT", "11/03/2007 23:00 EDT", 
    "11/04/2007 00:00 EDT", "11/04/2007 01:00 EDT", "11/04/2007 01:00 EST", 
    "11/04/2007 02:00 EST", "11/04/2007 03:00 EST", "11/04/2007 04:00 EST", 
    "11/04/2007 05:00 EST", "11/04/2007 06:00 EST", "11/04/2007 07:00 EST", 
    "11/04/2007 08:00 EST", "11/04/2007 09:00 EST", "11/04/2007 10:00 EST", 
    "11/04/2007 11:00 EST")