time_crisis 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +1 -1
- data/lib/time_crisis/holiday.rb +1 -1
- data/lib/time_crisis/nth_weekday.rb +16 -2
- data/time_crisis.gemspec +1 -1
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/time_crisis/holiday.rb
CHANGED
@@ -13,17 +13,25 @@ module TimeCrisis
|
|
13
13
|
|
14
14
|
def nth_weekday(h={})
|
15
15
|
raise ArgumentError unless h[:nth]
|
16
|
+
nth = h[:nth].to_s == 'last' ? 5 : h[:nth]
|
16
17
|
|
17
18
|
today = TimeCrisis::Date.today
|
18
19
|
h[:year] ||= today.year
|
19
20
|
h[:month] ||= today.month
|
20
21
|
h[:weekday] ||= today.wday
|
21
22
|
|
23
|
+
target_month = TimeCrisis::Date.new(h[:year], h[:month], 1)
|
24
|
+
days_in_month = target_month.days_in_month
|
22
25
|
target_weekday = h[:weekday].is_a?(Numeric) ? h[:weekday] : @@days_of_the_week[h[:weekday]]
|
23
|
-
first_weekday =
|
26
|
+
first_weekday = target_month.wday
|
24
27
|
|
25
28
|
offset = target_weekday > 0 ? target_weekday - first_weekday + 1 : 7 - (first_weekday - 1)
|
26
|
-
day =
|
29
|
+
day = calculate_day_with_offset(nth, offset)
|
30
|
+
|
31
|
+
while day > days_in_month
|
32
|
+
nth = nth - 1
|
33
|
+
day = calculate_day_with_offset(nth, offset)
|
34
|
+
end
|
27
35
|
|
28
36
|
if $DEBUG
|
29
37
|
STDERR.puts "Arguments: #{h.inspect}"
|
@@ -35,6 +43,12 @@ module TimeCrisis
|
|
35
43
|
|
36
44
|
TimeCrisis::Date.civil(h[:year], h[:month], day)
|
37
45
|
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def calculate_day_with_offset(nth, offset)
|
50
|
+
nth == 1 ? (offset > 0 ? offset : 7 + offset) : (7 * (nth - 1)) + offset
|
51
|
+
end
|
38
52
|
end
|
39
53
|
|
40
54
|
module InstanceMethods
|
data/time_crisis.gemspec
CHANGED