在 R 中将 EST/EDT 数据转换为 GMT

Convert EST/EDT data to GMT in R

我有一个包含 EST/EST 日期的数据集。

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

我想将此数据转换为 GMT。我尝试了以下代码

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

并得到以下输出。

[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"

这对于 01:00 EST 是不正确的。如何解决该问题?

您可能必须自己提供偏移量,然后在转换为 POSIXct 时指定使用 %z

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

数据

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