Forum: Ruby midnight

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-11 19:33
(Received via mailing list)
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 ?
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-11 20:06
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 ?
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-11 20:19
(Received via mailing list)
Jules Jacobs 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
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-11 20:31
(Received via mailing list)
misiek wrote:

> Jules Jacobs 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 Vallner
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-12 00:11
(Received via mailing list)
David Vallner 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 Vallner
>
>

  better now ?  Time.now > Time.now.beginning_of_day
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 00:41
(Received via mailing list)
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 Vallner
Dd54c22454b4e3c21cadf3bdb5192e28?d=identicon&s=25 Kero (Guest)
on 2006-01-12 16:23
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.