如何从 POSIXct 和 POSIXlt 对象中提取正确的时区?
time1 = as.POSIXlt("2010-07-01 16:00:00", tz="Europe/London")
time1
# [1] "2010-07-01 16:00:00 Europe/London"
但是
time2 = as.POSIXct("2010-07-01 16:00:00", tz="Europe/London")
time2
# [1] "2010-07-01 16:00:00 BST"
为什么时区的显示方式不同呢?这对我来说很重要,因为我需要从日期中提取时区。
base::format(time1, format="%Z")
# [1] "BST"
base::format(time2, format="%Z")
# [1] "BST"
两者都为英国节省时间提供相同的“BST”!
问题是“BST”不能被 POSIXct/POSIXlt 格式识别:
as.POSIXlt("2010-07-01 16:00:00", tz="BST")
# [1] "2010-07-01 16:00:00 BST"
# Warning messages:
# 1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
# unknown timezone 'BST'
# 2: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 3: In strptime(x, f, tz = tz) : unknown timezone 'BST'
as.POSIXct("2010-07-01 16:00:00", tz="BST")
# [1] "2010-07-01 16:00:00 GMT"
# Warning messages:
# 1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
# unknown timezone 'BST'
# 2: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 3: In strptime(x, f, tz = tz) : unknown timezone 'BST'
# 4: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 5: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST'
我真的很困惑。 我有 2 个问题:
1/ POSIXct 和 POSIXlt 格式之间有什么区别
2/ 有人知道我可以使用哪个时区吗?
“欧洲/伦敦” 适用于 POSIXlt,但不适用于 POSIXct。另外,它无法使用 base::format 从时间中提取
“BST” 在 as.POSIXct
或 as.POSIXlt
函数中未被识别为有效时区。
time1 = as.POSIXlt("2010-07-01 16:00:00", tz="Europe/London")
time1
# [1] "2010-07-01 16:00:00 Europe/London"
but
time2 = as.POSIXct("2010-07-01 16:00:00", tz="Europe/London")
time2
# [1] "2010-07-01 16:00:00 BST"
Why is the timezone presented differently? It is important for me because I need to extract the time zones from my date.
base::format(time1, format="%Z")
# [1] "BST"
base::format(time2, format="%Z")
# [1] "BST"
both give the same "BST" for British Saving Time!
The issue is that "BST" does not seam to be recognized by POSIXct/POSIXlt format:
as.POSIXlt("2010-07-01 16:00:00", tz="BST")
# [1] "2010-07-01 16:00:00 BST"
# Warning messages:
# 1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
# unknown timezone 'BST'
# 2: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 3: In strptime(x, f, tz = tz) : unknown timezone 'BST'
as.POSIXct("2010-07-01 16:00:00", tz="BST")
# [1] "2010-07-01 16:00:00 GMT"
# Warning messages:
# 1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
# unknown timezone 'BST'
# 2: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 3: In strptime(x, f, tz = tz) : unknown timezone 'BST'
# 4: In structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) :
# unknown timezone 'BST'
# 5: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST'
I am really confused.
I have 2 questions:
1/ What is the difference between POSIXct and POSIXlt formats
2/ Any one knows what time zone I can use?
"Europe/London" works with POSIXlt but not POSIXct. Plus it cannot be extracted from a time using base::format
"BST" is not recognized as a valid timezone in as.POSIXct
or as.POSIXlt
functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Koshke 已经向您展示了
您可以使用
attr()
以标准化方式获取时区。这将以zone.tab
文件中指定的形式获取时区,R 使用该文件来定义时区(更多信息参见?timezones
)。例如:
虽然 POSIXct 使用与 POSIXlt 不同的时区指示,但属性是相同的,我感到非常惊讶。显然,这个“BST”仅在打印 POSIXct 时才会弹出。在打印之前,POSIXct 再次转换为 POSIXlt,并且 tzone 属性用同义词进行修改:
这发生在 内部 R 函数
as.POSIXlt
下游的某个地方,我将其由于有更尖锐的问题需要解决,暂时无法查看。但请随意浏览一下,看看到底发生了什么。顺便说一句,在我的 Windows 7 / R 2.13.0 安装中,“BST”不被识别为时区(并且在 zone.tab 中也没有提及)。
@Koshke showed you already
You can get the timezone out in a standardized manner using
attr()
. This will get the timezone in the form specified in thezone.tab
file, which is used by R to define the timezones (More info in?timezones
).eg :
I am quite amazed though that POSIXct uses different indications for the timezones than POSIXlt, whereas the attributes are equal. Apparently, this "BST" only pops up when the POSIXct is printed. Before it gets printed, POSIXct gets converted again to POSIXlt, and the tzone attribute gets amended with synonyms :
This happens somewhere downstream of the internal R function
as.POSIXlt
, which I'm not able to look at for the moment due to more acute problems to solve. But feel free to go through it and see what exactly is going on there.On a sidenote, "BST" is not recognized as a timezone (and it is not mentioned in zone.tab either) on my Windows 7 / R 2.13.0 install.
也许,对对象进行
unclass
可以帮助您检查差异:因此,POSIXlt 将日期包含为组件列表,而 POSIXct 将其包含为数字,即 UNIX 纪元时间。
至于时区,就超出了R的范围。
请参阅 http://en.wikipedia.org/wiki/Tz_database 中的解释
至于
我怀疑 的不同行为是 as.POSIXct 中的一个错误,它不处理 tz 参数。
perhaps,
unclass
ing the objects helps you to inspect the differences:thus, the POSIXlt contains the date as a list of component, while the POSIXct contains it as a numeric, i.e., UNIX epoch time.
As for the timezone, it would be beyond the scope of R.
See the explanation in http://en.wikipedia.org/wiki/Tz_database
As for the different behavior of
I suspect there is a bug in as.POSIXct, which does not process the tz argument.
POSIXct
是自纪元以来的秒数< /a>POSIXlt
将日期时间拆分为%Y-%m-%d
或%Y/%m/%d %H:%M:%S
或其他此类格式POSIXct
is seconds since the epochPOSIXlt
splits datetimes into%Y-%m-%d
or%Y/%m/%d %H:%M:%S
or other such formats