Module: PartitionGardener::DateCalendar
- Defined in:
- lib/partition_gardener/date_calendar.rb
Class Method Summary collapse
- .add_days(date, count) ⇒ Object
- .add_months(date, count) ⇒ Object
- .add_quarters(date, count) ⇒ Object
- .add_weeks(date, count) ⇒ Object
- .add_years(date, count) ⇒ Object
- .beginning_of_day(date) ⇒ Object
- .beginning_of_month(date) ⇒ Object
- .beginning_of_quarter(date) ⇒ Object
- .beginning_of_week(date) ⇒ Object
- .beginning_of_year(date) ⇒ Object
- .next_month(date) ⇒ Object
- .next_year(date) ⇒ Object
- .to_date(value) ⇒ Object
Class Method Details
.add_days(date, count) ⇒ Object
34 35 36 |
# File 'lib/partition_gardener/date_calendar.rb', line 34 def add_days(date, count) to_date(date) + count end |
.add_months(date, count) ⇒ Object
59 60 61 62 63 |
# File 'lib/partition_gardener/date_calendar.rb', line 59 def add_months(date, count) base = beginning_of_month(date) month_index = (base.year * 12) + (base.month - 1) + count Date.new(month_index / 12, (month_index % 12) + 1, 1) end |
.add_quarters(date, count) ⇒ Object
42 43 44 |
# File 'lib/partition_gardener/date_calendar.rb', line 42 def add_quarters(date, count) add_months(date, count * 3) end |
.add_weeks(date, count) ⇒ Object
38 39 40 |
# File 'lib/partition_gardener/date_calendar.rb', line 38 def add_weeks(date, count) add_days(date, count * 7) end |
.add_years(date, count) ⇒ Object
54 55 56 57 |
# File 'lib/partition_gardener/date_calendar.rb', line 54 def add_years(date, count) value = to_date(date) Date.new(value.year + count, value.month, value.day) end |
.beginning_of_day(date) ⇒ Object
19 20 21 |
# File 'lib/partition_gardener/date_calendar.rb', line 19 def beginning_of_day(date) to_date(date) end |
.beginning_of_month(date) ⇒ Object
9 10 11 12 |
# File 'lib/partition_gardener/date_calendar.rb', line 9 def beginning_of_month(date) value = to_date(date) Date.new(value.year, value.month, 1) end |
.beginning_of_quarter(date) ⇒ Object
28 29 30 31 32 |
# File 'lib/partition_gardener/date_calendar.rb', line 28 def beginning_of_quarter(date) value = to_date(date) quarter_month = ((value.month - 1) / 3) * 3 + 1 Date.new(value.year, quarter_month, 1) end |
.beginning_of_week(date) ⇒ Object
23 24 25 26 |
# File 'lib/partition_gardener/date_calendar.rb', line 23 def beginning_of_week(date) value = to_date(date) value - ((value.wday + 6) % 7) end |
.beginning_of_year(date) ⇒ Object
14 15 16 17 |
# File 'lib/partition_gardener/date_calendar.rb', line 14 def beginning_of_year(date) value = to_date(date) Date.new(value.year, 1, 1) end |
.next_month(date) ⇒ Object
46 47 48 |
# File 'lib/partition_gardener/date_calendar.rb', line 46 def next_month(date) add_months(beginning_of_month(date), 1) end |
.next_year(date) ⇒ Object
50 51 52 |
# File 'lib/partition_gardener/date_calendar.rb', line 50 def next_year(date) Date.new(to_date(date).year + 1, 1, 1) end |
.to_date(value) ⇒ Object
5 6 7 |
# File 'lib/partition_gardener/date_calendar.rb', line 5 def to_date(value) value.respond_to?(:to_date) ? value.to_date : Date.parse(value.to_s) end |