opening_hours_converter 1.9.1 → 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/opening_hours_converter/week_index.rb +128 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffbf39995a37cbc20665e8f80b06366fb7190ef0029b501166e90a3e2e5055b0
|
4
|
+
data.tar.gz: 6d09adc6b4eab8973f2382786b8ab875b545b1ae218029565f58938b5e8f5d8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 126b99ba077709da252ca6c7c0aa4a1260e258c52346df49d6947acd64a2d7fa9009d9750c528cc466bad6e1e225d6f656a3556f784b4668537a8ec2442d27fa
|
7
|
+
data.tar.gz: 0eaa257f1f7f45c39adf25cc66f939e1dba0e0a7fd675586bf38e14341fc423ffda4d9942f473947e822c47855428c53d9e6b68595486428139188334029c107
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'opening_hours_converter/constants'
|
2
|
+
require 'opening_hours_converter/utils'
|
3
|
+
|
4
|
+
module OpeningHoursConverter
|
5
|
+
class WeekIndex
|
6
|
+
include Constants
|
7
|
+
extend Utils
|
8
|
+
|
9
|
+
def self.week_from_index(index, year = Time.now.year)
|
10
|
+
raise unless index <= week_count(year)
|
11
|
+
raise unless index >= 1
|
12
|
+
|
13
|
+
week = first_week(year)
|
14
|
+
offset = (index - 1) * 7
|
15
|
+
add_offset_to_week(week, offset)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.index_from_week(week, year = Time.now.year)
|
19
|
+
week_difference(first_week(year)[:from], week[:from]) + 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.week_count(year = Time.now.year)
|
23
|
+
p = proc { |year| (year + (year / 4) - (year / 100) + (year / 400)) % 7 }
|
24
|
+
|
25
|
+
return 53 if p.call(year) == 4 || p.call(year - 1) == 3
|
26
|
+
return 52
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.add_offset_to_week(week, offset)
|
30
|
+
{
|
31
|
+
from: week[:from] + offset,
|
32
|
+
to: week[:to] + offset
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.first_week(year = Time.now.year)
|
37
|
+
start_day = first_day_of_first_week(year)
|
38
|
+
{
|
39
|
+
from: start_day,
|
40
|
+
to: start_day + 6
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.last_week(year = Time.now.year)
|
45
|
+
end_day = last_day_of_last_week(year)
|
46
|
+
{
|
47
|
+
from: end_day - 6,
|
48
|
+
to: end_day
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.last_day_of_last_week(year = Time.now.year)
|
53
|
+
last_day_of_year = Date.new(year, 12, 31)
|
54
|
+
last_wday_of_year = reindex_sunday_week_to_monday_week(last_day_of_year.wday)
|
55
|
+
|
56
|
+
return last_day_of_year if last_wday_of_year == 6 # last day of year is sunday
|
57
|
+
return Date.new(year + 1, 1, 7 - last_wday_of_year - 1) if last_wday_of_year >= 3 # last day of year is thursday friday or saturday
|
58
|
+
return Date.new(year, 12, 31 - last_wday_of_year - 1) # last day of year is monday tuesday or wednesday
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.first_day_of_first_week(year = Time.now.year)
|
62
|
+
first_day_of_year = Date.new(year, 1, 1)
|
63
|
+
first_wday_of_year = reindex_sunday_week_to_monday_week(first_day_of_year.wday)
|
64
|
+
|
65
|
+
return first_day_of_year if first_wday_of_year == 0 # first day of year is monday
|
66
|
+
return Date.new(year - 1, 12, 31 - first_wday_of_year + 1) if first_wday_of_year < 4 # first day of year is tuesday wednesday or thursday
|
67
|
+
return Date.new(year, 1, 7 - first_wday_of_year + 1) # first day of year is friday saturday or sunday
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.nth_wday_of_month(n, wday, month, year = Time.now.year)
|
71
|
+
return last_wday_of_month(wday, month, year) if n == -1
|
72
|
+
|
73
|
+
first_day_of_month = Date.new(year, month, 1)
|
74
|
+
first_wday_of_month = reindex_sunday_week_to_monday_week(first_day_of_month.wday)
|
75
|
+
|
76
|
+
date =
|
77
|
+
if wday == first_wday_of_month
|
78
|
+
# first day of the month is the weekday we are looking for
|
79
|
+
|
80
|
+
first_day_of_month + (n - 1) * 7
|
81
|
+
elsif wday < first_wday_of_month
|
82
|
+
# first day of the month is after (in the week) than the weekday we are looking for
|
83
|
+
# so we look in the next week
|
84
|
+
|
85
|
+
last_monday_of_previous_month = first_day_of_month - first_wday_of_month
|
86
|
+
first_monday_of_the_month = last_monday_of_previous_month + 7
|
87
|
+
|
88
|
+
first_monday_of_the_month + wday + (n - 1) * 7
|
89
|
+
else
|
90
|
+
# first day of the month is before (in the week) than the weekday we are looking for
|
91
|
+
# so we look in the current week
|
92
|
+
|
93
|
+
first_day_of_the_week = first_day_of_month - first_wday_of_month
|
94
|
+
first_day_of_the_week + wday + (n - 1) * 7
|
95
|
+
end
|
96
|
+
raise 'Out of bound' unless date.month == month
|
97
|
+
|
98
|
+
date
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.last_wday_of_month(wday, month, year = Time.now.year)
|
102
|
+
last_day_of_month = Date.new(year, month, last_day_of_month(month - 1, year))
|
103
|
+
last_wday_of_month = reindex_sunday_week_to_monday_week(last_day_of_month.wday)
|
104
|
+
|
105
|
+
date =
|
106
|
+
if wday == last_wday_of_month
|
107
|
+
# last day of the month is the weekday we are looking for
|
108
|
+
|
109
|
+
last_day_of_month
|
110
|
+
elsif wday > last_wday_of_month
|
111
|
+
# last day of the month is before (in the week) than the weekday we are looking for
|
112
|
+
# so we look in the previous week
|
113
|
+
|
114
|
+
previous_week_monday = last_day_of_month - last_wday_of_month - 7
|
115
|
+
previous_week_monday + wday
|
116
|
+
else
|
117
|
+
# last day of the month is after (in the week) than the weekday we are looking for
|
118
|
+
# so we look in the current week
|
119
|
+
|
120
|
+
first_day_of_the_week = last_day_of_month - last_wday_of_month
|
121
|
+
first_day_of_the_week + wday
|
122
|
+
end
|
123
|
+
raise 'Out of bound' unless date.month == month
|
124
|
+
|
125
|
+
date
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opening_hours_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.9.
|
4
|
+
version: 1.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ziserman Martin
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/opening_hours_converter/tokens_handler.rb
|
63
63
|
- lib/opening_hours_converter/utils.rb
|
64
64
|
- lib/opening_hours_converter/week.rb
|
65
|
+
- lib/opening_hours_converter/week_index.rb
|
65
66
|
- lib/opening_hours_converter/wide_interval.rb
|
66
67
|
- lib/opening_hours_converter/year.rb
|
67
68
|
homepage: https://github.com/Publidata/opening_hours_converter
|