Module: ETL::CoreExtensions::Time::Calculations

Included in:
Time
Defined in:
lib/etl/core_ext/time/calculations.rb

Overview

Enables the use of time calculations within Time itself

Instance Method Summary collapse

Instance Method Details

#fiscal_year(offset_month = 10) ⇒ Object



27
28
29
# File 'lib/etl/core_ext/time/calculations.rb', line 27

def fiscal_year(offset_month=10)
  month >= offset_month ? year + 1 : year
end

#fiscal_year_month(offset_month = 10) ⇒ Object



19
20
21
22
23
# File 'lib/etl/core_ext/time/calculations.rb', line 19

def fiscal_year_month(offset_month=10)
  shifted_month = month - (offset_month - 1)
  shifted_month += 12 if shifted_month < 0
  shifted_month
end

#fiscal_year_quarter(offset_month = 10) ⇒ Object



24
25
26
# File 'lib/etl/core_ext/time/calculations.rb', line 24

def fiscal_year_quarter(offset_month=10)
  ((fiscal_year_month(offset_month) - 1) / 3) + 1
end

#fiscal_year_week(offset_month = 10) ⇒ Object



14
15
16
17
18
# File 'lib/etl/core_ext/time/calculations.rb', line 14

def fiscal_year_week(offset_month=10)
  fyw = ((fiscal_year_yday(offset_month) - 1) / 7) + 1
  fyw = 52 if fyw == 53
  fyw
end

#fiscal_year_yday(offset_month = 10) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/etl/core_ext/time/calculations.rb', line 30

def fiscal_year_yday(offset_month=10)
  offset_days = 0
  1.upto(offset_month - 1) { |m| offset_days += ::Time.days_in_month(m, year) }
  shifted_year_day = yday - offset_days
  shifted_year_day += 365 if shifted_year_day <= 0
  shifted_year_day
end

#quarterObject



11
12
13
# File 'lib/etl/core_ext/time/calculations.rb', line 11

def quarter
  ((month - 1) / 3) + 1
end

#weekObject



6
7
8
9
10
# File 'lib/etl/core_ext/time/calculations.rb', line 6

def week
  cyw = ((yday - 1) / 7) + 1
  cyw = 52 if cyw == 53
  cyw
end