Debian没有在R中考虑夏令时

时间:2017-06-12 08:00:31

标签: r timezone posix dst

我使用以下代码来查找变量年,月和日在中欧是否正在使用夏令时。

timeString = paste(toString(year), formatC(month, width = 2, flag="0"), formatC(day, width = 2, flag="0"), "12", sep = "-")
time = strptime(timeString, format = "%Y-%m-%d-%H")
diff = as.numeric(as.POSIXct(time, tz="UTC") - as.POSIXct(time, tz="Europe/Prague"))

在我的电脑上(Ubuntu 16.04),当夏令时处于活动状态时,差异为2,其他为1,但在使用Debian 8.8的服务器上,在所有情况下均为1。你知道如何设置服务器以表现为Ubuntu?感谢。

更新:Debian时间设置的更改也会改变crontab使用的时间,这是不可取的。使用新配置重新安装R似乎存在风险,因为每隔几分钟就会运行一些R脚本。所以我选择了R函数形式的“丑陋”解决方案:

DaylightSaving = function(year, month, day) {
      # years 2010-2030
      if (year < 2010 || year > 2030) {
        stop("The function is implemented now only for years 2010-2030")
      }
      dayStart = c(28, 27, 25, 31, 30, 29, 27, 26, 25, 31, 29, 28, 27, 26, 
    31, 30, 29, 28, 26, 25, 31)
      dayEnd = c(31, 30, 28, 27, 26, 25, 30, 29, 28, 27, 25, 31, 30, 29, 
    27, 26, 25, 31, 29, 28, 27)
      if (month < 3 || month > 10) {
        return(FALSE)
      } else if (month == 3 && day < dayStart[year - 2009]) {
        return(FALSE)
      } else if (month == 10 && day >= dayEnd[year - 2009]) {
        return(FALSE)
      }
      return(TRUE)
    }

1 个答案:

答案 0 :(得分:0)

首先,如果您想检查夏令时是否正在使用,您可以执行以下操作:

#Make a test date
atime <- as.POSIXct("2017-05-23 13:25", 
                    tz = "Europe/Prague")
#test for DST
as.POSIXlt(atime)$isdst > 0

POSIXlt类在内部是一个元素isdst的列表,如果夏令时未激活,则为0,如果是,则为正,如果该信息不可用则为负。 (见?DateTimeClasses)。

我还想从时区的帮助页面中指出以下内容:

  

请注意,除非更换,否则时区的操作是操作系统   服务,甚至在更换第三方数据库的地方使用和   可以更新(请参阅“时区名称”部分)。不正确   结果永远不会成为R问题,所以请确保您拥有   礼貌不要责怪他们。

问题不是R,而是你的Debian安装无视夏令时。您可以通过使用选项--with-internal-tzcode配置R来解决此问题,因此它使用自己的时区数据库。但是,如果Debian的时区系统设置正确,通常不需要这样做。有关如何配置R的更多信息,请参阅帮助页面?timezonesInstallation and Administration manual - appendix B

解决此问题的最佳方法是确保Debian安装正确处理夏令时。您可以先检查一下tzdata package是否具有正确的版本。

在unix.stackexchange.com上有一个类似的问题:

https://unix.stackexchange.com/questions/274878/system-disregards-daylight-saving-time

相关问题