Class: OpeningHoursConverter::WeekIndex
- Inherits:
-
Object
- Object
- OpeningHoursConverter::WeekIndex
show all
- Extended by:
- Utils
- Includes:
- Constants
- Defined in:
- lib/opening_hours_converter/week_index.rb
Constant Summary
Constants included
from Constants
Constants::DAYS, Constants::DAYS_MAX, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX
Class Method Summary
collapse
Methods included from Utils
add_days_to_time, datetime_to_time, day_difference, last_day_of_month, leap_year?, reindex_sunday_week_to_monday_week, seconds_in_day, time_to_datetime, timstring_as_minutes, week_difference
Class Method Details
.add_offset_to_week(week, offset) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/opening_hours_converter/week_index.rb', line 28
def self.add_offset_to_week(week, offset)
{
from: week[:from] + offset,
to: week[:to] + offset
}
end
|
.first_day_of_first_week(year = Time.now.year) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/opening_hours_converter/week_index.rb', line 60
def self.first_day_of_first_week(year = Time.now.year)
first_day_of_year = Date.new(year, 1, 1)
first_wday_of_year = reindex_sunday_week_to_monday_week(first_day_of_year.wday)
return first_day_of_year if first_wday_of_year == 0 return Date.new(year - 1, 12, 31 - first_wday_of_year + 1) if first_wday_of_year < 4 return Date.new(year, 1, 7 - first_wday_of_year + 1) end
|
.first_week(year = Time.now.year) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/opening_hours_converter/week_index.rb', line 35
def self.first_week(year = Time.now.year)
start_day = first_day_of_first_week(year)
{
from: start_day,
to: start_day + 6
}
end
|
.index_from_week(week, year = Time.now.year) ⇒ Object
17
18
19
|
# File 'lib/opening_hours_converter/week_index.rb', line 17
def self.index_from_week(week, year = Time.now.year)
week_difference(first_week(year)[:from], week[:from]) + 1
end
|
.last_day_of_last_week(year = Time.now.year) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/opening_hours_converter/week_index.rb', line 51
def self.last_day_of_last_week(year = Time.now.year)
last_day_of_year = Date.new(year, 12, 31)
last_wday_of_year = reindex_sunday_week_to_monday_week(last_day_of_year.wday)
return last_day_of_year if last_wday_of_year == 6 return Date.new(year + 1, 1, 7 - last_wday_of_year - 1) if last_wday_of_year >= 3 return Date.new(year, 12, 31 - last_wday_of_year - 1) end
|
.last_wday_of_month(wday, month, year = Time.now.year) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/opening_hours_converter/week_index.rb', line 100
def self.last_wday_of_month(wday, month, year = Time.now.year)
last_day_of_month = Date.new(year, month, last_day_of_month(month - 1, year))
last_wday_of_month = reindex_sunday_week_to_monday_week(last_day_of_month.wday)
date =
if wday == last_wday_of_month
last_day_of_month
elsif wday > last_wday_of_month
previous_week_monday = last_day_of_month - last_wday_of_month - 7
previous_week_monday + wday
else
first_day_of_the_week = last_day_of_month - last_wday_of_month
first_day_of_the_week + wday
end
raise ArgumentError, 'Out of bound' unless date.month == month
date
end
|
.last_week(year = Time.now.year) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/opening_hours_converter/week_index.rb', line 43
def self.last_week(year = Time.now.year)
end_day = last_day_of_last_week(year)
{
from: end_day - 6,
to: end_day
}
end
|
.nth_wday_of_month(n, wday, month, year = Time.now.year) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/opening_hours_converter/week_index.rb', line 69
def self.nth_wday_of_month(n, wday, month, year = Time.now.year)
return last_wday_of_month(wday, month, year) if n == -1
first_day_of_month = Date.new(year, month, 1)
first_wday_of_month = reindex_sunday_week_to_monday_week(first_day_of_month.wday)
date =
if wday == first_wday_of_month
first_day_of_month + (n - 1) * 7
elsif wday < first_wday_of_month
last_monday_of_previous_month = first_day_of_month - first_wday_of_month
first_monday_of_the_month = last_monday_of_previous_month + 7
first_monday_of_the_month + wday + (n - 1) * 7
else
first_day_of_the_week = first_day_of_month - first_wday_of_month
first_day_of_the_week + wday + (n - 1) * 7
end
raise ArgumentError, 'Out of bound' unless date.month == month
date
end
|
.week_count(year = Time.now.year) ⇒ Object
21
22
23
24
25
26
|
# File 'lib/opening_hours_converter/week_index.rb', line 21
def self.week_count(year = Time.now.year)
p = proc { |year| (year + (year / 4) - (year / 100) + (year / 400)) % 7 }
return 53 if p.call(year) == 4 || p.call(year - 1) == 3
return 52
end
|
.week_from_index(index, year = Time.now.year) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/opening_hours_converter/week_index.rb', line 9
def self.week_from_index(index, year = Time.now.year)
raise ArgumentError unless index >= 1
week = first_week(year)
offset = (index - 1) * 7
add_offset_to_week(week, offset)
end
|