根据时区更改时间,但R不识别CEST。

huangapple go评论51阅读模式
英文:

Changing the time based on the time zone but R doesn't recognize CEST

问题

我有一个长时间列表(UTC),我希望将其转换为本地时间,这里是CEST。为此,我一直在使用with_tz("2023-02-05 08:50:00", tzone = "Europe/Madrid")with_tz("2023-02-05 08:50:00", tzone = "CET"),但返回的时间比2023-02-05 09:50:00提前1小时。然而,在夏令时月份,R没有识别到我们实际上是在CEST,返回的时间应该是2023-02-05 10:50:00。我一直在使用OlsonNames()来获取所有被识别的时区的列表,但似乎找不到CEST,有人有解决方法或替代方法吗?

英文:

I have a long list of times (UTC) which I wish to convert to local time, which in this instance is CEST. To do this I have been using with_tz("2023-02-05 08:50:00", tzone = "Europe/Madrid") or 'with_tz("2023-02-05 08:50:00", tzone = "CET")' which returns a time 1 hour forward of 2023-02-05 09:50:00 however during the daylight savings months R doesn't recognize that we are in fact in CEST and the time being returned should be 2023-02-05 10:50:00. Ihave been using 'OlsonNames()' to get a list of all the recognized timezones but cant seem to see CEST on there so does anyone have a fix or an alternative for this?

答案1

得分: 1

这似乎运行正常,并在适当的情况下识别CET与CEST:

t1 <- as.POSIXct("2023-05-22 11:11:11", format="%Y-%m-%d %H:%M:%OS", tz = "UTC")
t2 <- as.POSIXct("2023-02-22 11:11:11", format="%Y-%m-%d %H:%M:%OS", tz = "UTC")
lubridate::with_tz(c(t1, t2), tzone = 'Europe/Berlin')
[1] "2023-05-22 13:11:11 CEST" "2023-02-22 12:11:11 CET" 

同样适用于以下情况:

lubridate::with_tz(c(t1, t2), tzone = 'Europe/Madrid')
[1] "2023-05-22 13:11:11 CEST" "2023-02-22 12:11:11 CET" 

也许需要检查您的日期或代码?或者提供一个可重现的示例。
英文:

This seems to work fine and recognise the CET vs CEST where appropriate:

t1 &lt;- as.POSIXct(&quot;2023-05-22 11:11:11&quot;, format=&quot;%Y-%m-%d %H:%M:%OS&quot;, tz = &quot;UTC&quot;)
t2 &lt;- as.POSIXct(&quot;2023-02-22 11:11:11&quot;, format=&quot;%Y-%m-%d %H:%M:%OS&quot;, tz = &quot;UTC&quot;)
lubridate::with_tz(c(t1, t2), tzone = &#39;Europe/Berlin&#39;)
[1] &quot;2023-05-22 13:11:11 CEST&quot; &quot;2023-02-22 12:11:11 CET&quot; 

also so does this:

lubridate::with_tz(c(t1, t2), tzone = &#39;Europe/Madrid&#39;)
[1] &quot;2023-05-22 13:11:11 CEST&quot; &quot;2023-02-22 12:11:11 CET&quot; 

Perhaps check your dates or code? or give a reproducible example.

huangapple
  • 本文由 发表于 2023年5月22日 19:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定