【问题标题】:Changing time zone in xts taking care of dst (daylight saving time)在 xts 中更改时区以照顾 dst(夏令时)
【发布时间】:2017-07-07 21:02:14
【问题描述】:

假设我们有这三个日期:

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 LA 时间(即 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)&lt;- "America/Los_Angeles"的时区转换没有考虑夏令时(dst),如dst函数所示:

> dst(my_xts)
[1] FALSE FALSE FALSE

问题是,我怎样才能更改时区以便处理 dst?

更多细节:

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

【问题讨论】:

    标签: r timezone xts posixct timezone-offset


    【解决方案1】:

    你说:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2011-10-31
      • 2018-03-14
      • 2015-06-07
      • 2019-04-09
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多