Skip to content

Instantly share code, notes, and snippets.

@brianmario
Created January 26, 2009 22:05
Show Gist options
  • Save brianmario/52992 to your computer and use it in GitHub Desktop.
Save brianmario/52992 to your computer and use it in GitHub Desktop.
Time extensions for Fiscal Calendar
# Time object extension adding fiscal calendar calculations based on an offset month
class Time
# The first day of the current fiscal quarter of this Time object instance, based on the passed offset_month
def beginning_of_fiscal_quarter(offset_month=1)
t = beginning_of_fiscal_year(offset_month)
4.times do
return t if self >= t && self < t+3.months
t = t+3.months
end
end
# The very last day of the current fiscal quarter of this Time object instance, based on the passed offset_month
def end_of_fiscal_quarter(offset_month=1)
beginning_of_fiscal_quarter(offset_month) + 3.months
end
# The first day of the fiscal year of this Time object instance, based on the passed offset_month
def beginning_of_fiscal_year(offset_month=1)
t = self.beginning_of_year + (offset_month-1).months
year = (t - t.yday.days).year unless t.yday == 1
t.change(:year => year)
end
# The very last day of the fiscal year of this Time object instance, based on the passed offset_month
def end_of_fiscal_year(offset_month=1)
beginning_of_fiscal_year(offset_month)+12.months
end
# Returns an array of starting dates for each quarter based on this Time object instance, and the passed offset_month
def to_fiscal_quarters(offset_month=1)
first_quarter = beginning_of_fiscal_year(offset_month)
second_quarter = first_quarter+3.months
third_quarter = second_quarter+3.months
fourth_quarter = third_quarter+3.months
[first_quarter, second_quarter, third_quarter, fourth_quarter]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment