lubridate:与时区不一致的行为

lubridate: inconsistent behavior with timezones

考虑以下示例

 library(lubridate)
 library(tidyverse)

> hour(ymd_hms('2008-01-04 00:00:00'))
[1] 0

现在,

dataframe <- data_frame(time = c(ymd_hms('2008-01-04 00:00:00'),
                                 ymd_hms('2008-01-04 00:01:00'),
                                 ymd_hms('2008-01-04 00:02:00'),
                                 ymd_hms('2008-01-04 00:03:00')),
                        value = c(1,2,3,4))

mutate(dataframe,hour = strftime(time, format="%H:%M:%S"),
hour2 = hour(time)) 

# A tibble: 4 × 4
                 time value     hour hour2
               <dttm> <dbl>    <chr> <int>
1 2008-01-03 19:00:00     1 19:00:00    19
2 2008-01-03 19:01:00     2 19:01:00    19
3 2008-01-03 19:02:00     3 19:02:00    19
4 2008-01-03 19:03:00     4 19:03:00    19

这是怎么回事?为什么日期会转换成我不知道的当地时间?

这不是 lubridate 的问题,而是 POSIXct 值组合成向量的方式的问题。

你有

> ymd_hms('2008-01-04 00:01:00')
[1] "2008-01-04 00:01:00 UTC"

但是当组合成一个向量时你会得到

> c(ymd_hms('2008-01-04 00:01:00'), ymd_hms('2008-01-04 00:01:00'))
[1] "2008-01-03 19:01:00 EST" "2008-01-03 19:01:00 EST"

原因是 tzone 属性在组合 POSIXct 值时丢失(请参阅 c.POSIXct)。

> attributes(ymd_hms('2008-01-04 00:01:00'))
$tzone
[1] "UTC"

$class
[1] "POSIXct" "POSIXt"

但是

> attributes(c(ymd_hms('2008-01-04 00:01:00')))
$class
[1] "POSIXct" "POSIXt"

您可以使用

> ymd_hms(c('2008-01-04 00:01:00', '2008-01-04 00:01:00'))
[1] "2008-01-04 00:01:00 UTC" "2008-01-04 00:01:00 UTC"

这将为所有参数使用默认值 tz = "UTC"

您还需要将 tz = "UTC" 传递给 strftime,因为它的默认时区是您当前的时区(与默认为 UTC 的 ymd_hms 不同)。