【发布时间】:2016-10-07 21:30:04
【问题描述】:
t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
这些似乎是同一时间:
> t.ct -t.lt
Time difference of 0 secs
> t.ct -t.st
Time difference of 0 secs
> str(t.ct)
POSIXct[1:1], format: "2009-01-04 21:19:00"
> str(t.lt)
POSIXlt[1:1], format: "2009-01-04 21:19:00"
> str(t.st)
POSIXlt[1:1], format: "2009-01-04 21:19:00"
>
但是这些似乎有不同的时区信息,这不是我所期望的:
> strftime(t.ct,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 -0500"
> strftime(t.lt,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 +1200"
> strftime(t.st,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 +1200"
>
我的 Mac 上的时区是:
> Sys.timezone()
[1] "America/New_York"
Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt 和 as.POSIXlt ignores tz argument 的问题似乎相关,但并没有为我澄清这一点。
我如何确定并使用它?
更新:
从下面用户 3293236 的回答来看,似乎应该始终声明字符串的时区,如果您正在解析“-hhmm”偏移量,则始终使用tz="UTC":
t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")
【问题讨论】: