从多个时区读取R中的时间戳数据
我有一列字符格式的时间戳,如下所示:从多个时区读取R中的时间戳数据,r,datetime,timezone,R,Datetime,Timezone,我有一列字符格式的时间戳,如下所示: dateTimeZone <- asPOSIXct(dateTimeZone, tz="UTC") 2015-09-24 06:00:00 UTC dateTimeZone <- c("2015-09-24 06:00:00 UTC","2015-09-24 05:00:00 UTC") 2015-09-24 05:00:00 UTC dateTimeZone <- c("2015-09-24 06:00:00 UTC","2015-0
dateTimeZone <- asPOSIXct(dateTimeZone, tz="UTC")
2015-09-24 06:00:00 UTC
dateTimeZone <- c("2015-09-24 06:00:00 UTC","2015-09-24 05:00:00 UTC")
2015-09-24 05:00:00 UTC
dateTimeZone <- c("2015-09-24 06:00:00 UTC","2015-09-24 05:00:00 UTC")
dateTimeZone您可以通过检查每一行并进行相应的处理,然后将所有内容恢复到一致的UTC时间来达到目的。(#编辑为现在包括将时区缩写与完整时区规范相匹配)
日期解决方案:
library(data.table)
data <- data.table(dateTimeZone=c("2015-09-24 06:00:00 UTC",
"2015-09-24 05:00:00 America/Los_Angeles"))
data[, timezone:=tstrsplit(dateTimeZone, split=" ")[[3]]]
data[, datetime.local:=as.POSIXct(dateTimeZone, tz=timezone), by=timezone]
data[, datetime.utc:=format(datetime.local, tz="UTC")]
库(data.table)
数据使用润滑的另一种方法
库(stringr)
图书馆(lubridate)
标准化。时区优雅的回答。我喜欢data.table-我以前从未遇到过tstrsplit
。这是值得的!
dates <- c(
"2015-09-24 06:00:00 UTC",
"2015-09-24 05:00:00 PDT"
)
#extract timezone from dates
datestz <- vapply(strsplit(dates," "), tail, 1, FUN.VALUE="")
## Make a master list of abbreviation to
## full timezone names. Used an arbitrary summer
## and winter date to try to catch daylight savings timezones.
tzabbrev <- vapply(
OlsonNames(),
function(x) c(
format(as.POSIXct("2000-01-01",tz=x),"%Z"),
format(as.POSIXct("2000-07-01",tz=x),"%Z")
),
FUN.VALUE=character(2)
)
tmp <- data.frame(Olson=OlsonNames(), t(tzabbrev), stringsAsFactors=FALSE)
final <- unique(data.frame(tmp[1], abbrev=unlist(tmp[-1])))
## Do the matching:
out <- Map(as.POSIXct, dates, tz=final$Olson[match(datestz,final$abbrev)])
as.POSIXct(unlist(out), origin="1970-01-01", tz="UTC")
# 2015-09-24 06:00:00 UTC 2015-09-24 05:00:00 PDT
#"2015-09-24 06:00:00 GMT" "2015-09-24 12:00:00 GMT"
library(data.table)
data <- data.table(dateTimeZone=c("2015-09-24 06:00:00 UTC",
"2015-09-24 05:00:00 America/Los_Angeles"))
data[, timezone:=tstrsplit(dateTimeZone, split=" ")[[3]]]
data[, datetime.local:=as.POSIXct(dateTimeZone, tz=timezone), by=timezone]
data[, datetime.utc:=format(datetime.local, tz="UTC")]