bug report with fix for timeDate::timeNthNdayInMonth

David Winsemius dwinsemius at comcast.net
Fri Sep 23 16:45:50 CEST 2011


There wasa question on StackOverflow this morning that I think reveals  
a bug in timeNthNdayInMonth. Results of my investiagion reported there  
are below:

Link to question:

http://stackoverflow.com/questions/7525226/find-whether-a-particular-date-is-an-option-expiration-friday-problem-with-time


---- My response ----
I am guessing it's a timezone problem. What happens if you use this:

format(dates[isOpExFriday], tz="UTC")
On second look, you probably need to put the 'tz=' argument inside the  
format call inside the as.Date(format(...)) call. The format function  
"freezes" that dates value as text.

EDIT: On testing however I think you are right about it being a bug.  
Even after trying to insert various timezone specs and setting  
myFinCenter in RmetricsOptions, I still get this error from deep  
inside your choice of functions:

timeNthNdayInMonth(as.Date("2011-09-01"), nday=5, nth=3)
America/New_York
[1] [2011-09-15]
I suspect it is because of this code since as I understand it Julian  
dates are not adjusted for timezones or daylight savings times:

ct = 24 * 3600 * (as.integer(julian.POSIXt(lt)) +
           (nth - 1) * 7 + (nday - lt1$wday)%%7)
ct=as.POSIXct(ct)
The ct value in seconds is then coverted to POSIXct from second since  
"origin" simply by coercion of class. If I change the code to:

ct=as.POSIXct(ct, origin="1970-01-01")  # correct results come back
My quantmod and timeDate versions are both current per CRAN. Running  
Mac with R 2.13.1 in 64 bit mode with a US locale. I have not yet  
tried to reproduce with a minimal session so there could still be some  
collision or hijacking with other packages:

 > sessionInfo()
R version 2.13.1 RC (2011-07-03 r56263)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      splines   stats     graphics  grDevices utils     datasets
[8] methods   base

other attached packages:
  [1] quantmod_0.3-17   TTR_0.20-3        xts_0.8-2
  [4] Defaults_1.1-1    timeDate_2130.93  zoo_1.7-4
  [7] gplots_2.10.1     KernSmooth_2.23-6 caTools_1.12
[10] bitops_1.0-4.1    gdata_2.8.1       gtools_2.6.2
[13] wordnet_0.1-8     ggplot2_0.8.9     proto_0.3-9.2
[16] reshape_0.8.4     plyr_1.6          rattle_2.6.10
[19] RGtk2_2.20.17     rms_3.3-1         Hmisc_3.8-3
[22] survival_2.36-9   sos_1.3-0         brew_1.0-6
[25] lattice_0.19-30

-- 
David Winsemius, MD
West Hartford, CT



More information about the Rmetrics-core mailing list