我有1974 - 2013年的时间序列数据,其中包含datetimeUTC(YYYY-MM-DD hh:mm +0000)的列,以及Olson格式的时区列(例如,加拿大/太平洋,加拿大/东部) 。我可以将整个UTCdatetime列转换为这样的公共时区:
dataset$datetimeEST <- strptime(
dataset$datetimeUTC, format="%Y-%m-%d %H:%M:%S%z", tz="Canada/Eastern"
)
如果每行中有相应的时区,如何将datetimeUTC
转换为datetimeLOCAL
?
让我回顾一下。我有来自全国各地的数据(6个时区),格式为ISO 8601表示1974-2013。时间戳是全年的当地标准时间(即,即使该地区的平民时间遵守DST,也会忽略DST)。我需要进行日期时间计算,这在UTC时间可能是最安全的,所以这很容易。但是,我还需要考虑DST来提取特定民用时段的数据,并针对该子集化数据进行计算和绘图(例如,所有6个时区中的高峰时段的所有数据)。
我在下面计算的datetimeCLOCKTIME似乎正在执行我想要的绘图,但是在进行日期时间计算时会给出错误的答案,因为它将日期时间存储在本地计算机的时区中而没有实际转换时间。 @thelatemail提供的解决方案是我正在寻找的解决方案,但我还没有能够在2012年的测试数据集中使用Windows(见下文)。另外,我使用strptime转换为POXITlt,他的解决方案是POXITct。我是R的新手,所以任何帮助都会受到无限的赞赏。
测试数据集:
dataset <- data.frame (timestampISO8601 = c("2012-04-25T22:00:00-08:00","2012-04-25T22:15:00-08:00","2012-04-25T22:30:00-08:00","2012-04-25T22:45:00-08:00","2012-04-25T23:00:00-08:00","2012-04-25T23:15:00-08:00","2012-04-25T23:30:00-08:00","2012-04-25T23:45:00-08:00","2012-04-26T00:00:00-08:00","2012-04-26T00:15:00-08:00","2012-04-26T00:30:00-08:00","2012-04-26T00:45:00-08:00","2012-04-26T01:00:00-08:00","2012-04-26T01:15:00-08:00","2012-04-26T01:30:00-08:00","2012-04-26T01:45:00-08:00","2012-04-26T02:00:00-08:00","2012-04-25T22:00:00-03:30","2012-04-25T22:15:00-03:30","2012-04-25T22:30:00-03:30","2012-04-25T22:45:00-03:30","2012-04-25T23:00:00-03:30","2012-04-25T23:15:00-03:30","2012-04-25T23:30:00-03:30","2012-04-25T23:45:00-03:30","2012-04-26T00:00:00-03:30","2012-04-26T00:15:00-03:30","2012-04-26T00:30:00-03:30","2012-04-26T00:45:00-03:30","2012-04-26T01:00:00-03:30","2012-04-26T01:15:00-03:30","2012-04-26T01:30:00-03:30","2012-04-26T01:45:00-03:30","2012-04-26T02:00:00-03:30"), olson = c("Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Pacific","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland","Canada/Newfoundland"), value = c(0,0,1,2,5,11,17,19,20,19,17,11,5,2,1,0,0,-3,-3,-2,-1,2,8,14,16,17,16,14,8,2,-1,-2,-3,-3), stringsAsFactors=FALSE)
删除&#34;:&#34;来自UTC偏移量。 (R期望UTC偏移的格式为nnnn):
dataset$timestampR<- paste(substr(dataset$timestampISO8601,1,22),substr(dataset$timestampISO8601,24,25),sep="")
转换为UTC时间时,R默认为UTC偏移的-ve,使时间戳中的-ve偏移为正:
dataset$datetimeUTC <- strptime(dataset$timestampR, format="%Y-%m-%dT%H:%M:%S%z", tz="UTC")
当像这样转换到MACHINE时间时,R读取输入时间并将其转换为本地机器时区的时间 - 在我的情况下,这是加拿大/东部:
dataset$datetimeMACHINE <- strptime(dataset$timestampR, format="%Y-%m-%dT%H:%M:%S%z")
当像这样转换到CLOCKTIME时,R读取输入时间并分配本地机器的时区(当前在我的机器上为EDT)而不进行任何时间转换:
dataset$datetimeCLOCKTIME <- strptime(dataset$timestampR,format="%Y-%m-%dT%H:%M:%S")
查看数据集的结构:
str(dataset)
绘图行为不同
library(ggplot2)
qplot(data=dataset,x=datetimeUTC,y=value)
qplot(data=dataset,x=datetimeMACHINE,y=value)
qplot(data=dataset,x=datetimeCLOCKTIME,y=value)
计算结果不同。 datetimeCLOCKTIME的计算结果不正确:
range (dataset$datetimeUTC)
range (dataset$datetimeMACHINE)
range (dataset$datetimeCLOCKTIME)
dataset$datetimeUTC[34] - dataset$datetimeUTC[1]
dataset$datetimeMACHINE[34] - dataset$datetimeMACHINE[1]
dataset$datetimeCLOCKTIME[34] - dataset$datetimeCLOCKTIME[1]
答案 0 :(得分:4)
您可以来回格式化以获得字符格式的本地时间表示。 E.g:
dataset <- data.frame(
datetimeUTC=c("2014-01-01 00:00 +0000","2014-01-01 00:00 +0000"),
olson=c("Canada/Eastern", "Canada/Pacific"),
stringsAsFactors=FALSE
)
# datetimeUTC olson
#1 2014-01-01 00:00 +0000 Canada/Eastern
#2 2014-01-01 00:00 +0000 Canada/Pacific
dataset$localtime <- with(dataset,
mapply(function(dt,ol) format(
as.POSIXct(dt,"%Y-%m-%d %H:%M %z",tz=ol),
"%Y-%m-%d %H:%M %z"),
datetimeUTC, olson
)
)
# datetimeUTC olson localtime
#1 2014-01-01 00:00 +0000 Canada/Eastern 2013-12-31 19:00 -0500
#2 2014-01-01 00:00 +0000 Canada/Pacific 2013-12-31 16:00 -0800
答案 1 :(得分:2)
如果您只有两个时区可以转换并知道UTC与这两者之间的时差。使用@ thelatemail&#39; s dataset
transform(dataset,
localtime=as.POSIXct(datetimeUTC, "%Y-%m-%d %H:%M %z")-
c(5*3600,8*3600)[as.numeric(factor(olson))])
# datetimeUTC olson localtime
#1 2014-01-01 00:00 +0000 Canada/Eastern 2013-12-31 19:00:00
#2 2014-01-01 00:00 +0000 Canada/Pacific 2013-12-31 16:00:00