如何从 POSIXct 和 POSIXlt 对象中提取正确的时区?

发布于 2024-11-08 15:39:47 字数 1671 浏览 0 评论 0原文

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.POSIXctas.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百思不得你姐 2024-11-15 15:39:47

@Koshke 已经向您展示了

  • 两种日期类型的内部表示的差异,并且
  • 在内部,两种时区规范是相同的。

您可以使用 attr() 以标准化方式获取时区。这将以 zone.tab 文件中指定的形式获取时区,R 使用该文件来定义时区(更多信息参见 ?timezones )。

例如:

> attr(time1,"tzone")
[1] "Europe/London"
> attr(time2,"tzone")
[1] "Europe/London"

虽然 POSIXct 使用与 POSIXlt 不同的时区指示,但属性是相同的,我感到非常惊讶。显然,这个“BST”仅在打印 POSIXct 时才会弹出。在打印之前,POSIXct 再次转换为 POSIXlt,并且 tzone 属性用同义词进行修改:

> attr(as.POSIXlt(time2),"tzone")
[1] "Europe/london" "GMT"           "BST"   

这发生在 内部 R 函数 as.POSIXlt 下游的某个地方,我将其由于有更尖锐的问题需要解决,暂时无法查看。但请随意浏览一下,看看到底发生了什么。

顺便说一句,在我的 Windows 7 / R 2.13.0 安装中,“BST”不被识别为时区(并且在 zone.tab 中也没有提及)。

@Koshke showed you already

  • the difference in internal representation of both date types, and
  • that internally, both timezone specifications are the same.

You can get the timezone out in a standardized manner using attr(). This will get the timezone in the form specified in the zone.tab file, which is used by R to define the timezones (More info in ?timezones ).

eg :

> attr(time1,"tzone")
[1] "Europe/London"
> attr(time2,"tzone")
[1] "Europe/London"

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 :

> attr(as.POSIXlt(time2),"tzone")
[1] "Europe/london" "GMT"           "BST"   

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.

苍景流年 2024-11-15 15:39:47

也许,对对象进行unclass可以帮助您检查差异:

> unclass(time1)
$sec
[1] 0

$min
[1] 0

... snip

$yday
[1] 181

$isdst
[1] 1

attr(,"tzone")
[1] "Europe/London"

> unclass(time2)
[1] 1277996400
attr(,"tzone")
[1] "Europe/London"

因此,POSIXlt 将日期包含为组件列表,而 POSIXct 将其包含为数字,即 UNIX 纪元时间。

至于时区,就超出了R的范围。
请参阅 http://en.wikipedia.org/wiki/Tz_database 中的解释

至于

as.POSIXct("2010-07-01 16:00:00", tz="BST")
as.POSIXlt("2010-07-01 16:00:00", tz="BST")

我怀疑 的不同行为是 as.POSIXct 中的一个错误,它不处理 tz 参数。

perhaps, unclassing the objects helps you to inspect the differences:

> unclass(time1)
$sec
[1] 0

$min
[1] 0

... snip

$yday
[1] 181

$isdst
[1] 1

attr(,"tzone")
[1] "Europe/London"

> unclass(time2)
[1] 1277996400
attr(,"tzone")
[1] "Europe/London"

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

as.POSIXct("2010-07-01 16:00:00", tz="BST")
as.POSIXlt("2010-07-01 16:00:00", tz="BST")

I suspect there is a bug in as.POSIXct, which does not process the tz argument.

别挽留 2024-11-15 15:39:47

1/ POSIXct 和 POSIXlt 格式有什么区别

1/ What is the difference between POSIXct and POSIXlt formats

  • POSIXct is seconds since the epoch
  • POSIXlt splits datetimes into %Y-%m-%d or %Y/%m/%d %H:%M:%S or other such formats
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文