Forum: Ruby Convert from DateTIme to Time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-03-17 23:49
Hello :)

How can I convert from DateTIme to Time?

Thank you!
F1806d1cb1a523b91b6b556c498cde11?d=identicon&s=25 Horacio Sanson (Guest)
on 2009-03-18 05:27
(Received via mailing list)
On Wed, Mar 18, 2009 at 7:46 AM, Guillaume Loader <picpic72@hotmail.com>
wrote:
> 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-03-18 14:11
(Received via mailing list)
On Wed, Mar 18, 2009 at 12:23 AM, Horacio Sanson
<horacio.sanson@gmail.com>wrote:

>        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
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-03-18 15:27
(Received via mailing list)
* Rick DeNatale <rick.denatale@gmail.com> [2009-03-18 22:07:29 +0900]:
> 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
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-03-18 15:57
(Received via mailing list)
Xie Hanjian wrote:
> Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz GenuineIntel GNU/Linux
>
>

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

-Justin
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-03-18 16:39
(Received via mailing list)
* Justin Collins <justincollins@ucla.edu> [2009-03-18 23:53:57 +0900]:

>> PREEMPT Sun Mar 8 10:55:58 CET 2009 x86_64
>> - Jan
>>
>>
>
> Pretty sure it's a 32 vs. 64 bit thing.

aha, I should keep a 32bit machine around :-)

Thanks,
Jan
This topic is locked and can not be replied to.