I have a problem wherein I need to convert back and forth from dates of the form "2000-04-11" to Julian dates. After some experimentation I found that > z <- strptime("2000-62",format="%Y-%j") > z gave me "2000-03-02" --- i.e. March 2 which is as it should be according to my (not too reliable) arithmetic. Going the other direction, more experimentation led me to > julian(z,origin=as.POSIXct("1999-12-31")) which gives Time difference of 62 days and I thought that I had a working (if not fully comprehended) syntax. My illusions were shattered when I tried another Julian date, 102: > z <- strptime("2000-102",format="%Y-%j") > z gives "2000-04-11" --- i.e. April 11, which checks with what I think it should be. But reversing the direction: > julian(z,origin=as.POSIXct("1999-12-31")) gives Time difference of 101.9583 days Where did the missing 0.0417 days go to? What syntax should I really be using? Thanks. cheers, Rolf Turner P. S. I'm still on R version 2.0.1 (Linux) if that's of any relevance. R. T.
Rolf Turner <rolf at math.unb.ca> writes:> I have a problem wherein I need to convert back and forth from > dates of the form "2000-04-11" to Julian dates. After some > experimentation I found that > > > z <- strptime("2000-62",format="%Y-%j") > > z > > gave me "2000-03-02" --- i.e. March 2 which is as it should be > according to my (not too reliable) arithmetic. > > Going the other direction, more experimentation led me to > > > julian(z,origin=as.POSIXct("1999-12-31")) > > which gives > > Time difference of 62 days > > and I thought that I had a working (if not fully comprehended) > syntax. My illusions were shattered when I tried another Julian > date, 102: > > > z <- strptime("2000-102",format="%Y-%j") > > z > > gives "2000-04-11" --- i.e. April 11, which checks with what I think > it should be. But reversing the direction: > > > julian(z,origin=as.POSIXct("1999-12-31")) > > gives > > Time difference of 101.9583 days > > Where did the missing 0.0417 days go to?Into the daylight savings account, I guess.> What syntax should I really be using?It's not the syntax as much as the timezone. But what has as.Date done wrong, since you seem set on ignoring it?> as.Date(z) - as.Date("1999-12-31")Time difference of 102 days -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Peter Dalgaard writes:> It's not the syntax as much as the timezone.Well, that's part of the overall syntax, or structure at least.> But what has as.Date done wrong, since you seem set on ignoring it?It's not that I'm ignoring it --- it's just that I haven't a clue what it means/does. The help on the date/time stuff is totally opaque. It appears to be meaningful only to those who are already completely au fait with the ``POSIX'' standards (whatever they are!), and thereby are not in need of help. (Reminds me of Sam Johnson's ``Letter to Lord Chesterfield'', but that's another story.) Anyhow, I found that all I could do was to proceed by trial and error, using all possible permutations and combination of strptime(), as.POSIXct(), as.POSIXlt(), format(), etc. etc. I actually did use a few perms and combs involving as.Date() --- but clearly not enough of them.> > as.Date(z) - as.Date("1999-12-31") > Time difference of 102 daysThat is exactly the syntax that I need, it would seem. Thank you! cheers, Rolf
On 5/14/05, Rolf Turner <rolf at math.unb.ca> wrote:> The help on the date/time stuff is > totally opaque.Check out the article on dates and times in RNews 4/1 in the Help Desk section. The table at the end of that article includes differencing of dates.