partial-date 1.1.10 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.markdown +6 -1
- data/lib/partial-date/date.rb +39 -21
- data/lib/partial-date/version.rb +1 -1
- metadata +2 -2
data/ChangeLog.markdown
CHANGED
data/lib/partial-date/date.rb
CHANGED
@@ -39,7 +39,14 @@ module PartialDate
|
|
39
39
|
# end
|
40
40
|
# end
|
41
41
|
|
42
|
-
FORMATS = {
|
42
|
+
FORMATS = {
|
43
|
+
:default => "%Y-%m-%d",
|
44
|
+
:short => "%d %m %Y",
|
45
|
+
:medium => "%d %b %Y",
|
46
|
+
:long => "%d %B %Y",
|
47
|
+
:number => "%Y%m%d"
|
48
|
+
}
|
49
|
+
|
43
50
|
FORMAT_METHODS = {
|
44
51
|
"%Y" => lambda { |d| (d.year != 0) ? d.year.to_s.rjust(4, '0') : ""},
|
45
52
|
"%m" => lambda { |d| (d.month != 0) ? d.month.to_s.rjust(2, '0') : "" },
|
@@ -53,12 +60,28 @@ module PartialDate
|
|
53
60
|
MONTH_NAMES = %w[January, February, March, April, May, June, July, August, September, October, November, December]
|
54
61
|
ABBR_MONTH_NAMES = %w[Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
|
55
62
|
|
56
|
-
# Public: A class for handling partial
|
57
|
-
#
|
63
|
+
# Public: A class for handling partial dates. Year (including negative
|
64
|
+
# values), month and day are optional although month must be set before
|
65
|
+
# day. Partial dates are stored internally in a single integer bit store
|
66
|
+
# using bitmask and bitwise operations to set and get year, month, and
|
67
|
+
# day values.
|
68
|
+
#
|
69
|
+
# A numerical (human readable) date value can be retrieved via
|
70
|
+
# the d.value accessor.
|
71
|
+
#
|
72
|
+
# Use d.value to get and set the numerical date value as well as for storing
|
73
|
+
# dates as a single value in a persistence store.
|
58
74
|
#
|
59
75
|
# Examples
|
60
76
|
#
|
61
77
|
# date = PartialDate::Date.new
|
78
|
+
# date.value = 20121201
|
79
|
+
# # => 2012-12-01
|
80
|
+
#
|
81
|
+
# date = PartialDate::Date.load 20121201
|
82
|
+
# # => 2012-12-01
|
83
|
+
#
|
84
|
+
# date = PartialDate::Date.new
|
62
85
|
# date.year = 2012
|
63
86
|
# date.month = 12
|
64
87
|
# date.day = 1
|
@@ -71,9 +94,6 @@ module PartialDate
|
|
71
94
|
include Comparable
|
72
95
|
|
73
96
|
# Public: Readonly accessor for the raw bit integer date value.
|
74
|
-
# This allows us to perform our <=> comparions against this
|
75
|
-
# value instead of comparing year, month and day, or date.value
|
76
|
-
# which requires multiplication to calculate.
|
77
97
|
#
|
78
98
|
# Returns the single Integer backing store with 'bits' flipped
|
79
99
|
# for year, month and day.
|
@@ -109,7 +129,7 @@ module PartialDate
|
|
109
129
|
#
|
110
130
|
# date = PartialDate::Date.load 201212201
|
111
131
|
# date.value
|
112
|
-
# # =>
|
132
|
+
# # => 20121200
|
113
133
|
# date.year
|
114
134
|
# # => 2012
|
115
135
|
# date.month
|
@@ -243,9 +263,10 @@ module PartialDate
|
|
243
263
|
self.class.get_day(@bits)
|
244
264
|
end
|
245
265
|
|
246
|
-
# Public: Returns a formatted string representation of
|
247
|
-
#
|
248
|
-
# %Y - Year with century
|
266
|
+
# Public: Returns a formatted string representation of date. A subset of
|
267
|
+
# date formatters have been implemented including:
|
268
|
+
# %Y - Year with century
|
269
|
+
# (can be negative, and will be padded to 4 digits at least)
|
249
270
|
# -0001, 0000, 1995, 2009, 14292, etc.
|
250
271
|
# %m - Month of the year, zero-padded (01..12)
|
251
272
|
# %B - The full month name ('January')
|
@@ -262,14 +283,12 @@ module PartialDate
|
|
262
283
|
# Returns string representation of date.
|
263
284
|
def to_s(format = :default)
|
264
285
|
format = FORMATS[format] if format.is_a?(Symbol)
|
265
|
-
|
266
286
|
s = format.dup
|
267
|
-
|
268
287
|
n = b = 0
|
269
288
|
a = 1
|
270
289
|
while n < s.length
|
271
|
-
if s[n] == "%"
|
272
|
-
t = FORMAT_METHODS[s[n..n+1]].call( self )
|
290
|
+
if s[n] == "%" && FORMAT_METHODS.include?(s[n..n+1])
|
291
|
+
t = FORMAT_METHODS[s[n..n+1]].call( self )
|
273
292
|
if t.length == 0
|
274
293
|
if n >= 0 && n < s.length - 2
|
275
294
|
a = a + 1 if s[n+2] =~ REMOVALS
|
@@ -287,7 +306,7 @@ module PartialDate
|
|
287
306
|
end
|
288
307
|
s
|
289
308
|
end
|
290
|
-
|
309
|
+
|
291
310
|
# Here for the moment for benchmark comparisons
|
292
311
|
def old_to_s(format = :default)
|
293
312
|
format = FORMATS[format] if format.is_a?(Symbol)
|
@@ -305,12 +324,11 @@ module PartialDate
|
|
305
324
|
lead_trim = (year != 0 && format.lstrip.start_with?("%Y")) ? /\A[\/\,\s]+/ : /\A[\/\,\-\s]+/
|
306
325
|
result = result.gsub(lead_trim, '').gsub(/\s\s/, ' ').gsub(/[\/\-\,]([\/\-\,])/, '\1').gsub(/[\/\,\-\s]+\z/, '')
|
307
326
|
end
|
308
|
-
|
309
|
-
# Public: Spaceship operator for date comparisons. Comparisons
|
310
|
-
#
|
311
|
-
#
|
312
|
-
#
|
313
|
-
# calculate.
|
327
|
+
|
328
|
+
# Public: Spaceship operator for date comparisons. Comparisons are made
|
329
|
+
# by cascading down from year, to month to day. This should be faster
|
330
|
+
# than passing to self.value <=> other_date.value since the integer value
|
331
|
+
# attribute requires multiplication to calculate.
|
314
332
|
#
|
315
333
|
# Returns -1, 1, or 0
|
316
334
|
def <=>(other_date)
|
data/lib/partial-date/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: partial-date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubygems-tasks
|