[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Time#succ ?

Hal E. Fulton

11/23/2003 8:37:00 PM

Should this be added? It would enable the creation
of Time ranges like t1..t2 and so on.

I'd assume that seconds would be the default resolution
(although the actual resolution is platform-dependent).

Hal



7 Answers

matz

11/24/2003 12:33:00 AM

0

Hi,

In message "Time#succ ?"
on 03/11/24, Hal Fulton <hal9000@hypermetrics.com> writes:

|Should this be added? It would enable the creation
|of Time ranges like t1..t2 and so on.
|
|I'd assume that seconds would be the default resolution
|(although the actual resolution is platform-dependent).

I like the idea. How should I treat sub-second value?

n = Time.now
p n.to_f # 1069633904.88943
p n.succ.to_f # 1069633905.88943 or 1069633905.0 or 1069633906.0?

matz.


Hal E. Fulton

11/24/2003 3:53:00 AM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Time#succ ?"
> on 03/11/24, Hal Fulton <hal9000@hypermetrics.com> writes:
>
> |Should this be added? It would enable the creation
> |of Time ranges like t1..t2 and so on.
> |
> |I'd assume that seconds would be the default resolution
> |(although the actual resolution is platform-dependent).
>
> I like the idea. How should I treat sub-second value?
>
> n = Time.now
> p n.to_f # 1069633904.88943
> p n.succ.to_f # 1069633905.88943 or 1069633905.0 or 1069633906.0?

I would expect the first case.

Then n.succ.to_f would behave the same as (n+1).to_f,
isn't that correct?

Hal




Paul Brannan

11/26/2003 9:29:00 PM

0

On Mon, Nov 24, 2003 at 05:36:49AM +0900, Hal Fulton wrote:
> Should this be added? It would enable the creation
> of Time ranges like t1..t2 and so on.

Does this not already work?

[pbrannan@zaphod ruby]$ irb
irb(main):001:0> t = Time.now
=> Wed Nov 26 16:27:46 EST 2003
irb(main):002:0> (t .. t+1)
=> Wed Nov 26 16:27:46 EST 2003..Wed Nov 26 16:27:47 EST 2003

(the problem only comes if you want to iterate over the range, which
seems to me to be an ill-defined operation).

Paul


Hal E. Fulton

11/26/2003 10:05:00 PM

0

Paul Brannan wrote:
> On Mon, Nov 24, 2003 at 05:36:49AM +0900, Hal Fulton wrote:
>
>>Should this be added? It would enable the creation
>>of Time ranges like t1..t2 and so on.
>
>
> Does this not already work?
>
> [pbrannan@zaphod ruby]$ irb
> irb(main):001:0> t = Time.now
> => Wed Nov 26 16:27:46 EST 2003
> irb(main):002:0> (t .. t+1)
> => Wed Nov 26 16:27:46 EST 2003..Wed Nov 26 16:27:47 EST 2003
>
> (the problem only comes if you want to iterate over the range, which
> seems to me to be an ill-defined operation).

Hmmm. For some reason I thought that to have a Range, the
endpoint objects had to know #succ.

Apparently Time ranges do work fine. I don't know why I
never thought of them before.

As for iterating: I agree it's ill-defined, but I think it's
reasonable to let t.succ be the same as t+1 (since the latter
is already meaningful).

But since I can construct a Time range and call include? on it,
I suppose that's all we really need.

Hal



Ara.T.Howard

11/26/2003 10:31:00 PM

0

sabbyxtabby

11/27/2003 4:37:00 PM

0

"Ara.T.Howard" <ahoward@ngdc.noaa.gov> wrote:

> anyone familiar with the 'jot' unix program? i think ranges should work like
> that - eg. one should be able to define the increment
>
> day = 60 * 60 * 24
>
> a = Time.now
> b = Time.now + (7 * day)
>
> week = (a...b)
>
> week.each(step = day) do |weekday|
> ...
> end

Maybe Range#step:

day = 60*60*24
a = Time.now
b = a + 7 * day

(a..b).step(day) {|t| puts t}

Unfortunately:

./gem:20:in `step': cannot iterate from Time (TypeError)

A work-around:

a = Time.now.to_i
b = a + 7 * day

(a..b).step(day) {|t| puts Time.at(t)}

Niklas Frykholm

11/28/2003 8:48:00 AM

0

>>anyone familiar with the 'jot' unix program? i think ranges should work like
>>that - eg. one should be able to define the increment
>>
>>day = 60 * 60 * 24
>>
>>a = Time.now
>>b = Time.now + (7 * day)
>>
>>week = (a...b)
>>
>>week.each(step = day) do |weekday|
>> ...
>>end

I think a rather nice way of doing this is "by example":

def loop_over(v1,v2,vn)
x = v1
step = v2 - v1
while x <= vn
yield x
x += step
end
end

loop_over(1,2,10) {|x| puts x} # -> 1,2,3,4,5,6,7,8,9,10

day = 60 * 60 * 24
t = Time.now

loop_over(t, t + day, t + 7*day) do |weekday|

// Niklas