DateTime to Seconds since Epoch

Posted by Matt Williams

Here's how to convert ruby DateTIme objects to the seconds since the epoch:
1
2
3
4
5
6
7
8
9
10

>> d=DateTime.now
=> Thu, 03 Jan 2008 15:49:21 -0500
>> t=Time.parse(d.strftime("%c"))
=> Thu Jan 03 15:49:21 -0500 2008
>> t.to_f
=> 1199393361.0
>> t.to_f.floor
=> 1199393361
>> 
Comments

Leave a response