# Midnight

**URL:** <https://www.ruby-forum.com/t/midnight/95048>\
**Category:** Ruby\
**Created:** [January 11, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/midnight/95048 "2006-01-11T19:33:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 11, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/midnight/95048/1 "2006-01-11T19:33:38Z")

</div>

how to use that method to returns a new time representing the start of  
the day (0:00)

134: def beginning\_of\_day  
135: (self - self.seconds\_since\_midnight).change(:usec =\> 0)  
136: end

i’d like to to some like taht

if Time.now \< modnight  
do…  
else  
do…  
end

any ideas ?

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 11, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/midnight/95048/2 "2006-01-11T20:06:18Z")

</div>

Time.now.beginning\_of\_day returns the beginning of today. But that’s  
probably not what you want because this:

Time.now \< Time.now.beginning\_of\_day

Would always be true unless it is 0:00. What do you want to achieve?

misiek wrote:

> how to use that method to returns a new time representing the start of  
> the day (0:00)
> 
> 134: def beginning\_of\_day  
> 135: (self - self.seconds\_since\_midnight).change(:usec =\> 0)  
> 136: end
> 
> i’d like to to some like taht
> 
> if Time.now \< modnight  
> do…  
> else  
> do…  
> end
> 
> any ideas ?

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 11, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/midnight/95048/3 "2006-01-11T20:19:44Z")

</div>

Jules J. wrote:

> Time.now.beginning\_of\_day returns the beginning of today. But that’s  
> probably not what you want because this:
> 
> Time.now \< Time.now.beginning\_of\_day

thank you , this is exactly what I want

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 12, 2006, 12:11am UTC](https://www.ruby-forum.com/t/midnight/95048/4 "2006-01-12T00:11:09Z")

</div>

David V. wrote:

> > 
> 
> Mind you, it’s pretty much always later than it was when the day  
> started. _wonders if misiek is hacking Ruby in a temporal anomaly_
> 
> David V.

better now ? Time.now \> Time.now.beginning\_of\_day

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 12, 2006, 12:41am UTC](https://www.ruby-forum.com/t/midnight/95048/5 "2006-01-12T00:41:34Z")

</div>

misiek wrote:

> > > >

I don’t know what you’re trying to achieve here. `Time.now > Time.now.beginning_of_day'' is false for one microsecond each day at midnight. You shouldn't really have to check for that, it's pretty much the same as writing`if true’’. Numerical methods coming to mind, you  
might instead want to check if the difference between Time.now and  
Time.now.beginnning\_of\_day is less than a given time period.

David V.

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 11, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/midnight/95048/6 "2006-01-11T20:31:56Z")

</div>

misiek wrote:

> Jules J. wrote:
> 
> > Time.now.beginning\_of\_day returns the beginning of today. But that’s  
> > probably not what you want because this:
> > 
> > Time.now \< Time.now.beginning\_of\_day
> 
> thank you , this is exactly what I want

Mind you, it’s pretty much always later than it was when the day  
started. _wonders if misiek is hacking Ruby in a temporal anomaly_

David V.

---

<div class="post-metadata">

**Author:** ![misiek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@misiek](https://www.ruby-forum.com/u/misiek)\
**Post date:** [January 12, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/midnight/95048/7 "2006-01-12T16:23:37Z")

</div>

> I don’t know what you’re trying to achieve here. `Time.now > Time.now.beginning_of_day'' is false for one microsecond each day at midnight. You shouldn't really have to check for that, it's pretty much the same as writing`if true’’. Numerical methods coming to mind, you  
> might instead want to check if the difference between Time.now and  
> Time.now.beginnning\_of\_day is less than a given time period.

And that is precisely why you need

now = Time.now  
if now.some\_value \< OtherValue.from(now)

instead of

if Time.now.some\_value \< OtherValue.from(Time.now)

for all practical purposess. The latter code will seem to work, even  
when  
you do unit tests; but occasionally it will break and you’ll never  
figure  
out why, since it always works when you debug.
