更改 xts 中的时区以处理 dst(夏令时)

Changing time zone in xts taking care of dst (daylight saving time)

假设我们有这三个日期:

original_dates<-  c("2015-12-31T07:00:00", "2015-12-31T08:00:00", "2015-12-31T09:00:00")

和这个向量:

vector<- c("a", "b", "c")

我们将日期转换为 POSIXct 格式:

original_dates2<- as.POSIXct(original_dates, format="%Y-%m-%dT%H", tz = "GMT")

并构建一个 xts:

my_xts<- xts(vector, order.by = original_dates2, frequency = "hourly")

所以我们有以下 xts 对象:

> my_xts
                    [,1]
2015-12-31 07:00:00 "a" 
2015-12-31 08:00:00 "b" 
2015-12-31 09:00:00 "c" 
Warning message:
timezone of object (GMT) is different than current timezone ().

如果我想更改为当地时间,我会更改时区:

indexTZ(my_xts)<- "America/Los_Angeles"

但这会产生错误的结果,因为我知道 2015-12-31 07:00:00 GMT 应该等于 2015-12-31 00:00:00 洛杉矶时间(即 7 小时之前),不是 2015-12-30 23:00:00(即 8 小时之前)

> my_xts
                    [,1]
2015-12-30 23:00:00 "a" 
2015-12-31 00:00:00 "b" 
2015-12-31 01:00:00 "c" 
Warning message:
timezone of object (America/Los_Angeles) is different than current timezone (). 

我猜这是因为时区转换 indexTZ(my_xts)<- "America/Los_Angeles" 没有考虑夏令时 (dst),如 dst 函数所示:

> dst(my_xts)
[1] FALSE FALSE FALSE

问题 是,如何更改时区以便处理夏令时?

更多详情:

> Sys.timezone()
[1] "Europe/Paris"
> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32                                
version.string R version 3.3.1 (2016-06-21)

你说:

but this yields a wrong result because I know that 2015-12-31 07:00:00 GMT should be equal to 2015-12-31 00:00:00 LA time (i.e. 7 h. before), not 2015-12-30 23:00:00 (i.e. 8 h. before)

这是不正确的。太平洋时区在标准时间为 UTC-8,在白天为 UTC-7。 12 月,标准时间 生效。因此,您看到的结果是符合预期的。