我有一个时区为"GMT"的xts对象,从我在这个网站上看到的情况来看,我可以使用indexTZ()
来更改时区。我尝试了所有可能的技巧,首先指定"GMT",然后将其更改为"IST"。当我第一次将时间戳转换为日期-时间格式时指定"IST":DF$DateTime= as.POSIXct(DF$DateTime/(10^9), origin="1970-01-01", tz = "IST")
,但它仍然以"GMT"为单位提供时间。基本上什么都没发生。
> head(auro)
MP avgVWAP TradePressure Modify
2015-11-30 03:45:01 0.0000000000 0.0000117703 37 107
2015-11-30 03:45:02 0.0016475976 0.0004038842 11 30
2015-11-30 03:45:03 0.0007009310 0.0013219980 13 34
2015-11-30 03:45:04 -0.0003656419 -0.0002547382 11 0
2015-11-30 03:45:05 -0.0004572404 -0.0003604543 4 0
2015-11-30 03:45:06 0.0005486634 0.0003343666 7 -14
Warning message:
timezone of object (GMT) is different than current timezone (IST).
> indexTZ(auro)<- "IST"
> head(auro)
MP avgVWAP TradePressure Modify
2015-11-30 03:45:01 0.0000000000 0.0000117703 37 107
2015-11-30 03:45:02 0.0016475976 0.0004038842 11 30
2015-11-30 03:45:03 0.0007009310 0.0013219980 13 34
2015-11-30 03:45:04 -0.0003656419 -0.0002547382 11 0
2015-11-30 03:45:05 -0.0004572404 -0.0003604543 4 0
2015-11-30 03:45:06 0.0005486634 0.0003343666 7 -14
> indexTZ(auro)<- "UTC"
> head(auro)
MP avgVWAP TradePressure Modify
2015-11-30 03:45:01 0.0000000000 0.0000117703 37 107
2015-11-30 03:45:02 0.0016475976 0.0004038842 11 30
2015-11-30 03:45:03 0.0007009310 0.0013219980 13 34
2015-11-30 03:45:04 -0.0003656419 -0.0002547382 11 0
2015-11-30 03:45:05 -0.0004572404 -0.0003604543 4 0
2015-11-30 03:45:06 0.0005486634 0.0003343666 7 -14
Warning message:
timezone of object (UTC) is different than current timezone (IST).
我注意到我的时间和时区之间存在脱节。时间为"GMT",但显示的时区为"IST"
> Sys.time()
[1] "2016-03-23 06:49:00.816 IST"
所以我认为首先改变系统时区可能会有所帮助,但事实并非如此。再说一遍,什么也没发生。
> Sys.setenv(TZ = "GMT")
> Sys.time()
[1] "2016-03-23 06:51:31.044 GMT"
> indexTZ(auro)<- "IST"
> head(auro)
MP avgVWAP TradePressure Modify
2015-11-30 03:45:01 0.0000000000 0.0000117703 37 107
2015-11-30 03:45:02 0.0016475976 0.0004038842 11 30
2015-11-30 03:45:03 0.0007009310 0.0013219980 13 34
2015-11-30 03:45:04 -0.0003656419 -0.0002547382 11 0
2015-11-30 03:45:05 -0.0004572404 -0.0003604543 4 0
2015-11-30 03:45:06 0.0005486634 0.0003343666 7 -14
Warning message:
timezone of object (IST) is different than current timezone (GMT).
知道为什么会发生这种事吗?
indexTZ()
或Sys.setenv()
是否只是更改时区而不将时间校准到指定的时区?
如果是这样的话,我该怎么做才能更改xts对象的时间和时区?
我做错什么了吗?
正如评论中所暗示的,"IST"不是有效的时区。最好以国家/城市格式指定时区。如果您这样做,打印的时间将被更新。
require(xts)
Lines <- "datetime,MP,avgVWAP,TradePressure,Modify
2015-11-30 03:45:01,0.0000000000,0.0000117703,37,107
2015-11-30 03:45:02,0.0016475976,0.0004038842,11,30
2015-11-30 03:45:03,0.0007009310,0.0013219980,13,34
2015-11-30 03:45:04,-0.0003656419,-0.0002547382,11,0
2015-11-30 03:45:05,-0.0004572404,-0.0003604543,4,0
2015-11-30 03:45:06,0.0005486634,0.0003343666,7,-14"
x <- as.xts(read.zoo(text=Lines,FUN=as.POSIXct,tz="GMT",header=TRUE,sep=","))
x
# MP avgVWAP TradePressure Modify
# 2015-11-30 03:45:01 0.0000000000 0.0000117703 37 107
# 2015-11-30 03:45:02 0.0016475976 0.0004038842 11 30
# 2015-11-30 03:45:03 0.0007009310 0.0013219980 13 34
# 2015-11-30 03:45:04 -0.0003656419 -0.0002547382 11 0
# 2015-11-30 03:45:05 -0.0004572404 -0.0003604543 4 0
# 2015-11-30 03:45:06 0.0005486634 0.0003343666 7 -14
indexTZ(x) <- "Asia/Kolkata"
x
# MP avgVWAP TradePressure Modify
# 2015-11-30 09:15:01 0.0000000000 0.0000117703 37 107
# 2015-11-30 09:15:02 0.0016475976 0.0004038842 11 30
# 2015-11-30 09:15:03 0.0007009310 0.0013219980 13 34
# 2015-11-30 09:15:04 -0.0003656419 -0.0002547382 11 0
# 2015-11-30 09:15:05 -0.0004572404 -0.0003604543 4 0
# 2015-11-30 09:15:06 0.0005486634 0.0003343666 7 -14