R 中的时区:如何避免含糊不清的术语,例如 EST? [英] timezones in R: how to avoid ambiguous terms such as EST?

查看:40
本文介绍了R 中的时区:如何避免含糊不清的术语,例如 EST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一系列字符时间戳.当我使用直观的方法将它们的类更改为 POSIXct 时,R 会分配不明确的时区 EST.

I have a series of character timestamps in R. When I change their class to POSIXct using intuitive methods, R assigns the ambiguous timezone EST.

例如:

as.POSIXct("2012-08-06 15:32:00") 
as.POSIXct("2012-08-06 15:32:00", tz = "Australia/Brisbane") 
as.POSIXct("2012-08-06 15:32:00", tz = "") 

在我的两个(Mac 和 Windows)机器上都产生相同的输出:

all produce the same output on my two (Mac and Windows) boxes:

"2012-08-06 15:32:00 EST"

这里的问题是 EST 可以是任意数量的时区:美国东部标准时间,或澳大利亚东部标准时间,或加拿大的其他时区(来自 ?timezone):

The problem here is EST could be any number of timezones: Eastern Standard Time in the USA, or Australian Eastern Standard Time, or another timezone in Canada (from ?timezone):

请注意,其中一些名称可能与您的想法不同:在特定的 EST 是加拿大使用的没有夏令时的时区时间,而不是 EST5EDT 或(澳大利亚)东部标准时间.

Beware that some of these designations may not be what you think: in particular EST is a time zone used in Canada without daylight savings time, and not EST5EDT nor (Australian) Eastern Standard Time.

有一种设置时区的方法可以避免这个 EST 标签.R ?timezone 帮助中提到了它,但没有完全解释.将 x 设置为好奇号登陆火星的时间,如 澳大利亚新闻服务:

There is a method to set the timezone which avoids this EST label. It is alluded to, but not fully explained in the R ?timezone help. Setting x as the time of the Curiosity landing on Mars as reported by an Australian news service:

x <- as.POSIXct("2012-08-06 15:32:00", tz = "Etc/GMT-10")
x
"2012-08-06 15:32:00 GMT-10"

我们可以通过将其转换为美国时区并检查 加州新闻报道:

And we can test that this is correct by converting it to a US timezone and checking with a Californian news report:

y <- format(x, tz = "America/Los_Angeles")
y
"2012-08-05 22:32:00"

如果使用此 Etc/GMT+nEtc/GMT-n 符号,请注意 ?timezone 中的以下警告:

If using this Etc/GMT+n or Etc/GMT-n notation, please beware of the following caveat from ?timezone :

许多系统支持 GMT+n 和 GMT-n 形式的时区,它们是以与 UTC 的固定偏移量(因此没有 DST).与某些用法相反(但与 PST8PDT 等名称一致),负偏移是时间UTC 之前(东部),正偏移是落后(西部)的时间世界标准时间.

Many systems support timezones of the form GMT+n and GMT-n, which are at a fixed offset from UTC (hence no DST). Contrary to some usage (but consistent with names such as PST8PDT), negative offsets are times ahead of (east of) UTC, positive offsets are times behind (west of) UTC.

推荐答案

第一个示例中的第一行和第三行产生相同的输出,因为 tz=""as 的默认值.POSIXct.第二行更有趣,因为时区是明确定义的.

The 1st and 3rd lines in your first example produce the same output because tz="" is the default for as.POSIXct. The second line is more interesting because the timezone is explicitly defined.

但请注意,"EST" 只是默认打印时区的方式.tzone 属性仍然是明确的.

But note that "EST" is only how the timezone is printed by default. The tzone attribute is still unambiguous.

R> x <- as.POSIXct("2012-08-06 15:32:00", tz="Australia/Brisbane")
R> x
[1] "2012-08-06 15:32:00 EST"
R> attr(x, "tzone")
[1] "Australia/Brisbane"

这篇关于R 中的时区:如何避免含糊不清的术语,例如 EST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆