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 N. Eder Gothenburg University VINKLA - Vigilance and Neurocognition laboratory
Derek Eder <derek.eder at lungall.gu.se> writes:> 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 ...) >This should be close:> format(ISOdatetime(1970,1,1,0,0,0)+1159884877406/1000,"%Y-%m-%d %H:%M:%OS3")[1] "2006-10-03 15:14:37.406" Beware the timezone issues though. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dirk Eddelbuettel
2006-Oct-04 13:34 UTC
[R] rendering date/times from 64bit julian format?
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