【问题标题】:DST correction when one time is local and one is GMT一次是本地时间,一次是格林威治标准时间时的 DST 校正
【发布时间】:2018-03-03 22:10:02
【问题描述】:

一个数据系列 (nycflights13::flights) 是当地时间,一个是 GMT (nycflights13::weather)。问题是以尊重 DST 的方式合并它们。如果我们查看 1 月 1 日,America/New_York 和 GMT 之间的时差应该是 5 小时。六月应该是4小时。在下面的示例中,我在 1 月和 6 月都得到了 5 小时的差异——unique(fw1$hour.y)unique(fw6$hour.y) 返回 17,但似乎 fw6$hour.y 应该是 16。我做错了什么?

library(tidyverse)
library(lubridate)
library(nycflights13)
weather$time_hour <- with_tz(weather$time_hour, 'GMT')
flights$time_hour <- force_tz(flights$time_hour, 'America/New_York')
fw <- left_join(flights, weather, by=c('origin', 'time_hour'))
fw1 <- filter(fw, origin == 'LGA', month.x == 1, day.x == 1, hour.x == 12)
unique(fw1$hour.y)

fw6 <- filter(fw, origin == 'LGA', month.x == 6, day.x == 1, hour.x == 12)
unique(fw6$hour.y)

【问题讨论】:

    标签: r lubridate


    【解决方案1】:

    我的理解是'time_hour'列应该由每个对象中的年、月、日、小时、列组成。但是当我查看“time_hour”与“month”列的月份时,我得到了奇怪的结果。

    library(lubridate)
    library(nycflights13)
    weather$time_hour <- with_tz(weather$time_hour, 'GMT')
    with(weather, table(month, month(time_hour)))
    
    month    1    2    3    4    5    6    7    8    9   10   11   12
       1  2229    0    0    0    0    0    0    0    0    0    0    0
       2     0 2010    0    0    0    0    0    0    0    0    0    0
       3     0    0 2227    0    0    0    0    0    0    0    0    0
       4     0    0    3 2156    0    0    0    0    0    0    0    0
       5     0    0    0    3 2229    0    0    0    0    0    0    0
       6     0    0    0    0    3 2157    0    0    0    0    0    0
       7     0    0    0    0    0    3 2225    0    0    0    0    0
       8     0    0    0    0    0    0    3 2214    0    0    0    0
       9     0    0    0    0    0    0    0    3 2156    0    0    0
       10    0    0    0    0    0    0    0    0    3 2209    0    0
       11    0    0    0    0    0    0    0    0    0    0 2138    0
       12    0    0    0    0    0    0    0    0    0    0    0 2159
    

    根据“月”列的值,您可以看到在夏令时期间有几天不是您所期望的。所以这似乎是源数据的问题......几乎就像应用了一个恒定的 4 小时偏移量,而不考虑夏令时。

    【讨论】:

    • 您的回答建议查看时间是如何在 nycflights13 源代码中生成的。我已经添加了对我认为正在发生的事情以及为什么我认为它在 nycflights13 的下一个版本中已修复的解释。
    • @RobertMcDonald 干得好!您应该从问题中删除更新并将其作为答案发布。然后,您可以单击答案旁边的复选标记图标,以便其他人知道该问题已得到解答。
    • 谢谢,我不确定什么是合适的,所以我很感激你的建议。
    • @RobertMcDonald:不客气。详情可以看Can I answer my own question?
    【解决方案2】:

    这似乎是nycflights13 2.2 版(可能更早)中的一个错误。文件weather.R 从下载的源天气文件生成月、日和小时。然后它在ISOdatetime() 中使用这些来生成time_hour 变量,但没有指定时区。

    这意味着,虽然原始天气数据是没有夏令时的 GMT,但在生成包时创建的time_hour 变量将包含在创建包的任何本地时区中指定的夏令时。在加载包时强制使用时区并不会改变夏令时已被纳入 time_hour 的事实。

    nycflights13 的当前开发版本在生成 time_hour 变量时指定了一个时区,因此在 nycflights13 的下一版本中应该不会出现此问题。

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 2011-02-20
      • 2011-05-13
      • 1970-01-01
      • 2011-09-10
      • 2017-02-11
      • 2013-03-10
      • 1970-01-01
      • 2010-12-02
      相关资源
      最近更新 更多