当我们切换到夏令时并返回时处理日期 [英] Handling dates when we switch to daylight savings time and back
问题描述
我想使用R进行时间序列分析。我想建立一个时间序列模型,并使用来自timeDate和Forecast包的函数。
I would like to use R for time series analysis. I want to make a time-series model and use functions from the packages timeDate and forecast.
我在CET时区有盘中数据(15分钟数据,4个数据每小时积分)。在3月31日实行夏令时,我错过了通常的96个数据点中的4个数据点。在10月28日,由于切换回时间,我有4个数据点过多。
I have intraday data in the CET time zone (15 minutes data, 4 data points per hour). On March 31st daylight savings time is implemented and I am missing 4 data points of the 96 that I usually have. On October 28th I have 4 data points too many as time is switched back.
对于我的时间序列模型,我总是需要96个数据点,否则盘中的季节性会变得混乱。
For my time series model I always need 96 data points, as otherwise the intraday seasonality gets messed up.
您对此有任何经验吗?您是否知道R函数或软件包可以自动执行此类数据处理?
谢谢!
Do you have any experiences with this? Do you know an R function or a package that would be of help to automat such data handling - something elegant? Thank you!
推荐答案
我对来自传感器的水文数据也遇到了类似的问题。我的时间戳记为UTC + 1(CET),但未切换为夏令时(UTC + 2,CEST)。因为我不希望数据减少一小时(如果使用UTC,情况会如此),所以我将%z
转换规范设为 strptime
。在?strptime
中,您会找到:
I had a similar problem with hydrological data from a sensor. My timestamps were in UTC+1 (CET) and did not switch to daylight saving time (UTC+2, CEST). As I didn't want my data to be one hour off (which would be the case if UTC were used) I took the %z
conversion specification of strptime
. In ?strptime
you'll find:
% z
距UTC的小时数和分钟数的符号偏移量,因此-0800比UTC落后8小时
。
%z
Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC.
例如:在2012年,从标准时间切换到夏令时发生在2012-03-25,因此这一天没有02:00。如果您尝试将 2012-03-25 02:00:00转换为POSIXct对象,则
For example: In 2012, the switch from Standard Time to DST occured on 2012-03-25, so there is no 02:00 on this day. If you try to convert "2012-03-25 02:00:00" to a POSIXct-Object,
> as.POSIXct("2012-03-25 02:00:00", tz="Europe/Vienna")
[1] "2012-03-25 CET"
您不会收到错误或警告,只是获得日期而没有时间(此行为已记录)。
you don't get an error or a warning, you just get date without the time (this behavior is documented).
使用 format =%z
可获得所需的结果:
Using format = "%z"
gives the desired result:
> as.POSIXct("2012-03-25 02:00:00 +0100", format="%F %T %z", tz="Europe/Vienna")
[1] "2012-03-25 03:00:00 CEST"
为了方便导入,我编写了一个小函数适当的默认值:
In order to facilitate this import, I wrote a small function with appropriate defaults values:
as.POSIXct.no.dst <- function (x, tz = "", format="%Y-%m-%d %H:%M", offset="+0100", ...)
{
x <- paste(x, offset)
format <- paste(format, "%z")
as.POSIXct(x, tz, format=format, ...)
}
> as.POSIXct.no.dst(c("2012-03-25 00:00", "2012-03-25 01:00", "2012-03-25 02:00", "2012-03-25 03:00"))
[1] "2012-03-25 00:00:00 CET" "2012-03-25 01:00:00 CET" "2012-03-25 03:00:00 CEST"
[4] "2012-03-25 04:00:00 CEST"
这篇关于当我们切换到夏令时并返回时处理日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!