11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/fiscal_date/date_ext.rb', line 11
def by_quarter_date(quarter_end_date, year_end_month = 12)
quarter_end_month = quarter_end_date.month - 1
year_end_month = year_end_month - 1
fiscal_year = quarter_end_date.year
fiscal_quarter = nil
if year_end_month == quarter_end_month
fiscal_quarter = 4
elsif (year_end_month + 3) % 12 == quarter_end_month
fiscal_quarter = 1
elsif (year_end_month + 6) % 12 == quarter_end_month
fiscal_quarter = 2
elsif (year_end_month + 9) % 12 == quarter_end_month
fiscal_quarter = 3
else
raise(InconsistentCalendar, "quarter ends month #{quarter_end_month} and year ends month #{year_end_month}")
end
fiscal_year += 1 if quarter_end_month > year_end_month
return new(fiscal_year, fiscal_quarter)
end
|