跨 DST 日期的 XTS 时间子选择 Linux 与 Windows [英] XTS time subselect across DST date Linux vs Windows

查看:18
本文介绍了跨 DST 日期的 XTS 时间子选择 Linux 与 Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 上,以下子选择代码生成不正确的 XTS 对象,但在我的 Ubuntu 机器上正常工作

On Windows the following subselect code produces an incorrect XTS object, but works correctly on my Ubuntu machine

library(xts)
theTimes <- seq(from=as.POSIXct('2016-03-10 12:00:00 CDT'),
              to=as.POSIXct('2016-03-20 12:10:00 CDT'),by=60)

ExampleData <- xts(rep(1,length(theTimes)),theTimes)
CutExampleData <- ExampleData['T02:00/T16:00']
any(duplicated(index(CutExampleData)))  ## Evaluates to TRUE on windows (incorrect) and FALSE on Ubuntu (correctly)

Linux PC 上的会话信息:

SessionInfo on Linux PC:

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RMySQL_0.10.9 DBI_0.5-1     Quandl_2.8.0  xts_0.9-7     zoo_1.7-13   

loaded via a namespace (and not attached):
[1] httr_1.2.1      R6_2.1.3        tools_3.3.1     grid_3.3.1      jsonlite_1.0    lattice_0.20-33

Windows PC 上的会话信息:

SessionInfo on Windows PC:

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.9-7  zoo_1.7-13

loaded via a namespace (and not attached):
[1] grid_3.3.2      lattice_0.20-34

我认为这与夏令时有关,因为索引跨越了最近的切换日期.有什么想法吗?

I think this has to do with DST, since the index spans the recent switch over date. Any ideas?

推荐答案

我能够在我的 Windows 机器上复制这个.在 R 的 *nix 和 Windows 版本之间,strptime 和/或 as.POSIXct.POSIXlt 看起来不正确.问题之所以出现是因为您的开始时间是 02:00:00,在 2016-03-13 不存在,因为由于夏令时,时间从美国/芝加哥时区的 01:59:59.999 到 03:00:00.

I was able to replicate this on my Windows machine. It looks like an infelicity in strptime and/or as.POSIXct.POSIXlt between *nix and Windows versions of R. The problem manifests because your start time is 02:00:00, which doesn't exist on 2016-03-13 because times go from 01:59:59.999 to 03:00:00 in the America/Chicago timezone due to daylight saving time.

一种解决方法是将您的开始时间设置为就在 02:00:00 之前.

A work-around is to set your start time to just before 02:00:00.

library(xts)
theTimes <- seq(from=as.POSIXct('2016-03-12 00:00:00', tz="America/Chicago"),
                to=as.POSIXct('2016-03-14 23:00:00', tz="America/Chicago"), by=60)
ExampleData <- xts(rep(1,length(theTimes)),theTimes)
# 01:59 instead of 02:00 to avoid DST issue
CutExampleData <- ExampleData['T01:59/T16:00']
anyDuplicated(index(ExampleData))
anyDuplicated(index(CutExampleData))  # 0 (no duplicates)

另请注意,CDT"不是在 R 中指定时区的好方法.三个字母的时区缩写(GMT"和UTC"除外)可能不明确,因此最好使用 Region/城市规范.

Also note that "CDT" is not a good way to specify a timezone in R. The three-letter timezone abbreviations (aside from "GMT" and "UTC") may be ambiguous, so it's better to use the Region/City specification.

这篇关于跨 DST 日期的 XTS 时间子选择 Linux 与 Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆