【发布时间】:2016-09-09 08:42:41
【问题描述】:
我无法理解 R 中如何处理时区字符串以及为什么 Sys.time() 使用与 Sys.timezone() 不同的字符串?
Sys.time() 返回:
[1]“2016-05-13 10:17:04 CEST”
as.POSIXct(Sys.time()) 工作:
[1]“2016-05-13 10:17:11 CEST”
as.POSIXct("2016-05-1 10:15:21 CEST") 也可以:
[1]“2016-05-01 10:15:21 CEST”
但是,as.POSIXct("2016-05-1 10:15:21", tz = "CEST") 确实不工作:
[1]“2016-05-01 10:15:21 GMT”
警告信息:
1: 在 strptime(xx, f 2:在 as.POSIXct.POSIXlt(x) 中:未知时区“CEST”
3:在 strptime(x, f, tz = tz) 中:未知时区“CEST”
4:在 as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) 中:未知时区 'CEST'
5:在 as.POSIXlt.POSIXct(x, tz) 中:未知时区 'CEST'
我知道我可以通过以下方式解决问题:
Sys.timezone(location = TRUE)
[1]“欧洲/柏林”
然后as.POSIXct("2016-05-1 10:15:21", tz = Sys.timezone(location = TRUE)) 返回:
[1]“2016-05-01 10:15:21 CEST”
但我想了解为什么上面的方法会这样? 假设我有来自不同时区(比如纽约)的数据,我怎么知道要使用哪个字符串?
编辑:
Matt 和 Adams 的链接提供了第二个问题的答案:可以在 Wikipedia 或 lubridate::olson.time.zones 上查看时区。
但是,主要问题仍未得到解答:为什么“2016-05-01 10:15:21 CEST”是有效的POSIXct class,而as.POSIXct("2016-05-1 10:15:21", tz = "CEST") 是不可能的。
【问题讨论】:
-
我觉得这很有帮助。 rstudio-pubs-static.s3.amazonaws.com/…
-
我有很好的 lubridate 经验。也许您正在寻找tz?