Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing date like 1240915075

Tags:

date

parsing

ruby

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!

like image 893
Mario Avatar asked Jan 02 '11 12:01

Mario


3 Answers

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)
like image 186
Kris C Avatar answered Sep 28 '22 18:09

Kris C


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)
like image 29
Dominic Rodger Avatar answered Sep 28 '22 17:09

Dominic Rodger


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
like image 20
Freddie Avatar answered Sep 28 '22 17:09

Freddie