# Time object problems - 1.8.2

**URL:** <https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945>\
**Category:** Ruby\
**Created:** [March 15, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945 "2006-03-15T13:00:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/1 "2006-03-15T13:00:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/2 "2006-03-15T13:51:27Z")

</div>

Greg L. scribbled on Wednesday 15 Mar 2006 12:55:

> datePart=t-t.to\_f.modulo(DAY\_IN\_SECS);
> 
> 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/3 "2006-03-15T14:11:18Z")

</div>

Greg L. schrieb:

> (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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/4 "2006-03-15T16:35:22Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/5 "2006-03-15T16:57:13Z")

</div>

On 3/15/06, Greg L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/pragmatic/view/33](http://www.recentrambles.com/pragmatic/view/33) 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/6 "2006-03-15T15:05:11Z")

</div>

Hi Greg,

On 3/15/06, Greg L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/7 "2006-03-15T17:29:34Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/8 "2006-03-15T18:38:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/9 "2006-03-15T20:15:59Z")

</div>

> > 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/10 "2006-03-15T18:46:22Z")

</div>

On Thu, 16 Mar 2006, Greg L. wrote:

> 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.

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

regards.

-a

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/11 "2006-03-15T20:16:15Z")

</div>

> > 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

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/12 "2006-03-15T21:24:31Z")

</div>

> > p Time.at(0)  
> > It starts at 1 o’clock.

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

> Greg, sorry for the noise. Time.at(0) is “epoch” (1970-01-01 00:00:00  
> GMT). My timezone is GMT + 1, so epoch happened to be at 1 o’clock. I  
> suppose that’s the reason why your algorithm didn’t work.

Well that is the answer, then. Thankyou very much for that. Perhaps the  
docs need expanding a little. I think even a computer scientist might  
trip up on “epoch”. I was thinking, in fact, that Time isn’t a  
practical Time/Date implementation and was possibly concieved in an  
academic mind, which your observation would seem to confirm; I don’t  
think I’ve ever used such an awkward time/date implementation. However  
I haven’t used Date yet!!! 🙂

Greg

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [March 15, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/time-object-problems-1-8-2/54945/13 "2006-03-15T20:44:25Z")

</div>

Greg L. schrieb:

> > p Time.at(0)  
> > It starts at 1 o’clock.
> 
> Unfathomable! What does it mean?!?! I am entirely perplexed by this  
> reality.

Greg, sorry for the noise. Time.at(0) is “epoch” (1970-01-01 00:00:00  
GMT). My timezone is GMT + 1, so epoch happened to be at 1 o’clock. I  
suppose that’s the reason why your algorithm didn’t work.

Regards,  
Pit
