Sha256: 458cb2cc6ea5366d9853f4113d406b8ce23e2dc883551a9fe00c71007e1c7fae

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "active_support"
require "active_support/time"

module FeCoreExt::CoreExt
end

module FeCoreExt::CoreExt::Date
  def end_of_month?
    self == end_of_month
  end

  def range(duration)
    Range.new(*[self + duration, self].minmax)
  end

  def to_time_range
    Range.new(beginning_of_day, end_of_day)
  end

  def nth_weekday(nth, weekday)
    first_day = Date.new(year, month, 1)
    weekdays_hash = { sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6 }
    weekday_dates = (first_day..first_day.end_of_month).select { |d| d.wday == weekdays_hash[weekday] }
    weekday_dates[nth - 1]
  end
end

module FeCoreExt::CoreExt::DateClassMethods
  def parsable?(string)
    parsed_hash = _parse(string)
    return true if parsed_hash.has_key?(:year) && parsed_hash.has_key?(:mon)

    false
  end

  def parse_as_future(string)
    date = parse(string)
    date > current ? date : date + 1.year
  end

  def parse_heisei(string)
    string.match('平成(\d+)年(\d+)月(\d+)日') do
      Date.new(::Regexp.last_match(1).to_i + 1988, ::Regexp.last_match(2).to_i, ::Regexp.last_match(3).to_i)
    end
  end

  def parse_reiwa(string)
    string.match('令和(\S+)年(\d+)月(\d+)日') do
      year = 1 if ::Regexp.last_match(1) == "元"
      year ||= ::Regexp.last_match(1).to_i
      Date.new(year + 2018, ::Regexp.last_match(2).to_i, ::Regexp.last_match(3).to_i)
    end
  end

  def parse_gengo(string)
    parse_heisei(string) || parse_reiwa(string)
  end

  def parse_nengappi(string)
    string.match(/(\d{4})年(\d+)月(\d+)日/) do
      Date.new(::Regexp.last_match(1).to_i, ::Regexp.last_match(2).to_i, ::Regexp.last_match(3).to_i)
    end
  end

  def parse_ja(string)
    parse_nengappi(string) || parse_gengo(string)
  end
end

class Date
  include FeCoreExt::CoreExt::Date
  extend FeCoreExt::CoreExt::DateClassMethods
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fe_core_ext-0.30.0 lib/fe_core_ext/core_ext/date.rb
fe_core_ext-0.29.1 lib/fe_core_ext/core_ext/date.rb