lib/fiscal_year/half.rb in fiscal_year-0.1.0 vs lib/fiscal_year/half.rb in fiscal_year-0.2.0
- old
+ new
@@ -1,11 +1,10 @@
# frozen_string_literal: true
module FiscalYear
class Half
class << self
-
def first
FiscalYear.halfs.first
end
def second
@@ -19,24 +18,46 @@
def second?(month)
!first?(month)
end
def first_range_by(year)
- Date.parse("#{year}/#{first.first}")..Date.parse("#{year}/#{first.last}").end_of_month
+ # care Date#parse 2 digit year auto complete.
+ # 99 + 1 = 100, but expect 2000 this context.
+ year = 1999 if year == 99
+ end_year = normalize_year_by_month(year, first.last)
+
+ Date.parse("#{year}/#{first.first}/01")..Date.parse("#{end_year}/#{first.last}/01").end_of_month
end
def second_range_by(year)
- Date.parse("#{year}/#{second.first}")..Date.parse("#{year + 1}/#{second.last}").end_of_month
+ # care Date#parse 2 digit year auto complete.
+ # 99 + 1 = 100, but expect 2000 this context.
+ year = 1999 if year == 99
+ start_year = normalize_year_by_month(year, second.first)
+ end_year = normalize_year_by_month(year, second.last)
+
+ Date.parse("#{start_year}/#{second.first}/01")..Date.parse("#{end_year}/#{second.last}/01").end_of_month
end
def range_by(date)
month = date.month
year = date.year
+ # if passed crossed year value, normalize to not crossed year value
+ year -= 1 if FiscalYear.cross_year_month?(month)
+
+ first?(month) ? first_range_by(year) : second_range_by(year)
+ end
+
+ def normalize_year_by_month(year, month)
if FiscalYear.cross_year_month?(month)
- Date.parse("#{year - 1}/#{second.first}")..Date.parse("#{year}/#{second.last}").end_of_month
+ year + 1
else
- first?(month) ? first_range_by(date.year) : second_range_by(date.year)
+ year
end
+ end
+
+ def cross_year_in_half?(half)
+ FiscalYear.cross_year? && half.any? { |month| month == 12 }
end
end
end
end