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.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -4
- data/README.org +38 -409
- data/bin/console +1 -3
- data/lib/fat_core/all.rb +0 -1
- data/lib/fat_core/string.rb +0 -20
- data/lib/fat_core/version.rb +3 -3
- data/lib/fat_core.rb +0 -2
- data/spec/lib/range_spec.rb +91 -90
- data/spec/lib/string_spec.rb +0 -9
- metadata +4 -8
- data/bin/easter +0 -45
- data/lib/fat_core/date.rb +0 -1897
- data/spec/lib/date_spec.rb +0 -1390
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 771688ad5fad07ba4d5bc75d160a73176832478a745b47627322aa69551b70a8
|
|
4
|
+
data.tar.gz: 22fe86a39c44b1e1745c92738c5b30ff691d32bfb151c9e85e7c7aa311c07bc1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 462a89d63a05b8a6157d3ac28851aae0429e870a9bb348bf966a4e506baf9b98b8237a5e4b03e82577a128bcd3a736d4a32f138c1f16dda3af1fec8d116f05a0
|
|
7
|
+
data.tar.gz: d8ce29fc89c5a20cc730cd28f43cf2f5b359bb65f2bc9949fd41a7adaef4d56be84542008e339be5314d2181a9227b3f1597a65e6da9df47854912442db5c1bb
|
data/.rubocop.yml
CHANGED
data/README.org
CHANGED
|
@@ -1,10 +1,45 @@
|
|
|
1
|
+
#+TITLE: FatCore Guide
|
|
2
|
+
#+OPTIONS: toc:5
|
|
3
|
+
#+PROPERTY: header-args:ruby :colnames no :hlines yes :exports both :wrap example :ruby ruby
|
|
4
|
+
#+PROPERTY: header-args:sh :exports code
|
|
5
|
+
|
|
1
6
|
[[https://travis-ci.org/ddoherty03/fat_core.svg?branch=master]]
|
|
2
7
|
|
|
8
|
+
* README Setup Do First for Code Blocks
|
|
9
|
+
Run this block before all others to ensure that we are reading the libraries
|
|
10
|
+
from the source directory.
|
|
11
|
+
|
|
12
|
+
#+begin_src ruby :results output
|
|
13
|
+
puts "Current directory: #{Dir.pwd}"
|
|
14
|
+
puts "Ruby LOADPATH:"
|
|
15
|
+
$:.unshift("./lib") unless $:[0] == './lib'
|
|
16
|
+
$:[0..10].each { |d| puts d }
|
|
17
|
+
puts "..."
|
|
18
|
+
require 'fat_core/all' # => true
|
|
19
|
+
#+end_src
|
|
20
|
+
|
|
21
|
+
#+RESULTS:
|
|
22
|
+
#+begin_example
|
|
23
|
+
Current directory: /home/ded/src/fat_core
|
|
24
|
+
Ruby LOADPATH:
|
|
25
|
+
./lib
|
|
26
|
+
/home/ded/.rbenv/rbenv.d/exec/gem-rehash
|
|
27
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/site_ruby/3.4.0
|
|
28
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/site_ruby/3.4.0/x86_64-linux
|
|
29
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/site_ruby
|
|
30
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/vendor_ruby/3.4.0
|
|
31
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/vendor_ruby/3.4.0/x86_64-linux
|
|
32
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/vendor_ruby
|
|
33
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/3.4.0
|
|
34
|
+
/home/ded/.rbenv/versions/3.4.1/lib/ruby/3.4.0/x86_64-linux
|
|
35
|
+
...
|
|
36
|
+
#+end_example
|
|
37
|
+
|
|
38
|
+
|
|
3
39
|
* FatCore
|
|
4
40
|
|
|
5
41
|
~fat-core~ is a simple gem to collect core extensions and a few new classes
|
|
6
|
-
that I find useful in multiple projects.
|
|
7
|
-
Date class to make it more useful in financial applications.
|
|
42
|
+
that I find useful in multiple projects.
|
|
8
43
|
|
|
9
44
|
** Installation
|
|
10
45
|
|
|
@@ -33,7 +68,6 @@ You can extend classes individually by requiring the corresponding file:
|
|
|
33
68
|
#+begin_SRC ruby
|
|
34
69
|
require 'fat_core/array'
|
|
35
70
|
require 'fat_core/bigdecimal'
|
|
36
|
-
require 'fat_core/date'
|
|
37
71
|
require 'fat_core/enumerable'
|
|
38
72
|
require 'fat_core/hash'
|
|
39
73
|
require 'fat_core/kernel'
|
|
@@ -43,7 +77,6 @@ You can extend classes individually by requiring the corresponding file:
|
|
|
43
77
|
require 'fat_core/symbol'
|
|
44
78
|
#+end_SRC
|
|
45
79
|
|
|
46
|
-
|
|
47
80
|
Or, you can require them all:
|
|
48
81
|
|
|
49
82
|
#+begin_SRC ruby
|
|
@@ -53,410 +86,6 @@ Or, you can require them all:
|
|
|
53
86
|
Many of these have little that is of general interest, but there are a few
|
|
54
87
|
goodies.
|
|
55
88
|
|
|
56
|
-
*** Date
|
|
57
|
-
**** Constants
|
|
58
|
-
~FatCore~ adds two date constants to the ~Date~ class, Date::BOT and
|
|
59
|
-
Date::EOT. These represent the earliest and latest dates of practical
|
|
60
|
-
commercial interest. The exact values are rather arbitrary, but they prove
|
|
61
|
-
useful in date ranges, for example. They are defined as:
|
|
62
|
-
|
|
63
|
-
- ~Date::BOT~ :: January 1, 1900
|
|
64
|
-
- ~Date::EOT~ :: December 31, 3000
|
|
65
|
-
- ~Date::FEDERAL_DECREED_HOLIDAYS~ :: an Array of dates declared as non-work
|
|
66
|
-
days for federal employees by presidential proclamation
|
|
67
|
-
- ~Date::PRESIDENTIAL_FUNERALS~ :: an Array of dates of presidential funerals,
|
|
68
|
-
which are observed with a closing of most federal agencies
|
|
69
|
-
|
|
70
|
-
**** Ensure
|
|
71
|
-
The ~Date.ensure~ class method tries to convert its argument to a ~Date~
|
|
72
|
-
object by (1) applying the ~#to_date~ method or (2) applying the ~Date.parse~
|
|
73
|
-
method to a String. This is handy when you want to define a method that takes
|
|
74
|
-
a date argument but want the caller to be able to supply anything that can
|
|
75
|
-
reasonably be converted to a ~Date~:
|
|
76
|
-
|
|
77
|
-
#+begin_src ruby
|
|
78
|
-
$:.unshift("~/src/fat_core/lib")
|
|
79
|
-
require 'fat_core/date' # => true
|
|
80
|
-
|
|
81
|
-
def tomorow_tomorrow(arg)
|
|
82
|
-
from = Date.ensure(arg) # => ArgumentError: cannot convert class 'Array' to a Date or DateTime
|
|
83
|
-
from + 2.days # => Mon, 03 Jun 2024, Wed, 16 Oct 2024 05:47:30 -0500, Sun, 03 Mar 2024
|
|
84
|
-
end # => :tomorow_tomorrow
|
|
85
|
-
|
|
86
|
-
tomorow_tomorrow('June 1') # => Mon, 03 Jun 2024
|
|
87
|
-
tomorow_tomorrow(Time.now) # => Wed, 16 Oct 2024 05:47:30 -0500
|
|
88
|
-
# But it's only as good as Date.parse!
|
|
89
|
-
tomorow_tomorrow('Ides of March') # => Sun, 03 Mar 2024
|
|
90
|
-
|
|
91
|
-
tomorow_tomorrow([])
|
|
92
|
-
# =>
|
|
93
|
-
|
|
94
|
-
# ~> ArgumentError
|
|
95
|
-
# ~> cannot convert class 'Array' to a Date or DateTime
|
|
96
|
-
# ~>
|
|
97
|
-
# ~> /home/ded/src/fat_core/lib/fat_core/date.rb:1849:in `ensure_date'
|
|
98
|
-
# ~> /home/ded/src/fat_core/lib/fat_core/date.rb:1863:in `ensure'
|
|
99
|
-
# ~> /tmp/seeing_is_believing_temp_dir20241014-1457038-xj4k5x/program.rb:5:in `tomorow_tomorrow'
|
|
100
|
-
# ~> /tmp/seeing_is_believing_temp_dir20241014-1457038-xj4k5x/program.rb:14:in `<main>'
|
|
101
|
-
#+end_src
|
|
102
|
-
|
|
103
|
-
**** Formatting
|
|
104
|
-
|
|
105
|
-
~FatCore~ provides some concise methods for printing string versions of dates
|
|
106
|
-
that are often useful:
|
|
107
|
-
|
|
108
|
-
#+begin_SRC ruby :results output :wrap example :exports both
|
|
109
|
-
require 'fat_core/date'
|
|
110
|
-
d = Date.parse('1957-09-22')
|
|
111
|
-
puts "ISO: #{d.iso}"
|
|
112
|
-
puts "All Numbers: #{d.num}"
|
|
113
|
-
puts "Emacs Org Mode Inactive: #{d.org}"
|
|
114
|
-
puts "Emacs Org Mode Active: #{d.org(true)}"
|
|
115
|
-
puts "LaTeX: #{d.tex_quote}"
|
|
116
|
-
puts "English: #{d.eng}"
|
|
117
|
-
puts "American: #{d.american}"
|
|
118
|
-
#+end_SRC
|
|
119
|
-
|
|
120
|
-
#+begin_example
|
|
121
|
-
ISO: 1957-09-22
|
|
122
|
-
All Numbers: 19570922
|
|
123
|
-
Emacs Org Mode Inactive: [1957-09-22 Sun]
|
|
124
|
-
Emacs Org Mode Active: <1957-09-22 Sun>
|
|
125
|
-
LaTeX: 1957--09--22
|
|
126
|
-
English: September 22, 1957
|
|
127
|
-
American: 9/22/1957
|
|
128
|
-
#+end_example
|
|
129
|
-
|
|
130
|
-
Most of these are self-explanatory, but a couple are not. The ~#org~ method
|
|
131
|
-
formats a date as an Emacs org-mode timestamp, by default an inactive
|
|
132
|
-
timestamp that does not show up in the org agenda, but can be made active with
|
|
133
|
-
the optional parameter set to a truthy value. See
|
|
134
|
-
[[https://orgmode.org/manual/Timestamps.html#Timestamps]].
|
|
135
|
-
|
|
136
|
-
The ~#tex_quote~ method formats the date in iso form but using TeX's
|
|
137
|
-
convention of using en-dashes to separate the components.
|
|
138
|
-
|
|
139
|
-
**** Chunks
|
|
140
|
-
|
|
141
|
-
Many of the methods provided by ~FatCore~ deal with various calendar periods
|
|
142
|
-
that are less common than those provided by the Ruby Standard Library or gems
|
|
143
|
-
such as ~active_support~. This documentation refers to these calendar periods
|
|
144
|
-
as "chunks", and they are the following:
|
|
145
|
-
|
|
146
|
-
- year,
|
|
147
|
-
- half,
|
|
148
|
-
- quarter,
|
|
149
|
-
- bimonth,
|
|
150
|
-
- month,
|
|
151
|
-
- semimonth,
|
|
152
|
-
- biweek,
|
|
153
|
-
- week, and
|
|
154
|
-
- day
|
|
155
|
-
|
|
156
|
-
~FatCore~ provides methods that query whether the date falls on the beginning
|
|
157
|
-
or end of each of these chunks:
|
|
158
|
-
|
|
159
|
-
#+begin_SRC ruby :wrap example :exports both
|
|
160
|
-
require 'fat_core/date'
|
|
161
|
-
|
|
162
|
-
tab = []
|
|
163
|
-
d = Date.parse('2017-06-30')
|
|
164
|
-
%i[beginning end].each do |side|
|
|
165
|
-
%i(year half quarter bimonth month semimonth biweek week).each do |chunk|
|
|
166
|
-
meth = "#{side}_of_#{chunk}?".to_sym
|
|
167
|
-
tab << [d.iso, meth.to_s, "#{d.send(meth)}"]
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
tab
|
|
171
|
-
#+end_SRC
|
|
172
|
-
|
|
173
|
-
#+RESULTS:
|
|
174
|
-
#+begin_example
|
|
175
|
-
| 2017-06-30 | beginning_of_year? | false |
|
|
176
|
-
| 2017-06-30 | beginning_of_half? | false |
|
|
177
|
-
| 2017-06-30 | beginning_of_quarter? | false |
|
|
178
|
-
| 2017-06-30 | beginning_of_bimonth? | false |
|
|
179
|
-
| 2017-06-30 | beginning_of_month? | false |
|
|
180
|
-
| 2017-06-30 | beginning_of_semimonth? | false |
|
|
181
|
-
| 2017-06-30 | beginning_of_biweek? | false |
|
|
182
|
-
| 2017-06-30 | beginning_of_week? | false |
|
|
183
|
-
| 2017-06-30 | end_of_year? | false |
|
|
184
|
-
| 2017-06-30 | end_of_half? | true |
|
|
185
|
-
| 2017-06-30 | end_of_quarter? | true |
|
|
186
|
-
| 2017-06-30 | end_of_bimonth? | true |
|
|
187
|
-
| 2017-06-30 | end_of_month? | true |
|
|
188
|
-
| 2017-06-30 | end_of_semimonth? | true |
|
|
189
|
-
| 2017-06-30 | end_of_biweek? | false |
|
|
190
|
-
| 2017-06-30 | end_of_week? | false |
|
|
191
|
-
#+end_example
|
|
192
|
-
|
|
193
|
-
It also provides corresponding methods that return the date at the beginning
|
|
194
|
-
or end of the calendar chunk, starting at the given date:
|
|
195
|
-
|
|
196
|
-
#+begin_SRC ruby :wrap example :exports both
|
|
197
|
-
require 'fat_core/date'
|
|
198
|
-
|
|
199
|
-
tab = []
|
|
200
|
-
d = Date.parse('2017-04-21')
|
|
201
|
-
%i[beginning end].each do |side|
|
|
202
|
-
%i(year half quarter bimonth month semimonth biweek week ).each do |chunk|
|
|
203
|
-
meth = "#{side}_of_#{chunk}".to_sym
|
|
204
|
-
tab << [d.iso, "d.#{meth}", "#{d.send(meth)}"]
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
tab
|
|
208
|
-
#+end_SRC
|
|
209
|
-
|
|
210
|
-
#+RESULTS:
|
|
211
|
-
#+begin_example
|
|
212
|
-
| 2017-04-21 | d.beginning_of_year | 2017-01-01 |
|
|
213
|
-
| 2017-04-21 | d.beginning_of_half | 2017-01-01 |
|
|
214
|
-
| 2017-04-21 | d.beginning_of_quarter | 2017-04-01 |
|
|
215
|
-
| 2017-04-21 | d.beginning_of_bimonth | 2017-03-01 |
|
|
216
|
-
| 2017-04-21 | d.beginning_of_month | 2017-04-01 |
|
|
217
|
-
| 2017-04-21 | d.beginning_of_semimonth | 2017-04-16 |
|
|
218
|
-
| 2017-04-21 | d.beginning_of_biweek | 2017-04-10 |
|
|
219
|
-
| 2017-04-21 | d.beginning_of_week | 2017-04-17 |
|
|
220
|
-
| 2017-04-21 | d.end_of_year | 2017-12-31 |
|
|
221
|
-
| 2017-04-21 | d.end_of_half | 2017-06-30 |
|
|
222
|
-
| 2017-04-21 | d.end_of_quarter | 2017-06-30 |
|
|
223
|
-
| 2017-04-21 | d.end_of_bimonth | 2017-04-30 |
|
|
224
|
-
| 2017-04-21 | d.end_of_month | 2017-04-30 |
|
|
225
|
-
| 2017-04-21 | d.end_of_semimonth | 2017-04-30 |
|
|
226
|
-
| 2017-04-21 | d.end_of_biweek | 2017-04-23 |
|
|
227
|
-
| 2017-04-21 | d.end_of_week | 2017-04-23 |
|
|
228
|
-
#+end_example
|
|
229
|
-
|
|
230
|
-
You can query which numerical half, quarter, etc. that a given date falls in:
|
|
231
|
-
|
|
232
|
-
#+begin_SRC ruby :exports both :wrap example
|
|
233
|
-
require 'fat_core/date'
|
|
234
|
-
|
|
235
|
-
tab = []
|
|
236
|
-
%i(year half quarter bimonth month semimonth biweek week ).each do |chunk|
|
|
237
|
-
d = Date.parse('2017-04-21') + rand(100)
|
|
238
|
-
meth = "#{chunk}".to_sym
|
|
239
|
-
tab << [d.iso, "d.#{meth}", "in #{chunk} number #{d.send(meth)}"]
|
|
240
|
-
end
|
|
241
|
-
tab
|
|
242
|
-
#+end_SRC
|
|
243
|
-
|
|
244
|
-
#+RESULTS:
|
|
245
|
-
#+begin_example
|
|
246
|
-
| 2017-07-05 | d.year | in year number 2017 |
|
|
247
|
-
| 2017-06-03 | d.half | in half number 1 |
|
|
248
|
-
| 2017-05-30 | d.quarter | in quarter number 2 |
|
|
249
|
-
| 2017-07-08 | d.bimonth | in bimonth number 4 |
|
|
250
|
-
| 2017-06-28 | d.month | in month number 6 |
|
|
251
|
-
| 2017-05-14 | d.semimonth | in semimonth number 9 |
|
|
252
|
-
| 2017-07-25 | d.biweek | in biweek number 15 |
|
|
253
|
-
| 2017-06-19 | d.week | in week number 25 |
|
|
254
|
-
#+end_example
|
|
255
|
-
|
|
256
|
-
**** Parsing
|
|
257
|
-
|
|
258
|
-
~FatCore~ also adds some convenience methods for parsing strings as ~Date~
|
|
259
|
-
objects.
|
|
260
|
-
|
|
261
|
-
***** American Dates
|
|
262
|
-
Americans often write dates in the form M/d/Y, and the normal parse method
|
|
263
|
-
will parse such a string as d/M/Y, often resulting in invalid date errors.
|
|
264
|
-
~FatCore~ adds the specialty parsing method, ~Date.parse_american~ to handle
|
|
265
|
-
such strings.
|
|
266
|
-
|
|
267
|
-
#+begin_SRC ruby :results output :exports both :wrap example
|
|
268
|
-
require 'fat_core/date'
|
|
269
|
-
|
|
270
|
-
begin
|
|
271
|
-
ss = '9/22/1957'
|
|
272
|
-
Date.parse(ss)
|
|
273
|
-
rescue Date::Error => ex
|
|
274
|
-
puts "Date.parse('#{ss}') raises #{ex.class} (#{ex}), but"
|
|
275
|
-
puts "Date.parse_american('#{ss}') => #{Date.parse_american(ss)}"
|
|
276
|
-
end
|
|
277
|
-
#+end_SRC
|
|
278
|
-
|
|
279
|
-
#+RESULTS:
|
|
280
|
-
#+begin_example
|
|
281
|
-
Date.parse('9/22/1957') raises Date::Error (invalid date), but
|
|
282
|
-
Date.parse_american('9/22/1957') => 1957-09-22
|
|
283
|
-
#+end_example
|
|
284
|
-
|
|
285
|
-
***** Date Specs
|
|
286
|
-
It is often desirable to get the first or last date of a specified time
|
|
287
|
-
period. For this ~FatCore~ provides the ~parse_spec~ method that takes a
|
|
288
|
-
string and an optional ~spec_type~ parameter of either ~:from~, indicating
|
|
289
|
-
that the first date of the period should be returned or ~:to~, indicating that
|
|
290
|
-
the last date of the period should be returned.
|
|
291
|
-
|
|
292
|
-
This method supports a rich set of ways to specify periods of time:
|
|
293
|
-
|
|
294
|
-
- YYYY-MM-DD :: returns a single day as the time period,
|
|
295
|
-
- YYYY-MM :: returns the specified month, beginning or end
|
|
296
|
-
- YYYY :: returns the specified year, beginning or end
|
|
297
|
-
- YYYY-ddd :: returns the ddd'th day of the specified year, beginning or end
|
|
298
|
-
- MM :: returns the specified month of the current year, beginning or end
|
|
299
|
-
- MM-DD :: returns the specified day of the specified month in the current
|
|
300
|
-
year, beginning or end,
|
|
301
|
-
- YYYY-Wnn or YYYY-nnW :: returns the nn'th commercial week of the given year
|
|
302
|
-
according to the ISO 8601 standard, in which the week containing the first
|
|
303
|
-
Thursday of the year counts as the first commercial week, even if that week
|
|
304
|
-
started in the prior calendar year,
|
|
305
|
-
- Wnn or nnW :: returns the nn'th commercial week of the current year,
|
|
306
|
-
- YYYY-1H or YYYY-2H :: returns the specified half year for the given year,
|
|
307
|
-
- 1H or 2H :: returns the specified half year for the current year,
|
|
308
|
-
- YYYY-1Q, YYYY-2Q, etc :: returns the calendar quarter for the given year,
|
|
309
|
-
- 1Q, 2Q, etc :: returns the calendar quarter for the current year,
|
|
310
|
-
- YYYY-MM-I or YYYY-MM-II :: returns the semi-month for the given month and
|
|
311
|
-
year, where the first semi-month always runs from the 1st to the 15th and
|
|
312
|
-
the second semi-month always runs from the 16th to the last day of the given
|
|
313
|
-
month, regardless of the number of days in the month,
|
|
314
|
-
- YYYY-MM-i or YYYY-MM-ii up to YYYY-MM-vi :: returns the given week within
|
|
315
|
-
the month, including any partial weeks,
|
|
316
|
-
- MM-i or MM-ii up to MM-vi :: returns the given week within the month of the
|
|
317
|
-
current year, including any partial weeks,
|
|
318
|
-
- i or ii up to vi :: returns the given week within the current month of the current
|
|
319
|
-
year, including any partial weeks,
|
|
320
|
-
- YYYY-MM-nSu up to YYYY-MM-nSa :: returns the single date that is the n'th
|
|
321
|
-
Sunday, Monday, etc., in the given month using the first two letters of the
|
|
322
|
-
English names for the days of the week,
|
|
323
|
-
- MM-nSu up to MM-nSa :: returns the single date that is the n'th Sunday,
|
|
324
|
-
Monday, etc., in the given month of the current year using the first two
|
|
325
|
-
letters of the English names for the days of the week,
|
|
326
|
-
- nSu up to nSa :: returns the single date that is the n'th Sunday, Monday,
|
|
327
|
-
etc., in the current month of the current year using the first two letters
|
|
328
|
-
of the English names for the days of the week,
|
|
329
|
-
- YYYY-nnn :: is the nnn'th day of the given year, exactly three digits needed,
|
|
330
|
-
- nnn :: is the nnn'th day of the current year, exactly three digits needed,
|
|
331
|
-
- YYYY-E :: returns the single date of Easter in the Western church for the
|
|
332
|
-
given year,
|
|
333
|
-
- E :: returns the single date of Easter in the Western church for the current
|
|
334
|
-
year,
|
|
335
|
-
- YYYY-E-n or YYYY-E+n :: returns the single date that falls n days before (-)
|
|
336
|
-
or after (+) Easter in the Western church for the given year,
|
|
337
|
-
- E-n or E+n :: returns the single date that falls n days before (-) or after
|
|
338
|
-
(+) Easter in the Western church for the current year,
|
|
339
|
-
- yesterday or yesteryear or lastday or last_year, etc :: the relative
|
|
340
|
-
prefixes, 'last' or 'yester' prepended to any chunk name returns the period
|
|
341
|
-
named by the chunk that precedes today's date.
|
|
342
|
-
- today or toyear or this-year or thissemimonth, etc :: the relative prefixes,
|
|
343
|
-
'to' or 'this' prepended to any chunk name returns the period named by
|
|
344
|
-
the chunk that contains today's date.
|
|
345
|
-
- nextday or nextyear or next-year or nextsemimonth, etc :: the relative
|
|
346
|
-
prefixes, 'next' prepended to any chunk name returns the period named by the
|
|
347
|
-
chunk that follows today's date. As a special case, 'tomorrow' is treated as
|
|
348
|
-
equivalent to 'nextday'.
|
|
349
|
-
- forever :: returns the period Date::BOT to Date::EOT, which, for financial
|
|
350
|
-
applications is meant to stand in for eternity.
|
|
351
|
-
- never :: returns nil, representing no date.
|
|
352
|
-
|
|
353
|
-
Some things to note with respect to ~Date.parse_spec~:
|
|
354
|
-
|
|
355
|
-
1. The second argument should be either ~:from~ or ~:to~, but it defaults to
|
|
356
|
-
~:from~. If it is ~:from~, ~parse_spec~ returns the first date of the
|
|
357
|
-
specified period; if it is ~:to~, it returns the last date of the specified
|
|
358
|
-
period. When the "period" resolves to a single day, both arguments return
|
|
359
|
-
the same date, so ~parse_spec('2024-E', :from)~ and ~parse_spec('2024-E',
|
|
360
|
-
:to)~ both result in March 31, 2024.
|
|
361
|
-
2. Where relevant, ~parse_spec~ accepts letters of either upper or lower case:
|
|
362
|
-
so 2024-1Q can be written 2024-1q and 'yesteryear' can be written
|
|
363
|
-
'YeSterYeaR', and likewise for all components of the spec using letters.
|
|
364
|
-
3. Date components can be separated with either a hyphen, as in the examples
|
|
365
|
-
above, or with a '/' as is common. Thus, 2024-11-09 can also be
|
|
366
|
-
2024/11/09, or indeed, 2024/11-09 or 2024-11/09.
|
|
367
|
-
4. The prefixes for relative periods can be separated from the period name by
|
|
368
|
-
a hyphen, and underscore, or by nothing at all. Thus, yester-day,
|
|
369
|
-
yester_day, and yesterday are all acceptable. Clearly neologisms such as
|
|
370
|
-
'yestermonth' are quaint, but not harmful.
|
|
371
|
-
5. On the other hand, to get a day-of-year spec right, you must use exactly 3
|
|
372
|
-
digits: 2024-011 is the 11th day of 2024, but 2024-11 is November of 2024.
|
|
373
|
-
|
|
374
|
-
**** Holidays and Workdays
|
|
375
|
-
One of the original motivations for this library was to provide an easy way to
|
|
376
|
-
determine whether a given date is a federal holiday in the United States or,
|
|
377
|
-
nearly but not quite the same, a non-trading day on the New York Stock
|
|
378
|
-
Exchange. To that end, ~FatCore~ provides the following methods:
|
|
379
|
-
|
|
380
|
-
- Date#weekend? -- is this date on a weekend?
|
|
381
|
-
- Date#weekday? -- is this date on a week day?
|
|
382
|
-
- Date#easter_this_year -- the date of Easter in the Date's year
|
|
383
|
-
|
|
384
|
-
Methods concerning Federal holidays:
|
|
385
|
-
|
|
386
|
-
- Date#fed_holiday? -- is this date a Federal holiday? It knows about
|
|
387
|
-
obscurities such as holidays decreed by past Presidents, dates of
|
|
388
|
-
Presidential funerals, and the Federal rule for when holidays fall on a
|
|
389
|
-
weekend, whether it is moved to the prior Friday or the following Monday.
|
|
390
|
-
- Date#fed_workday? -- is it a date when the Federal government is open?,
|
|
391
|
-
inverse of Date#fed_holiday?
|
|
392
|
-
- Date#add_fed_workdays(n) -- n Federal workdays following (or preceding if n
|
|
393
|
-
negative) this date,
|
|
394
|
-
- Date#next_fed_workday -- the next Federal workday following this date,
|
|
395
|
-
- Date#prior_fed_workday -- the previous Federal workday before this date,
|
|
396
|
-
- Date#next_until_fed_workday -- starting with this date, move forward until
|
|
397
|
-
we hit a Federal workday
|
|
398
|
-
- Date#prior_until_fed_workday -- starting with this date, move back until
|
|
399
|
-
we hit a Federal workday
|
|
400
|
-
|
|
401
|
-
And we have similar methods for "holidays" or non-trading days on the NYSE:
|
|
402
|
-
|
|
403
|
-
- Date#nyse_holiday? -- is this date a NYSE holiday?
|
|
404
|
-
- Date#nyse_workday? -- is it a date when the NYSE is open for trading?,
|
|
405
|
-
inverse of Date#nyse_holiday?
|
|
406
|
-
- Date#add_nyse_workdays(n) -- n NYSE workdays following (or preceding if n
|
|
407
|
-
negative) this date,
|
|
408
|
-
- Date#next_nyse_workday -- the next NYSE workday following this date,
|
|
409
|
-
- Date#prior_nyse_workday -- the previous NYSE workday before this date,
|
|
410
|
-
- Date#next_until_nyse_~~workday -- starting with this date, move forward until
|
|
411
|
-
we hit a NYSE workday
|
|
412
|
-
- Date#prior_until_nyse_workday -- starting with this date, move back until
|
|
413
|
-
we hit a Federal workday
|
|
414
|
-
|
|
415
|
-
**** Ordinal Weekdays in Month
|
|
416
|
-
It is often useful to find the 1st, 2nd, etc, Sunday, Monday, etc. in a given
|
|
417
|
-
month. ~FatCore~ provides the class method ~Date.nth_wday_in_year_month(nth,
|
|
418
|
-
wday, year, month)~ to return such dates. The first parameter can be
|
|
419
|
-
negative, which will count from the end of the month.
|
|
420
|
-
|
|
421
|
-
**** Easter
|
|
422
|
-
The ~Date~ class extension adds two methods for determining whether a given
|
|
423
|
-
date is a US federal holiday as defined by federal law, including such things
|
|
424
|
-
as federal holidays established by executive decree:
|
|
425
|
-
|
|
426
|
-
#+begin_SRC ruby
|
|
427
|
-
require 'fat_core/date'
|
|
428
|
-
Date.parse('2014-05-18').fed_holiday? => true # It's a weekend
|
|
429
|
-
Date.parse('2014-01-01').fed_holiday? => true # It's New Years
|
|
430
|
-
#+end_SRC
|
|
431
|
-
|
|
432
|
-
Likewise, days on which the NYSE is closed can be gotten with:
|
|
433
|
-
|
|
434
|
-
#+begin_SRC ruby
|
|
435
|
-
Date.parse('2014-04-18').nyse_holiday? => true # It's Good Friday
|
|
436
|
-
#+end_SRC
|
|
437
|
-
|
|
438
|
-
Conversely, ~Date#fed_workday?~ and ~Date#nyse_workday?~ return true if the
|
|
439
|
-
federal government and the NYSE respectively are open for business on those
|
|
440
|
-
days.
|
|
441
|
-
|
|
442
|
-
In addition, the Date class, as extended by FatCore, adds ~#next_<chunk>~
|
|
443
|
-
methods for calendar periods in addition to those provided by the core Date
|
|
444
|
-
class: ~#next_half~, ~#next_quarter~, ~#next_bimonth~, and ~#next_semimonth~,
|
|
445
|
-
~#next_biweek~. There are also ~#prior_<chunk>~ variants of these, as well as
|
|
446
|
-
methods for finding the end and beginning of all these periods (e.g.,
|
|
447
|
-
~#beginning_of_bimonth~) and for querying whether a Date is at the beginning or
|
|
448
|
-
end of these periods (e.g., ~#beginning_of_bimonth?~, ~#end_of_bimonth?~, etc.).
|
|
449
|
-
|
|
450
|
-
FatCore also provides convenience formatting methods, such as ~Date#iso~ for
|
|
451
|
-
quickly converting a Date to a string of the form 'YYYY-MM-DD', ~Date#org~ for
|
|
452
|
-
formatting a Date as an Emacs org-mode timestamp, and several others.
|
|
453
|
-
|
|
454
|
-
Finally, it provides a ~#parse_spec~ method for parsing a string, typically
|
|
455
|
-
provided by a user, allowing all the period chunks to be conveniently and
|
|
456
|
-
tersely specified by a user. For example, the string '2Q' will be parsed as the
|
|
457
|
-
second calendar quarter of the current year, while '2014-3Q' will be parsed as
|
|
458
|
-
the third quarter of the year 2014.
|
|
459
|
-
|
|
460
89
|
*** Range
|
|
461
90
|
|
|
462
91
|
You can also extend the Range class with several useful methods that emphasize
|
|
@@ -477,7 +106,7 @@ coverage on one Range by an Array of other Ranges:
|
|
|
477
106
|
(0..12).gaps([(0..2), (5..7), (10..12)]) => [(3..4), (8..9)]
|
|
478
107
|
#+end_SRC
|
|
479
108
|
|
|
480
|
-
|
|
109
|
+
*** Enumerable
|
|
481
110
|
FatCore::Enumerable extends Enumerable with the ~#each_with_flags~ method that
|
|
482
111
|
yields the elements of the Enumerable but also yields two booleans, ~first~ and
|
|
483
112
|
~last~ that are set to true on respectively, the first and last element of the
|
data/bin/console
CHANGED
|
@@ -6,8 +6,6 @@ require 'pry'
|
|
|
6
6
|
|
|
7
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
8
|
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
-
@
|
|
10
|
-
@dd2 = Date.parse('2016-01-30')
|
|
11
|
-
@dd3 = Date.parse('2016-01-29')
|
|
9
|
+
@hh = { a: 1, b: 2, c: 3, d: 2 }
|
|
12
10
|
|
|
13
11
|
Pry.start
|
data/lib/fat_core/all.rb
CHANGED
data/lib/fat_core/string.rb
CHANGED
|
@@ -93,26 +93,6 @@ module FatCore
|
|
|
93
93
|
r.gsub('XzXzXcbXzXzX', '\\}')
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
# Convert a string representing a date with only digits, hyphens, or slashes
|
|
97
|
-
# to a Date.
|
|
98
|
-
#
|
|
99
|
-
# @example
|
|
100
|
-
# "20090923".as_date.iso -> "2009-09-23"
|
|
101
|
-
# "2009/09/23".as_date.iso -> "2009-09-23"
|
|
102
|
-
# "2009-09-23".as_date.iso -> "2009-09-23"
|
|
103
|
-
# "2009-9-23".as_date.iso -> "2009-09-23"
|
|
104
|
-
#
|
|
105
|
-
# @return [Date] the translated Date
|
|
106
|
-
def as_date
|
|
107
|
-
if self =~ %r{(?<yr>\d\d\d\d)[-/]?(?<mo>\d\d?)[-/]?(?<dy>\d\d?)}
|
|
108
|
-
::Date.new(
|
|
109
|
-
Regexp.last_match[:yr].to_i,
|
|
110
|
-
Regexp.last_match[:mo].to_i,
|
|
111
|
-
Regexp.last_match[:dy].to_i,
|
|
112
|
-
)
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
|
|
116
96
|
UPPERS = ('A'..'Z').to_a
|
|
117
97
|
REGEXP_META_CHARACTERS = "\\$()*+.<>?[]^{|}".chars.freeze
|
|
118
98
|
|
data/lib/fat_core/version.rb
CHANGED