Convert from DateTIme to Time

H

Horacio Sanson

Hello :)

How can I convert from DateTIme to Time?

require "date"

class DateTime
def to_time
Time.mktime(year, month, day, hour, min, sec)
end
end


date = DateTime.now
time = date.to_time
time.class => Time

Horacio
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

require "date"

class DateTime
def to_time
Time.mktime(year, month, day, hour, min, sec)
end
end


date = DateTime.now
time = date.to_time
time.class => Time

One little glitch. Time can only represent values through January 1, 2038,
DateTime has a longer range.


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Justin Collins

Xie said:
* Rick DeNatale said:
One little glitch. Time can only represent values through January 1, 2038,
DateTime has a longer range.

This problem seems only exist on mac.

22:20:~$ uname -a
Linux aiur 2.6.28-ARCH #1 SMP PREEMPT Sun Mar 8 10:55:58 CET 2009 x86_64
Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz GenuineIntel GNU/Linux

22:20:~$ ruby -v -e 'p Time.mktime(3000)'
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
Wed Jan 01 00:00:00 +0800 3000

22:20:~$ ruby186 -v -e 'p Time.mktime(3000)'
ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]
Wed Jan 01 00:00:00 +0800 3000

- Jan

Pretty sure it's a 32 vs. 64 bit thing.

-Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top