weekday 1.0.3 → 1.0.4

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/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 1.0.4 / 2011-09-21
2
+
3
+ * 2 enhancements
4
+ * new method like next_first_thursday(date=Date.today).
5
+ * new method like previous_first_thursday(date=Date.today)
6
+
1
7
  == 1.0.3 / 2011-04-06
2
8
 
3
9
  * 2 enhancements
data/README.md CHANGED
@@ -3,6 +3,11 @@ weekday
3
3
 
4
4
  This gem calculates date like 'first tuesday in december 2010' or 'third friday in may 2011' or 'last Saturday in december 2010'
5
5
 
6
+ Support this project
7
+ --------------------
8
+
9
+ If you find this gem useful and you want to support this work, send Bitcoins to the address <b><code>1ASyu5EuTkUEmiNz3y2ikSDAjdySpDJSMb</code></b>.
10
+
6
11
  Features
7
12
  --------
8
13
 
data/README.txt ADDED
@@ -0,0 +1,89 @@
1
+ = weekday
2
+
3
+ This gem calculates date like 'first tuesday in december 2010' or 'third friday in may 2011' or 'last Saturday in december 2010'
4
+
5
+ == Support this project
6
+
7
+ If you find this gem useful and you want to support this work, send Bitcoins to the address <b><code>1ASyu5EuTkUEmiNz3y2ikSDAjdySpDJSMb</code></b>.
8
+
9
+ == Features
10
+
11
+ * All method names are a combination of 'first', 'second', 'third', 'fourth', 'fifth' and 'monday', 'tuesday', ...
12
+ * Also 'last' + 'monday', 'tuesday', ... is possible
13
+ * If there is for example no fifth saturday in a specific month then nil is returned
14
+ * Calculations like next (previous) third friday
15
+ * Calculations like all first mondays in a complete year
16
+
17
+ == Examples
18
+
19
+ require 'rubygems'
20
+ require 'weekday'
21
+ require 'date'
22
+ #
23
+ # calculate every first thursday in a year
24
+ 1.upto 12 do |month|
25
+ day = Weekday.first_thursday(2010, month)
26
+ puts day
27
+ end
28
+ #
29
+ # calculate last monday in September 2011
30
+ puts Weekday.last_monday(2011,9)
31
+ #
32
+ # calculate fifth monday in September 2011
33
+ # this will return a nil value because there is no fifth monday in this month
34
+ puts Weekday.fifth_monday(2011,9)
35
+ #
36
+ # calculate all third wednesdays in 2011
37
+ # this returns an array of Date objects
38
+ days = Weekday.third_wednesdays(2011)
39
+ #
40
+ # calculates the next second tuesdays from today
41
+ puts Weekday.next_second_tuesday
42
+ #
43
+ # if next_... is called with an argument of type Date this date is used for
44
+ # next-calculation
45
+ puts Weekday.next_second_tuesday(Date.new(2012,1,1))
46
+ #
47
+ # there is also a calculation of the last previous date
48
+ puts Weekday.previous_second_tuesday
49
+ #
50
+ # next- and previous-calculations will automatically skip months if there
51
+ # is no such specified date in this month. This example results in the date
52
+ # 2011-06-30 because this was the last fifth thursday in a month when called
53
+ # with at 2011-09-20.
54
+ puts Weekday.previous_fifth_thursday(Date.new(2011,9,20))
55
+
56
+ For a complete listing of all call syntax see the documentation of the Weekday class.
57
+
58
+ == Install
59
+
60
+ gem install weekday
61
+
62
+ == Author
63
+
64
+ Original author: Thomas Preymesser
65
+
66
+ == License
67
+
68
+ (The MIT License)
69
+
70
+ Copyright (c) 2010-2011 Thomas Preymesser
71
+
72
+ Permission is hereby granted, free of charge, to any person obtaining
73
+ a copy of this software and associated documentation files (the
74
+ 'Software'), to deal in the Software without restriction, including
75
+ without limitation the rights to use, copy, modify, merge, publish,
76
+ distribute, sublicense, and/or sell copies of the Software, and to
77
+ permit persons to whom the Software is furnished to do so, subject to
78
+ the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be
81
+ included in all copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
84
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
85
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
86
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
87
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
88
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
89
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/weekday.rb CHANGED
@@ -25,6 +25,8 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
25
25
  PREV = %w(nil nil) + COUNTWORDS
26
26
  ILLEGAL_ARGUMENTS = "illegal month or year number"
27
27
  DAYS_PER_WEEK = 7
28
+ NEXT = 'next'
29
+ PREVIOUS = 'previous'
28
30
 
29
31
  # Weekday.method_missing implements the methods like
30
32
  # * Weekday.second_tuesday(2010,12) or
@@ -35,9 +37,61 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
35
37
  # * Weekday.last_monday(2011) returns the very last monday in this year
36
38
  # * Weekday.mondays(2011) returns all mondays in this year as an array of Date objects
37
39
  # * Weekday.first_thursdays(2011) returns all first thursdays in this year as an array of Date objects
40
+ # * Weekday.next_first_tuesday returns the next specified date (today if this date matches today). This method can take a argument of type Date - if no argument is given, this argument defaults to the current date.
41
+ # * Weekday.previous_first_tuesday returns the previous specified date (today if this date matches today). This method can take a argument of type Date - if no argument is given, this argument defaults to the current date.
38
42
  def self.method_missing(m, *args)
39
- # first_thursdays(year)
40
43
  count_s,weekday = m.to_s.split('_')
44
+ # next_first_thursday
45
+ if m.to_s.split('_').first == NEXT
46
+ n,count_s,weekday = m.to_s.split('_')
47
+ today = args[0]
48
+ today ||= Date.today
49
+ month = today.month
50
+ year = today.year
51
+ nn = nil
52
+ loop do
53
+ nn = self.send(count_s+'_'+weekday,year,month)
54
+ break if nn
55
+ year,month = next_month(year,month)
56
+ end
57
+ if nn < today
58
+ loop do
59
+ nn = self.send(count_s+'_'+weekday,next_month(year,month)[0],
60
+ next_month(year,month)[1])
61
+ break if nn
62
+ year,month = next_month(year,month)
63
+ end
64
+ end
65
+ return nn
66
+ end
67
+ # end next_first_thursday
68
+
69
+ # previous_first_thursday
70
+ if m.to_s.split('_').first == PREVIOUS
71
+ n,count_s,weekday = m.to_s.split('_')
72
+ today = args[0]
73
+ today ||= Date.today
74
+ month = today.month
75
+ year = today.year
76
+ nn = nil
77
+ loop do
78
+ nn = self.send(count_s+'_'+weekday,year,month)
79
+ break if nn
80
+ year,month = previous_month(year,month)
81
+ end
82
+ if nn > today
83
+ loop do
84
+ nn = self.send(count_s+'_'+weekday, previous_month(year,month)[0],
85
+ previous_month(year,month)[1])
86
+ break if nn
87
+ year,month = previous_month(year,month)
88
+ end
89
+ end
90
+ return nn
91
+ end
92
+ # end previous_first_thursday
93
+
94
+ # first_thursdays(year)
41
95
  if DAYNAMESS.include?(weekday) and args.size == 1
42
96
  unless COUNTWORDS.include?(count_s)
43
97
  raise NoMethodError, "undefined method `#{m}'"
@@ -47,11 +101,12 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
47
101
  year = args[0]
48
102
  result = []
49
103
  1.upto(12) do |month|
50
- result << self.send(count_s+"_"+DAYNAMES[wnum],year,month)
104
+ result << self.send(count_s+"_"+DAYNAMES[wnum],year,month)
51
105
  end
52
106
  return result
53
107
  end
54
108
  # end first_thursdays
109
+
55
110
  # self.mondays .. self.sundays
56
111
  if DAYNAMESS.include?(m.to_s)
57
112
  wnum = DAYNAMESS.index(m.to_s)
@@ -149,4 +204,23 @@ If a certain date like 'fifth_saturday' is not possible, nil is returned.
149
204
  self.send(s,year,month)
150
205
  end
151
206
 
207
+ private
208
+
209
+ def self.next_month(year,month)
210
+ month += 1
211
+ if month > 12
212
+ month = 1
213
+ year += 1
214
+ end
215
+ return year,month
216
+ end
217
+
218
+ def self.previous_month(year,month)
219
+ month -= 1
220
+ if month < 1
221
+ month = 12
222
+ year -= 1
223
+ end
224
+ return year,month
225
+ end
152
226
  end
data/test/test_wd.rb CHANGED
@@ -158,24 +158,62 @@ class TestWd < Test::Unit::TestCase
158
158
  def test_third_sunday_with_nth
159
159
  assert_equal(Date.new(2010,12,19),
160
160
  Weekday.nth(3, # third
161
- 0, # sunday = 0, monday = 1 ... saturday = 6
162
- 2010, # year
163
- 12 # month
164
- ))
161
+ 0, # sunday = 0, monday = 1 ... saturday = 6
162
+ 2010, # year
163
+ 12 # month
164
+ ))
165
165
  end
166
166
 
167
167
  def test_last_sunday_with_nth
168
- assert_equal Date.new(2010,12,26),
169
- Weekday.nth(-1,0,2010,12)
168
+ assert_equal Date.new(2010,12,26), Weekday.nth(-1,0,2010,12)
170
169
  end
171
170
 
172
171
  def test_first_thursdays
173
172
  expected = []
174
- y = 2011
175
173
  1.upto 12 do |m|
176
- expected << Weekday.first_thursday(y,m)
174
+ expected << Weekday.first_thursday(2011,m)
177
175
  end
178
176
  assert_equal expected, Weekday.first_thursdays(2011)
179
177
  end
178
+
179
+ def test_next_first_thursday
180
+ # next month
181
+ today = Date.new(2011,9,19)
182
+ assert_equal Date.new(2011,10,6), Weekday.next_first_thursday(today)
183
+
184
+ # today
185
+ today = Date.new(2011,10,6)
186
+ assert_equal Date.new(2011,10,6), Weekday.next_first_thursday(today)
187
+
188
+ # end of year
189
+ today = Date.new(2011,12,25)
190
+ assert_equal Date.new(2012,1,5), Weekday.next_first_thursday(today)
191
+
192
+ # Monate ueberspringen, wenn z.B. kein fuenfter Donnerstag vorhanden
193
+ today = Date.new(2011,9,30)
194
+ assert_equal Date.new(2011,12,29), Weekday.next_fifth_thursday(today)
195
+ end
196
+
197
+ def test_previous_first_thursday
198
+ # today
199
+ today = Date.new(2011,9,15)
200
+ assert_equal Date.new(2011,9,15), Weekday.previous_third_thursday(today)
201
+
202
+ # im gleichen Monat schon vergangen
203
+ today = Date.new(2011,9,21)
204
+ assert_equal Date.new(2011,9,15), Weekday.previous_third_thursday(today)
205
+
206
+ # im gleichen Monat noch nicht vergangen
207
+ today = Date.new(2011,9,8)
208
+ assert_equal Date.new(2011,8,18), Weekday.previous_third_thursday(today)
209
+
210
+ # previous year
211
+ today = Date.new(2011,1,1)
212
+ assert_equal Date.new(2010,12,2), Weekday.previous_first_thursday(today)
213
+
214
+ # Monate ueberspringen, wenn z.B. kein fuenfter Donnerstag vorhanden
215
+ today = Date.new(2011,9,1)
216
+ assert_equal Date.new(2011,6,30), Weekday.previous_fifth_thursday(today)
217
+ end
180
218
  end
181
219
 
data/version.txt CHANGED
@@ -1 +1 @@
1
- 1.0.3
1
+ 1.0.4
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weekday
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-04-06 00:00:00.000000000Z
12
+ date: 2011-09-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bones
16
- requirement: &19667736 !ruby/object:Gem::Requirement
16
+ requirement: &16913364 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.6.5
21
+ version: 3.7.1
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *19667736
24
+ version_requirements: *16913364
25
25
  description: This gem calculates date like 'first tuesday in december 2010' or 'third
26
26
  friday in may 2011' or 'last Saturday in december 2010'
27
27
  email: thopre@gmail.com
@@ -29,6 +29,7 @@ executables: []
29
29
  extensions: []
30
30
  extra_rdoc_files:
31
31
  - History.txt
32
+ - README.txt
32
33
  - lib/doc/Object.html
33
34
  - lib/doc/Weekday.html
34
35
  - lib/doc/created.rid
@@ -62,6 +63,7 @@ extra_rdoc_files:
62
63
  files:
63
64
  - History.txt
64
65
  - README.md
66
+ - README.txt
65
67
  - Rakefile
66
68
  - examples/termine.rb
67
69
  - lib/doc/Object.html
@@ -103,7 +105,7 @@ licenses: []
103
105
  post_install_message:
104
106
  rdoc_options:
105
107
  - --main
106
- - README.md
108
+ - README.txt
107
109
  require_paths:
108
110
  - lib
109
111
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -120,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
122
  version: '0'
121
123
  requirements: []
122
124
  rubyforge_project: weekday
123
- rubygems_version: 1.7.1
125
+ rubygems_version: 1.8.10
124
126
  signing_key:
125
127
  specification_version: 3
126
128
  summary: This gem calculates date like 'first tuesday in december 2010' or 'third