是否可以更改 POSIXct 日期的时区组件?

Is it possible to change the timezone component of POSIXct dates?

我在 class = c("POSIXct", "POSIXt") 中存储了一系列日期。其中一些日期跨越英国时钟变化,导致连续几天的时区不同,这会导致在计算日期之间的天数时出现问题 例如29/3/2015 的时钟更改将时区从 GMT 更改为 BST

"2015-03-29 GMT" "2015-03-30 BST"

我可以强制我所有的日期都在同一个时区而不弄乱日期吗?

我试过使用 as.Date(),当所有日期都在同一时区时它工作正常,但当日期跨时钟变化时它会导致问题。

这是一个可重现的例子(可能只在英国?):

df2 <- as.data.frame(
  structure(
    list(DateOfEntry = structure(c(1426809600, 1426896000, 
                                   1426982400, 1427068800, 
                                   1427155200, 1427241600, 
                                   1427328000, 1427414400, 
                                   1427500800, 1427587200, 
                                   1427670000, 1427756400), 
                                 class = c("POSIXct", "POSIXt"), 
                                 tzone = "", label = "Date of entry")), 
    class = "data.frame", .Names = c("DateOfEntry"), 
    row.names = c(NA, -12L)))
df2$new_date <- as.Date(df2$DateOfEntry)

我得到了这个输出(在英国,时钟在 2015 年 3 月 29 日更改):

DateOfEntry  new_date
2015-03-20   2015-03-20
2015-03-21   2015-03-21
2015-03-22   2015-03-22
2015-03-23   2015-03-23
2015-03-24   2015-03-24
2015-03-25   2015-03-25
2015-03-26   2015-03-26
2015-03-27   2015-03-27
2015-03-28   2015-03-28
2015-03-29   2015-03-29
2015-03-30   2015-03-29
2015-03-31   2015-03-30

注意 29/3/2015 的重复以及 DateofEntry==2015-03-30new_date==2015-03-29

之间的 1 天差异

您尝试过 lubridate 功能吗? with_tzforce_tz 使用时区。