20

I am learning programming and I choose Ruby as the first language to learn.

I am parsing an XML where dates are in this form: 1240915075 1224855068

How is this format called? How to use that value in a Date or Time object?

Thank you!

3 Answers 3

45

This is UNIX time (sometimes called Epoch time). It measures the number of seconds elapsed since January 1, 1970 (The Unix epoch is the time 00:00:00 UTC on 1 January 1970)

Here's an example converter: http://www.esqsoft.com/javascript_examples/date-to-epoch.htm

A stackoverflow question regarding converting integer time using Ruby: Ruby / Rails: convert int to time OR get time from integer?

use the Time.at function to convert e.g.:

t = Time.at(i)
2
  • Nitpick: I'd call 1 Jan 1970 epoch time. This is called a unix timestamp.
    – moinudin
    Commented Jan 2, 2011 at 12:16
  • good point - updated for accuracy. I've kind of got into the habit of calling it EPOCH time...
    – Kris C
    Commented Jan 2, 2011 at 12:25
9

That's Epoch Time (the first one corresponds to Tue Apr 28 2009 11:37:55 GMT+0100).

You can get a datetime out of it, using Time.at, like this:

Time.at(1240915075)
0
4

That is a unix timestamp - the number of seconds since jan 1st 1970.

An example of how to use it in Ruby is here:

t = Time.at(1215163257)
puts t.to_date
>> 2008-07-04
1
  • 1
    You should include a copy of the code in your answer. That link might change / die in the future.
    – moinudin
    Commented Jan 2, 2011 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.