Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Date.end_of_week not working in production mode

#5548

This Date method is returning wrong values when used in production mode. You can reproduce it with a "rails c -e production" in a fresh install, then:

a = Date.new(2010, 9, 30)
a.end_of_week
=> Mon, 31 May 2720

I have tracked it back until I realize this different behaviours:

In development:
irb(main):009:0> 4.days
=> 4 days

In production:
irb(main):072:0> 4.days
=> 345600

So, Date.today + 345600 is far away in the future but in development this is somehow automatically fixed. Anyway, I attach here a very simple solution:

File activesupport/lib/active_support/core_ext/date/calculations.rb, line 178

    def end_of_week
      days_to_sunday = self.wday!=0 ? 7-self.wday : 0
      result = self + days_to_sunday.days
      self.acts_like?(:time) ? result.end_of_day : result
    end

In this piece of code "days_to_sunday.days" have to be changed to "days_to_sunday" because it's already an integer.

Reported by Roger Campos · September 3rd, 2010 @ 03:45 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer

    I was unable to reproduce this on master:

    $ script/rails c
    13.Loading development environment (Rails 3.0.0)
    >> 13.days
    => 13 days
    >> exit
    $ script/rails c production
    Loading production environment (Rails 3.0.0)
    >> 13.days
    => 13 days
    >> exit
    

    September 5th, 2010 @ 10:59 AM

  2. Rafael Mendonça França
    Rafael Mendonça França

    I was unable to reproduce this on rails 3.0.0:

    $ script/rails c
    >> a = Date.new(2010,9,30)
    => Thu, 30 Sep 2010
    >> a.end_of_week
    => Sun, 03 Oct 2010
    >> exit
    $ script/rails c production
    Loading production environment (Rails 3.0.0)
    >> a = Date.new(2010,9,30)
    => Thu, 30 Sep 2010
    >> a.end_of_week
    => Sun, 03 Oct 2010
    >> exit
    

    September 5th, 2010 @ 05:10 PM

  3. David Trasbo
    David Trasbo

    Neither was I:

    ➜  script/rails c
    Loading development environment (Rails 3.1.0.beta)
    ruby-1.9.2-p0 > a = Date.new(2010, 9, 30)
     => Thu, 30 Sep 2010 
    ruby-1.9.2-p0 > a.end_of_week
     => Sun, 03 Oct 2010 
    ruby-1.9.2-p0 > exit
    ➜  script/rails c production
    Loading production environment (Rails 3.1.0.beta)
    ruby-1.9.2-p0 > a = Date.new(2010, 9, 30)
     => Thu, 30 Sep 2010 
    ruby-1.9.2-p0 > a.end_of_week
     => Sun, 03 Oct 2010 
    ruby-1.9.2-p0 > exit
    

    (Edge Rails).

    September 5th, 2010 @ 07:04 PM

  4. Andrew White
    Andrew White
    • State changed from new to invalid
    • Importance changed from to Low

    Closing as no-one seems to be able to reproduce (myself included).

    September 5th, 2010 @ 10:24 PM

  5. Roger Campos
    Roger Campos

    I just realize now that, as you all say, it's working properly with a new rails 3.0.0 application, my mistake.

    However I'm still having this wired behaviour with an app I'm currently working on (http://github.com/Itnig/intranet3). Tested with ubuntu and mac platforms I was unable to reach the source of this problem.

    Anyway, looking at the implementations of end_of_week and beginning_of_week methods in activesupport/lib/active_support/core_ext/date/calculations.rb I still believe that 'result = self + days_to_sunday.days' (line 180) should be replaced with 'result = self + days_to_sunday'.
    The same change has already been made in beginning_of_week method, symetrical of end_of_week.

    September 5th, 2010 @ 10:29 PM

  6. Yuri Tomanek
    Yuri Tomanek

    I am also getting this error.

    Loading production environment (Rails 3.0.0)
    irb(main):001:0> 13.days
    => 1123200
    irb(main):002:0> a = Date.new(2010,9,30)
    => Thu, 30 Sep 2010
    irb(main):003:0> a.end_of_week
    => Mon, 31 May 2720
    

    Though this is happening in both development and production environments.
    I have tested on an OSX machine and also Linux box with same results.
    Im using ruby 1.8.7 (2009-06-12 patchlevel 174) on both machines.

    September 14th, 2010 @ 01:44 PM

  7. Andrew White
    Andrew White

    Still can't reproduce this using the system ruby on Mac OS X. Looking at your irb output it seems as though 13.days is returning an integer instead of a duration. Is there a plugin or gem that may have overwritten the days method? The other possibility is an old version of ActiveSupport::CoreExtensions::Numeric::Time being included - in Rails 1.2.x it returns an integer and not a duration.

    September 14th, 2010 @ 02:21 PM

  8. Yuri Tomanek
    Yuri Tomanek

    I will look into the possibility that a gem or plugin is overriding the days method.
    The weird thing is that this only started happening tonight, no new gems or plugins we added but I did do 3 small changes to the app, Though I would not expect those changes to effect the days method nor override it.

    September 14th, 2010 @ 02:26 PM

  9. Yuri Tomanek
    Yuri Tomanek

    Oh I lied!
    I did add a new gem to my Gemfile, I just did it late last night and had not got around to deploying the changes.
    God was the culprit, not that I can remember why I wanted to add it to my Gemfile.
    It was a long one last night.

    Thanks for putting me on the right track.

    September 14th, 2010 @ 02:33 PM

  10. Roger Campos
    Roger Campos

    You were right, the 'god' gem (god-0.11.0) was causing the problem redefining .days method in /lib/god/sugar.rb:20.

    Thanks for your advice, finally solved!

    September 14th, 2010 @ 03:08 PM