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.

Add concept of weekdays

#2304

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

Reported by Matt Darby · March 20th, 2009 @ 08:39 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: Low

Activity

  1. Benjamin Allfree
    Benjamin Allfree

    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.

    March 30th, 2009 @ 04:07 PM

  2. Matt Darby
    Matt Darby

    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.

    March 30th, 2009 @ 09:19 PM

  3. Matt Darby
    Matt Darby

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

    March 30th, 2009 @ 09:20 PM

  4. Geoff Buesing
    Geoff Buesing
    • 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.

    March 31st, 2009 @ 02:51 PM

  5. Matt Darby
    Matt Darby

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

    March 31st, 2009 @ 03:54 PM

  6. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:53 AM

  7. bingbing