This project is archived and is in readonly mode.

#5548 ✓invalid
Roger Campos

Date.end_of_week not working in production mode

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

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.

Comments and changes to this ticket

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer September 5th, 2010 @ 10:59 AM

    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
    
  • Rafael Mendonça França

    Rafael Mendonça França September 5th, 2010 @ 05:10 PM

    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
    
  • David Trasbo

    David Trasbo September 5th, 2010 @ 07:04 PM

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

  • Andrew White

    Andrew White September 5th, 2010 @ 10:24 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

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

  • Roger Campos

    Roger Campos September 5th, 2010 @ 10:29 PM

    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.

  • Yuri Tomanek

    Yuri Tomanek September 14th, 2010 @ 01:44 PM

    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.

  • Andrew White

    Andrew White September 14th, 2010 @ 02:21 PM

    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.

  • Yuri Tomanek

    Yuri Tomanek September 14th, 2010 @ 02:26 PM

    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.

  • Yuri Tomanek

    Yuri Tomanek September 14th, 2010 @ 02:33 PM

    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.

  • Roger Campos

    Roger Campos September 14th, 2010 @ 03:08 PM

    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!

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages