time_crisis 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
+ :patch: 8
4
5
  :build:
5
- :patch: 7
@@ -14,7 +14,7 @@ module TimeCrisis::Holiday
14
14
  last_monday_of_may = TimeCrisis::Date.nth_weekday({
15
15
  :month => 5,
16
16
  :weekday => :monday,
17
- :nth => 4
17
+ :nth => :last
18
18
  })
19
19
 
20
20
  [last_monday_of_may.month, last_monday_of_may.day]
@@ -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 = TimeCrisis::Date.new(h[:year], h[:month], 1).wday
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 = h[:nth] == 1 ? 7 + offset : (7 * (h[:nth] - 1)) + offset
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{time_crisis}
8
- s.version = "0.1.7"
8
+ s.version = "0.1.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis Tilley"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_crisis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Tilley