# Very simple Date math goes randomly bonkers

**URL:** <https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156>\
**Category:** Rails\
**Created:** [December 8, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156 "2008-12-08T19:56:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/1 "2008-12-08T19:56:36Z")

</div>

I have a “timesheet” in my app that simply displays the week range  
based on a passed in date (params[:date]).  
The view iterates through the Date::ABBR\_DAYNAMES and shows a link to  
the corresponding date.

Trouble is that about 40% of the time the calculated dates go bonkers.  
Am I missing something?

Model:

# Get a date range of the week that a particular date falls in

# This is broken out like mad for diagnostics

def self.get\_work\_week\_date\_range(date)  
monday = date.monday  
sunday = monday - 1.day  
eow = date.end\_of\_week  
saturday = eow - 1.day

```
dates = [sunday, saturday]

logger.info

```

("\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>")  
logger.info("\>\>\>\>\> #{date}.monday = #{monday}")  
logger.info("\>\>\>\>\> #{monday}-1.day = #{sunday}")  
logger.info("\>\>\>\>\> #{date}.end\_of\_week = #{eow}")  
logger.info("\>\>\>\>\> #{eow}-1.day = #{saturday}")  
logger.info("\>\>\>\>\> #{date} == [#{dates[0]}, #{dates[1]}]")  
logger.info  
("\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>")

```
dates

```

end

Logger output:

> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
> > > > > 
> > > > > 2008-12-08.monday = 2008-12-08  
> > > > > 2008-12-08-1.day = 2008-12-07  
> > > > > 2008-12-08.end\_of\_week = 2008-12-14  
> > > > > 2008-12-14-1.day = 2008-12-13  
> > > > > 2008-12-08 == [2008-12-07, 2008-12-13]
> > > > > 
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >

… n times later …

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/2 "2008-12-08T20:16:05Z")

</div>

On Dec 8, 2008, at 10:55 AM, Matt D. wrote:

> I have a “timesheet” in my app that simply displays the week range  
> based on a passed in date (params[:date]).  
> The view iterates through the Date::ABBR\_DAYNAMES and shows a link to  
> the corresponding date.

What do you pass to get\_work\_week\_date\_range? Is it a string? Or an  
instance of Date? Or Time?

If it worked once it should always work. Which suggests that maybe  
you’re passing in something different from time to time? One time a  
Date, one time a Time?

Maybe?

Put together a test and explicitly pass the _same_ argument (class and  
value) and see if you can duplicate it.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/3 "2008-12-08T20:30:15Z")

</div>

> What do you pass to get\_work\_week\_date\_range? Is it a string? Or an  
> instance of Date? Or Time?

I pull the date in from the params hash, and immediately parse it to a  
Date:  
Date.parse(params[:date]) rescue Date.today

> If it worked once it should always work. Which suggests that maybe  
> you’re passing in something different from time to time? One time a  
> Date, one time a Time?

You’re right, that’s why I’ve pounded my head against the wall for a  
week!  
This method is only called from one other method, and I’ve extracted  
it really only for testing.

> Put together a test and explicitly pass the _same_ argument (class and  
> value) and see if you can duplicate it.

All tests pass ☹

it “should know how to get the week based on a date” do  
TimeCard.get\_work\_week\_date\_range(Date.parse(“2008-08-05”)).should  
== [Date.parse(“2008-08-03”), Date.parse(“2008-08-09”)]  
TimeCard.get\_work\_week\_date\_range(Date.parse(“2008-01-01”)).should  
== [Date.parse(“2007-12-30”), Date.parse(“2008-01-05”)]  
TimeCard.get\_work\_week\_date\_range(Date.parse(“2008-02-29”)).should  
== [Date.parse(“2008-02-24”), Date.parse(“2008-03-01”)]  
TimeCard.get\_work\_week\_date\_range(Date.parse(“2007-02-28”)).should  
== [Date.parse(“2007-02-25”), Date.parse(“2007-03-03”)]  
end

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/4 "2008-12-08T21:41:32Z")

</div>

> Perhaps the key thing to note is that 1772-05-19 is about 236 years  
> and 7 months ago, which (get your calculators out) is about 86400, ie  
> the number of seconds in a day. Whereas on an instance of Time, +/- 1  
> means +/- 1 second, on instances of Date/DateTime +/- means +/- 1  
> second. That is I suspect the basic problem.

Man, good call.

> On newer versions of rails, things like 1.day are smart enough to do  
> the right thing whether or not you add a date or a Time to them (the +  
> method on Date is overriden to check if the argument is a thing like  
> 1.day).

I’m using 2.2.2, but…

> Somewhere this has gone wrong. If you have any plugins that mess with  
> dates (or if you’ve being doing stuff like that yourself) then I’d  
> have a look at those - it could be something like once some random  
> model is loaded some extensions to Date are pulled in at that point  
> and from then onwards calculations are borked.

… I do monkeypatch Date, but I ensured that I wasn’t actually  
overriding any existing method.

Thanks for you help Fred, this smells like the right track.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/5 "2008-12-08T23:05:06Z")

</div>

It was the facets/time library. It was loaded in a totally separate  
part of the app, in a method that had not much to do with anything.

Thanks again Frederick; that one got your a WWR recommendation 😉

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 8, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/very-simple-date-math-goes-randomly-bonkers/155156/6 "2008-12-08T21:15:06Z")

</div>

On Dec 8, 6:55 pm, Matt D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > > > 2008-12-08.monday = 2008-12-08  
> > > > > > 2008-12-08-1.day = 1772-05-19  
> > > > > > 2008-12-08.end\_of\_week = 3428-04-08  
> > > > > > 3428-04-08-1.day = 3191-09-18  
> > > > > > 2008-12-08 == [1772-05-19, 3191-09-18]

Perhaps the key thing to note is that 1772-05-19 is about 236 years  
and 7 months ago, which (get your calculators out) is about 86400, ie  
the number of seconds in a day. Whereas on an instance of Time, +/- 1  
means +/- 1 second, on instances of Date/DateTime +/- means +/- 1  
second. That is I suspect the basic problem.

On newer versions of rails, things like 1.day are smart enough to do  
the right thing whether or not you add a date or a Time to them (the +  
method on Date is overriden to check if the argument is a thing like  
1.day).  
Somewhere this has gone wrong. If you have any plugins that mess with  
dates (or if you’ve being doing stuff like that yourself) then I’d  
have a look at those - it could be something like once some random  
model is loaded some extensions to Date are pulled in at that point  
and from then onwards calculations are borked.

Fred
