问题描述
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")
这些似乎是相同的时间:
These seem to be the same times:
> 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"
>
但是这些似乎在其中具有不同的时区信息,这不是我期望的:
But these appear to have different timezone information in them, and it is not what I'd expect:
> 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上的时区为:
The timezone on my Mac is:
> Sys.timezone()
[1] "America/New_York"
问题和似乎是相关的,但没有为我澄清这一点。
The questions Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt and as.POSIXlt ignores tz argument seemed related, but didn't clarify this for me.
如何确定地设置时间并使用它?
How do I definitively set a time and use it?
更新:
根据下面的user3293236的回答,似乎应该始终声明字符串的时区,如果正在解析 -hhmm偏移量,然后始终使用 tz = UTC
:
From user3293236's answer below, it seems one should always declare the timezone of the string, and if you are parsing the '-hhmm' offset, then always use 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")
推荐答案
如果不专门使用时区,则POSIXct和POSIXlt将参考您当地的时区。但是,这并不完全可靠。 POSIXlt不会在输出字符串中显示时区。
If you do not use a timezone specifically, POSIXct and POSIXlt will reference to your local timezone. However, this is not entirely reliable. POSIXlt will not display the timezone in the output string.
注意,未设置tzone参数。
Note, the tzone argument is not set.
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.ct
t.lt
attr(t.ct,"tzone") #""
attr(t.lt,"tzone") #NULL
如果您确实想避免歧义行为,则必须指定一个时区。输出字符串仍将不同(默认情况下POSIXlt不显示任何时区),但是属性相同
If you do want to avoid ambiguous behaviour, you have to specifiy a time zone. The output string will still be different (by default POSIXlt shows no timezone), but the attribute is the same
t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.ct
t.lt
attr(t.ct,"tzone") #Europe/Helsinki
attr(t.lt,"tzone") #Europe/Helsinki
现在,如果您想在原始任务后更改时区:
Now, if you want to change time zones after the original assignment:
attr(t.ct, "tzone") <- "UTC" #this will SHIFT the time zone to UTC
attr(t.lt, "tzone") <- "UTC" #this will REPLACE the time zone to UTC
t.ct
t.lt
关于 strftime
和%z
,这不会为您提供时区属性。您情况的不同可能是字符串格式,对象转换和时区格式(IMO)的组合。但也许有人更了解,可以澄清这一点。
As for your problem with strftime
and %z
, this does not give you the time zone attribute. The difference in your case, probably comes from a combination of string formatting, object conversions and time zone formating, IMO. But maybe somebody more knowledgable, can clarify this.
这篇关于在R中的POSIXct,POSIXlt,strptime等中确定并设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!