module Hizuke::QuarterCalculator

def calculate_last_quarter_date

Returns:
  • (Date) - the first day of the last quarter
def calculate_last_quarter_date
  today = Date.today
  current_month = today.month
  last_quarter_month = determine_last_quarter_month(current_month)
  last_quarter_year = determine_last_quarter_year(today.year, current_month)
  Date.new(last_quarter_year, last_quarter_month, 1)
end

def calculate_next_quarter_date

Returns:
  • (Date) - the first day of the next quarter
def calculate_next_quarter_date
  today = Date.today
  current_month = today.month
  next_quarter_month = determine_next_quarter_month(current_month)
  next_quarter_year = determine_next_quarter_year(today.year, current_month)
  Date.new(next_quarter_year, next_quarter_month, 1)
end

def calculate_quarter_date(date_value)

Returns:
  • (Date) - the calculated date

Parameters:
  • date_value (Symbol) -- the date keyword (:next_quarter or :last_quarter)
def calculate_quarter_date(date_value)
  case date_value
  when :next_quarter
    calculate_next_quarter_date
  when :last_quarter
    calculate_last_quarter_date
  end
end

def determine_last_quarter_month(current_month)

Returns:
  • (Integer) - the month number of the last quarter star

Parameters:
  • current_month (Integer) -- the current month (1-12)
def determine_last_quarter_month(current_month)
  if current_month <= 3
    10 # Q4 of last year starts in October
  elsif current_month <= 6
    1  # Q1 starts in January
  elsif current_month <= 9
    4  # Q2 starts in April
  else
    7  # Q3 starts in July
  end
end

def determine_last_quarter_year(current_year, current_month)

Returns:
  • (Integer) - the year of the last quarter

Parameters:
  • current_month (Integer) -- the current month (1-12)
  • current_year (Integer) -- the current year
def determine_last_quarter_year(current_year, current_month)
  current_year - (current_month <= 3 ? 1 : 0)
end

def determine_next_quarter_month(current_month)

Returns:
  • (Integer) - the month number of the next quarter star

Parameters:
  • current_month (Integer) -- the current month (1-12)
def determine_next_quarter_month(current_month)
  if current_month <= 3
    4  # Q2 starts in April
  elsif current_month <= 6
    7  # Q3 starts in July
  elsif current_month <= 9
    10 # Q4 starts in October
  else
    1  # Q1 of next year starts in January
  end
end

def determine_next_quarter_year(current_year, current_month)

Returns:
  • (Integer) - the year of the next quarter

Parameters:
  • current_month (Integer) -- the current month (1-12)
  • current_year (Integer) -- the current year
def determine_next_quarter_year(current_year, current_month)
  current_year + (current_month > 9 ? 1 : 0)
end