jlp
jlp

Reputation: 175

Changing time zone in xts taking care of dst (daylight saving time)

Suppose we have these three dates:

original_dates<-  c("2015-12-31T07:00:00", "2015-12-31T08:00:00", "2015-12-31T09:00:00")

and this vector:

vector<- c("a", "b", "c")

We transform the dates to POSIXct format:

original_dates2<- as.POSIXct(original_dates, format="%Y-%m-%dT%H", tz = "GMT")

and build a xts:

my_xts<- xts(vector, order.by = original_dates2, frequency = "hourly")

so we have the following xts object:

> my_xts
                    [,1]
2015-12-31 07:00:00 "a" 
2015-12-31 08:00:00 "b" 
2015-12-31 09:00:00 "c" 
Warning message:
timezone of object (GMT) is different than current timezone ().

If I want to change to local time I would change the time zone:

indexTZ(my_xts)<- "America/Los_Angeles"

but this yields a wrong result because I know that 2015-12-31 07:00:00 GMT should be equal to 2015-12-31 00:00:00 LA time (i.e. 7 h. before), not 2015-12-30 23:00:00 (i.e. 8 h. before)

> my_xts
                    [,1]
2015-12-30 23:00:00 "a" 
2015-12-31 00:00:00 "b" 
2015-12-31 01:00:00 "c" 
Warning message:
timezone of object (America/Los_Angeles) is different than current timezone (). 

I guess this happens because the timezone conversion indexTZ(my_xts)<- "America/Los_Angeles" does not consider the daylight savings time (dst), as shown by the dst function:

> dst(my_xts)
[1] FALSE FALSE FALSE

The question is, how can I change the timezone such that the dst is taken care of?

More details:

> Sys.timezone()
[1] "Europe/Paris"
> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32                                
version.string R version 3.3.1 (2016-06-21)

Upvotes: 0

Views: 365

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241485

You said:

but this yields a wrong result because I know that 2015-12-31 07:00:00 GMT should be equal to 2015-12-31 00:00:00 LA time (i.e. 7 h. before), not 2015-12-30 23:00:00 (i.e. 8 h. before)

That is incorrect. The Pacific Time zone is UTC-8 during standard time, and UTC-7 during daylight time. In December, standard time is in effect. Therefore, the result you see is as expected.

Upvotes: 2

Related Questions