[R-SIG-Finance] getting xts endpoints() and apply.daiy() to work with local TZ?

Christian Gunning icos.atropa at gmail.com
Wed Jul 8 08:26:47 CEST 2009

I'm looking for the best way to get apply.daily() to use breaks
according to local TZ.  Sorry, i know timezone handling is perennial
issue.  I reviewed R News 4/1, R-sig-fin, ?indexTZ, and
?DateTimeClasses and xts R source.

Currently, apply.daily() in a non-GMT timezone computes breaks
according to GMT (A note about this in apply.daily() or endpoints()
might preempt user confusion?).

Below, I compute the difference between local time and GMT in seconds,
and add it into the call to which() in endpoints()  to get my desired
answer. Is endpoints() support of TZ on the map, and is there a better
way than this to add it (particularly, calculating tmpdiff below)? Is
this a bad idea altogether?

christian gunning
university of new mexico

oldtz = Sys.getenv('TZ')

tmpind = as.POSIXct(seq(from=0, to=5*24*60*60-60, by=60*60),

tmpx = xts(1:(5*24), tmpind)
indexTZ(tmpx)      ### correctly reports TZ as "GMT+7"
tmpind[1]          ### format.POSIXlt? reports TZ as "GMT",
tmpx[endpoints(tmpx, on='days')]  ### daybreaks as per GMT

### from endpoints(), added tmpdiff, daybreaks as per local TZ
tmpdiff = as.integer(as.POSIXct(0, origin='2000-01-01', tz='GMT')) -
as.integer(as.POSIXct(0, origin='2000-01-01'))

tmpdays = as.integer(c(0,
which(diff((.index(tmpx)+tmpdiff)%/%86400L%/%k + 1) != 0),



More information about the R-SIG-Finance mailing list