Project

General

Profile

Actions

Feature #12211

open

introduce Date#first_of_month and Date#last_of_month

Feature #12211: introduce Date#first_of_month and Date#last_of_month

Added by araipiyo (Shunichi Arai) almost 10 years ago. Updated over 9 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:74524]

Description

Proposes new methods Date#first_of_month and Date#last_of_month which can be very helpful in reporting applications which need the first and the last day of a month very frequently.

class Date
    def first_of_month
      Date.new(year, month, 1)
    end
    def last_of_month
      next_month.first_of_month.prev_day
    end
end

Current way to do it Date.new(date.year, date.month, 1) is not elegant and short enough for this simple and common operation.

Updated by zverok (Victor Shepelev) almost 10 years ago Actions #1 [ruby-core:74526]

IMHO, consistent approach would be "allow Date/Time to be rounded to any component", there are many uses for this. Maybe something like ceil/floor/round methods set with Symbol argument? Like floor(:month) and ceil(:month)?

Updated by shyouhei (Shyouhei Urabe) over 9 years ago Actions #2 [ruby-core:74545]

Naming-wise, Rails (ActiveSupport) has Date#beginning_of_month. That name can be more familiar to other devs.

I'm against floor(:month) and such, especially against Time class. The concept of "the second that a day begins" is not that concrete when it relates to summer time. I recommend you to stick this proposal to Date only.

Updated by araipiyo (Shunichi Arai) over 9 years ago Actions #3 [ruby-core:74583]

Shyouhei Urabe wrote:

Naming-wise, Rails (ActiveSupport) has Date#beginning_of_month. That name can be more familiar to other devs.

I'm against floor(:month) and such, especially against Time class. The concept of "the second that a day begins" is not that concrete when it relates to summer time. I recommend you to stick this proposal to Date only.

I think Date#beginning_of_month is bit verbose but okay too. How about to have an alias to Date#first_of_month?

floor(:month) looks elegant, but I think it's bit counter-intuitive. How about Date#day(n), Date#wday(n), or Date#year(n) which create a new date object with diffrent day / wday / year?

Actions

Also available in: PDF Atom