Module: Decentworks::DateSupport::DateExtension::ClassMethods

Included in:
Date
Defined in:
lib/decentworks/date_support/date_extension.rb,
sig/decentworks/date_support/date_extension.rbs

Instance Method Summary collapse

Instance Method Details

#expiration_date(from:, months:) ⇒ ::Date

満了日

fromから起算してmonthsヶ月が満了する日を返す。

from: 2026-01-15, months: 1 # => 2026-02-15(起算日2026-01-16の応当日2026-02-16の前日)
from: 2026-01-30, months: 1 # => 2026-02-28(起算日2026-01-31の応当日が存在しないため2月の末日)
from: 2026-02-28, months: 1 # => 2026-03-31(起算日2026-03-01の応当日2026-04-01の前日)

Parameters:

  • from: (::Date)
  • months: (::Integer)

Returns:



54
55
56
57
58
59
# File 'lib/decentworks/date_support/date_extension.rb', line 54

def expiration_date(from:, months:)
  beginning_date = from.next_day                # 起算日(初日不算入)
  corresponding_date = beginning_date >> months # 応当日(存在しない場合はその月の末日に繰り下がる)

  corresponding_date.day == beginning_date.day ? corresponding_date.prev_day : corresponding_date
end

#whole_months_elapsed(from:, to:) ⇒ ::Integer

満経過月数

fromからtoまでに満了した月数を返す。 民法第140条(初日不算入)に従いfromの翌日を起算日とし、 民法第143条第2項に従い応当日の前日をもって満了とする。 応当日が存在しない月は、同項ただし書に従いその月の末日をもって満了とする。

::Date.whole_months_elapsed(from: ::Date.new(2026, 1, 31), to: ::Date.new(2026, 2, 27)) # => 0
::Date.whole_months_elapsed(from: ::Date.new(2026, 1, 31), to: ::Date.new(2026, 2, 28)) # => 1
::Date.whole_months_elapsed(from: ::Date.new(2026, 2, 28), to: ::Date.new(2026, 3, 30)) # => 0
::Date.whole_months_elapsed(from: ::Date.new(2026, 2, 28), to: ::Date.new(2026, 3, 31)) # => 1
::Date.whole_months_elapsed(from: ::Date.new(2024, 2, 29), to: ::Date.new(2025, 2, 28)) # => 12

Parameters:

Returns:

  • (::Integer)

Raises:

  • (ArgumentError)

    toがfromより前の日付の場合



36
37
38
39
40
41
42
43
# File 'lib/decentworks/date_support/date_extension.rb', line 36

def whole_months_elapsed(from:, to:)
  raise ::ArgumentError, "to must be on or after from (from: #{from}, to: #{to})" if to < from

  months = ((to.year - from.year) * 12) + (to.month - from.month)

  # 満了日に達していない場合は1ヶ月に満たない
  expiration_date(from:, months:) > to ? months - 1 : months
end