[R-sig-finance] Date calculations

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Wed Mar 22 08:14:02 CET 2006


P. Hénaff wrote:

>I'm a rank beginner in R and R-metrics, but experienced in matlab and other languages. I'm strugling with date arithmetics in R. I'm doing statistical analysis on financial time series, using its, which uses POSIXct objects to measure time. What is the best way to perform simple date arithmetic with these objects? I do not care about h/m/s, nor do I care about daylight saving time. Example of a problem I faced:
>
>Add one day to 3-apr-2005. (daylight saving starts on 4-apr-2005).
>If I do
>  
>
>>as.POSIXct("2005-04-03") + 86400
>>    
>>
in Rmetrics use:

myFinCenter = "GMT" # for daily dates
timeDate("2005-04-03")
[1] "GMT"
[1] [2005-04-03]
timeDate("2005-04-03") + 3*24*3600
[1] "GMT"
[1] [2005-04-06]

# Living in Zurich
myFinCenter = "Zurich"
timeDate("2005-04-03")
[1] "Zurich"
[1] [2005-04-03 02:00:00]
timeDate("2005-04-03") + 3*24*3600
[1] "Zurich"
[1] [2005-04-06 02:00:00]

# Use for Daily dates:
timeDate("2005-04-03", zone = "Zurich")
[1] "Zurich"
[1] [2005-04-03]
timeDate("2005-04-03", zone = "Zurich") + 3*24*3600
[1] "Zurich"
[1] [2005-04-06]


>I get 1 a.m. on the 4th of april.
>
>So, I do something like
>  
>
>>dt1 <- as.POSIXct("2005-04-03")
>>x1 <- timeDate(dt1)
>>x2 <- timeNdayOnOrAfter(x1, n=2)
>>dt2 <- as.POSIXct(as.POSIXlt(x2))
>>    
>>
>
>There has to be something simpler!
>
>Another problem. The tseries and timeDate packages seem to require that one changes his timezone environment variable to GMT. This is not really possible in a corporate environment. Is there a work-around?
>  
>
It is no longer necessary to specify the timezone. This holds since the 
latest
version 221.10065 (download it from CRAN).
For daily dates just be sure that the time zone where you recorded the data
(zone=...) and the time zone where you use your data (FinCenter=..) are
specified as the same. both GMT, or the financial Center where you are
living and your operating system's time zone environment variable is set.
(Note, this is not necessarily the same timezone your clock works on your
windows box, which doesn't make use of the Windows time zone environment
variable TZ).

Please let me know if you have further questions (or problems) with 
timeDate()
Diethelm

>Any help will be much appreciated.
>
>Patrick Henaff
>
>	[[alternative HTML version deleted]]
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>



More information about the R-sig-finance mailing list