This project is archived and is in readonly mode.

#2304 ✓wontfix
Matt Darby

Add concept of weekdays

Reported by Matt Darby | March 20th, 2009 @ 08:39 PM | in 2.x

When writing domain logic for my Rails apps, I always run across the need to calculate spans of weekdays. As an example:


def due_date
  3.weekdays_from(created_at)
end

def is_stale?
  created_at >= 3.weekdays_ago
end

def total_length_in_weekdays
  start_date.weekdays_until(end_date)
end

This patch adds:

  • ActiveSupport::CoreExtensions::Date::Calculations
    • Date#weekday?
    • Date#weekdays_until
  • ActiveSupport::CoreExtensions::Numeric::Time
    • Numeric#weekdays_ago
    • Numeric#weekdays_from
    • Numeric#weekdays_from_now

Comments and changes to this ticket

  • Benjamin Allfree

    Benjamin Allfree March 30th, 2009 @ 04:07 PM

    Very useful, great job Matt.

    Attached a monkey patch version that people can try from lib/ and include in environment.rb.

    Had a couple thoughts/changes to get it working for me

    1) I had to add this to ActiveSupport::CoreExtensions::Time::Calculations too

    2) 0.weeks_from(Time.now) breaks, I fixed the code to handle that. weekdays_ago had the same bug, I think both are fixed now.

    3) I haven't tried it, but if you do -5.weekdays_from(Time.now) I suspect it will break. I don't really know enough about the existing date calc code to decide if this is normal/expected.

  • Matt Darby

    Matt Darby March 30th, 2009 @ 09:19 PM

    Thanks Benjamin!

    1) I've updated the patch (weekdays_take_two.diff) and it now adds the following:

    • ActiveSupport::CoreExtensions::Date::Calculations

      • weekday?

      • weekdays_until(date)

    • ActiveSupport::CoreExtensions::Time::Calculations

      • weekday?

      • weekdays_until?(date)

    • ActiveSupport::CoreExtensions::DateTime::Calculations

      • weekday?

    • ActiveSupport::CoreExtensions::Numeric::Time

      • weekdays_from(time)

      • weekdays_from_now

      • weekdays_ago(time)

    2) Awesome -- good catch! I've added tests for zero lengths.

    3) (-5.days.ago == 5.days.from_now), so I have mimiced the same; (-5.weekdays_ago == 5.weekdays_from_now) Tests have been updated to reflect this update.

  • Matt Darby

    Matt Darby March 30th, 2009 @ 09:20 PM

    I've updated the monkey patch you've provided (weekday_calculations_take_two.rb) with the latest changes.

  • Geoff Buesing

    Geoff Buesing March 31st, 2009 @ 02:51 PM

    • State changed from “new” to “wontfix”

    Nice work, would be nice as a gem/plugin, but I don't think this is right for core. Reasons:

    1. "weekday" can mean either "day of the week" (i.e., all seven weekdays) or "work week day" (i.e, Monday - Friday, for many of us), so the names of methods like #weekdays_from are kind of confusing.

    2. The work week itself isn't Monday-Friday in many parts of the world -- see http://en.wikipedia.org/wiki/Wor... . One could i18n these methods, but that would be a lot more work

    3. the implementation of these methods instantiates all the Time instances between two times, so that it can call #weekday? on each -- this would be fine for small time spans, but pretty intensive for longer spans. An algorithm would be nicer here.

  • Matt Darby

    Matt Darby March 31st, 2009 @ 03:54 PM

    Thanks for your feedback -- I've released a gem containing this logic: http://github.com/mdarby/weekday...

  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:53 AM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • bingbing

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