[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Initializing a DateTime object the best way

Wes Gamble

7/17/2006 9:15:00 PM

Wow - dates and times sure are all over the place in Ruby.

I want to initialize a DateTime object to have MDY of 12/30/1899 but
time components matching the current time.

I've tried:

DateTime.strptime("%Y-%m-%d %h:%M:%S", "1899-12-30
#{time.hour}:#{time.min}:#{time.sec}")

but strptime doesn't seem to like it.

Do I need to use a Time object - where's the correct string parsing
method for me to use?

Thanks,
Wes

--
Posted via http://www.ruby-....

4 Answers

Wes Gamble

7/17/2006 9:50:00 PM

0

Wes Gamble wrote:
> Wow - dates and times sure are all over the place in Ruby.
>
> I want to initialize a DateTime object to have MDY of 12/30/1899 but
> time components matching the current time.
>
> I've tried:
>
> DateTime.strptime("%Y-%m-%d %h:%M:%S", "1899-12-30
> #{time.hour}:#{time.min}:#{time.sec}")
>
> but strptime doesn't seem to like it.
>
> Do I need to use a Time object - where's the correct string parsing
> method for me to use?
>
> Thanks,
> Wes

Then I tried

Time.local(1899,"dec",30,10,0,0)

and 1899 is too far back.

Is there any way that I can create a time object with arbitrary year,
month, and day and current hour, minute, and second?

Thanks,
Wes

--
Posted via http://www.ruby-....

khaines

7/17/2006 9:58:00 PM

0

Wes Gamble

7/17/2006 10:06:00 PM

0

Wes Gamble wrote:
> Wes Gamble wrote:
>> Wow - dates and times sure are all over the place in Ruby.
>>
>> I want to initialize a DateTime object to have MDY of 12/30/1899 but
>> time components matching the current time.
>>
>> I've tried:
>>
>> DateTime.strptime("%Y-%m-%d %h:%M:%S", "1899-12-30
>> #{time.hour}:#{time.min}:#{time.sec}")
>>
>> but strptime doesn't seem to like it.
>>
>> Do I need to use a Time object - where's the correct string parsing
>> method for me to use?
>>
>> Thanks,
>> Wes
>
> Then I tried
>
> Time.local(1899,"dec",30,10,0,0)
>
> and 1899 is too far back.
>
> Is there any way that I can create a time object with arbitrary year,
> month, and day and current hour, minute, and second?
>
> Thanks,
> Wes

This seems to get me pretty close.

DateTime.civil(1899, 12, 30, time.hour, time.min, time.sec,
DateTime.now.offset())

WG


--
Posted via http://www.ruby-....

S Wayne

7/17/2006 11:32:00 PM

0

This is an interesting conversation. Can you tell me where this
DateTime class or the civil method comes from?

irb(main):001:0> DateTime.new(1899,12,30,8,30,33)
ArgumentError: wrong number of arguments (6 for 0)
from (irb):1:in `initialize'
from (irb):1

irb(main):002:0> DateTime.civil(1899,12,30,8,30,33)
NoMethodError: undefined method `civil' for DateTime:Class
from (irb):2

....