fat_core 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/lib/fat_core/date.rb +2 -2
- data/lib/fat_core/period.rb +18 -21
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/date_spec.rb +215 -161
- data/spec/lib/period_spec.rb +120 -103
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db29f2ee46cdaa3602d37b2a1f9af253f07e651e
|
4
|
+
data.tar.gz: 85dddc16c5d33ca582b081a138de6a3fb617522d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6badb8b5fc9cb201aa41a8244f8b53b95f180ce683d22b8b1e1be6fc352b0783b50f6149b402b54b354aa207079b33f31bb1b38a827c726aa10d2fc09501a320
|
7
|
+
data.tar.gz: 5fc8edbd029a4de5f7922c2ca3e7f4cb06f1bb125dd3a366aab98f4c85ec877beef09f557bfc069b098ffb047f6b388f54bfcaafcaa6d71aeb34e8ed94c7052e
|
data/lib/fat_core/date.rb
CHANGED
@@ -65,7 +65,7 @@ class Date
|
|
65
65
|
def self.parse_spec(spec, spec_type = :from)
|
66
66
|
spec = spec.to_s.strip
|
67
67
|
unless [:from, :to].include?(spec_type)
|
68
|
-
raise ArgumentError "invalid date spec type: '#{spec_type}'"
|
68
|
+
raise ArgumentError, "invalid date spec type: '#{spec_type}'"
|
69
69
|
end
|
70
70
|
|
71
71
|
today = Date.current
|
@@ -491,7 +491,7 @@ class Date
|
|
491
491
|
when :day
|
492
492
|
self
|
493
493
|
else
|
494
|
-
raise
|
494
|
+
raise ArgumentError, "unknown chunk sym: '#{sym}'"
|
495
495
|
end
|
496
496
|
end
|
497
497
|
|
data/lib/fat_core/period.rb
CHANGED
@@ -205,6 +205,8 @@ class Period
|
|
205
205
|
60
|
206
206
|
when :quarter
|
207
207
|
90
|
208
|
+
when :half
|
209
|
+
180
|
208
210
|
when :year
|
209
211
|
365
|
210
212
|
when :irregular
|
@@ -225,6 +227,8 @@ class Period
|
|
225
227
|
62
|
226
228
|
when :quarter
|
227
229
|
92
|
230
|
+
when :half
|
231
|
+
183
|
228
232
|
when :year
|
229
233
|
366
|
230
234
|
when :irregular
|
@@ -245,6 +249,8 @@ class Period
|
|
245
249
|
case days
|
246
250
|
when 356..376
|
247
251
|
:year
|
252
|
+
when 180..183
|
253
|
+
:half
|
248
254
|
when 86..96
|
249
255
|
:quarter
|
250
256
|
when 59..62
|
@@ -444,51 +450,42 @@ class Period
|
|
444
450
|
case size
|
445
451
|
when :year
|
446
452
|
unless partial_first
|
447
|
-
until chunk_start.beginning_of_year?
|
448
|
-
chunk_start += 1.day
|
449
|
-
end
|
453
|
+
chunk_start += 1.day until chunk_start.beginning_of_year?
|
450
454
|
end
|
451
455
|
chunk_end = chunk_start.end_of_year
|
456
|
+
when :half
|
457
|
+
unless partial_first
|
458
|
+
chunk_start += 1.day until chunk_start.beginning_of_half?
|
459
|
+
end
|
460
|
+
chunk_end = chunk_start.end_of_half
|
452
461
|
when :quarter
|
453
462
|
unless partial_first
|
454
|
-
until chunk_start.beginning_of_quarter?
|
455
|
-
chunk_start += 1.day
|
456
|
-
end
|
463
|
+
chunk_start += 1.day until chunk_start.beginning_of_quarter?
|
457
464
|
end
|
458
465
|
chunk_end = chunk_start.end_of_quarter
|
459
466
|
when :bimonth
|
460
467
|
unless partial_first
|
461
|
-
|
462
|
-
chunk_start += 1.day
|
463
|
-
end
|
468
|
+
chunk_start += 1.day until chunk_start.beginning_of_bimonth?
|
464
469
|
end
|
465
470
|
chunk_end = (chunk_start.end_of_month + 1.day).end_of_month
|
466
471
|
when :month
|
467
472
|
unless partial_first
|
468
|
-
|
469
|
-
chunk_start += 1.day
|
470
|
-
end
|
473
|
+
chunk_start += 1.day until chunk_start.beginning_of_month?
|
471
474
|
end
|
472
475
|
chunk_end = chunk_start.end_of_month
|
473
476
|
when :semimonth
|
474
477
|
unless partial_first
|
475
|
-
|
476
|
-
chunk_start += 1.day
|
477
|
-
end
|
478
|
+
chunk_start += 1.day until chunk_start.beginning_of_semimonth?
|
478
479
|
end
|
479
480
|
chunk_end = chunk_start.end_of_semimonth
|
480
481
|
when :biweek
|
481
482
|
unless partial_first
|
482
|
-
|
483
|
-
chunk_start += 1.day
|
484
|
-
end
|
483
|
+
chunk_start += 1.day until chunk_start.beginning_of_biweek?
|
485
484
|
end
|
486
485
|
chunk_end = chunk_start.end_of_biweek
|
487
486
|
when :week
|
488
487
|
unless partial_first
|
489
|
-
|
490
|
-
chunk_start += 1.day
|
491
|
-
end
|
488
|
+
chunk_start += 1.day until chunk_start.beginning_of_week?
|
492
489
|
end
|
493
490
|
chunk_end = chunk_start.end_of_week
|
494
491
|
when :day
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/date_spec.rb
CHANGED
@@ -5,13 +5,13 @@ describe Date do
|
|
5
5
|
before :each do
|
6
6
|
# Pretend it is this date. Not at beg or end of year, quarter,
|
7
7
|
# month, or week. It is a Wednesday
|
8
|
-
allow(Date).to receive_messages(:
|
9
|
-
allow(Date).to receive_messages(:
|
8
|
+
allow(Date).to receive_messages(today: Date.parse('2012-07-18'))
|
9
|
+
allow(Date).to receive_messages(current: Date.parse('2012-07-18'))
|
10
10
|
end
|
11
11
|
|
12
|
-
describe
|
13
|
-
describe
|
14
|
-
it
|
12
|
+
describe 'class methods' do
|
13
|
+
describe 'date arithmetic' do
|
14
|
+
it 'should know the number of days in a month' do
|
15
15
|
expect(Date.days_in_month(2000, 1)).to eq 31
|
16
16
|
expect(Date.days_in_month(1900, 2)).to eq 28
|
17
17
|
expect(Date.days_in_month(2000, 2)).to eq 29
|
@@ -27,10 +27,10 @@ describe Date do
|
|
27
27
|
expect(Date.days_in_month(2004, 10)).to eq 31
|
28
28
|
expect(Date.days_in_month(2004, 11)).to eq 30
|
29
29
|
expect(Date.days_in_month(2004, 12)).to eq 31
|
30
|
-
expect { Date.days_in_month(2004, 13) }.to raise_error
|
30
|
+
expect { Date.days_in_month(2004, 13) }.to raise_error(ArgumentError)
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
33
|
+
it 'should know the nth weekday in a year, month' do
|
34
34
|
# Sunday is 0, Saturday is 6
|
35
35
|
# January 2014
|
36
36
|
# Su Mo Tu We Th Fr Sa
|
@@ -65,18 +65,18 @@ describe Date do
|
|
65
65
|
expect {
|
66
66
|
# N is zero
|
67
67
|
Date.nth_wday_in_year_month(0, 6, 2014, 1)
|
68
|
-
}.to raise_error
|
68
|
+
}.to raise_error(ArgumentError)
|
69
69
|
expect {
|
70
70
|
# Wday too big
|
71
71
|
Date.nth_wday_in_year_month(3, 7, 2014, 1)
|
72
|
-
}.to raise_error
|
72
|
+
}.to raise_error(ArgumentError)
|
73
73
|
expect {
|
74
74
|
# Month too big
|
75
75
|
Date.nth_wday_in_year_month(3, 1, 2014, 13)
|
76
|
-
}.to raise_error
|
76
|
+
}.to raise_error(ArgumentError)
|
77
77
|
end
|
78
78
|
|
79
|
-
it
|
79
|
+
it 'should know Easter for a given year' do
|
80
80
|
# Grabbed these dates of Easter from
|
81
81
|
# http://tlarsen2.tripod.com/thomaslarsen/easterdates.html
|
82
82
|
easters = {
|
@@ -179,7 +179,7 @@ describe Date do
|
|
179
179
|
2096 => '2096-04-15',
|
180
180
|
2097 => '2097-03-31',
|
181
181
|
2098 => '2098-04-20',
|
182
|
-
2099 => '2099-04-12'
|
182
|
+
2099 => '2099-04-12'
|
183
183
|
}
|
184
184
|
easters.each_pair do |year, date|
|
185
185
|
expect(Date.easter(year)).to eq Date.parse(date)
|
@@ -187,8 +187,8 @@ describe Date do
|
|
187
187
|
end
|
188
188
|
end
|
189
189
|
|
190
|
-
describe
|
191
|
-
it
|
190
|
+
describe 'parsing' do
|
191
|
+
it 'should be able to parse an American-style date' do
|
192
192
|
expect(Date.parse_american('2/12/2011').iso).to eq('2011-02-12')
|
193
193
|
expect(Date.parse_american('2 / 12/ 2011').iso).to eq('2011-02-12')
|
194
194
|
expect(Date.parse_american('2 / 1 / 2011').iso).to eq('2011-02-01')
|
@@ -196,20 +196,20 @@ describe Date do
|
|
196
196
|
expect(Date.parse_american(' 2 / 1 / 15 ').iso).to eq('2015-02-01')
|
197
197
|
expect {
|
198
198
|
Date.parse_american('xx/1/15')
|
199
|
-
}.to raise_error
|
199
|
+
}.to raise_error(ArgumentError)
|
200
200
|
end
|
201
201
|
end
|
202
202
|
|
203
|
-
describe
|
203
|
+
describe 'parse_spec' do
|
204
204
|
# For these tests, today is 2012-07-18
|
205
205
|
|
206
|
-
it
|
206
|
+
it 'should choke if spec type is neither :from or :to' do
|
207
207
|
expect {
|
208
208
|
Date.parse_spec('2011-07-15', :form)
|
209
|
-
}.to raise_error
|
209
|
+
}.to raise_error(ArgumentError)
|
210
210
|
end
|
211
211
|
|
212
|
-
it
|
212
|
+
it 'should parse plain iso dates correctly' do
|
213
213
|
expect(Date.parse_spec('2011-07-15')).to eq Date.parse('2011-07-15')
|
214
214
|
expect(Date.parse_spec('2011-08-05')).to eq Date.parse('2011-08-05')
|
215
215
|
end
|
@@ -222,10 +222,10 @@ describe Date do
|
|
222
222
|
expect(Date.parse_spec('23W', :to)).to eq Date.parse('2012-06-10')
|
223
223
|
expect {
|
224
224
|
Date.parse_spec('W83', :to)
|
225
|
-
}.to raise_error
|
225
|
+
}.to raise_error(ArgumentError)
|
226
226
|
end
|
227
227
|
|
228
|
-
it
|
228
|
+
it 'should parse year-week numbers \'YYYY-W23\' correctly' do
|
229
229
|
expect(Date.parse_spec('2003-W1')).to eq Date.parse('2002-12-30')
|
230
230
|
expect(Date.parse_spec('2003-W1', :to)).to eq Date.parse('2003-01-05')
|
231
231
|
expect(Date.parse_spec('2003-W23')).to eq Date.parse('2003-06-02')
|
@@ -234,137 +234,152 @@ describe Date do
|
|
234
234
|
expect(Date.parse_spec('2003-23W', :to)).to eq Date.parse('2003-06-08')
|
235
235
|
expect {
|
236
236
|
Date.parse_spec('2003-W83', :to)
|
237
|
-
}.to raise_error
|
237
|
+
}.to raise_error(ArgumentError)
|
238
238
|
end
|
239
239
|
|
240
|
-
it
|
240
|
+
it 'should parse year-half specs such as YYYY-NH or YYYY-HN' do
|
241
241
|
expect(Date.parse_spec('2011-2H', :from)).to eq Date.parse('2011-07-01')
|
242
242
|
expect(Date.parse_spec('2011-2H', :to)).to eq Date.parse('2011-12-31')
|
243
243
|
expect(Date.parse_spec('2011-H1', :from)).to eq Date.parse('2011-01-01')
|
244
244
|
expect(Date.parse_spec('2011-H1', :to)).to eq Date.parse('2011-06-30')
|
245
|
-
expect { Date.parse_spec('2011-3H') }.to raise_error
|
245
|
+
expect { Date.parse_spec('2011-3H') }.to raise_error(ArgumentError)
|
246
246
|
end
|
247
247
|
|
248
|
-
it
|
248
|
+
it 'should parse half-only specs such as NH or HN' do
|
249
249
|
expect(Date.parse_spec('2H', :from)).to eq Date.parse('2012-07-01')
|
250
250
|
expect(Date.parse_spec('H2', :to)).to eq Date.parse('2012-12-31')
|
251
251
|
expect(Date.parse_spec('1H', :from)).to eq Date.parse('2012-01-01')
|
252
252
|
expect(Date.parse_spec('H1', :to)).to eq Date.parse('2012-06-30')
|
253
|
-
expect { Date.parse_spec('8H') }.to raise_error
|
253
|
+
expect { Date.parse_spec('8H') }.to raise_error(ArgumentError)
|
254
254
|
end
|
255
255
|
|
256
|
-
it
|
256
|
+
it 'should parse year-quarter specs such as YYYY-NQ or YYYY-QN' do
|
257
257
|
expect(Date.parse_spec('2011-4Q', :from)).to eq Date.parse('2011-10-01')
|
258
258
|
expect(Date.parse_spec('2011-4Q', :to)).to eq Date.parse('2011-12-31')
|
259
259
|
expect(Date.parse_spec('2011-Q4', :from)).to eq Date.parse('2011-10-01')
|
260
260
|
expect(Date.parse_spec('2011-Q4', :to)).to eq Date.parse('2011-12-31')
|
261
|
-
expect { Date.parse_spec('2011-5Q') }.to raise_error
|
261
|
+
expect { Date.parse_spec('2011-5Q') }.to raise_error(ArgumentError)
|
262
262
|
end
|
263
263
|
|
264
|
-
it
|
264
|
+
it 'should parse quarter-only specs such as NQ or QN' do
|
265
265
|
expect(Date.parse_spec('4Q', :from)).to eq Date.parse('2012-10-01')
|
266
266
|
expect(Date.parse_spec('4Q', :to)).to eq Date.parse('2012-12-31')
|
267
267
|
expect(Date.parse_spec('Q4', :from)).to eq Date.parse('2012-10-01')
|
268
268
|
expect(Date.parse_spec('Q4', :to)).to eq Date.parse('2012-12-31')
|
269
|
-
expect { Date.parse_spec('5Q') }.to raise_error
|
269
|
+
expect { Date.parse_spec('5Q') }.to raise_error(ArgumentError)
|
270
270
|
end
|
271
271
|
|
272
|
-
it
|
272
|
+
it 'should parse year-month specs such as YYYY-MM' do
|
273
273
|
expect(Date.parse_spec('2010-5', :from)).to eq Date.parse('2010-05-01')
|
274
274
|
expect(Date.parse_spec('2010-5', :to)).to eq Date.parse('2010-05-31')
|
275
|
-
expect { Date.parse_spec('2010-13') }.to raise_error
|
275
|
+
expect { Date.parse_spec('2010-13') }.to raise_error(ArgumentError)
|
276
276
|
end
|
277
277
|
|
278
|
-
it
|
278
|
+
it 'should parse month-only specs such as MM' do
|
279
279
|
expect(Date.parse_spec('10', :from)).to eq Date.parse('2012-10-01')
|
280
280
|
expect(Date.parse_spec('10', :to)).to eq Date.parse('2012-10-31')
|
281
|
-
expect { Date.parse_spec('99') }.to raise_error
|
282
|
-
expect { Date.parse_spec('011') }.to raise_error
|
281
|
+
expect { Date.parse_spec('99') }.to raise_error(ArgumentError)
|
282
|
+
expect { Date.parse_spec('011') }.to raise_error(ArgumentError)
|
283
283
|
end
|
284
284
|
|
285
|
-
it
|
285
|
+
it 'should parse year-only specs such as YYYY' do
|
286
286
|
expect(Date.parse_spec('2010', :from)).to eq Date.parse('2010-01-01')
|
287
287
|
expect(Date.parse_spec('2010', :to)).to eq Date.parse('2010-12-31')
|
288
|
-
expect { Date.parse_spec('99999') }.to raise_error
|
288
|
+
expect { Date.parse_spec('99999') }.to raise_error(ArgumentError)
|
289
289
|
end
|
290
290
|
|
291
|
-
it
|
291
|
+
it 'should parse relative day names: today, yesterday' do
|
292
292
|
expect(Date.parse_spec('today')).to eq Date.current
|
293
293
|
expect(Date.parse_spec('this_day')).to eq Date.current
|
294
294
|
expect(Date.parse_spec('yesterday')).to eq Date.current - 1.day
|
295
295
|
expect(Date.parse_spec('last_day')).to eq Date.current - 1.day
|
296
296
|
end
|
297
297
|
|
298
|
-
it
|
298
|
+
it 'should parse relative weeks: this_week, last_week' do
|
299
299
|
expect(Date.parse_spec('this_week')).to eq Date.parse('2012-07-16')
|
300
300
|
expect(Date.parse_spec('this_week', :to)).to eq Date.parse('2012-07-22')
|
301
301
|
expect(Date.parse_spec('last_week')).to eq Date.parse('2012-07-09')
|
302
302
|
expect(Date.parse_spec('last_week', :to)).to eq Date.parse('2012-07-15')
|
303
303
|
end
|
304
304
|
|
305
|
-
it
|
305
|
+
it 'should parse relative biweeks: this_biweek, last_biweek' do
|
306
306
|
expect(Date.parse_spec('this_biweek')).to eq Date.parse('2012-07-16')
|
307
|
-
expect(Date.parse_spec('this_biweek', :to))
|
307
|
+
expect(Date.parse_spec('this_biweek', :to))
|
308
|
+
.to eq Date.parse('2012-07-29')
|
308
309
|
expect(Date.parse_spec('last_biweek')).to eq Date.parse('2012-07-02')
|
309
|
-
expect(Date.parse_spec('last_biweek', :to))
|
310
|
+
expect(Date.parse_spec('last_biweek', :to))
|
311
|
+
.to eq Date.parse('2012-07-15')
|
310
312
|
end
|
311
313
|
|
312
|
-
it
|
314
|
+
it 'should parse relative months: this_semimonth, last_semimonth' do
|
313
315
|
expect(Date.parse_spec('this_semimonth')).to eq Date.parse('2012-07-16')
|
314
|
-
expect(Date.parse_spec('this_semimonth', :to))
|
315
|
-
|
316
|
-
expect(Date.parse_spec('last_semimonth'
|
316
|
+
expect(Date.parse_spec('this_semimonth', :to))
|
317
|
+
.to eq Date.parse('2012-07-31')
|
318
|
+
expect(Date.parse_spec('last_semimonth'))
|
319
|
+
.to eq Date.parse('2012-07-01')
|
320
|
+
expect(Date.parse_spec('last_semimonth', :to))
|
321
|
+
.to eq Date.parse('2012-07-15')
|
317
322
|
end
|
318
323
|
|
319
|
-
it
|
324
|
+
it 'should parse relative months: this_month, last_month' do
|
320
325
|
expect(Date.parse_spec('this_month')).to eq Date.parse('2012-07-01')
|
321
|
-
expect(Date.parse_spec('this_month', :to))
|
326
|
+
expect(Date.parse_spec('this_month', :to))
|
327
|
+
.to eq Date.parse('2012-07-31')
|
322
328
|
expect(Date.parse_spec('last_month')).to eq Date.parse('2012-06-01')
|
323
|
-
expect(Date.parse_spec('last_month', :to))
|
329
|
+
expect(Date.parse_spec('last_month', :to))
|
330
|
+
.to eq Date.parse('2012-06-30')
|
324
331
|
end
|
325
332
|
|
326
|
-
it
|
333
|
+
it 'should parse relative bimonths: this_bimonth, last_bimonth' do
|
327
334
|
expect(Date.parse_spec('this_bimonth')).to eq Date.parse('2012-07-01')
|
328
|
-
expect(Date.parse_spec('this_bimonth', :to))
|
329
|
-
|
330
|
-
expect(Date.parse_spec('last_bimonth'
|
335
|
+
expect(Date.parse_spec('this_bimonth', :to))
|
336
|
+
.to eq Date.parse('2012-08-31')
|
337
|
+
expect(Date.parse_spec('last_bimonth'))
|
338
|
+
.to eq Date.parse('2012-05-01')
|
339
|
+
expect(Date.parse_spec('last_bimonth', :to))
|
340
|
+
.to eq Date.parse('2012-06-30')
|
331
341
|
|
332
342
|
# Set today to 2014-12-12: Found that last_bimonth was reporting
|
333
343
|
# current bimonth when today was in the second month of the current
|
334
344
|
# bimonth, i.e., an even month
|
335
|
-
allow(Date).to receive_messages(:
|
336
|
-
allow(Date).to receive_messages(:
|
345
|
+
allow(Date).to receive_messages(today: Date.parse('2014-12-12'))
|
346
|
+
allow(Date).to receive_messages(current: Date.parse('2014-12-12'))
|
337
347
|
|
338
|
-
expect(Date.parse_spec('last_bimonth'))
|
339
|
-
|
348
|
+
expect(Date.parse_spec('last_bimonth'))
|
349
|
+
.to eq Date.parse('2014-09-01')
|
350
|
+
expect(Date.parse_spec('last_bimonth', :to))
|
351
|
+
.to eq Date.parse('2014-10-31')
|
340
352
|
|
341
|
-
allow(Date).to receive_messages(:
|
342
|
-
allow(Date).to receive_messages(:
|
353
|
+
allow(Date).to receive_messages(today: Date.parse('2012-07-18'))
|
354
|
+
allow(Date).to receive_messages(current: Date.parse('2012-07-18'))
|
343
355
|
end
|
344
356
|
|
345
|
-
it
|
357
|
+
it 'should parse relative quarters: this_quarter, last_quarter' do
|
346
358
|
expect(Date.parse_spec('this_quarter')).to eq Date.parse('2012-07-01')
|
347
|
-
expect(Date.parse_spec('this_quarter', :to))
|
348
|
-
|
349
|
-
expect(Date.parse_spec('last_quarter'
|
359
|
+
expect(Date.parse_spec('this_quarter', :to))
|
360
|
+
.to eq Date.parse('2012-09-30')
|
361
|
+
expect(Date.parse_spec('last_quarter'))
|
362
|
+
.to eq Date.parse('2012-04-01')
|
363
|
+
expect(Date.parse_spec('last_quarter', :to))
|
364
|
+
.to eq Date.parse('2012-06-30')
|
350
365
|
end
|
351
366
|
|
352
367
|
# Today is set to '2012-07-18'
|
353
|
-
it
|
368
|
+
it 'should parse relative halves: this_half, last_half' do
|
354
369
|
expect(Date.parse_spec('this_half')).to eq Date.parse('2012-07-01')
|
355
370
|
expect(Date.parse_spec('this_half', :to)).to eq Date.parse('2012-12-31')
|
356
371
|
expect(Date.parse_spec('last_half')).to eq Date.parse('2012-01-01')
|
357
372
|
expect(Date.parse_spec('last_half', :to)).to eq Date.parse('2012-06-30')
|
358
373
|
end
|
359
374
|
|
360
|
-
it
|
375
|
+
it 'should parse relative years: this_year, last_year' do
|
361
376
|
expect(Date.parse_spec('this_year')).to eq Date.parse('2012-01-01')
|
362
377
|
expect(Date.parse_spec('this_year', :to)).to eq Date.parse('2012-12-31')
|
363
378
|
expect(Date.parse_spec('last_year')).to eq Date.parse('2011-01-01')
|
364
379
|
expect(Date.parse_spec('last_year', :to)).to eq Date.parse('2011-12-31')
|
365
380
|
end
|
366
381
|
|
367
|
-
it
|
382
|
+
it 'should parse forever and never' do
|
368
383
|
expect(Date.parse_spec('forever')).to eq Date::BOT
|
369
384
|
expect(Date.parse_spec('forever', :to)).to eq Date::EOT
|
370
385
|
expect(Date.parse_spec('never')).to be_nil
|
@@ -372,47 +387,47 @@ describe Date do
|
|
372
387
|
end
|
373
388
|
end
|
374
389
|
|
375
|
-
describe
|
376
|
-
describe
|
377
|
-
it
|
390
|
+
describe 'instance methods' do
|
391
|
+
describe 'print as string' do
|
392
|
+
it 'should be able to print itself as an American-style date' do
|
378
393
|
expect(Date.parse('2011-02-12').american).to eq('2/12/2011')
|
379
394
|
end
|
380
395
|
|
381
|
-
it
|
396
|
+
it 'should be able to print itself in iso form' do
|
382
397
|
expect(Date.today.iso).to eq '2012-07-18'
|
383
398
|
end
|
384
399
|
|
385
|
-
it
|
400
|
+
it 'should be able to print itself in tex_quote form' do
|
386
401
|
expect(Date.today.tex_quote).to eq '2012-07-18'
|
387
402
|
end
|
388
403
|
|
389
|
-
it
|
404
|
+
it 'should be able to print itself in org form' do
|
390
405
|
expect(Date.today.org).to eq('[2012-07-18 Wed]')
|
391
406
|
expect((Date.today + 1.day).org).to eq('[2012-07-19 Thu]')
|
392
407
|
end
|
393
408
|
|
394
|
-
it
|
409
|
+
it 'should be able to print itself in eng form' do
|
395
410
|
expect(Date.today.eng).to eq('July 18, 2012')
|
396
411
|
expect((Date.today + 1.day).eng).to eq('July 19, 2012')
|
397
412
|
end
|
398
413
|
|
399
|
-
it
|
414
|
+
it 'should be able to print itself in numeric form' do
|
400
415
|
expect(Date.today.num).to eq('20120718')
|
401
416
|
expect((Date.today + 1.day).num).to eq('20120719')
|
402
417
|
end
|
403
418
|
end
|
404
419
|
|
405
|
-
describe
|
406
|
-
it
|
407
|
-
expect(Date.parse('2014-11-13').nth_wday_in_month?(2, 4, 11))
|
408
|
-
to be true
|
409
|
-
expect(Date.parse('2014-11-13').nth_wday_in_month?(-3, 4, 11))
|
410
|
-
to be true
|
411
|
-
expect(Date.parse('2014-11-13').nth_wday_in_month?(2, 4, 10))
|
412
|
-
to be false
|
420
|
+
describe 'date arithmetic' do
|
421
|
+
it 'should know if its the nth weekday in a given month' do
|
422
|
+
expect(Date.parse('2014-11-13').nth_wday_in_month?(2, 4, 11))
|
423
|
+
.to be true
|
424
|
+
expect(Date.parse('2014-11-13').nth_wday_in_month?(-3, 4, 11))
|
425
|
+
.to be true
|
426
|
+
expect(Date.parse('2014-11-13').nth_wday_in_month?(2, 4, 10))
|
427
|
+
.to be false
|
413
428
|
end
|
414
429
|
|
415
|
-
it
|
430
|
+
it 'should know if its a weekend or a weekday' do
|
416
431
|
expect(Date.parse('2014-05-17')).to be_weekend
|
417
432
|
expect(Date.parse('2014-05-17')).to_not be_weekday
|
418
433
|
expect(Date.parse('2014-05-18')).to be_weekend
|
@@ -422,12 +437,12 @@ describe Date do
|
|
422
437
|
expect(Date.parse('2014-05-22')).to_not be_weekend
|
423
438
|
end
|
424
439
|
|
425
|
-
it
|
426
|
-
expect(Date.today.pred).to eq
|
427
|
-
expect(Date.today.succ).to eq
|
440
|
+
it 'should know its pred and succ (for Range)' do
|
441
|
+
expect(Date.today.pred).to eq(Date.today - 1)
|
442
|
+
expect(Date.today.succ).to eq(Date.today + 1)
|
428
443
|
end
|
429
444
|
|
430
|
-
it
|
445
|
+
it 'should be able to state its quarter' do
|
431
446
|
expect(Date.today.quarter).to eq(3)
|
432
447
|
expect(Date.parse('2012-02-29').quarter).to eq(1)
|
433
448
|
expect(Date.parse('2012-01-01').quarter).to eq(1)
|
@@ -443,14 +458,14 @@ describe Date do
|
|
443
458
|
expect(Date.parse('2012-12-31').quarter).to eq(4)
|
444
459
|
end
|
445
460
|
|
446
|
-
it
|
461
|
+
it 'should know about years' do
|
447
462
|
expect(Date.parse('2013-01-01')).to be_beginning_of_year
|
448
463
|
expect(Date.parse('2013-12-31')).to be_end_of_year
|
449
464
|
expect(Date.parse('2013-04-01')).to_not be_beginning_of_year
|
450
465
|
expect(Date.parse('2013-12-30')).to_not be_end_of_year
|
451
466
|
end
|
452
467
|
|
453
|
-
it
|
468
|
+
it 'should know about halves' do
|
454
469
|
expect(Date.parse('2013-01-01')).to be_beginning_of_half
|
455
470
|
expect(Date.parse('2013-12-31')).to be_end_of_half
|
456
471
|
expect(Date.parse('2013-07-01')).to be_beginning_of_half
|
@@ -460,7 +475,7 @@ describe Date do
|
|
460
475
|
expect(Date.parse('2013-07-31').half).to eq(2)
|
461
476
|
end
|
462
477
|
|
463
|
-
it
|
478
|
+
it 'should know about quarters' do
|
464
479
|
expect(Date.parse('2013-01-01')).to be_beginning_of_quarter
|
465
480
|
expect(Date.parse('2013-12-31')).to be_end_of_quarter
|
466
481
|
expect(Date.parse('2013-04-01')).to be_beginning_of_quarter
|
@@ -469,9 +484,11 @@ describe Date do
|
|
469
484
|
expect(Date.parse('2013-07-31')).to_not be_end_of_quarter
|
470
485
|
end
|
471
486
|
|
472
|
-
it
|
473
|
-
expect(Date.parse('2013-11-04').beginning_of_bimonth)
|
474
|
-
|
487
|
+
it 'should know about bimonths' do
|
488
|
+
expect(Date.parse('2013-11-04').beginning_of_bimonth)
|
489
|
+
.to eq Date.parse('2013-11-01')
|
490
|
+
expect(Date.parse('2013-11-04').end_of_bimonth)
|
491
|
+
.to eq Date.parse('2013-12-31')
|
475
492
|
expect(Date.parse('2013-03-01')).to be_beginning_of_bimonth
|
476
493
|
expect(Date.parse('2013-04-30')).to be_end_of_bimonth
|
477
494
|
expect(Date.parse('2013-01-01')).to be_beginning_of_bimonth
|
@@ -482,7 +499,7 @@ describe Date do
|
|
482
499
|
expect(Date.parse('2013-07-31')).to_not be_end_of_bimonth
|
483
500
|
end
|
484
501
|
|
485
|
-
it
|
502
|
+
it 'should know about months' do
|
486
503
|
expect(Date.parse('2013-01-01')).to be_beginning_of_month
|
487
504
|
expect(Date.parse('2013-12-31')).to be_end_of_month
|
488
505
|
expect(Date.parse('2013-05-01')).to be_beginning_of_month
|
@@ -491,25 +508,35 @@ describe Date do
|
|
491
508
|
expect(Date.parse('2013-07-30')).to_not be_end_of_month
|
492
509
|
end
|
493
510
|
|
494
|
-
it
|
495
|
-
expect(Date.parse('2013-11-24').beginning_of_semimonth)
|
496
|
-
|
497
|
-
expect(Date.parse('2013-11-04').
|
498
|
-
|
499
|
-
expect(Date.parse('2013-
|
500
|
-
|
501
|
-
expect(Date.parse('2013-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
expect(Date.parse('2013-
|
511
|
+
it 'should know about semimonths' do
|
512
|
+
expect(Date.parse('2013-11-24').beginning_of_semimonth)
|
513
|
+
.to eq Date.parse('2013-11-16')
|
514
|
+
expect(Date.parse('2013-11-04').beginning_of_semimonth)
|
515
|
+
.to eq Date.parse('2013-11-01')
|
516
|
+
expect(Date.parse('2013-11-04').end_of_semimonth)
|
517
|
+
.to eq Date.parse('2013-11-15')
|
518
|
+
expect(Date.parse('2013-11-24').end_of_semimonth)
|
519
|
+
.to eq Date.parse('2013-11-30')
|
520
|
+
expect(Date.parse('2013-03-01'))
|
521
|
+
.to be_beginning_of_semimonth
|
522
|
+
expect(Date.parse('2013-03-16'))
|
523
|
+
.to be_beginning_of_semimonth
|
524
|
+
expect(Date.parse('2013-04-15'))
|
525
|
+
.to be_end_of_semimonth
|
526
|
+
expect(Date.parse('2013-04-30'))
|
527
|
+
.to be_end_of_semimonth
|
528
|
+
end
|
529
|
+
|
530
|
+
it 'should know about biweeks' do
|
531
|
+
expect(Date.parse('2013-11-07').beginning_of_biweek)
|
532
|
+
.to eq Date.parse('2013-11-04')
|
533
|
+
expect(Date.parse('2013-11-07').end_of_biweek)
|
534
|
+
.to eq Date.parse('2013-11-17')
|
508
535
|
expect(Date.parse('2013-03-11')).to be_beginning_of_biweek
|
509
536
|
expect(Date.parse('2013-03-24')).to be_end_of_biweek
|
510
537
|
end
|
511
538
|
|
512
|
-
it
|
539
|
+
it 'should know about weeks' do
|
513
540
|
expect(Date.parse('2013-11-04')).to be_beginning_of_week
|
514
541
|
expect(Date.parse('2013-11-10')).to be_end_of_week
|
515
542
|
expect(Date.parse('2013-12-02')).to be_beginning_of_week
|
@@ -518,22 +545,31 @@ describe Date do
|
|
518
545
|
expect(Date.parse('2013-10-19')).to_not be_end_of_week
|
519
546
|
end
|
520
547
|
|
521
|
-
it
|
522
|
-
expect(Date.parse('2013-11-04').beginning_of_chunk(:year))
|
523
|
-
|
524
|
-
expect(Date.parse('2013-11-04').beginning_of_chunk(:
|
525
|
-
|
526
|
-
expect(Date.parse('2013-11-04').beginning_of_chunk(:
|
527
|
-
|
528
|
-
expect(Date.parse('2013-
|
529
|
-
|
530
|
-
expect(Date.parse('2013-11-
|
548
|
+
it 'should know the beginning of chunks' do
|
549
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:year))
|
550
|
+
.to eq Date.parse('2013-01-01')
|
551
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:half))
|
552
|
+
.to eq Date.parse('2013-07-01')
|
553
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:quarter))
|
554
|
+
.to eq Date.parse('2013-10-01')
|
555
|
+
expect(Date.parse('2013-12-04').beginning_of_chunk(:bimonth))
|
556
|
+
.to eq Date.parse('2013-11-01')
|
557
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:month))
|
558
|
+
.to eq Date.parse('2013-11-01')
|
559
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:semimonth))
|
560
|
+
.to eq Date.parse('2013-11-01')
|
561
|
+
expect(Date.parse('2013-11-24').beginning_of_chunk(:semimonth))
|
562
|
+
.to eq Date.parse('2013-11-16')
|
563
|
+
expect(Date.parse('2013-11-08').beginning_of_chunk(:biweek))
|
564
|
+
.to eq Date.parse('2013-11-04')
|
565
|
+
expect(Date.parse('2013-11-08').beginning_of_chunk(:week))
|
566
|
+
.to eq Date.parse('2013-11-04')
|
531
567
|
expect {
|
532
568
|
Date.parse('2013-11-04').beginning_of_chunk(:wek)
|
533
|
-
}.to raise_error
|
569
|
+
}.to raise_error(ArgumentError)
|
534
570
|
end
|
535
571
|
|
536
|
-
it
|
572
|
+
it 'should be able to add a chuck sym to itself' do
|
537
573
|
# Date.today is '2012-07-18'
|
538
574
|
expect(Date.today.add_chunk(:year)).to eq(Date.parse('2013-07-18'))
|
539
575
|
expect(Date.today.add_chunk(:half)).to eq(Date.parse('2013-01-18'))
|
@@ -547,34 +583,52 @@ describe Date do
|
|
547
583
|
to eq(Date.parse('2012-07-19'))
|
548
584
|
expect {
|
549
585
|
Date.today.add_chunk(:hour)
|
550
|
-
}.to raise_error
|
551
|
-
end
|
552
|
-
|
553
|
-
it
|
554
|
-
expect(Date.parse('2013-07-04').end_of_chunk(:year))
|
555
|
-
|
556
|
-
expect(Date.parse('2013-
|
557
|
-
|
558
|
-
expect(Date.parse('2013-07-04').end_of_chunk(:
|
559
|
-
|
560
|
-
expect(Date.parse('2013-
|
561
|
-
|
562
|
-
expect(Date.parse('2013-07-04').end_of_chunk(:
|
586
|
+
}.to raise_error(ArgumentError)
|
587
|
+
end
|
588
|
+
|
589
|
+
it 'should know the end of chunks' do
|
590
|
+
expect(Date.parse('2013-07-04').end_of_chunk(:year))
|
591
|
+
.to eq Date.parse('2013-12-31')
|
592
|
+
expect(Date.parse('2013-05-04').end_of_chunk(:half))
|
593
|
+
.to eq Date.parse('2013-06-30')
|
594
|
+
expect(Date.parse('2013-07-04').end_of_chunk(:quarter))
|
595
|
+
.to eq Date.parse('2013-09-30')
|
596
|
+
expect(Date.parse('2013-12-04').end_of_chunk(:bimonth))
|
597
|
+
.to eq Date.parse('2013-12-31')
|
598
|
+
expect(Date.parse('2013-07-04').end_of_chunk(:month))
|
599
|
+
.to eq Date.parse('2013-07-31')
|
600
|
+
expect(Date.parse('2013-11-04').end_of_chunk(:semimonth))
|
601
|
+
.to eq Date.parse('2013-11-15')
|
602
|
+
expect(Date.parse('2013-11-24').end_of_chunk(:semimonth))
|
603
|
+
.to eq Date.parse('2013-11-30')
|
604
|
+
expect(Date.parse('2013-11-08').end_of_chunk(:biweek))
|
605
|
+
.to eq Date.parse('2013-11-17')
|
606
|
+
expect(Date.parse('2013-07-04').end_of_chunk(:week))
|
607
|
+
.to eq Date.parse('2013-07-07')
|
563
608
|
expect {
|
564
609
|
Date.parse('2013-11-04').end_of_chunk(:wek)
|
565
|
-
}.to raise_error
|
566
|
-
end
|
567
|
-
|
568
|
-
it
|
569
|
-
expect(Date.parse('2013-07-04').expand_to_period(:year))
|
570
|
-
|
571
|
-
expect(Date.parse('2013-07-04').expand_to_period(:
|
572
|
-
|
573
|
-
expect(Date.parse('2013-07-04').expand_to_period(:
|
574
|
-
|
575
|
-
expect(Date.parse('2013-07-04').expand_to_period(:
|
576
|
-
|
577
|
-
expect(Date.parse('2013-07-04').expand_to_period(:
|
610
|
+
}.to raise_error(ArgumentError)
|
611
|
+
end
|
612
|
+
|
613
|
+
it 'should know how to expand to chunk periods' do
|
614
|
+
expect(Date.parse('2013-07-04').expand_to_period(:year))
|
615
|
+
.to eq Period.new('2013-01-01', '2013-12-31')
|
616
|
+
expect(Date.parse('2013-07-04').expand_to_period(:half))
|
617
|
+
.to eq Period.new('2013-07-01', '2013-12-31')
|
618
|
+
expect(Date.parse('2013-07-04').expand_to_period(:quarter))
|
619
|
+
.to eq Period.new('2013-07-01', '2013-09-30')
|
620
|
+
expect(Date.parse('2013-07-04').expand_to_period(:bimonth))
|
621
|
+
.to eq Period.new('2013-07-01', '2013-08-31')
|
622
|
+
expect(Date.parse('2013-07-04').expand_to_period(:month))
|
623
|
+
.to eq Period.new('2013-07-01', '2013-07-31')
|
624
|
+
expect(Date.parse('2013-07-04').expand_to_period(:semimonth))
|
625
|
+
.to eq Period.new('2013-07-01', '2013-07-15')
|
626
|
+
expect(Date.parse('2013-07-04').expand_to_period(:biweek))
|
627
|
+
.to eq Period.new('2013-07-01', '2013-07-14')
|
628
|
+
expect(Date.parse('2013-07-04').expand_to_period(:week))
|
629
|
+
.to eq Period.new('2013-07-01', '2013-07-07')
|
630
|
+
expect(Date.parse('2013-07-04').expand_to_period(:day))
|
631
|
+
.to eq Period.new('2013-07-04', '2013-07-04')
|
578
632
|
end
|
579
633
|
|
580
634
|
it "should know if it's within 6 months of another date" do
|
@@ -593,14 +647,14 @@ describe Date do
|
|
593
647
|
end
|
594
648
|
end
|
595
649
|
|
596
|
-
describe
|
597
|
-
it
|
650
|
+
describe 'holidays' do
|
651
|
+
it 'should know Easter in its year' do
|
598
652
|
expect(Date.today.easter_this_year).to eq(Date.parse('2012-04-08'))
|
599
653
|
expect(Date.parse('2014-04-20').easter?).to be true
|
600
654
|
expect(Date.parse('2014-03-20').easter?).to be false
|
601
655
|
end
|
602
656
|
|
603
|
-
it
|
657
|
+
it 'should know if its a federal holiday' do
|
604
658
|
# Got these from:
|
605
659
|
# http://www.opm.gov/policy-data-oversight/snow-dismissal-procedures/federal-holidays/
|
606
660
|
|
@@ -702,11 +756,11 @@ describe Date do
|
|
702
756
|
expect(Date.parse('2014-11-23')).to be_fed_holiday
|
703
757
|
end
|
704
758
|
|
705
|
-
it
|
759
|
+
it 'should know if its an NYSE holiday' do
|
706
760
|
################# 2014 2015 2016
|
707
|
-
# New Year
|
761
|
+
# New Year's Day January 1 January 1 January 1
|
708
762
|
# Martin Luther King, Jr. Day January 20 January 19 January 18
|
709
|
-
# Washington
|
763
|
+
# Washington's Birthday February 17 February 16 February 15
|
710
764
|
# Good Friday April 18 April 3 March 25
|
711
765
|
# Memorial Day May 26 May 25 May 30
|
712
766
|
# Independence Day July 4 July 3 July 4
|
@@ -783,7 +837,7 @@ describe Date do
|
|
783
837
|
expect(Date.parse('2007-01-02')).to be_nyse_holiday
|
784
838
|
end
|
785
839
|
|
786
|
-
it
|
840
|
+
it 'should know if it is a Federal workday' do
|
787
841
|
# Some holidays
|
788
842
|
expect(Date.parse('2017-02-20')).not_to be_fed_workday
|
789
843
|
expect(Date.parse('2017-05-29')).not_to be_fed_workday
|
@@ -806,7 +860,7 @@ describe Date do
|
|
806
860
|
expect(Date.parse('2014-11-23')).not_to be_fed_workday
|
807
861
|
end
|
808
862
|
|
809
|
-
it
|
863
|
+
it 'should know if it is an NYSE workday' do
|
810
864
|
# Some holidays
|
811
865
|
expect(Date.parse('2016-01-01')).not_to be_nyse_workday
|
812
866
|
expect(Date.parse('2016-01-18')).not_to be_nyse_workday
|
@@ -833,7 +887,7 @@ describe Date do
|
|
833
887
|
expect(Date.parse('2014-11-23')).not_to be_trading_day
|
834
888
|
end
|
835
889
|
|
836
|
-
it
|
890
|
+
it 'should know the next federal workday' do
|
837
891
|
expect(Date.parse('2015-12-31').next_fed_workday)
|
838
892
|
.to eq Date.parse('2016-01-04')
|
839
893
|
expect(Date.parse('2016-04-20').next_fed_workday)
|
@@ -842,7 +896,7 @@ describe Date do
|
|
842
896
|
.to eq Date.parse('2016-04-25')
|
843
897
|
end
|
844
898
|
|
845
|
-
it
|
899
|
+
it 'should know the prior federal workday' do
|
846
900
|
expect(Date.parse('2016-01-04').prior_fed_workday)
|
847
901
|
.to eq Date.parse('2015-12-31')
|
848
902
|
expect(Date.parse('2016-04-21').prior_fed_workday)
|
@@ -851,7 +905,7 @@ describe Date do
|
|
851
905
|
.to eq Date.parse('2016-04-22')
|
852
906
|
end
|
853
907
|
|
854
|
-
it
|
908
|
+
it 'should know the next NYSE workday' do
|
855
909
|
expect(Date.parse('2015-12-31').next_nyse_workday)
|
856
910
|
.to eq Date.parse('2016-01-04')
|
857
911
|
expect(Date.parse('2016-04-20').next_nyse_workday)
|
@@ -862,7 +916,7 @@ describe Date do
|
|
862
916
|
.to eq Date.parse('2016-04-25')
|
863
917
|
end
|
864
918
|
|
865
|
-
it
|
919
|
+
it 'should know the prior NYSE workday' do
|
866
920
|
# The Monday after Easter; go to prior Thur since Good Friday
|
867
921
|
# is an NYSE holiday.
|
868
922
|
expect(Date.parse('2014-04-21').prior_nyse_workday)
|
@@ -877,7 +931,7 @@ describe Date do
|
|
877
931
|
.to eq Date.parse('2016-04-22')
|
878
932
|
end
|
879
933
|
|
880
|
-
it
|
934
|
+
it 'should be able to skip until it hits a trading day' do
|
881
935
|
# A Wednesday
|
882
936
|
expect(Date.parse('2014-03-26').prior_until_trading_day)
|
883
937
|
.to eq(Date.parse('2014-03-26'))
|
@@ -892,7 +946,7 @@ describe Date do
|
|
892
946
|
.to eq(Date.parse('2014-03-31'))
|
893
947
|
end
|
894
948
|
|
895
|
-
it
|
949
|
+
it 'should be able to add n trading days' do
|
896
950
|
# Add n trading days
|
897
951
|
expect(Date.parse('2014-03-30').add_trading_days(10))
|
898
952
|
.to eq(Date.parse('2014-04-11'))
|