Search code examples
rlubridatedst

subtract periods and DST changes


I have data

library(data.table); library(lubridate)
dat <- data.table(t=as.POSIXct(c("2014-09-26 01:01:00","2014-09-26 02:01:00","2014-09-26 03:01:00"), tz="CET"))
> dat
                     t
1: 2014-09-26 01:01:00
2: 2014-09-26 02:01:00
3: 2014-09-26 03:01:00

and I would like to subtract 180 days. Because of DST change, the result using days(.) of lubridate is

> dat$t - days(180)
[1] "2014-03-30 01:01:00 CET"  NA                         "2014-03-30 03:01:00 CEST"

and I wonder whether there is a way of subtracting days that accounts for DST changes.


Solution

  • Subtract the number of seconds in 180 days:

    dat$t - 180*60*24*60
    [1] "2014-03-30 00:01:00 CET"  "2014-03-30 01:01:00 CET"  "2014-03-30 03:01:00 CEST"