【发布时间】: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)<- "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