[R] rendering date/times from 64bit julian format?

Dirk Eddelbuettel edd at debian.org
Wed Oct 4 15:34:09 CEST 2006


On 4 October 2006 at 14:50, Derek Eder wrote:
| I have date/times in 64-bit Julian format, that is the number of 
| milliseconds since 1 jan 1970 (or something like that).    E.g.,   
| "1159884877406"
| 
| For the life of me I can't figure out how to render these in a more 
| readable format, e.g.,  "2006-10-04  23:12:93.191"  (and I have tried to 
| do my best searching the archives and help etc ...)

To paraphrase an old saying about Unix, you could say that R's very powerful
Date/Time operations are indeed very user-friendly -- but unfortunately also
picky in selecting their friends.

To key to this conversion is to use implicit casting.  Witness

> now <- Sys.time()
> class(now)
[1] "POSIXt"  "POSIXct"
> format(as.numeric(now), digits=16)
[1] "1159968337.833141"

so we *do* have the current time in a POSIXct as such a number.  So for your
purposes, create an 'offset', maybe via 

 > offset <- ISOdatetime(1970,1,1,0,0,0,tz="GMT")
> class(offset)
[1] "POSIXt"  "POSIXct"
> as.numeric(offset)
[1] 0

which gives you half the solution -- a POSIXct to start from, conveniently
placed at the 'epoch.. Pick whichever timezone works for you.

Then simply add your milliseconds -- but converted to seconds as that is how
the internal representation is scaled:

> offset + 1159884877406/1000
[1] "2006-10-03 14:14:37.406 GMT"
> class(offset + 1159884877406/1000)
[1] "POSIXt"  "POSIXct"

Now your returned object is still POSIXct so you get to do all sort of fany
conversions for free.

The really nice thing is that thanks for a number of post-R 2.3.1
enhancements by Brian Ripley, we do have reall milisecond granularity in R.

Hope this helps,  Dirk

PS  Kurt, would this be worthy of a new FAQ entry?
 

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



More information about the R-help mailing list