[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

new Date from Time.now().to_a

warhero

3/12/2007 1:03:00 AM

I don't see any obvious ways of making a new Date object based off of
what Time.now rerurns. I'm ultimately trying to make a new Date from
milliseconds:

tn = Time.now
ms = ("%.6f" % tn.to_f) #this would ultimately be comming from another
source
nd = Time.at( ms.to_f / 1000.0 )
puts nd.to_a.inspect
#d = Date.new(??)

The next step is making the date object. I don't see anything obvious in
the Date class. Any help?

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

5 Answers

Ryan Davis

3/12/2007 1:05:00 AM

0


On Mar 11, 2007, at 6:02 PM, Aaron Smith wrote:

> I don't see any obvious ways of making a new Date object based off of
> what Time.now rerurns. I'm ultimately trying to make a new Date from
> milliseconds:
>
> tn = Time.now
> ms = ("%.6f" % tn.to_f) #this would ultimately be comming from another
> source
> nd = Time.at( ms.to_f / 1000.0 )
> puts nd.to_a.inspect

Remember, ri is your friend™.

% ri Date::civil
------------------------------------------------------------ Date::civil
Date::civil(y=-4712, m=1, d=1, sg=ITALY)
------------------------------------------------------------------------



warhero

3/12/2007 1:25:00 AM

0

Ryan Davis wrote:
> On Mar 11, 2007, at 6:02 PM, Aaron Smith wrote:
>
>> I don't see any obvious ways of making a new Date object based off of
>> what Time.now rerurns. I'm ultimately trying to make a new Date from
>> milliseconds:
>>
>> tn = Time.now
>> ms = ("%.6f" % tn.to_f) #this would ultimately be comming from another
>> source
>> nd = Time.at( ms.to_f / 1000.0 )
>> puts nd.to_a.inspect
>
> Remember, ri is your friend�.
>
> % ri Date::civil
> ------------------------------------------------------------ Date::civil
> Date::civil(y=-4712, m=1, d=1, sg=ITALY)

require 'date'


ms = 1173658884500.0
nd = Time.at( ms.to_f / 1000.0 )
#puts nd
#puts nd.to_a.inspect
f = nd.to_a
n = Date.parse("#{f[4]}-#{f[3]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
#{f[9]}")
#puts n
#puts n.class

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

warhero

3/12/2007 1:28:00 AM

0


> ms = 1173658884500.0
> nd = Time.at( ms.to_f / 1000.0 )
> #puts nd
> #puts nd.to_a.inspect
> f = nd.to_a
> n = Date.parse("#{f[4]}-#{f[3]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
> #{f[9]}")
> #puts n
> #puts n.class

correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
#{f[9]}")


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

Robert Klemme

3/12/2007 7:01:00 PM

0

On 12.03.2007 02:27, Aaron Smith wrote:
>> ms = 1173658884500.0
>> nd = Time.at( ms.to_f / 1000.0 )
>> #puts nd
>> #puts nd.to_a.inspect
>> f = nd.to_a
>> n = Date.parse("#{f[4]}-#{f[3]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
>> #{f[9]}")
>> #puts n
>> #puts n.class
>
> correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
> #{f[9]}")

Why the parsing?

irb(main):042:0> t=Time.now
=> Mon Mar 12 19:59:33 +0100 2007
irb(main):043:0> d=Date.new(t.year, t.month, t.day)
=> #<Date: 4908343/2,0,2299161>
irb(main):044:0> d.strftime
=> "2007-03-12"

Kind regards

robert

warhero

3/12/2007 7:50:00 PM

0

Robert Klemme wrote:
> On 12.03.2007 02:27, Aaron Smith wrote:
>> correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}
>> #{f[9]}")
>
> Why the parsing?
>
robert

thanks. thats exactly what I was looking for.

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