Skip to content

rendering date/times from 64bit julian format?

3 messages · Derek Eder, Peter Dalgaard, Dirk Eddelbuettel

#
Hellos,

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 ...)


Thank you most sincerely,

Derek Eder
#
Derek Eder <derek.eder at lungall.gu.se> writes:
This should be close:
[1] "2006-10-03 15:14:37.406"

Beware the timezone issues though.
#
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
[1] "POSIXt"  "POSIXct"
[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")
[1] "POSIXt"  "POSIXct"
[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:
[1] "2006-10-03 14:14:37.406 GMT"
[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?