date_utils 0.1.3 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/date_utils.rb +123 -44
- metadata +2 -2
data/lib/date_utils.rb
CHANGED
@@ -41,6 +41,11 @@ module DateUtils
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
def self.extract_options_from_args!(args)
|
45
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
46
|
+
options
|
47
|
+
end
|
48
|
+
|
44
49
|
|
45
50
|
end
|
46
51
|
|
@@ -88,21 +93,30 @@ module DateUtils
|
|
88
93
|
# create a new Week-instance with the given initial Date or Week-number
|
89
94
|
# if 'date' is nil, create an instance with Date.today
|
90
95
|
#
|
91
|
-
def initialize(
|
92
|
-
if
|
96
|
+
def initialize(val=nil)
|
97
|
+
if val.nil?
|
93
98
|
_date = Date.today
|
94
|
-
elsif date.kind_of?(Date)
|
95
|
-
_date = date
|
96
|
-
elsif date.kind_of?(Fixnum)
|
97
|
-
_date = Year.new.get_week(date).date
|
98
99
|
else
|
99
|
-
|
100
|
+
_date = val.is_a?(Date) ? val : (raise ArgumentError.new("neither Fixnum nor Date given."))
|
101
|
+
end
|
102
|
+
set_date(_date)
|
103
|
+
create_instance_variables
|
104
|
+
end
|
105
|
+
|
106
|
+
# create a Week-instance
|
107
|
+
# call with hash: year and week
|
108
|
+
# :call-seq:
|
109
|
+
# Week.create(:year => x, :week => y)
|
110
|
+
# Week.create(:week => x)
|
111
|
+
# Week.create(:year => x)
|
112
|
+
#
|
113
|
+
def self.create(*args)
|
114
|
+
options = Common::extract_options_from_args!(args)
|
115
|
+
year_date = options.has_key?(:year) ? Date::civil(options[:year].to_i, 1,1) : Date.today
|
116
|
+
unless options.has_key?(:week) && !options[:week].nil?
|
117
|
+
return Week.new(year_date)
|
100
118
|
end
|
101
|
-
|
102
|
-
@date = _date
|
103
|
-
@num_week = _date.cweek
|
104
|
-
@first_day = _date - ( _date.cwday - 1 )
|
105
|
-
@last_day = _date + ( 7 - _date.cwday )
|
119
|
+
return Year.new(year_date).get_week(options[:week].to_i)
|
106
120
|
end
|
107
121
|
|
108
122
|
# return new Week -instance one week after self
|
@@ -125,6 +139,23 @@ module DateUtils
|
|
125
139
|
arr
|
126
140
|
end
|
127
141
|
|
142
|
+
private
|
143
|
+
|
144
|
+
# prepare instance variables
|
145
|
+
#
|
146
|
+
def create_instance_variables
|
147
|
+
@month = Month.new(@date)
|
148
|
+
@num_week = @date.cweek
|
149
|
+
@first_day = @date - ( @date.cwday - 1 )
|
150
|
+
@last_day = @date + ( 7 - @date.cwday )
|
151
|
+
end
|
152
|
+
|
153
|
+
# set base-date for self
|
154
|
+
#
|
155
|
+
def set_date(date)
|
156
|
+
@date = date
|
157
|
+
end
|
158
|
+
|
128
159
|
end
|
129
160
|
|
130
161
|
# Represents a 'Month'
|
@@ -149,41 +180,71 @@ module DateUtils
|
|
149
180
|
|
150
181
|
# create a new Month of given Date
|
151
182
|
#
|
152
|
-
def initialize(
|
153
|
-
|
154
|
-
|
155
|
-
@date = date
|
156
|
-
@month = date.month
|
157
|
-
@first_day = date.mday > 1 ? (date - ( date.mday - 1)) : date
|
158
|
-
@num_days = 31 if [1,3,5,7,8,10,12].include?(@month)
|
159
|
-
@num_days = 30 if [4,6,9,11].include?(@month)
|
160
|
-
( date.leap? ? (@num_days = 29) : (@num_days = 28) ) if @month == 2
|
161
|
-
@last_day = @first_day + ( @num_days - 1 )
|
183
|
+
def initialize(val=nil)
|
184
|
+
if val.nil?
|
185
|
+
_date = Date.today
|
162
186
|
else
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
end
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
def previous
|
175
|
-
return Month.new((@first_day - 1).to_date)
|
176
|
-
end
|
187
|
+
if val.is_a?(Date)
|
188
|
+
_date = val
|
189
|
+
elsif val.is_a?(Fixnum) && val <= 12
|
190
|
+
_date = Date::civil(Date.today.year.to_i,val,1)
|
191
|
+
else
|
192
|
+
raise ArgumentError.new("neither Fixnum nor Date given.")
|
193
|
+
end
|
194
|
+
end
|
195
|
+
@date = _date
|
196
|
+
create_instance_variables
|
197
|
+
end
|
177
198
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
199
|
+
# create a Month-instance
|
200
|
+
# call with hash: year and month
|
201
|
+
# :call-seq:
|
202
|
+
# Month.create(:year => x, :month => y)
|
203
|
+
# Month.create(:month => x)
|
204
|
+
# Month.create(:year => x)
|
205
|
+
#
|
206
|
+
def self.create(*args)
|
207
|
+
options = Common::extract_options_from_args!(args)
|
208
|
+
int_year = options.has_key?(:year) && options[:year].is_a?(Fixnum) ? options[:year] : nil
|
209
|
+
int_month = options.has_key?(:month) && options[:month].is_a?(Fixnum) && options[:month] <= 12 ? options[:month] : nil
|
210
|
+
return Month.new(Date::civil(int_year || Date.today.year, int_month || 1))
|
211
|
+
end
|
212
|
+
|
213
|
+
# returns new Month -instance one Month later than self
|
214
|
+
#
|
215
|
+
def next
|
216
|
+
return Month.new(@last_day + 1)
|
217
|
+
end
|
218
|
+
|
219
|
+
# returns a new Month -instance one Month prior to self
|
220
|
+
#
|
221
|
+
def previous
|
222
|
+
return Month.new((@first_day - 1).to_date)
|
223
|
+
end
|
185
224
|
|
225
|
+
# returns collection of days as Date -instances of self
|
226
|
+
#
|
227
|
+
def days
|
228
|
+
arr = []
|
229
|
+
@first_day.upto(@last_day) { |date| arr << date }
|
230
|
+
arr
|
231
|
+
end
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
def set_date(date)
|
236
|
+
@date = date
|
186
237
|
end
|
238
|
+
|
239
|
+
def create_instance_variables
|
240
|
+
@month = @date.month
|
241
|
+
@first_day = @date.mday > 1 ? (@date - ( @date.mday - 1)) : @date
|
242
|
+
@num_days = 31 if [1,3,5,7,8,10,12].include?(@month)
|
243
|
+
@num_days = 30 if [4,6,9,11].include?(@month)
|
244
|
+
( date.leap? ? (@num_days = 29) : (@num_days = 28) ) if @month == 2
|
245
|
+
@last_day = @first_day + ( @num_days - 1 )
|
246
|
+
end
|
247
|
+
|
187
248
|
end
|
188
249
|
|
189
250
|
# represents a Year
|
@@ -205,6 +266,18 @@ module DateUtils
|
|
205
266
|
raise ArgumentError, "needs Date as input!"
|
206
267
|
end
|
207
268
|
@year = date.year
|
269
|
+
@date = date
|
270
|
+
end
|
271
|
+
|
272
|
+
# create a Year-instance
|
273
|
+
# call with hash: year
|
274
|
+
# :call-seq:
|
275
|
+
# Year.create(:week => x)
|
276
|
+
#
|
277
|
+
def self.create(*args)
|
278
|
+
options = Common::extract_options_from_args!(args)
|
279
|
+
date_year = options.has_key?(:year) ? Date.civil(options[:year]) : (raise ArgumentError.new("no key :year in hash."))
|
280
|
+
return Year.new(date_year)
|
208
281
|
end
|
209
282
|
|
210
283
|
# returns collection of Month -instances of self
|
@@ -238,5 +311,11 @@ module DateUtils
|
|
238
311
|
! num.kind_of?(Fixnum) || num > 52 ? ArgumentError.new("invalid week-number 'num'"): self.weeks[num -1]
|
239
312
|
end
|
240
313
|
|
314
|
+
# returns Month 'num' of self
|
315
|
+
#
|
316
|
+
def get_month(num)
|
317
|
+
! num.kind_of?(Fixnum) || num > 12 ? ArgumentError.new("invalid week-number 'num'"): self.months[num-1]
|
318
|
+
end
|
319
|
+
|
241
320
|
end
|
242
|
-
end
|
321
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: date_utils
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-03-
|
6
|
+
version: "0.2"
|
7
|
+
date: 2007-03-03 00:00:00 +01:00
|
8
8
|
summary: Some handy utils to deal with Date
|
9
9
|
require_paths:
|
10
10
|
- lib
|