fat_core 5.6.1 → 6.0.0

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.
@@ -1,1390 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
- require 'fat_core/date'
5
-
6
- describe Date do
7
- before do
8
- # Pretend it is this date. Not at beg or end of year, quarter,
9
- # month, or week. It is a Wednesday
10
- allow(described_class).to receive_messages(today: described_class.parse('2012-07-18'))
11
- allow(described_class).to receive_messages(current: described_class.parse('2012-07-18'))
12
- end
13
-
14
- describe 'class methods' do
15
- describe 'ensure_date parsing' do
16
- it 'parses a String as a date' do
17
- expect(described_class.ensure_date('2018-11-12').class).to be described_class
18
- expect(described_class.ensure('2018-11-12').class).to be described_class
19
- end
20
-
21
- it 'leaves a Date as a date' do
22
- expect(described_class.ensure_date(described_class.today).class).to be described_class
23
- expect(described_class.ensure(described_class.today).class).to be described_class
24
- end
25
-
26
- it 'converts Time as a DateTime' do
27
- expect(described_class.ensure_date(Time.now).class).to be DateTime
28
- expect(described_class.ensure(Time.now).class).to be DateTime
29
- end
30
-
31
- it 'raises an error for bad date string' do
32
- expect { described_class.ensure_date('2012-mm-tu') }.to raise_error(/cannot convert string/)
33
- expect { described_class.ensure('2012-mm-tu') }.to raise_error(/cannot convert string/)
34
- end
35
-
36
- it 'raises an error for unknown class' do
37
- expect { described_class.ensure_date([2011, 11, 12]) }
38
- .to raise_error(/cannot convert/)
39
- expect { described_class.ensure([2011, 11, 12]) }
40
- .to raise_error(/cannot convert/)
41
- end
42
- end
43
-
44
- describe 'date arithmetic' do
45
- it 'knows the number of days in a month' do
46
- expect(described_class.days_in_month(2000, 1)).to eq 31
47
- expect(described_class.days_in_month(1900, 2)).to eq 28
48
- expect(described_class.days_in_month(2000, 2)).to eq 29
49
- expect(described_class.days_in_month(2001, 2)).to eq 28
50
- expect(described_class.days_in_month(2004, 2)).to eq 29
51
- expect(described_class.days_in_month(2004, 3)).to eq 31
52
- expect(described_class.days_in_month(2004, 4)).to eq 30
53
- expect(described_class.days_in_month(2004, 5)).to eq 31
54
- expect(described_class.days_in_month(2004, 6)).to eq 30
55
- expect(described_class.days_in_month(2004, 7)).to eq 31
56
- expect(described_class.days_in_month(2004, 8)).to eq 31
57
- expect(described_class.days_in_month(2004, 9)).to eq 30
58
- expect(described_class.days_in_month(2004, 10)).to eq 31
59
- expect(described_class.days_in_month(2004, 11)).to eq 30
60
- expect(described_class.days_in_month(2004, 12)).to eq 31
61
- expect { described_class.days_in_month(2004, 13) }.to raise_error(ArgumentError)
62
- end
63
-
64
- it 'knows a Date\'s half year' do
65
- expect(Date.parse('2024-05-14').half).to eq(1)
66
- expect(Date.parse('2024-09-14').half).to eq(2)
67
- end
68
-
69
- it 'knows a Date\'s quarter year' do
70
- expect(Date.parse('2024-03-14').quarter).to eq(1)
71
- expect(Date.parse('2024-05-14').quarter).to eq(2)
72
- expect(Date.parse('2024-09-14').quarter).to eq(3)
73
- expect(Date.parse('2024-11-14').quarter).to eq(4)
74
- end
75
-
76
- it 'knows a Date\'s bimonth' do
77
- expect(Date.parse('2024-03-14').bimonth).to eq(2)
78
- expect(Date.parse('2024-05-14').bimonth).to eq(3)
79
- expect(Date.parse('2024-09-14').bimonth).to eq(5)
80
- expect(Date.parse('2024-11-14').bimonth).to eq(6)
81
- end
82
-
83
- it 'knows a Date\'s semimonth' do
84
- expect(Date.parse('2024-03-14').semimonth).to eq(5)
85
- expect(Date.parse('2024-05-14').semimonth).to eq(9)
86
- expect(Date.parse('2024-09-24').semimonth).to eq(18)
87
- expect(Date.parse('2024-11-14').semimonth).to eq(21)
88
- end
89
-
90
- it 'knows the nth weekday in a year, month' do
91
- # Sunday is 0, Saturday is 6
92
- # January 2014
93
- # Su Mo Tu We Th Fr Sa
94
- # 1 2 3 4
95
- # 5 6 7 8 9 10 11
96
- # 12 13 14 15 16 17 18
97
- # 19 20 21 22 23 24 25
98
- # 26 27 28 29 30 31
99
-
100
- # First Monday
101
- expect(described_class.nth_wday_in_year_month(1, 1, 2014, 1))
102
- .to eq described_class.parse('2014-01-06')
103
- # Second Monday
104
- expect(described_class.nth_wday_in_year_month(2, 1, 2014, 1))
105
- .to eq described_class.parse('2014-01-13')
106
- # Third Sunday
107
- expect(described_class.nth_wday_in_year_month(3, 0, 2014, 1))
108
- .to eq described_class.parse('2014-01-19')
109
- # Third Sunday (float floored)
110
- expect(described_class.nth_wday_in_year_month(3.2, 0, 2014, 1))
111
- .to eq described_class.parse('2014-01-19')
112
- # Negative wday counts from end: Last Sunday
113
- expect(described_class.nth_wday_in_year_month(-1, 0, 2014, 1))
114
- .to eq described_class.parse('2014-01-26')
115
- expect(described_class.nth_wday_in_year_month(-3, 0, 2014, 1))
116
- .to eq described_class.parse('2014-01-12')
117
- # Negative wday counts from end: Last Thursday
118
- expect(described_class.nth_wday_in_year_month(-1, 4, 2014, 1))
119
- .to eq described_class.parse('2014-01-30')
120
-
121
- # Exceptions
122
- expect {
123
- # N is zero
124
- described_class.nth_wday_in_year_month(0, 6, 2014, 1)
125
- }.to raise_error(ArgumentError)
126
- expect {
127
- # Wday too big
128
- described_class.nth_wday_in_year_month(3, 7, 2014, 1)
129
- }.to raise_error(ArgumentError)
130
- expect {
131
- # Month too big
132
- described_class.nth_wday_in_year_month(3, 1, 2014, 13)
133
- }.to raise_error(ArgumentError)
134
- end
135
-
136
- it 'knows Easter for a given year' do
137
- # Grabbed these dates of Easter from
138
- # http://tlarsen2.tripod.com/thomaslarsen/easterdates.html
139
- easters = {
140
- 2000 => '2000-04-23',
141
- 2001 => '2001-04-15',
142
- 2002 => '2002-03-31',
143
- 2003 => '2003-04-20',
144
- 2004 => '2004-04-11',
145
- 2005 => '2005-03-27',
146
- 2006 => '2006-04-16',
147
- 2007 => '2007-04-08',
148
- 2008 => '2008-03-23',
149
- 2009 => '2009-04-12',
150
- 2010 => '2010-04-04',
151
- 2011 => '2011-04-24',
152
- 2012 => '2012-04-08',
153
- 2013 => '2013-03-31',
154
- 2014 => '2014-04-20',
155
- 2015 => '2015-04-05',
156
- 2016 => '2016-03-27',
157
- 2017 => '2017-04-16',
158
- 2018 => '2018-04-01',
159
- 2019 => '2019-04-21',
160
- 2020 => '2020-04-12',
161
- 2021 => '2021-04-04',
162
- 2022 => '2022-04-17',
163
- 2023 => '2023-04-09',
164
- 2024 => '2024-03-31',
165
- 2025 => '2025-04-20',
166
- 2026 => '2026-04-05',
167
- 2027 => '2027-03-28',
168
- 2028 => '2028-04-16',
169
- 2029 => '2029-04-01',
170
- 2030 => '2030-04-21',
171
- 2031 => '2031-04-13',
172
- 2032 => '2032-03-28',
173
- 2033 => '2033-04-17',
174
- 2034 => '2034-04-09',
175
- 2035 => '2035-03-25',
176
- 2036 => '2036-04-13',
177
- 2037 => '2037-04-05',
178
- 2038 => '2038-04-25',
179
- 2039 => '2039-04-10',
180
- 2040 => '2040-04-01',
181
- 2041 => '2041-04-21',
182
- 2042 => '2042-04-06',
183
- 2043 => '2043-03-29',
184
- 2044 => '2044-04-17',
185
- 2045 => '2045-04-09',
186
- 2046 => '2046-03-25',
187
- 2047 => '2047-04-14',
188
- 2048 => '2048-04-05',
189
- 2049 => '2049-04-18',
190
- 2050 => '2050-04-10',
191
- 2051 => '2051-04-02',
192
- 2052 => '2052-04-21',
193
- 2053 => '2053-04-06',
194
- 2054 => '2054-03-29',
195
- 2055 => '2055-04-18',
196
- 2056 => '2056-04-02',
197
- 2057 => '2057-04-22',
198
- 2058 => '2058-04-14',
199
- 2059 => '2059-03-30',
200
- 2060 => '2060-04-18',
201
- 2061 => '2061-04-10',
202
- 2062 => '2062-03-26',
203
- 2063 => '2063-04-15',
204
- 2064 => '2064-04-06',
205
- 2065 => '2065-03-29',
206
- 2066 => '2066-04-11',
207
- 2067 => '2067-04-03',
208
- 2068 => '2068-04-22',
209
- 2069 => '2069-04-14',
210
- 2070 => '2070-03-30',
211
- 2071 => '2071-04-19',
212
- 2072 => '2072-04-10',
213
- 2073 => '2073-03-26',
214
- 2074 => '2074-04-15',
215
- 2075 => '2075-04-07',
216
- 2076 => '2076-04-19',
217
- 2077 => '2077-04-11',
218
- 2078 => '2078-04-03',
219
- 2079 => '2079-04-23',
220
- 2080 => '2080-04-07',
221
- 2081 => '2081-03-30',
222
- 2082 => '2082-04-19',
223
- 2083 => '2083-04-04',
224
- 2084 => '2084-03-26',
225
- 2085 => '2085-04-15',
226
- 2086 => '2086-03-31',
227
- 2087 => '2087-04-20',
228
- 2088 => '2088-04-11',
229
- 2089 => '2089-04-03',
230
- 2090 => '2090-04-16',
231
- 2091 => '2091-04-08',
232
- 2092 => '2092-03-30',
233
- 2093 => '2093-04-12',
234
- 2094 => '2094-04-04',
235
- 2095 => '2095-04-24',
236
- 2096 => '2096-04-15',
237
- 2097 => '2097-03-31',
238
- 2098 => '2098-04-20',
239
- 2099 => '2099-04-12'
240
- }
241
- easters.each_pair do |year, date|
242
- expect(described_class.easter(year)).to eq described_class.parse(date)
243
- end
244
- end
245
- end
246
-
247
- describe 'parsing' do
248
- it 'parses an American-style date' do
249
- expect(described_class.parse_american('2/12/2011').iso).to eq('2011-02-12')
250
- expect(described_class.parse_american('2 / 12/ 2011').iso).to eq('2011-02-12')
251
- expect(described_class.parse_american('2 / 1 / 2011').iso).to eq('2011-02-01')
252
- expect(described_class.parse_american(' 2 / 1 / 2011 ').iso).to eq('2011-02-01')
253
- expect(described_class.parse_american(' 2 / 1 / 15 ').iso).to eq('2015-02-01')
254
- expect(described_class.parse_american(' 2-1-15 ').iso).to eq('2015-02-01')
255
- expect {
256
- described_class.parse_american('xx/1/15')
257
- }.to raise_error(ArgumentError)
258
- end
259
- end
260
-
261
- describe 'parse_spec' do
262
- # For these tests, today is 2012-07-18
263
-
264
- it 'chokes if spec type is neither :from or :to' do
265
- expect {
266
- described_class.parse_spec('2011-07-15', :form)
267
- }.to raise_error(ArgumentError)
268
- end
269
-
270
- it 'parses plain iso dates correctly' do
271
- expect(described_class.parse_spec('2011-07-15')).to eq described_class.parse('2011-07-15')
272
- expect(described_class.parse_spec('2011/08/05')).to eq described_class.parse('2011-08-05')
273
- end
274
-
275
- it 'parses YYYY-ddd day-of-year dates correctly' do
276
- expect(described_class.parse_spec('2011-115')).to eq described_class.parse('2011-04-25')
277
- expect(described_class.parse_spec('2011/001')).to eq described_class.parse('2011-01-01')
278
- expect {
279
- described_class.parse_spec('2023-366')
280
- }.to raise_error(/invalid day-of-year/)
281
- end
282
-
283
- it "parses week numbers such as 'W23' or '23W' correctly" do
284
- expect(described_class.parse_spec('W1')).to eq described_class.parse('2012-01-02')
285
- expect(described_class.parse_spec('W23')).to eq described_class.parse('2012-06-04')
286
- expect(described_class.parse_spec('W23', :to)).to eq described_class.parse('2012-06-10')
287
- expect(described_class.parse_spec('23W')).to eq described_class.parse('2012-06-04')
288
- expect(described_class.parse_spec('23W', :to)).to eq described_class.parse('2012-06-10')
289
- expect {
290
- described_class.parse_spec('W83', :to)
291
- }.to raise_error(ArgumentError)
292
- end
293
-
294
- it "parses week-day numbers such as 'W23-4' or '23W-3' correctly" do
295
- expect(described_class.parse_spec('W1-4')).to eq described_class.parse('2012-01-05')
296
- expect(described_class.parse_spec('W23-3')).to eq described_class.parse('2012-06-06')
297
- expect(described_class.parse_spec('W23-3', :to)).to eq described_class.parse('2012-06-06')
298
- expect(described_class.parse_spec('23W-2')).to eq described_class.parse('2012-06-05')
299
- expect(described_class.parse_spec('23W-2', :to)).to eq described_class.parse('2012-06-05')
300
- expect {
301
- described_class.parse_spec('W38-9', :to)
302
- }.to raise_error(ArgumentError)
303
- end
304
-
305
- it 'parse year-week numbers \'YYYY-Wnn\' correctly' do
306
- expect(described_class.parse_spec('2002-W52')).to eq described_class.parse('2002-12-23')
307
- expect(described_class.parse_spec('2003-W1')).to eq described_class.parse('2002-12-30')
308
- expect(described_class.parse_spec('2003-W1', :to)).to eq described_class.parse('2003-01-05')
309
- expect(described_class.parse_spec('2003-W23')).to eq described_class.parse('2003-06-02')
310
- expect(described_class.parse_spec('2003-W23', :to)).to eq described_class.parse('2003-06-08')
311
- expect(described_class.parse_spec('2003-23W')).to eq described_class.parse('2003-06-02')
312
- expect(described_class.parse_spec('2003/23W', :to)).to eq described_class.parse('2003-06-08')
313
- expect {
314
- described_class.parse_spec('2003-W83', :to)
315
- }.to raise_error(ArgumentError)
316
- end
317
-
318
- it 'parse year-week-day numbers \'YYYY-Wnn-d\' correctly' do
319
- # Examples from the Wikipedia page on ISO 8601 dates
320
- expect(described_class.parse_spec('1976-W53-6')).to eq described_class.parse('1977-01-01')
321
- expect(described_class.parse_spec('1977-W52-6')).to eq described_class.parse('1977-12-31')
322
- expect(described_class.parse_spec('1977-W52-7')).to eq described_class.parse('1978-01-01')
323
- expect(described_class.parse_spec('1978-W01-1')).to eq described_class.parse('1978-01-02')
324
- expect(described_class.parse_spec('1978-W52-7')).to eq described_class.parse('1978-12-31')
325
- expect(described_class.parse_spec('1979-W01-1')).to eq described_class.parse('1979-01-01')
326
- expect(described_class.parse_spec('1979-W52-7')).to eq described_class.parse('1979-12-30')
327
- expect(described_class.parse_spec('1980-W01-1')).to eq described_class.parse('1979-12-31')
328
- expect(described_class.parse_spec('1980-W52-7')).to eq described_class.parse('1980-12-28')
329
- expect(described_class.parse_spec('1981-W01-2')).to eq described_class.parse('1980-12-30')
330
- expect(described_class.parse_spec('1981-W01-3')).to eq described_class.parse('1980-12-31')
331
- expect(described_class.parse_spec('1981-W01-4')).to eq described_class.parse('1981-01-01')
332
- expect(described_class.parse_spec('1981-W53-4')).to eq described_class.parse('1981-12-31')
333
- expect(described_class.parse_spec('1981-W53-5')).to eq described_class.parse('1982-01-01')
334
- expect {
335
- described_class.parse_spec('2003-W83', :to)
336
- }.to raise_error(ArgumentError)
337
- end
338
-
339
- it 'parses year-half specs such as YYYY-NH or YYYY-HN' do
340
- expect(described_class.parse_spec('2011-2H', :from)).to eq described_class.parse('2011-07-01')
341
- expect(described_class.parse_spec('2011-2H', :to)).to eq described_class.parse('2011-12-31')
342
- expect(described_class.parse_spec('2011-H1', :from)).to eq described_class.parse('2011-01-01')
343
- expect(described_class.parse_spec('2011/H1', :to)).to eq described_class.parse('2011-06-30')
344
- expect { described_class.parse_spec('2011-3H') }.to raise_error(ArgumentError)
345
- end
346
-
347
- it 'parses half-only specs such as NH or HN' do
348
- expect(described_class.parse_spec('2H', :from)).to eq described_class.parse('2012-07-01')
349
- expect(described_class.parse_spec('H2', :to)).to eq described_class.parse('2012-12-31')
350
- expect(described_class.parse_spec('1H', :from)).to eq described_class.parse('2012-01-01')
351
- expect(described_class.parse_spec('H1', :to)).to eq described_class.parse('2012-06-30')
352
- expect { described_class.parse_spec('8H') }.to raise_error(ArgumentError)
353
- end
354
-
355
- it 'parses year-quarter specs such as YYYY-NQ or YYYY-QN' do
356
- expect(described_class.parse_spec('2011-4Q', :from)).to eq described_class.parse('2011-10-01')
357
- expect(described_class.parse_spec('2011-4Q', :to)).to eq described_class.parse('2011-12-31')
358
- expect(described_class.parse_spec('2011-Q4', :from)).to eq described_class.parse('2011-10-01')
359
- expect(described_class.parse_spec('2011/Q4', :to)).to eq described_class.parse('2011-12-31')
360
- expect { described_class.parse_spec('2011-5Q') }.to raise_error(ArgumentError)
361
- end
362
-
363
- it 'parses quarter-only specs such as NQ or QN' do
364
- expect(described_class.parse_spec('4Q', :from)).to eq described_class.parse('2012-10-01')
365
- expect(described_class.parse_spec('4Q', :to)).to eq described_class.parse('2012-12-31')
366
- expect(described_class.parse_spec('Q4', :from)).to eq described_class.parse('2012-10-01')
367
- expect(described_class.parse_spec('Q4', :to)).to eq described_class.parse('2012-12-31')
368
- expect { described_class.parse_spec('5Q') }.to raise_error(ArgumentError)
369
- end
370
-
371
- it 'parses year-month specs such as YYYY-MM' do
372
- expect(described_class.parse_spec('2010-5', :from)).to eq described_class.parse('2010-05-01')
373
- expect(described_class.parse_spec('2010/5', :to)).to eq described_class.parse('2010-05-31')
374
- expect { described_class.parse_spec('2010-13') }.to raise_error(ArgumentError)
375
- end
376
-
377
- it 'parses month-only specs such as MM' do
378
- expect(described_class.parse_spec('10', :from)).to eq described_class.parse('2012-10-01')
379
- expect(described_class.parse_spec('10', :to)).to eq described_class.parse('2012-10-31')
380
- expect { described_class.parse_spec('99') }.to raise_error(ArgumentError)
381
- # This is a valid day-of-year spec
382
- expect { described_class.parse_spec('011') }.not_to raise_error
383
- end
384
-
385
- it 'parses month-day specs such as MM-DD' do
386
- expect(described_class.parse_spec('10-12', :from)).to eq described_class.parse('2012-10-12')
387
- expect(described_class.parse_spec('10-2', :from)).to eq described_class.parse('2012-10-02')
388
- expect(described_class.parse_spec('5-12', :from)).to eq described_class.parse('2012-05-12')
389
- expect(described_class.parse_spec('5-2', :from)).to eq described_class.parse('2012-05-02')
390
- expect(described_class.parse_spec('10/12', :from)).to eq described_class.parse('2012-10-12')
391
- expect(described_class.parse_spec('10/2', :from)).to eq described_class.parse('2012-10-02')
392
- expect(described_class.parse_spec('5/12', :from)).to eq described_class.parse('2012-05-12')
393
- expect(described_class.parse_spec('5/2', :from)).to eq described_class.parse('2012-05-02')
394
- expect { described_class.parse_spec('99-3') }.to raise_error(ArgumentError)
395
- expect { described_class.parse_spec('3-33') }.to raise_error(ArgumentError)
396
- expect { described_class.parse_spec('99/3') }.to raise_error(ArgumentError)
397
- expect { described_class.parse_spec('3/33') }.to raise_error(ArgumentError)
398
- end
399
-
400
- it 'parses year-only specs such as YYYY' do
401
- expect(described_class.parse_spec('2010', :from)).to eq described_class.parse('2010-01-01')
402
- expect(described_class.parse_spec('2010', :to)).to eq described_class.parse('2010-12-31')
403
- expect { described_class.parse_spec('99999') }.to raise_error(ArgumentError)
404
- end
405
-
406
- it 'parses half-month specs such as YYYY-MM-I and YYYY-MM-II' do
407
- expect(described_class.parse_spec('2010-09-I', :from)).to eq described_class.parse('2010-09-01')
408
- expect(described_class.parse_spec('2010-09-I', :to)).to eq described_class.parse('2010-09-15')
409
- expect(described_class.parse_spec('2010-09-II', :from)).to eq described_class.parse('2010-09-16')
410
- expect(described_class.parse_spec('2010-09-II', :to)).to eq described_class.parse('2010-09-30')
411
- expect(described_class.parse_spec('2010-05-I', :from)).to eq described_class.parse('2010-05-01')
412
- expect(described_class.parse_spec('2010-05-I', :to)).to eq described_class.parse('2010-05-15')
413
- end
414
-
415
- it 'parses intra-month week specs such as YYYY-MM-i and YYYY-MM-v begin Sunday' do
416
- expect(described_class.parse_spec('2010-09-i', :from)).to eq described_class.parse('2010-09-01')
417
- expect(described_class.parse_spec('2010-09-ii', :from)).to eq described_class.parse('2010-09-06')
418
- expect(described_class.parse_spec('2010-09-iii', :from)).to eq described_class.parse('2010-09-13')
419
- expect(described_class.parse_spec('2010-09-iv', :from)).to eq described_class.parse('2010-09-20')
420
- expect(described_class.parse_spec('2010-09-v', :from)).to eq described_class.parse('2010-09-27')
421
- expect { described_class.parse_spec('2010-09-vi', :from) }.to raise_error(/no week/)
422
- expect(described_class.parse_spec('2011-01-vi', :from)).to eq described_class.parse('2011-01-31')
423
-
424
- expect(described_class.parse_spec('2010-09-i', :to)).to eq described_class.parse('2010-09-05')
425
- expect(described_class.parse_spec('2010-09-ii', :to)).to eq described_class.parse('2010-09-12')
426
- expect(described_class.parse_spec('2010-09-iii', :to)).to eq described_class.parse('2010-09-19')
427
- expect(described_class.parse_spec('2010-09-iv', :to)).to eq described_class.parse('2010-09-26')
428
- expect(described_class.parse_spec('2010-09-v', :to)).to eq described_class.parse('2010-09-30')
429
- expect { described_class.parse_spec('2010-09-vi', :to) }.to raise_error(/no week/)
430
- expect(described_class.parse_spec('2011-01-vi', :to)).to eq described_class.parse('2011-01-31')
431
- end
432
-
433
- it 'parses intra-month week specs such as YYYY-MM-i and YYYY-MM-v begin Monday' do
434
- expect(described_class.parse_spec('2010-09-i', :from)).to eq described_class.parse('2010-09-01')
435
- expect(described_class.parse_spec('2010-09-ii', :from)).to eq described_class.parse('2010-09-06')
436
- expect(described_class.parse_spec('2010-09-iii', :from)).to eq described_class.parse('2010-09-13')
437
- expect(described_class.parse_spec('2010-09-iv', :from)).to eq described_class.parse('2010-09-20')
438
- expect(described_class.parse_spec('2010-09-v', :from)).to eq described_class.parse('2010-09-27')
439
- expect { described_class.parse_spec('2010-09-vi', :from) }.to raise_error(/no week/)
440
- expect { described_class.parse_spec('2010-10-vi', :from) }.to raise_error(/no week/)
441
- expect(described_class.parse_spec('2011-01-vi', :to)).to eq described_class.parse('2011-01-31')
442
-
443
- expect(described_class.parse_spec('2010-09-i', :to)).to eq described_class.parse('2010-09-05')
444
- expect(described_class.parse_spec('2010-09-ii', :to)).to eq described_class.parse('2010-09-12')
445
- expect(described_class.parse_spec('2010-09-iii', :to)).to eq described_class.parse('2010-09-19')
446
- expect(described_class.parse_spec('2010-09-iv', :to)).to eq described_class.parse('2010-09-26')
447
- expect(described_class.parse_spec('2010-09-v', :to)).to eq described_class.parse('2010-09-30')
448
- expect { described_class.parse_spec('2010-09-vi', :to) }.to raise_error(/no week/)
449
- expect { described_class.parse_spec('2010-09-vi', :to) }.to raise_error(/no week/)
450
- expect(described_class.parse_spec('2011-01-vi', :to)).to eq described_class.parse('2011-01-31')
451
-
452
- travel_to Time.local(2020, 9, 15)
453
- expect(described_class.parse_spec('9-i', :from)).to eq described_class.parse('2020-09-01')
454
- expect(described_class.parse_spec('09-i', :to)).to eq described_class.parse('2020-09-06')
455
- expect(described_class.parse_spec('09-iii', :from)).to eq described_class.parse('2020-09-14')
456
- expect(described_class.parse_spec('09-iii', :to)).to eq described_class.parse('2020-09-20')
457
- expect(described_class.parse_spec('09-v', :from)).to eq described_class.parse('2020-09-28')
458
- expect(described_class.parse_spec('09-v', :to)).to eq described_class.parse('2020-09-30')
459
-
460
- expect(described_class.parse_spec('i', :to)).to eq described_class.parse('2020-09-06')
461
- expect(described_class.parse_spec('ii', :to)).to eq described_class.parse('2020-09-13')
462
- expect(described_class.parse_spec('iii', :to)).to eq described_class.parse('2020-09-20')
463
- expect(described_class.parse_spec('iv', :to)).to eq described_class.parse('2020-09-27')
464
- expect(described_class.parse_spec('v', :to)).to eq described_class.parse('2020-09-30')
465
- travel_back
466
- end
467
-
468
- it 'parses Easter-relative dates' do
469
- expect(described_class.parse_spec('2024-E')).to eq described_class.parse('2024-03-31')
470
- expect(described_class.parse_spec('2024-E+12')).to eq described_class.parse('2024-04-12')
471
- expect(described_class.parse_spec('2024-E-12')).to eq described_class.parse('2024-03-19')
472
- travel_to Time.local(2020, 9, 15)
473
- expect(described_class.parse_spec('E-12')).to eq described_class.parse('2020-03-31')
474
- expect(described_class.parse_spec('E+12')).to eq described_class.parse('2020-04-24')
475
- travel_back
476
- end
477
-
478
- it 'parses DOW ordinals' do
479
- expect(described_class.parse_spec('2024-11-4Th')).to eq described_class.parse('2024-11-28')
480
- expect { described_class.parse_spec('2024-11-40Th') }.to raise_error(/invalid ordinal/)
481
- expect { described_class.parse_spec('2024-15-4Th') }.to raise_error(/invalid month/)
482
- expect { described_class.parse_spec('2024-11-5Th') }.to raise_error(/there is no 5th/i)
483
- travel_to Time.local(2020, 9, 15)
484
- expect(described_class.parse_spec('11-4Th')).to eq described_class.parse('2020-11-26')
485
- expect(described_class.parse_spec('4Th')).to eq described_class.parse('2020-09-24')
486
- travel_back
487
- end
488
-
489
- it 'parses DOY three-digit specs' do
490
- expect(described_class.parse_spec('2024-001')).to eq described_class.parse('2024-01-01')
491
- expect(described_class.parse_spec('2024-366')).to eq described_class.parse('2024-12-31')
492
- expect { described_class.parse_spec('2024-885') }.to raise_error(/invalid day-of-year/)
493
- travel_to Time.local(2020, 9, 15)
494
- expect(described_class.parse_spec('150')).to eq described_class.parse('2020-05-29')
495
- travel_back
496
- end
497
-
498
- it 'parses relative day names: today, yesterday' do
499
- expect(described_class.parse_spec('today')).to eq described_class.current
500
- expect(described_class.parse_spec('this_day')).to eq described_class.current
501
- expect(described_class.parse_spec('yesterday')).to eq described_class.current - 1.day
502
- expect(described_class.parse_spec('last_day')).to eq described_class.current - 1.day
503
- expect(described_class.parse_spec('tomorrow')).to eq described_class.current + 1.day
504
- end
505
-
506
- it 'parses relative weeks: this_week, last_week' do
507
- expect(described_class.parse_spec('this_week')).to eq described_class.parse('2012-07-16')
508
- expect(described_class.parse_spec('this_week', :to)).to eq described_class.parse('2012-07-22')
509
- expect(described_class.parse_spec('last_week')).to eq described_class.parse('2012-07-09')
510
- expect(described_class.parse_spec('last_week', :to)).to eq described_class.parse('2012-07-15')
511
- end
512
-
513
- it 'parses relative biweeks: this_biweek, last_biweek' do
514
- expect(described_class.parse_spec('this_biweek')).to eq described_class.parse('2012-07-16')
515
- expect(described_class.parse_spec('this_biweek', :to))
516
- .to eq described_class.parse('2012-07-29')
517
- expect(described_class.parse_spec('last_biweek')).to eq described_class.parse('2012-07-02')
518
- expect(described_class.parse_spec('last_biweek', :to))
519
- .to eq described_class.parse('2012-07-15')
520
- end
521
-
522
- it 'parses relative semi-months: this_semimonth, last_semimonth' do
523
- expect(described_class.parse_spec('this_semimonth')).to eq described_class.parse('2012-07-16')
524
- expect(described_class.parse_spec('this_semimonth', :to))
525
- .to eq described_class.parse('2012-07-31')
526
- expect(described_class.parse_spec('last_semimonth'))
527
- .to eq described_class.parse('2012-07-01')
528
- expect(described_class.parse_spec('last_semimonth', :to))
529
- .to eq described_class.parse('2012-07-15')
530
- end
531
-
532
- it 'parses relative months: this_month, last_month' do
533
- expect(described_class.parse_spec('this_month')).to eq described_class.parse('2012-07-01')
534
- expect(described_class.parse_spec('this_month', :to))
535
- .to eq described_class.parse('2012-07-31')
536
- expect(described_class.parse_spec('last_month')).to eq described_class.parse('2012-06-01')
537
- expect(described_class.parse_spec('last_month', :to))
538
- .to eq described_class.parse('2012-06-30')
539
- end
540
-
541
- it 'parses relative bimonths: this_bimonth, last_bimonth' do
542
- expect(described_class.parse_spec('this_bimonth')).to eq described_class.parse('2012-07-01')
543
- expect(described_class.parse_spec('this_bimonth', :to))
544
- .to eq described_class.parse('2012-08-31')
545
- expect(described_class.parse_spec('last_bimonth'))
546
- .to eq described_class.parse('2012-05-01')
547
- expect(described_class.parse_spec('last_bimonth', :to))
548
- .to eq described_class.parse('2012-06-30')
549
-
550
- # Set today to 2014-12-12: Found that last_bimonth was reporting
551
- # current bimonth when today was in the second month of the current
552
- # bimonth, i.e., an even month
553
- allow(Date).to receive_messages(today: described_class.parse('2014-12-12'))
554
- allow(Date).to receive_messages(current: described_class.parse('2014-12-12'))
555
-
556
- expect(described_class.parse_spec('last_bimonth'))
557
- .to eq described_class.parse('2014-09-01')
558
- expect(described_class.parse_spec('last_bimonth', :to))
559
- .to eq described_class.parse('2014-10-31')
560
-
561
- allow(Date).to receive_messages(today: described_class.parse('2012-07-18'))
562
- allow(Date).to receive_messages(current: described_class.parse('2012-07-18'))
563
- end
564
-
565
- it 'parses relative quarters: this_quarter, last_quarter' do
566
- expect(described_class.parse_spec('this_quarter')).to eq described_class.parse('2012-07-01')
567
- expect(described_class.parse_spec('this_quarter', :to))
568
- .to eq described_class.parse('2012-09-30')
569
- expect(described_class.parse_spec('last_quarter'))
570
- .to eq described_class.parse('2012-04-01')
571
- expect(described_class.parse_spec('last_quarter', :to))
572
- .to eq described_class.parse('2012-06-30')
573
- end
574
-
575
- # Today is set to '2012-07-18'
576
- it 'parses relative halves: this_half, last_half' do
577
- expect(described_class.parse_spec('this_half')).to eq described_class.parse('2012-07-01')
578
- expect(described_class.parse_spec('this_half', :to)).to eq described_class.parse('2012-12-31')
579
- expect(described_class.parse_spec('last_half')).to eq described_class.parse('2012-01-01')
580
- expect(described_class.parse_spec('last_half', :to)).to eq described_class.parse('2012-06-30')
581
- end
582
-
583
- it 'parses relative years: this_year, last_year' do
584
- expect(described_class.parse_spec('this_year')).to eq described_class.parse('2012-01-01')
585
- expect(described_class.parse_spec('this_year', :to)).to eq described_class.parse('2012-12-31')
586
- expect(described_class.parse_spec('last_year')).to eq described_class.parse('2011-01-01')
587
- expect(described_class.parse_spec('last_year', :to)).to eq described_class.parse('2011-12-31')
588
- end
589
-
590
- it 'parses forever and never' do
591
- expect(described_class.parse_spec('forever')).to eq Date::BOT
592
- expect(described_class.parse_spec('forever', :to)).to eq Date::EOT
593
- expect(described_class.parse_spec('never')).to be_nil
594
- end
595
-
596
- it 'converts a month name into its sequential number' do
597
- expect(described_class.mo_name_to_num(' January')).to eq 1
598
- expect(described_class.mo_name_to_num(' feb ')).to eq 2
599
- expect(described_class.mo_name_to_num(' mAr ')).to eq 3
600
- expect(described_class.mo_name_to_num(' Aprol ')).to eq 4
601
- expect(described_class.mo_name_to_num("\t \tmaybe")).to eq 5
602
- expect(described_class.mo_name_to_num("\t \tjunta\t \t")).to eq 6
603
- expect(described_class.mo_name_to_num("\t \tjulia\t \t")).to eq 7
604
- expect(described_class.mo_name_to_num("\t \tAugustus\t \t")).to eq 8
605
- expect(described_class.mo_name_to_num("September")).to eq 9
606
- expect(described_class.mo_name_to_num("octagon")).to eq 10
607
- expect(described_class.mo_name_to_num(" novena this month")).to eq 11
608
- expect(described_class.mo_name_to_num("decimal")).to eq 12
609
- expect(described_class.mo_name_to_num(" dewey decimal")).to be_nil
610
- end
611
- end
612
- end
613
-
614
- describe 'instance methods' do
615
- describe 'print as string' do
616
- it 'prints itself as an American-style date' do
617
- expect(described_class.parse('2011-02-12').american).to eq('2/12/2011')
618
- end
619
-
620
- it 'prints itself in iso form' do
621
- expect(described_class.today.iso).to eq '2012-07-18'
622
- end
623
-
624
- it 'prints itself in tex_quote form' do
625
- expect(described_class.today.tex_quote).to eq '2012--07--18'
626
- end
627
-
628
- it 'prints itself in org form' do
629
- expect(described_class.today.org).to eq('[2012-07-18 Wed]')
630
- expect((described_class.today + 1.day).org).to eq('[2012-07-19 Thu]')
631
- expect((described_class.today + 1.day).org(active: true)).to eq('<2012-07-19 Thu>')
632
- end
633
-
634
- it 'prints itself in eng form' do
635
- expect(described_class.parse('2016-01-05').eng).to eq('January 5, 2016')
636
- expect(described_class.today.eng).to eq('July 18, 2012')
637
- expect((described_class.today + 1.day).eng).to eq('July 19, 2012')
638
- end
639
-
640
- it 'prints itself in numeric form' do
641
- expect(described_class.today.num).to eq('20120718')
642
- expect((described_class.today + 1.day).num).to eq('20120719')
643
- end
644
- end
645
-
646
- describe 'date arithmetic' do
647
- it 'knows if its the nth weekday in a given month' do
648
- expect(described_class.parse('2014-11-13').nth_wday_in_month?(2, 4, 11))
649
- .to be true
650
- expect(described_class.parse('2014-11-13').nth_wday_in_month?(-3, 4, 11))
651
- .to be true
652
- expect(described_class.parse('2014-11-13').nth_wday_in_month?(2, 4, 10))
653
- .to be false
654
- end
655
-
656
- it 'knows if its a weekend or a weekday' do
657
- expect(described_class.parse('2014-05-17')).to be_weekend
658
- expect(described_class.parse('2014-05-17')).not_to be_weekday
659
- expect(described_class.parse('2014-05-18')).to be_weekend
660
- expect(described_class.parse('2014-05-18')).not_to be_weekday
661
-
662
- expect(described_class.parse('2014-05-22')).to be_weekday
663
- expect(described_class.parse('2014-05-22')).not_to be_weekend
664
- end
665
-
666
- it 'knows its pred and succ (for Range)' do
667
- expect(described_class.today.pred).to eq(described_class.today - 1)
668
- expect(described_class.today.succ).to eq(described_class.today + 1)
669
- end
670
-
671
- it 'knows its quarter' do
672
- expect(described_class.today.quarter).to eq(3)
673
- expect(described_class.parse('2012-02-29').quarter).to eq(1)
674
- expect(described_class.parse('2012-01-01').quarter).to eq(1)
675
- expect(described_class.parse('2012-03-31').quarter).to eq(1)
676
- expect(described_class.parse('2012-04-01').quarter).to eq(2)
677
- expect(described_class.parse('2012-05-15').quarter).to eq(2)
678
- expect(described_class.parse('2012-06-30').quarter).to eq(2)
679
- expect(described_class.parse('2012-07-01').quarter).to eq(3)
680
- expect(described_class.parse('2012-08-15').quarter).to eq(3)
681
- expect(described_class.parse('2012-09-30').quarter).to eq(3)
682
- expect(described_class.parse('2012-10-01').quarter).to eq(4)
683
- expect(described_class.parse('2012-11-15').quarter).to eq(4)
684
- expect(described_class.parse('2012-12-31').quarter).to eq(4)
685
- end
686
-
687
- it 'knows about years' do
688
- expect(described_class.parse('2013-01-01')).to be_beginning_of_year
689
- expect(described_class.parse('2013-12-31')).to be_end_of_year
690
- expect(described_class.parse('2013-04-01')).not_to be_beginning_of_year
691
- expect(described_class.parse('2013-12-30')).not_to be_end_of_year
692
- end
693
-
694
- it 'knows about halves' do
695
- expect(described_class.parse('2013-01-01')).to be_beginning_of_half
696
- expect(described_class.parse('2013-12-31')).to be_end_of_half
697
- expect(described_class.parse('2013-07-01')).to be_beginning_of_half
698
- expect(described_class.parse('2013-06-30')).to be_end_of_half
699
- expect(described_class.parse('2013-05-01')).not_to be_beginning_of_half
700
- expect(described_class.parse('2013-05-01').half).to eq(1)
701
- expect(described_class.parse('2013-07-31').half).to eq(2)
702
- end
703
-
704
- it 'knows about quarters' do
705
- expect(described_class.parse('2013-01-01')).to be_beginning_of_quarter
706
- expect(described_class.parse('2013-12-31')).to be_end_of_quarter
707
- expect(described_class.parse('2013-04-01')).to be_beginning_of_quarter
708
- expect(described_class.parse('2013-06-30')).to be_end_of_quarter
709
- expect(described_class.parse('2013-05-01')).not_to be_beginning_of_quarter
710
- expect(described_class.parse('2013-07-31')).not_to be_end_of_quarter
711
- end
712
-
713
- it 'knows about bimonths' do
714
- expect(described_class.parse('2013-11-04').beginning_of_bimonth)
715
- .to eq described_class.parse('2013-11-01')
716
- expect(described_class.parse('2013-11-04').end_of_bimonth)
717
- .to eq described_class.parse('2013-12-31')
718
- expect(described_class.parse('2013-03-01')).to be_beginning_of_bimonth
719
- expect(described_class.parse('2013-04-30')).to be_end_of_bimonth
720
- expect(described_class.parse('2013-01-01')).to be_beginning_of_bimonth
721
- expect(described_class.parse('2013-12-31')).to be_end_of_bimonth
722
- expect(described_class.parse('2013-05-01')).to be_beginning_of_bimonth
723
- expect(described_class.parse('2013-06-30')).to be_end_of_bimonth
724
- expect(described_class.parse('2013-06-01')).not_to be_beginning_of_bimonth
725
- expect(described_class.parse('2013-07-31')).not_to be_end_of_bimonth
726
- end
727
-
728
- it 'knows about months' do
729
- expect(described_class.parse('2013-01-01')).to be_beginning_of_month
730
- expect(described_class.parse('2013-12-31')).to be_end_of_month
731
- expect(described_class.parse('2013-05-01')).to be_beginning_of_month
732
- expect(described_class.parse('2013-07-31')).to be_end_of_month
733
- expect(described_class.parse('2013-05-02')).not_to be_beginning_of_month
734
- expect(described_class.parse('2013-07-30')).not_to be_end_of_month
735
- end
736
-
737
- it 'knows about semimonths' do
738
- expect(described_class.parse('2013-11-24').beginning_of_semimonth)
739
- .to eq described_class.parse('2013-11-16')
740
- expect(described_class.parse('2013-11-04').beginning_of_semimonth)
741
- .to eq described_class.parse('2013-11-01')
742
- expect(described_class.parse('2013-11-04').end_of_semimonth)
743
- .to eq described_class.parse('2013-11-15')
744
- expect(described_class.parse('2013-11-24').end_of_semimonth)
745
- .to eq described_class.parse('2013-11-30')
746
- expect(described_class.parse('2013-03-01'))
747
- .to be_beginning_of_semimonth
748
- expect(described_class.parse('2013-03-16'))
749
- .to be_beginning_of_semimonth
750
- expect(described_class.parse('2013-04-15'))
751
- .to be_end_of_semimonth
752
- expect(described_class.parse('2013-04-30'))
753
- .to be_end_of_semimonth
754
- end
755
-
756
- it 'knows about biweeks' do
757
- expect(described_class.parse('2013-11-07').beginning_of_biweek)
758
- .to eq described_class.parse('2013-10-28')
759
- expect(described_class.parse('2013-11-07').end_of_biweek)
760
- .to eq described_class.parse('2013-11-10')
761
- expect(described_class.parse('2013-03-04')).to be_beginning_of_biweek
762
- expect(described_class.parse('2013-03-17')).to be_end_of_biweek
763
- expect(described_class.parse('2013-12-30').end_of_biweek)
764
- .to eq described_class.parse('2014-01-05')
765
- expect(described_class.parse('2009-12-30').end_of_biweek)
766
- .to eq described_class.parse('2010-01-03')
767
- expect(described_class.parse('2010-01-03').biweek)
768
- .to eq described_class.parse('2009-12-31').biweek
769
- end
770
-
771
- it 'knows that a Monday is the beginning of the week' do
772
- # A Monday
773
- expect(described_class.parse('2013-11-04')).to be_beginning_of_week
774
- expect(described_class.parse('2013-12-02')).to be_beginning_of_week
775
- # A Sunday
776
- expect(described_class.parse('2013-10-13')).not_to be_beginning_of_week
777
- end
778
-
779
- it 'knows that a Sunday is the end of the week' do
780
- # A Sunday
781
- expect(described_class.parse('2013-11-10')).to be_end_of_week
782
- expect(described_class.parse('2013-12-08')).to be_end_of_week
783
- # A Saturday
784
- expect(described_class.parse('2013-10-19')).not_to be_end_of_week
785
- end
786
-
787
- it 'knows the beginning of non-week chunks' do
788
- expect(described_class.parse('2013-11-04').beginning_of_chunk(:year))
789
- .to eq described_class.parse('2013-01-01')
790
- expect(described_class.parse('2013-11-04').beginning_of_chunk(:half))
791
- .to eq described_class.parse('2013-07-01')
792
- expect(described_class.parse('2013-11-04').beginning_of_chunk(:quarter))
793
- .to eq described_class.parse('2013-10-01')
794
- expect(described_class.parse('2013-12-04').beginning_of_chunk(:bimonth))
795
- .to eq described_class.parse('2013-11-01')
796
- expect(described_class.parse('2013-11-04').beginning_of_chunk(:month))
797
- .to eq described_class.parse('2013-11-01')
798
- expect(described_class.parse('2013-11-04').beginning_of_chunk(:semimonth))
799
- .to eq described_class.parse('2013-11-01')
800
- expect(described_class.parse('2013-11-24').beginning_of_chunk(:semimonth))
801
- .to eq described_class.parse('2013-11-16')
802
- end
803
-
804
- it 'knows the beginning and end of bi-week-based chunks' do
805
- # First Friday to prior Monday
806
- expect(described_class.parse('2013-11-08').beginning_of_chunk(:biweek))
807
- .to eq described_class.parse('2013-10-28')
808
- # Second Wednesday to 2 prior Monday
809
- expect(described_class.parse('2013-11-13').beginning_of_chunk(:biweek))
810
- .to eq described_class.parse('2013-11-11')
811
- end
812
-
813
- it 'knows the beginning and end of week-based chunks' do
814
- # A Friday to prior Monday
815
- expect(described_class.parse('2013-11-08').beginning_of_chunk(:week))
816
- .to eq described_class.parse('2013-11-04')
817
- # A Friday to following Sunday
818
- expect(described_class.parse('2013-11-08').end_of_chunk(:week))
819
- .to eq described_class.parse('2013-11-10')
820
- # A Sunday to prior Monday
821
- expect(described_class.parse('2013-11-10').beginning_of_chunk(:week))
822
- .to eq described_class.parse('2013-11-04')
823
- # A Sunday to itself
824
- expect(described_class.parse('2013-11-10').end_of_chunk(:week))
825
- .to eq described_class.parse('2013-11-10')
826
- expect {
827
- described_class.parse('2013-11-04').beginning_of_chunk(:wek)
828
- }.to raise_error(ArgumentError)
829
- end
830
-
831
- it 'tests the beginning of chunks' do
832
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:year))
833
- .to be false
834
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:year))
835
- .to be true
836
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:half))
837
- .to be false
838
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:half))
839
- .to be true
840
- expect(described_class.parse('2013-07-01').beginning_of_chunk?(:half))
841
- .to be true
842
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:quarter))
843
- .to be false
844
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:quarter))
845
- .to be true
846
- expect(described_class.parse('2013-07-01').beginning_of_chunk?(:quarter))
847
- .to be true
848
- expect(described_class.parse('2013-10-01').beginning_of_chunk?(:quarter))
849
- .to be true
850
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:bimonth))
851
- .to be false
852
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:bimonth))
853
- .to be true
854
- expect(described_class.parse('2013-02-01').beginning_of_chunk?(:bimonth))
855
- .to be false
856
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:month))
857
- .to be false
858
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:month))
859
- .to be true
860
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:semimonth))
861
- .to be false
862
- expect(described_class.parse('2013-01-01').beginning_of_chunk?(:semimonth))
863
- .to be true
864
- expect(described_class.parse('2013-01-16').beginning_of_chunk?(:semimonth))
865
- .to be true
866
- expect(described_class.parse('2013-11-01').beginning_of_chunk?(:week))
867
- .to be false
868
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:week))
869
- .to be true
870
- expect(described_class.parse('2013-11-03').beginning_of_chunk?(:week))
871
- .to be false
872
- expect(described_class.parse('2013-11-01').beginning_of_chunk?(:day))
873
- .to be true
874
- expect(described_class.parse('2013-11-04').beginning_of_chunk?(:day))
875
- .to be true
876
- expect(described_class.parse('2013-11-03').beginning_of_chunk?(:day))
877
- .to be true
878
-
879
- expect {
880
- described_class.parse('2013-11-04').beginning_of_chunk?(:wek)
881
- }.to raise_error(ArgumentError)
882
- end
883
-
884
- it 'tests the end of chunks' do
885
- expect(described_class.parse('2013-11-04').end_of_chunk?(:year))
886
- .to be false
887
- expect(described_class.parse('2013-12-31').end_of_chunk?(:year))
888
- .to be true
889
- expect(described_class.parse('2013-11-04').end_of_chunk?(:half))
890
- .to be false
891
- expect(described_class.parse('2013-12-31').end_of_chunk?(:half))
892
- .to be true
893
- expect(described_class.parse('2013-06-30').end_of_chunk?(:half))
894
- .to be true
895
- expect(described_class.parse('2013-11-04').end_of_chunk?(:quarter))
896
- .to be false
897
- expect(described_class.parse('2013-12-31').end_of_chunk?(:quarter))
898
- .to be true
899
- expect(described_class.parse('2013-06-30').end_of_chunk?(:quarter))
900
- .to be true
901
- expect(described_class.parse('2013-09-30').end_of_chunk?(:quarter))
902
- .to be true
903
- expect(described_class.parse('2013-11-04').end_of_chunk?(:bimonth))
904
- .to be false
905
- expect(described_class.parse('2013-12-31').end_of_chunk?(:bimonth))
906
- .to be true
907
- expect(described_class.parse('2013-02-01').end_of_chunk?(:bimonth))
908
- .to be false
909
- expect(described_class.parse('2013-11-04').end_of_chunk?(:month))
910
- .to be false
911
- expect(described_class.parse('2013-12-31').end_of_chunk?(:month))
912
- .to be true
913
- expect(described_class.parse('2013-11-04').end_of_chunk?(:semimonth))
914
- .to be false
915
- expect(described_class.parse('2013-12-31').end_of_chunk?(:semimonth))
916
- .to be true
917
- expect(described_class.parse('2013-01-15').end_of_chunk?(:semimonth))
918
- .to be true
919
- expect(described_class.parse('2013-11-01').end_of_chunk?(:week))
920
- .to be false
921
- expect(described_class.parse('2013-11-04').end_of_chunk?(:week))
922
- .to be false
923
- expect(described_class.parse('2013-11-09').end_of_chunk?(:week))
924
- .to be false
925
- expect(described_class.parse('2013-11-10').end_of_chunk?(:week))
926
- .to be true
927
- expect(described_class.parse('2013-11-01').end_of_chunk?(:day))
928
- .to be true
929
- expect(described_class.parse('2013-11-04').end_of_chunk?(:day))
930
- .to be true
931
- expect(described_class.parse('2013-11-03').end_of_chunk?(:day))
932
- .to be true
933
-
934
- expect {
935
- described_class.parse('2013-11-04').end_of_chunk?(:wek)
936
- }.to raise_error(ArgumentError)
937
- end
938
-
939
- it 'adds a chunk sym to itself' do
940
- # described_class.today is '2012-07-18'
941
- expect(described_class.today.add_chunk(:year)).to eq(described_class.parse('2013-07-18'))
942
- expect(described_class.today.add_chunk(:half)).to eq(described_class.parse('2013-01-18'))
943
- expect(described_class.today.add_chunk(:quarter)).to eq(described_class.parse('2012-10-18'))
944
- expect(described_class.today.add_chunk(:bimonth)).to eq(described_class.parse('2012-09-18'))
945
- expect(described_class.today.add_chunk(:month)).to eq(described_class.parse('2012-08-18'))
946
- expect(described_class.today.add_chunk(:semimonth)).to eq(described_class.parse('2012-08-03'))
947
- expect(described_class.today.add_chunk(:biweek)).to eq(described_class.parse('2012-08-01'))
948
- expect(described_class.today.add_chunk(:week)).to eq(described_class.parse('2012-07-25'))
949
- expect(described_class.today.add_chunk(:day)).to eq(described_class.parse('2012-07-19'))
950
- expect {
951
- described_class.today.add_chunk(:hour)
952
- }.to raise_error(ArgumentError)
953
- end
954
-
955
- it 'adds n chunks to itself' do
956
- # described_class.today is '2012-07-18'
957
- expect(described_class.today.add_chunk(:year, 5)).to eq(described_class.parse('2017-07-18'))
958
- expect(described_class.today.add_chunk(:half, 5)).to eq(described_class.parse('2015-01-18'))
959
- expect(described_class.today.add_chunk(:quarter, 5)).to eq(described_class.parse('2013-10-18'))
960
- expect(described_class.today.add_chunk(:bimonth, 5)).to eq(described_class.parse('2013-05-18'))
961
- expect(described_class.today.add_chunk(:month, 5)).to eq(described_class.parse('2012-12-18'))
962
- expect(described_class.today.add_chunk(:semimonth, 5)).to eq(described_class.parse('2012-10-03'))
963
- expect(described_class.today.add_chunk(:biweek, 5)).to eq(described_class.parse('2012-09-26'))
964
- expect(described_class.today.add_chunk(:week, 5)).to eq(described_class.parse('2012-08-22'))
965
- expect(described_class.today.add_chunk(:day, 5)).to eq(described_class.parse('2012-07-23'))
966
- expect {
967
- described_class.today.add_chunk(:hour)
968
- }.to raise_error(ArgumentError)
969
- end
970
-
971
- it 'knows the end of chunks' do
972
- expect(described_class.parse('2013-07-04').end_of_chunk(:year))
973
- .to eq described_class.parse('2013-12-31')
974
- expect(described_class.parse('2013-05-04').end_of_chunk(:half))
975
- .to eq described_class.parse('2013-06-30')
976
- expect(described_class.parse('2013-07-04').end_of_chunk(:quarter))
977
- .to eq described_class.parse('2013-09-30')
978
- expect(described_class.parse('2013-12-04').end_of_chunk(:bimonth))
979
- .to eq described_class.parse('2013-12-31')
980
- expect(described_class.parse('2013-07-04').end_of_chunk(:month))
981
- .to eq described_class.parse('2013-07-31')
982
- expect(described_class.parse('2013-11-04').end_of_chunk(:semimonth))
983
- .to eq described_class.parse('2013-11-15')
984
- expect(described_class.parse('2013-11-24').end_of_chunk(:semimonth))
985
- .to eq described_class.parse('2013-11-30')
986
- expect(described_class.parse('2013-11-08').end_of_chunk(:biweek))
987
- .to eq described_class.parse('2013-11-10')
988
- expect(described_class.parse('2013-07-04').end_of_chunk(:week))
989
- .to eq described_class.parse('2013-07-07')
990
- expect {
991
- described_class.parse('2013-11-04').end_of_chunk(:wek)
992
- }.to raise_error(ArgumentError)
993
- end
994
-
995
- it "knows if it's within 6 months of another date" do
996
- # This uses Section 16's logic that one date is "within a
997
- # period of less than six months" of another date only if it
998
- # is within the date six months minus 2 days away from the
999
- # current described_class.
1000
- expect(described_class.parse('2014-01-12'))
1001
- .to be_within_6mos_of(described_class.parse('2014-06-12'))
1002
- expect(described_class.parse('2014-01-12'))
1003
- .not_to be_within_6mos_of(described_class.parse('2014-07-12'))
1004
- expect(described_class.parse('2014-01-12'))
1005
- .not_to be_within_6mos_of(described_class.parse('2014-07-11'))
1006
- expect(described_class.parse('2014-01-12'))
1007
- .to be_within_6mos_of(described_class.parse('2014-07-10'))
1008
- end
1009
-
1010
- it "knows if it's within 6 months of another date if it's near end of month" do
1011
- # This tests for the Jammies Interntional twist where there is no
1012
- # corresponding day in the sixth month before or after the given described_class.
1013
-
1014
- # Looking backward to Feb
1015
- expect(described_class.parse('2014-02-28'))
1016
- .not_to be_within_6mos_of(described_class.parse('2014-08-31'))
1017
- expect(described_class.parse('2014-03-01'))
1018
- .not_to be_within_6mos_of(described_class.parse('2014-08-31'))
1019
- expect(described_class.parse('2014-03-02'))
1020
- .to be_within_6mos_of(described_class.parse('2014-08-31'))
1021
- # Looking forward to Feb
1022
- expect(described_class.parse('2015-02-28'))
1023
- .not_to be_within_6mos_of(described_class.parse('2014-08-31'))
1024
- expect(described_class.parse('2015-02-27'))
1025
- .not_to be_within_6mos_of(described_class.parse('2014-08-31'))
1026
- expect(described_class.parse('2015-02-26'))
1027
- .to be_within_6mos_of(described_class.parse('2014-08-31'))
1028
- # Same in a leap year, backward
1029
- expect(described_class.parse('2012-02-29'))
1030
- .not_to be_within_6mos_of(described_class.parse('2012-08-31'))
1031
- expect(described_class.parse('2012-03-01'))
1032
- .not_to be_within_6mos_of(described_class.parse('2012-08-31'))
1033
- expect(described_class.parse('2012-03-02'))
1034
- .to be_within_6mos_of(described_class.parse('2012-08-31'))
1035
- # Same in a leap year, forward
1036
- expect(described_class.parse('2012-02-29'))
1037
- .not_to be_within_6mos_of(described_class.parse('2011-08-31'))
1038
- expect(described_class.parse('2012-02-28'))
1039
- .not_to be_within_6mos_of(described_class.parse('2011-08-31'))
1040
- expect(described_class.parse('2012-02-27'))
1041
- .to be_within_6mos_of(described_class.parse('2011-08-31'))
1042
-
1043
- # Now try from October to April, as 31->30 test.
1044
- expect(described_class.parse('2012-04-30'))
1045
- .not_to be_within_6mos_of(described_class.parse('2012-10-31'))
1046
- expect(described_class.parse('2012-05-01'))
1047
- .not_to be_within_6mos_of(described_class.parse('2012-10-31'))
1048
- expect(described_class.parse('2012-05-02'))
1049
- .to be_within_6mos_of(described_class.parse('2012-10-31'))
1050
- # And forward
1051
- expect(described_class.parse('2013-04-30'))
1052
- .not_to be_within_6mos_of(described_class.parse('2012-10-31'))
1053
- expect(described_class.parse('2013-04-29'))
1054
- .not_to be_within_6mos_of(described_class.parse('2012-10-31'))
1055
- expect(described_class.parse('2013-04-28'))
1056
- .to be_within_6mos_of(described_class.parse('2012-10-31'))
1057
-
1058
- # It's not symmetrical: notice the second example here is within six
1059
- # months if measured from April, but not if measured from October.
1060
- expect(described_class.parse('2012-10-31'))
1061
- .not_to be_within_6mos_of(described_class.parse('2013-04-30'))
1062
- expect(described_class.parse('2012-10-31'))
1063
- .to be_within_6mos_of(described_class.parse('2013-04-29'))
1064
- expect(described_class.parse('2012-10-31'))
1065
- .to be_within_6mos_of(described_class.parse('2013-04-28'))
1066
- end
1067
- end
1068
-
1069
- describe 'holidays' do
1070
- it 'knows Easter in its year' do
1071
- expect(described_class.today.easter_this_year).to eq(described_class.parse('2012-04-08'))
1072
- expect(Date.easter(2012)).to eq(Date.parse('2012-04-08'))
1073
- expect(described_class.parse('2014-04-20').easter?).to be true
1074
- expect(described_class.parse('2014-03-20').easter?).to be false
1075
- end
1076
-
1077
- it 'knows if its a federal holiday' do
1078
- # Got these from:
1079
- # http://www.opm.gov/policy-data-oversight/snow-dismissal-procedures/federal-holidays/
1080
-
1081
- # For 2011:
1082
- # Friday, December 31, 2010 * New Year's Day
1083
- # Monday, January 17 Birthday of Martin Luther King, Jr.
1084
- # Monday, February 21 ** Washington's Birthday
1085
- # Monday, May 30 Memorial Day
1086
- # Monday, July 4 Independence Day
1087
- # Monday, September 5 Labor Day
1088
- # Monday, October 10 Columbus Day
1089
- # Friday, November 11 Veterans Day
1090
- # Thursday, November 24 Thanksgiving Day
1091
- # Tuesday, December 26 *** Christmas Day
1092
- expect(described_class.parse('2010-12-31')).to be_fed_holiday
1093
- expect(described_class.parse('2011-01-17')).to be_fed_holiday
1094
- expect(described_class.parse('2011-02-21')).to be_fed_holiday
1095
- expect(described_class.parse('2011-05-30')).to be_fed_holiday
1096
- expect(described_class.parse('2011-07-04')).to be_fed_holiday
1097
- expect(described_class.parse('2011-09-05')).to be_fed_holiday
1098
- expect(described_class.parse('2011-10-10')).to be_fed_holiday
1099
- expect(described_class.parse('2011-11-11')).to be_fed_holiday
1100
- expect(described_class.parse('2011-11-24')).to be_fed_holiday
1101
- expect(described_class.parse('2011-12-26')).to be_fed_holiday
1102
-
1103
- # For 2014:
1104
- # Wednesday, January 1 New Year's Day
1105
- # Monday, January 20 Birthday of Martin Luther King, Jr.
1106
- # Monday, February 17 Washington's Birthday
1107
- # Monday, May 26 Memorial Day
1108
- # Friday, July 4 Independence Day
1109
- # Monday, September 1 Labor Day
1110
- # Monday, October 13 Columbus Day
1111
- # Tuesday, November 11 Veterans Day
1112
- # Thursday, November 27 Thanksgiving Day
1113
- # Thursday, December 25 Christmas Day
1114
- expect(described_class.parse('2014-01-01')).to be_fed_holiday
1115
- expect(described_class.parse('2014-01-20')).to be_fed_holiday
1116
- expect(described_class.parse('2014-02-17')).to be_fed_holiday
1117
- expect(described_class.parse('2014-05-26')).to be_fed_holiday
1118
- expect(described_class.parse('2014-07-04')).to be_fed_holiday
1119
- expect(described_class.parse('2014-09-01')).to be_fed_holiday
1120
- expect(described_class.parse('2014-10-13')).to be_fed_holiday
1121
- expect(described_class.parse('2014-11-11')).to be_fed_holiday
1122
- expect(described_class.parse('2014-11-27')).to be_fed_holiday
1123
- expect(described_class.parse('2014-12-25')).to be_fed_holiday
1124
- # Not holidays
1125
- expect(described_class.parse('2014-02-14')).not_to be_fed_holiday
1126
- expect(described_class.parse('2014-04-18')).not_to be_fed_holiday
1127
-
1128
- # For 2017:
1129
- # Monday, January 2 New Year's Day
1130
- # Monday, January 16 Birthday of Martin Luther King, Jr.
1131
- # Monday, February 20 Washington's Birthday
1132
- # Monday, May 29 Memorial Day
1133
- # Tuesday, July 4 Independence Day
1134
- # Monday, September 4 Labor Day
1135
- # Monday, October 9 Columbus Day
1136
- # Friday, November 10 Veterans Day
1137
- # Thursday, November 23 Thanksgiving Day
1138
- # Monday, December 25 Christmas Day
1139
- expect(described_class.parse('2017-01-02')).to be_fed_holiday
1140
- expect(described_class.parse('2017-01-16')).to be_fed_holiday
1141
- expect(described_class.parse('2017-02-20')).to be_fed_holiday
1142
- expect(described_class.parse('2017-05-29')).to be_fed_holiday
1143
- expect(described_class.parse('2017-07-04')).to be_fed_holiday
1144
- expect(described_class.parse('2017-09-04')).to be_fed_holiday
1145
- expect(described_class.parse('2017-10-09')).to be_fed_holiday
1146
- expect(described_class.parse('2017-11-10')).to be_fed_holiday
1147
- expect(described_class.parse('2017-11-23')).to be_fed_holiday
1148
- expect(described_class.parse('2017-12-25')).to be_fed_holiday
1149
-
1150
- # 2003 and 2008 had Christmas on Thur and this apparently makes
1151
- # the following Friday a holiday. I can't find any authority
1152
- # for this, but the government appeared to be shut down on these
1153
- # days.
1154
- expect(described_class.parse('2003-12-26')).to be_fed_holiday
1155
- expect(described_class.parse('2008-12-26')).to be_fed_holiday
1156
-
1157
- # Some non-holidays
1158
- # New Year's Eve is /not/ a holiday unless on a weekend
1159
- # New Year's Eve on a Thursday
1160
- expect(described_class.parse('2015-12-31')).not_to be_fed_holiday
1161
- # New Year's Eve on a Saturday
1162
- expect(described_class.parse('2016-12-31')).to be_fed_holiday
1163
- # Monday
1164
- expect(described_class.parse('2014-11-17')).not_to be_fed_holiday
1165
- # Tuesday
1166
- expect(described_class.parse('2014-11-18')).not_to be_fed_holiday
1167
- # Wednesday
1168
- expect(described_class.parse('2014-11-19')).not_to be_fed_holiday
1169
- # Thursday
1170
- expect(described_class.parse('2014-11-20')).not_to be_fed_holiday
1171
- # Friday
1172
- expect(described_class.parse('2014-11-21')).not_to be_fed_holiday
1173
-
1174
- # Weekends are holidays, regardless
1175
- expect(described_class.parse('2014-11-22')).to be_fed_holiday
1176
- expect(described_class.parse('2014-11-23')).to be_fed_holiday
1177
- end
1178
-
1179
- it 'knows that Juneteenth is a federal holiday from 2021' do
1180
- expect(described_class.parse('2020-06-19')).not_to be_fed_holiday
1181
- # Saturday
1182
- expect(described_class.parse('2021-06-19')).to be_fed_holiday
1183
- # Observed Friday
1184
- expect(described_class.parse('2021-06-18')).to be_fed_holiday
1185
- # Sunday
1186
- expect(described_class.parse('2022-06-19')).to be_fed_holiday
1187
- # Observed Monday
1188
- expect(described_class.parse('2022-06-20')).to be_fed_holiday
1189
- end
1190
-
1191
- it 'knows if its an NYSE holiday' do
1192
- ################# 2014 2015 2016
1193
- # New Year's Day January 1 January 1 January 1
1194
- # Martin Luther King, Jr. Day January 20 January 19 January 18
1195
- # Washington's Birthday February 17 February 16 February 15
1196
- # Good Friday April 18 April 3 March 25
1197
- # Memorial Day May 26 May 25 May 30
1198
- # Independence Day July 4 July 3 July 4
1199
- # Labor Day September 1 September 7 September 5
1200
- # Thanksgiving Day November 27 November 26 November 24
1201
- # Christmas Day December 25 December 25 December 26
1202
- expect(described_class.parse('2014-01-01')).to be_nyse_holiday
1203
- expect(described_class.parse('2014-01-20')).to be_nyse_holiday
1204
- expect(described_class.parse('2014-02-17')).to be_nyse_holiday
1205
- expect(described_class.parse('2014-04-18')).to be_nyse_holiday
1206
- expect(described_class.parse('2014-05-26')).to be_nyse_holiday
1207
- expect(described_class.parse('2014-07-04')).to be_nyse_holiday
1208
- expect(described_class.parse('2014-09-01')).to be_nyse_holiday
1209
- expect(described_class.parse('2014-10-13')).not_to be_nyse_holiday
1210
- expect(described_class.parse('2014-11-11')).not_to be_nyse_holiday
1211
- expect(described_class.parse('2014-11-27')).to be_nyse_holiday
1212
- expect(described_class.parse('2014-12-25')).to be_nyse_holiday
1213
-
1214
- expect(described_class.parse('2015-01-01')).to be_nyse_holiday
1215
- expect(described_class.parse('2015-01-19')).to be_nyse_holiday
1216
- expect(described_class.parse('2015-02-16')).to be_nyse_holiday
1217
- expect(described_class.parse('2015-04-03')).to be_nyse_holiday
1218
- expect(described_class.parse('2015-05-25')).to be_nyse_holiday
1219
- expect(described_class.parse('2015-07-03')).to be_nyse_holiday
1220
- expect(described_class.parse('2015-09-07')).to be_nyse_holiday
1221
- expect(described_class.parse('2015-10-13')).not_to be_nyse_holiday
1222
- expect(described_class.parse('2015-11-11')).not_to be_nyse_holiday
1223
- expect(described_class.parse('2015-11-26')).to be_nyse_holiday
1224
- expect(described_class.parse('2015-12-25')).to be_nyse_holiday
1225
-
1226
- expect(described_class.parse('2016-01-01')).to be_nyse_holiday
1227
- expect(described_class.parse('2016-01-18')).to be_nyse_holiday
1228
- expect(described_class.parse('2016-02-15')).to be_nyse_holiday
1229
- expect(described_class.parse('2016-03-25')).to be_nyse_holiday
1230
- expect(described_class.parse('2016-05-30')).to be_nyse_holiday
1231
- expect(described_class.parse('2016-07-04')).to be_nyse_holiday
1232
- expect(described_class.parse('2016-09-05')).to be_nyse_holiday
1233
- expect(described_class.parse('2016-10-13')).not_to be_nyse_holiday
1234
- expect(described_class.parse('2016-11-11')).not_to be_nyse_holiday
1235
- expect(described_class.parse('2016-11-26')).to be_nyse_holiday
1236
- expect(described_class.parse('2016-12-26')).to be_nyse_holiday
1237
-
1238
- # Some non-holidays
1239
- # Monday
1240
- expect(described_class.parse('2014-11-17')).not_to be_nyse_holiday
1241
- # Tuesday
1242
- expect(described_class.parse('2014-11-18')).not_to be_nyse_holiday
1243
- # Wednesday
1244
- expect(described_class.parse('2014-11-19')).not_to be_nyse_holiday
1245
- # Thursday
1246
- expect(described_class.parse('2014-11-20')).not_to be_nyse_holiday
1247
- # Friday
1248
- expect(described_class.parse('2014-11-21')).not_to be_nyse_holiday
1249
-
1250
- # Weekends are holidays, regardless
1251
- expect(described_class.parse('2014-11-22')).to be_nyse_holiday
1252
- expect(described_class.parse('2014-11-23')).to be_nyse_holiday
1253
-
1254
- # 9-11 Attacks
1255
- expect(described_class.parse('2001-09-11')).to be_nyse_holiday
1256
- expect(described_class.parse('2001-09-14')).to be_nyse_holiday
1257
-
1258
- # 1968 Paperwork Crisis (Closed every Wed unless other holiday in
1259
- # week) from June 12 to December 31, 1968
1260
- expect(described_class.parse('1968-06-12')).to be_nyse_holiday
1261
- expect(described_class.parse('1968-07-03')).not_to be_nyse_holiday
1262
- expect(described_class.parse('1968-08-21')).to be_nyse_holiday
1263
-
1264
- # Hurricane Sandy
1265
- expect(described_class.parse('2012-10-29')).to be_nyse_holiday
1266
- expect(described_class.parse('2012-10-30')).to be_nyse_holiday
1267
-
1268
- # Death of President Ford
1269
- expect(described_class.parse('2007-01-02')).to be_nyse_holiday
1270
- end
1271
-
1272
- it 'knows if it is a Federal workday' do
1273
- # Some holidays
1274
- expect(described_class.parse('2017-02-20')).not_to be_fed_workday
1275
- expect(described_class.parse('2017-05-29')).not_to be_fed_workday
1276
- expect(described_class.parse('2017-07-04')).not_to be_fed_workday
1277
-
1278
- # Some non-holidays
1279
- # Monday
1280
- expect(described_class.parse('2014-11-17')).to be_fed_workday
1281
- # Tuesday
1282
- expect(described_class.parse('2014-11-18')).to be_fed_workday
1283
- # Wednesday
1284
- expect(described_class.parse('2014-11-19')).to be_fed_workday
1285
- # Thursday
1286
- expect(described_class.parse('2014-11-20')).to be_fed_workday
1287
- # Friday
1288
- expect(described_class.parse('2014-11-21')).to be_fed_workday
1289
-
1290
- # Weekends are holidays, regardless
1291
- expect(described_class.parse('2014-11-22')).not_to be_fed_workday
1292
- expect(described_class.parse('2014-11-23')).not_to be_fed_workday
1293
- end
1294
-
1295
- it 'knows if it is an NYSE workday' do
1296
- # Some holidays
1297
- expect(described_class.parse('2016-01-01')).not_to be_nyse_workday
1298
- expect(described_class.parse('2016-01-18')).not_to be_nyse_workday
1299
- expect(described_class.parse('2016-02-15')).not_to be_nyse_workday
1300
-
1301
- # Some non-holidays
1302
- # Monday
1303
- expect(described_class.parse('2014-11-17')).to be_nyse_workday
1304
- # Tuesday
1305
- expect(described_class.parse('2014-11-18')).to be_nyse_workday
1306
- # Wednesday
1307
- expect(described_class.parse('2014-11-19')).to be_nyse_workday
1308
- # Thursday
1309
- expect(described_class.parse('2014-11-20')).to be_nyse_workday
1310
- # Friday
1311
- expect(described_class.parse('2014-11-21')).to be_nyse_workday
1312
-
1313
- # Weekends are holidays, regardless
1314
- expect(described_class.parse('2014-11-22')).not_to be_nyse_workday
1315
- expect(described_class.parse('2014-11-23')).not_to be_nyse_workday
1316
-
1317
- # Alias to trading_day?
1318
- expect(described_class.parse('2014-11-22')).not_to be_trading_day
1319
- expect(described_class.parse('2014-11-23')).not_to be_trading_day
1320
- end
1321
-
1322
- it 'knows the next federal workday' do
1323
- expect(described_class.parse('2015-12-31').next_fed_workday)
1324
- .to eq described_class.parse('2016-01-04')
1325
- expect(described_class.parse('2016-04-20').next_fed_workday)
1326
- .to eq described_class.parse('2016-04-21')
1327
- expect(described_class.parse('2016-04-22').next_fed_workday)
1328
- .to eq described_class.parse('2016-04-25')
1329
- end
1330
-
1331
- it 'knows the prior federal workday' do
1332
- expect(described_class.parse('2016-01-04').prior_fed_workday)
1333
- .to eq described_class.parse('2015-12-31')
1334
- expect(described_class.parse('2016-04-21').prior_fed_workday)
1335
- .to eq described_class.parse('2016-04-20')
1336
- expect(described_class.parse('2016-04-25').prior_fed_workday)
1337
- .to eq described_class.parse('2016-04-22')
1338
- end
1339
-
1340
- it 'knows the next NYSE workday' do
1341
- expect(described_class.parse('2015-12-31').next_nyse_workday)
1342
- .to eq described_class.parse('2016-01-04')
1343
- expect(described_class.parse('2016-04-20').next_nyse_workday)
1344
- .to eq described_class.parse('2016-04-21')
1345
- expect(described_class.parse('2016-04-22').next_nyse_workday)
1346
- .to eq described_class.parse('2016-04-25')
1347
- expect(described_class.parse('2016-04-22').next_trading_day)
1348
- .to eq described_class.parse('2016-04-25')
1349
- end
1350
-
1351
- it 'knows the prior NYSE workday' do
1352
- # The Monday after Easter; go to prior Thur since Good Friday
1353
- # is an NYSE holiday.
1354
- expect(described_class.parse('2014-04-21').prior_nyse_workday)
1355
- .to eq described_class.parse('2014-04-17')
1356
- expect(described_class.parse('2016-01-04').prior_nyse_workday)
1357
- .to eq described_class.parse('2015-12-31')
1358
- expect(described_class.parse('2016-04-21').prior_nyse_workday)
1359
- .to eq described_class.parse('2016-04-20')
1360
- expect(described_class.parse('2016-04-25').prior_nyse_workday)
1361
- .to eq described_class.parse('2016-04-22')
1362
- expect(described_class.parse('2016-04-25').prior_trading_day)
1363
- .to eq described_class.parse('2016-04-22')
1364
- end
1365
-
1366
- it 'can skip until it hits a trading day' do
1367
- # A Wednesday
1368
- expect(described_class.parse('2014-03-26').prior_until_trading_day)
1369
- .to eq(described_class.parse('2014-03-26'))
1370
- # A Sunday
1371
- expect(described_class.parse('2014-03-30').prior_until_trading_day)
1372
- .to eq(described_class.parse('2014-03-28'))
1373
- # A Wednesday
1374
- expect(described_class.parse('2014-03-26').next_until_trading_day)
1375
- .to eq(described_class.parse('2014-03-26'))
1376
- # A Sunday
1377
- expect(described_class.parse('2014-03-30').next_until_trading_day)
1378
- .to eq(described_class.parse('2014-03-31'))
1379
- end
1380
-
1381
- it 'can add n trading days' do
1382
- # Add n trading days
1383
- expect(described_class.parse('2014-03-30').add_trading_days(10))
1384
- .to eq(described_class.parse('2014-04-11'))
1385
- expect(described_class.parse('2014-03-30').add_trading_days(-10))
1386
- .to eq(described_class.parse('2014-03-17'))
1387
- end
1388
- end
1389
- end
1390
- end