Class: Date

Inherits:
Object show all
Defined in:
lib/coaster/core_ext/date.rb

Instance Method Summary collapse

Instance Method Details

#next_specific_date(day_num) ⇒ Object

Raises:

  • (Date::Error)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/coaster/core_ext/date.rb', line 31

def next_specific_date(day_num)
  raise Date::Error, 'invalid date' if day_num < 1 || day_num > 31

  m = Month.from(self)
  if day < day_num
    if m.last_date.day < day_num
      if day < m.last_date.day
        m.last_date
      else
        begin
          m.later.date_for_day(day_num)
        rescue Date::Error
          m.later.last_date
        end
      end
    else
      begin
        m.date_for_day(day_num)
      rescue Date::Error
        m.last_date
      end
    end
  else
    begin
      m.later.date_for_day(day_num)
    rescue Date::Error
      m.later.last_date
    end
  end
end

#prev_specific_date(day_num) ⇒ Object

Raises:

  • (Date::Error)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/coaster/core_ext/date.rb', line 12

def prev_specific_date(day_num)
  raise Date::Error, 'invalid date' if day_num < 1 || day_num > 31

  m = Month.from(self)
  if day_num < day
    begin
      m.date_for_day(day_num)
    rescue Date::Error
      m.last_date
    end
  else
    begin
      m.previous.date_for_day(day_num)
    rescue Date::Error
      m.previous.last_date
    end
  end
end

#to_time_range(timezone = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/coaster/core_ext/date.rb', line 2

def to_time_range(timezone = nil)
  if timezone
    timezone = ActiveSupport::TimeZone[timezone] if timezone.is_a?(String)
    b_day = in_time_zone(timezone)
    b_day...(b_day + 1.day)
  else
    beginning_of_day...(self + 1.day).beginning_of_day
  end
end