[R] time date stamp since, january 1st 1970

Dirk Eddelbuettel edd at debian.org
Fri Jan 23 00:35:31 CET 2009


On 22 January 2009 at 17:39, zubin wrote:
| Hello, we are receiving some data, sample below - with a weird time/date 
| stamp format, we need some help with R on converting this time date 
| stamp to a useable field in R, date and time in a data-frame.  The 
| developer says its the number of milliseconds since midnight, January 1, 
| 1970. 
| 
| sample:  *1232558018624*
| ---------------------
| 
| 
| How do I interpret the time stamp? Is there a date, i need a date and time.
| site_id,survey_id,version_id,survey_start_ts,survey_question_id,start_ts,end_ts,answer
| 2,registration,1,1232558018625,z1,*1232558018624*,*1232558266179*,4
| 
| 
| Answer: The timestamp is a number representing the exact date and time. 
| it is the number of milliseconds since midnight, January 1, 1970. Are 
| you using it in the DB or R? I am not sure about R's conversion of 
| numbers and dates. is there a way to add a number of seconds or 
| milliseconds to a date in R?

This is one of these cases where R excels once you understand what it does.
Consider:

   > Sys.time()
   [1] "2009-01-22 23:23:42 UTC"
   > as.numeric(Sys.time())
   [1] 1232666630
   > 

Internal R representation is also a number (modulo maybe a factor of 1000 for
your milliseconds) !    R represents time as seconds (incl. fractional parts)
since Jan 1, 1970.

All you need is do what help(as.POSIXct) suggests, after adjusting for
milli-seconds (ie note the dot):

   > as.POSIXct(1232558018.624, origin="1970-01-01")
   [1] "2009-01-21 17:13:38 UTC"
   > 

Dirk

-- 
Three out of two people have difficulties with fractions.




More information about the R-help mailing list