[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Time object problems - 1.8.2

Greg Lorriman

3/15/2006 11:56:00 AM

I am trying to efficiently get the date part of a Time object.

To remove the time part of the Time object I subtract the modulo of a
day-in-seconds. But if I do this I get a Time object of the date *plus
1 hour*!!! Perhaps I'm just tired, but I don;t understand why.

ie :

DAY_IN_SECS=60*60*24;
t=Time.parse("20-Oct-2005 04:04:00")
datePart=t-t.to_f.modulo(DAY_IN_SECS);

(and I might apply Time.at to get a Time object rather than a float.)

However, the result of the above is

20-Oct-2005 01:00:00

instead of

20-Oct-2005 00:00:00

Anyone got any ideas?

Greg

13 Answers

Bernhard 'elven' Stoeckner

3/15/2006 12:43:00 PM

0

Greg Lorriman scribbled on Wednesday 15 Mar 2006 12:55:

> I am trying to efficiently get the date part of a Time object.
>
> To remove the time part of the Time object I subtract the modulo of a
> day-in-seconds. But if I do this I get a Time object of the date *plus
> 1 hour*!!! Perhaps I'm just tired, but I don;t understand why.
>
> ie :
>
> DAY_IN_SECS=60*60*24;
> t=Time.parse("20-Oct-2005 04:04:00")
> datePart=t-t.to_f.modulo(DAY_IN_SECS);
>
> (and I might apply Time.at to get a Time object rather than a float.)
>
> However, the result of the above is
>
> 20-Oct-2005 01:00:00
>
> instead of
>
> 20-Oct-2005 00:00:00
>
> Anyone got any ideas?
>
> Greg

irb(main):001:0> t = Time.now; t - t.sec - 60 * t.min - 3600 * t.hour
=> Wed Mar 15 00:00:00 CET 2006

Pit Capitain

3/15/2006 1:06:00 PM

0

Greg Lorriman schrieb:
> I am trying to efficiently get the date part of a Time object.
>
> To remove the time part of the Time object I subtract the modulo of a
> day-in-seconds. But if I do this I get a Time object of the date *plus
> 1 hour*!!! Perhaps I'm just tired, but I don;t understand why.
>
> DAY_IN_SECS=60*60*24;
> t=Time.parse("20-Oct-2005 04:04:00")
> datePart=t-t.to_f.modulo(DAY_IN_SECS);
>
> (and I might apply Time.at to get a Time object rather than a float.)
>
> However, the result of the above is
>
> 20-Oct-2005 01:00:00
>
> instead of
>
> 20-Oct-2005 00:00:00
>
> Anyone got any ideas?

Greg, look at this:

p Time.at(0)

It starts at 1 o'clock.

Regards,
Pit


Dimitri Aivaliotis

3/15/2006 2:02:00 PM

0

Hi Greg,

On 3/15/06, Greg Lorriman <temp@lorriman.com> wrote:
> I am trying to efficiently get the date part of a Time object.

I've just been playing around with Time and Date a bit. I have to ask: why?

You can get just the date part much more easily by using a Date object:

Date.parse("20-Oct-2005 04:04:00").to_s
=> "2005-10-20"

If you absolutely need a time object out of it, you can use 'facets':

require 'facet/date/to_time'
Date.parse("20-Oct-2005 04:04:00").to_time
=> Thu Oct 20 00:00:00 CEST 2005

HTH

- Dimitri


Greg Lorriman

3/15/2006 3:33:00 PM

0


>I've just been playing around with Time and Date a bit. I have to ask: why?

>You can get just the date part much more easily by using a Date object:

Unfortunately the Date class is horrifically slow. I'm already being
hammered by other slow aspects of ruby, which I can't avoid. So Time it
is!

Dimitri Aivaliotis

3/15/2006 3:56:00 PM

0

On 3/15/06, Greg Lorriman <temp@lorriman.com> wrote:
>
> Unfortunately the Date class is horrifically slow. I'm already being
> hammered by other slow aspects of ruby, which I can't avoid. So Time it
> is!

Indeed. I just discovered
http://www.recentrambles.com/pragmat... from another thread.
Interestingly enough, by replacing DateTime with Date in the example,
the benchmark comes out about 3 times faster. I guess having to work
with hours, minutes, and seconds is expensive.

- Dimitri


gordon

3/15/2006 4:28:00 PM

0


> irb(main):001:0> t = Time.now; t - t.sec - 60 * t.min - 3600 * t.hour
> => Wed Mar 15 00:00:00 CET 2006

How about:

irb(main):001:0> t = Time.now; Time.local(t.year,t.month,t.day)
=> Wed Mar 15 00:00:00 Central Standard Time 2006

Greg Lorriman

3/15/2006 5:29:00 PM

0

It makes me think that an effort is needed to convert many of the
libraries that do basic heavy-lifting, like DateTime and CSV and
others, to C libraries.

I like the purity of the idea of a "pure Ruby" library, but I am
suffering the consequences and even considering jumping to Python; the
pain is getting to be too much.

Ara.T.Howard

3/15/2006 5:45:00 PM

0

Greg Lorriman

3/15/2006 7:09:00 PM

0

>> I like the purity of the idea of a "pure Ruby" library, but I am
>> suffering the consequences and even considering jumping to Python; the
>> pain is getting to be too much.

>check out the speed gains one can get with FasterCSV and reconsider - and it's
>still pure ruby

Sounds promising. I particularly like the sound of "Faster", in
"FasterCSV".

thanks for that

Greg

Greg Lorriman

3/15/2006 7:10:00 PM

0

>> Anyone got any ideas?

>Greg, look at this:

> p Time.at(0)

>It starts at 1 o'clock.

Unfathomable! What does it mean?!?! I am entirely perplexed by this
reality.

Greg