0

我有一个带有这种格式日期的向量(前 6 行的示例):

 Dates<-c(
   "Sun Oct 04 20:33:05 EEST 2015",
   "Sun Oct 04 20:49:23 EEST 2015",
   "Sun Oct 04 21:05:25 EEST 2015",
   "Mon Sep 28 10:02:38 IDT 2015", 
   "Mon Sep 28 10:17:50 IDT 2015",
   "Mon Sep 28 10:39:48 IDT 2015")

我尝试使用函数将此变量读Dates入 R :as.Date()

as.Date(Dates,format = "%a %b %d %H:%M:%S %Z %Y")

但该过程失败,因为%Z输入中不支持参数。整个向量中的时区不同。关于时区正确读取数据的替代方法是什么?

4

1 回答 1

1

这个解决方案需要一些简化的假设。假设您的向量中有许多元素,最好的方法是使用时区偏移数据库来确定每次是什么(在选定的语言环境中,例如 GMT)。我使用的时区数据是来自https://timezonedb.com/download的 timezone.csv 文件

#Create sample data
Dates<-c(
  "Sun Oct 04 20:33:05 EEST 2015",
  "Sun Oct 04 20:49:23 EEST 2015",
  "Sun Oct 04 21:05:25 EEST 2015",
  "Mon Sep 28 10:02:38 IDT 2015", 
  "Mon Sep 28 10:17:50 IDT 2015",
  "Mon Sep 28 10:39:48 IDT 2015")

#separate timezone string from date/time info
no_timezone <- paste(substr(Dates, 1, 19), substr(Dates, nchar(Dates)-3, nchar(Dates)))
timezone <- as.data.frame(substr(Dates, 21, nchar(Dates)-5))
colnames(timezone) <- "abbreviation"

#reference timezone database to get offsets from GMT
timezone_db <- read.csv(file="timezonedb/timezone.csv", header=FALSE)
colnames(timezone_db) <- c("zone_id", "abbreviation", "time_start", "gmt_offset", "dst")
timezone_db <- timezone_db[timezone_db$dst == 0, ]
timezone_db <- unique(timezone_db[,c("abbreviation", "gmt_offset")])
timezone_db <- timezone_db[!duplicated(timezone_db$abbreviation), ]

#adjust all time to GMT
time_adjust <- merge(timezone, timezone_db, all.x=TRUE, by="abbreviation")
gmt_time <- strptime(no_timezone, format = "%a %b %d %H:%M:%S %Y", tz="GMT")

#final data
Dates_final <- gmt_time - time_adjust$gmt_offset

根据您的数据需要的精确程度,如有必要,请小心调整夏令时。另外,我对时区了解不多,但我注意到出于某种原因,某些时区可以有多个偏移量。在原始数据库中,出于某种原因,CLT(智利时间)可能与 GMT 相差 3-5 小时。

对于这个练习,我的代码只是从数据库中获取每个时区的第一个偏移量,并假设没有夏令时。如果您的工作不需要这样的精度,这可能就足够了,但是您应该以任何方式对您的工作进行 QA 和验证。

另外,请注意,此解决方案对于日期更改也应该是稳健的。例如,如果将时间从凌晨 1 点调整到晚上 11 点,则日期应回退一天。

于 2015-10-22T20:48:42.370 回答