date_time_precision 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/date_time_precision.gemspec +1 -1
- data/lib/date_time_precision/format/string.rb +8 -0
- data/lib/date_time_precision/lib.rb +28 -9
- data/lib/date_time_precision/patch/1.8.7/date_time.rb +0 -4
- data/lib/date_time_precision/patch/1.9.3/date_time.rb +0 -4
- data/lib/date_time_precision/patch/common/date_time.rb +13 -0
- data/lib/date_time_precision/version.rb +1 -1
- data/spec/active_support_spec.rb +36 -0
- data/spec/date_time_precision_spec.rb +61 -24
- data/spec/format_spec.rb +5 -5
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MTdkODVjOTc0YWNkOGI1ZTNiMTU5MmVlODBmNTY5MTFiMzMwZDE4ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjIwYmMzODc3M2I3OGQ1NDFmYTkwZWZlYTkyN2RiZDE2MmEwYWU4ZQ==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDE5ZTIwZTljOTQ2MTgyMGI1MDI3ZjdhN2I3ZWY4Zjc4MGQ2ZDJmZjE0YTk0
|
10
|
+
MDI4OWI3YzlkM2IxMTM2N2VhNjFlYzI0MmI0YzI4NDk5NjljMzE1OWNlZjMw
|
11
|
+
MThkZWI5YTE4OGFlODU0YWIyMWMwNTc5NjEzNjYxMmIyNDc3MGE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OWQ4NmFhNjVhNWE0MGIzY2U4MWEwMjcwZDI3NDQ2Y2M2MzJiMjdkMzdlYWU4
|
14
|
+
ZTUxZDNkYWE2MTUyMjgzM2I5MmI4MTg5MDkxYjE5YTQ2MTkxMTk3YjMxNTll
|
15
|
+
MjhhZTkyYzRkOTMzZWE2ZGI0NzNlNWU2YmRkZThmODMyYWVjZjI=
|
data/date_time_precision.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.license = 'MIT'
|
18
18
|
|
19
19
|
gem.add_development_dependency 'rake'
|
20
|
-
gem.add_development_dependency 'rspec'
|
20
|
+
gem.add_development_dependency 'rspec', '> 3'
|
21
21
|
gem.add_development_dependency 'appraisal'
|
22
22
|
|
23
23
|
gem.add_development_dependency 'activesupport'
|
@@ -9,6 +9,10 @@ Date::DATE_FORMATS[:long] = lambda do |date|
|
|
9
9
|
date.strftime("%B %Y")
|
10
10
|
when date.precision == DateTimePrecision::YEAR
|
11
11
|
date.strftime("%Y")
|
12
|
+
when date.precision == DateTimePrecision::DECADE
|
13
|
+
"#{date.decade}s"
|
14
|
+
when date.precision == DateTimePrecision::CENTURY
|
15
|
+
"#{date.century}s"
|
12
16
|
when date.month? && date.day?
|
13
17
|
date.strftime("%B %e")
|
14
18
|
when date.month?
|
@@ -30,6 +34,10 @@ Time::DATE_FORMATS[:long] = lambda do |time|
|
|
30
34
|
time.strftime("%B %Y")
|
31
35
|
when time.precision == DateTimePrecision::YEAR
|
32
36
|
time.strftime("%Y")
|
37
|
+
when time.precision == DateTimePrecision::DECADE
|
38
|
+
"#{time.decade}s"
|
39
|
+
when time.precision == DateTimePrecision::CENTURY
|
40
|
+
"#{time.century}s"
|
33
41
|
when time.month? && time.day?
|
34
42
|
time.strftime("%B %e")
|
35
43
|
when time.month?
|
@@ -2,7 +2,7 @@ require 'date'
|
|
2
2
|
require 'time'
|
3
3
|
|
4
4
|
if defined?(ActiveSupport)
|
5
|
-
['active_support/core_ext/date', 'active_support/core_ext/datetime', 'active_support/core_ext/time', 'active_support/time', 'active_support/time_with_zone'].each do |f|
|
5
|
+
['active_support/core_ext/date', 'active_support/core_ext/datetime', 'active_support/core_ext/date_time', 'active_support/core_ext/time', 'active_support/time', 'active_support/time_with_zone'].each do |f|
|
6
6
|
begin
|
7
7
|
require f
|
8
8
|
rescue LoadError; end
|
@@ -28,13 +28,15 @@ module DateTimePrecision
|
|
28
28
|
|
29
29
|
unless constants.include? "NONE"
|
30
30
|
USEC = FRAC = 7
|
31
|
-
SEC
|
32
|
-
MIN
|
33
|
-
HOUR
|
34
|
-
DAY
|
35
|
-
MONTH
|
36
|
-
YEAR
|
37
|
-
|
31
|
+
SEC = 6
|
32
|
+
MIN = 5
|
33
|
+
HOUR = 4
|
34
|
+
DAY = 3
|
35
|
+
MONTH = 2
|
36
|
+
YEAR = 1
|
37
|
+
DECADE = 0.5
|
38
|
+
CENTURY = 0.25
|
39
|
+
NONE = 0
|
38
40
|
|
39
41
|
# Default values for y,m,d,h,m,s,frac
|
40
42
|
NEW_DEFAULTS = [-4712,1,1,0,0,0,0]
|
@@ -50,6 +52,8 @@ module DateTimePrecision
|
|
50
52
|
]
|
51
53
|
|
52
54
|
DATE_ATTRIBUTE_PRECISIONS = {
|
55
|
+
:century => CENTURY,
|
56
|
+
:decade => DECADE,
|
53
57
|
:year => YEAR,
|
54
58
|
:mon => MONTH,
|
55
59
|
:day => DAY,
|
@@ -91,6 +95,10 @@ module DateTimePrecision
|
|
91
95
|
MONTH
|
92
96
|
when val[:year], val[:y]
|
93
97
|
YEAR
|
98
|
+
when val[:decade]
|
99
|
+
DECADE
|
100
|
+
when val[:century]
|
101
|
+
CENTURY
|
94
102
|
else
|
95
103
|
NONE
|
96
104
|
end
|
@@ -121,6 +129,17 @@ module DateTimePrecision
|
|
121
129
|
def normalize_new_args(args)
|
122
130
|
self.class.normalize_new_args(args)
|
123
131
|
end
|
132
|
+
|
133
|
+
def decade
|
134
|
+
year_with_bce_adjustment = (self.year > 0) ? self.year : self.year + 10
|
135
|
+
(year_with_bce_adjustment - year_with_bce_adjustment % 10)
|
136
|
+
end
|
137
|
+
|
138
|
+
def century
|
139
|
+
year_with_bce_adjustment = (self.year > 0) ? self.year : self.year + 100
|
140
|
+
year_with_bce_adjustment - year_with_bce_adjustment % 100
|
141
|
+
end
|
142
|
+
|
124
143
|
protected :normalize_new_args
|
125
144
|
|
126
145
|
module ClassMethods
|
@@ -197,7 +216,7 @@ module DateTimePrecision
|
|
197
216
|
protected :#{attribute_name}_set=
|
198
217
|
EOM
|
199
218
|
end
|
200
|
-
|
219
|
+
|
201
220
|
base.class_eval <<-EOM, __FILE__, __LINE__
|
202
221
|
def attributes_set(*vals)
|
203
222
|
#{DATE_ATTRIBUTES.map{|attribute| "@#{attribute}_set"}.join(', ')} = *(vals.flatten.map{|v| !!v})
|
@@ -19,4 +19,17 @@ class DateTime < Date
|
|
19
19
|
(sec_fraction.to_f*1_000_000).to_i
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
if method_defined?(:utc)
|
24
|
+
alias_method :utc_orig, :utc
|
25
|
+
def utc
|
26
|
+
dt = utc_orig
|
27
|
+
dt.precision = precision
|
28
|
+
dt
|
29
|
+
end
|
30
|
+
else
|
31
|
+
def utc
|
32
|
+
self.to_time.utc.to_datetime
|
33
|
+
end
|
34
|
+
end
|
22
35
|
end
|
data/spec/active_support_spec.rb
CHANGED
@@ -68,5 +68,41 @@ describe DateTimePrecision, 'Conversions' do
|
|
68
68
|
expect(Time.mktime(nil, 5, 13).to_s(:long)).to eq("May 13")
|
69
69
|
expect(Time.mktime(nil, 6).to_s(:long)).to eq("June")
|
70
70
|
end
|
71
|
+
|
72
|
+
it 'takes precision into account for decade and century' do
|
73
|
+
decade_date = Date.new(1853)
|
74
|
+
decade_date.precision = DateTimePrecision::DECADE
|
75
|
+
expect(decade_date.to_s(:long)).to eq("1850s")
|
76
|
+
|
77
|
+
century_date = Date.new(1853)
|
78
|
+
century_date.precision = DateTimePrecision::CENTURY
|
79
|
+
expect(century_date.to_s(:long)).to eq("1800s")
|
80
|
+
|
81
|
+
decade_date_time = DateTime.new(1853)
|
82
|
+
decade_date_time.precision = DateTimePrecision::DECADE
|
83
|
+
expect(decade_date_time.to_s(:long)).to eq("1850s")
|
84
|
+
|
85
|
+
century_date_time = DateTime.new(1853)
|
86
|
+
century_date_time.precision = DateTimePrecision::CENTURY
|
87
|
+
expect(century_date_time.to_s(:long)).to eq("1800s")
|
88
|
+
|
89
|
+
decade_time = Time.mktime(1853)
|
90
|
+
decade_time.precision = DateTimePrecision::DECADE
|
91
|
+
expect(decade_time.to_s(:long)).to eq("1850s")
|
92
|
+
|
93
|
+
century_time = Time.mktime(1853)
|
94
|
+
century_time.precision = DateTimePrecision::CENTURY
|
95
|
+
expect(century_time.to_s(:long)).to eq("1800s")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'UTC' do
|
100
|
+
it 'does not crash and burn when calling #utc' do
|
101
|
+
expect { DateTime.now.utc }.to_not raise_error
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'preserves precision when converting to UTC' do
|
105
|
+
expect(DateTime.new(2000, 5).utc.precision).to eq DateTimePrecision::MONTH
|
106
|
+
end
|
71
107
|
end
|
72
108
|
end
|
@@ -6,11 +6,11 @@ describe DateTimePrecision do
|
|
6
6
|
it 'has no precision for unspecified date' do
|
7
7
|
d = Date.new
|
8
8
|
expect(d.precision).to eq(DateTimePrecision::NONE)
|
9
|
-
expect(d.year?).to
|
9
|
+
expect(d.year?).to be false
|
10
10
|
|
11
11
|
dt = DateTime.new
|
12
12
|
expect(dt.precision).to eq(DateTimePrecision::NONE)
|
13
|
-
expect(dt.year?).to
|
13
|
+
expect(dt.year?).to be false
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'has no precision for nil values' do
|
@@ -20,45 +20,47 @@ describe DateTimePrecision do
|
|
20
20
|
it 'has year precision when only year is supplied' do
|
21
21
|
d = Date.new(1982)
|
22
22
|
expect(d.precision).to eq(DateTimePrecision::YEAR)
|
23
|
-
expect(d.year?).to
|
24
|
-
expect(d.month?).to
|
25
|
-
expect(d.day?).to
|
23
|
+
expect(d.year?).to be true
|
24
|
+
expect(d.month?).to be false
|
25
|
+
expect(d.day?).to be false
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'has month precision when year and month are supplied' do
|
29
29
|
d = Date.new(1982, 11)
|
30
30
|
expect(d.precision).to eq(DateTimePrecision::MONTH)
|
31
|
-
expect(d.year?).to
|
32
|
-
expect(d.month?).to
|
33
|
-
expect(d.day?).to
|
31
|
+
expect(d.year?).to be true
|
32
|
+
expect(d.month?).to be true
|
33
|
+
expect(d.day?).to be false
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'has day precision when year, month, and day are passed in' do
|
37
37
|
dt = DateTime.new(1987,10,19)
|
38
38
|
expect(dt.precision).to eq(DateTimePrecision::DAY)
|
39
|
-
expect(dt.year?).to
|
40
|
-
expect(dt.month?).to
|
41
|
-
expect(dt.day?).to
|
42
|
-
expect(dt.hour?).to
|
39
|
+
expect(dt.year?).to be true
|
40
|
+
expect(dt.month?).to be true
|
41
|
+
expect(dt.day?).to be true
|
42
|
+
expect(dt.hour?).to be false
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'has hour precision' do
|
46
46
|
dt = DateTime.new(1970, 1, 2, 3)
|
47
47
|
expect(dt.precision).to eq(DateTimePrecision::HOUR)
|
48
|
-
expect(dt.year?).to
|
49
|
-
expect(dt.month?).to
|
50
|
-
expect(dt.day?).to
|
51
|
-
expect(dt.hour?).to
|
52
|
-
expect(dt.min?).to
|
48
|
+
expect(dt.year?).to be true
|
49
|
+
expect(dt.month?).to be true
|
50
|
+
expect(dt.day?).to be true
|
51
|
+
expect(dt.hour?).to be true
|
52
|
+
expect(dt.min?).to be false
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'tracks which attributes were explicitly set separately from precision' do
|
56
56
|
[Date.new(nil, 11, 12), DateTime.new(nil, 10, 11, nil), Time.mktime(nil, 12, 13, nil, 14)].each do |d|
|
57
|
-
expect(d.
|
58
|
-
expect(d.
|
59
|
-
expect(d.
|
60
|
-
expect(d.
|
61
|
-
expect(d.
|
57
|
+
expect(d.decade?).to be false
|
58
|
+
expect(d.century?).to be false
|
59
|
+
expect(d.year?).to be false
|
60
|
+
expect(d.month?).to be true
|
61
|
+
expect(d.day?).to be true
|
62
|
+
expect(d.hour?).to be false
|
63
|
+
expect(d.min?).to be true if d.is_a? Time
|
62
64
|
expect(d.precision).to eq(DateTimePrecision::NONE)
|
63
65
|
end
|
64
66
|
end
|
@@ -172,9 +174,44 @@ describe DateTimePrecision do
|
|
172
174
|
it 'should match when differing only in day precision' do
|
173
175
|
d1 = Date.new(2001,3,2)
|
174
176
|
d2 = Date.new(2001,3)
|
175
|
-
expect(d1.partial_match?(d2)).to
|
176
|
-
expect(d2.partial_match?(d1)).to
|
177
|
+
expect(d1.partial_match?(d2)).to be true
|
178
|
+
expect(d2.partial_match?(d1)).to be true
|
177
179
|
end
|
178
180
|
end
|
179
181
|
|
182
|
+
context 'Decades and Centuries' do
|
183
|
+
it 'should have the proper precision when outputting decades or centuries' do
|
184
|
+
no_date = Date.new
|
185
|
+
full_date = Date.new(1853,10,10)
|
186
|
+
century_date_time = DateTime.new(1853)
|
187
|
+
century_date_time.precision = DateTimePrecision::CENTURY
|
188
|
+
decade_time = Time.mktime(1853)
|
189
|
+
decade_time.precision = DateTimePrecision::DECADE
|
190
|
+
|
191
|
+
expect(full_date.decade).to eq(1850)
|
192
|
+
expect(full_date.century).to eq(1800)
|
193
|
+
|
194
|
+
expect(decade_time.decade).to eq(1850)
|
195
|
+
expect(decade_time.century).to eq(1800)
|
196
|
+
|
197
|
+
expect(century_date_time.decade).to eq(1850)
|
198
|
+
expect(century_date_time.century).to eq(1800)
|
199
|
+
|
200
|
+
expect(no_date.decade?).to be false
|
201
|
+
expect(full_date.decade?).to be true
|
202
|
+
expect(decade_time.decade?).to be true
|
203
|
+
expect(century_date_time.decade?).to be false
|
204
|
+
|
205
|
+
expect(no_date.century?).to be false
|
206
|
+
expect(full_date.century?).to be true
|
207
|
+
expect(decade_time.century?).to be true
|
208
|
+
expect(century_date_time.century?).to be true
|
209
|
+
end
|
210
|
+
it 'properly handles negative years' do
|
211
|
+
date_bce = Date.new(-531, 10, 5)
|
212
|
+
expect(date_bce.decade).to eq(-530)
|
213
|
+
expect(date_bce.century).to eq(-500)
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
180
217
|
end
|
data/spec/format_spec.rb
CHANGED
@@ -220,15 +220,15 @@ describe DateTimePrecision do
|
|
220
220
|
[:date, :datetime, :time].each do |klass|
|
221
221
|
it "accepts month and day without year when converting to a #{klass}" do
|
222
222
|
date = { :month => 5, :day => 18, :min => 48 }.send("to_#{klass}")
|
223
|
-
expect(date.year?).to
|
224
|
-
expect(date.month?).to
|
223
|
+
expect(date.year?).to be false
|
224
|
+
expect(date.month?).to be true
|
225
225
|
expect(date.month).to eq(5)
|
226
|
-
expect(date.day?).to
|
226
|
+
expect(date.day?).to be true
|
227
227
|
expect(date.day).to eq(18)
|
228
|
-
expect(date.hour?).to
|
228
|
+
expect(date.hour?).to be false
|
229
229
|
|
230
230
|
unless klass == :date
|
231
|
-
expect(date.min?).to
|
231
|
+
expect(date.min?).to be true
|
232
232
|
expect(date.min).to eq(48)
|
233
233
|
end
|
234
234
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_time_precision
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Butler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ! '>'
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ! '>'
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: appraisal
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ! '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: json
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ! '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ! '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
@@ -115,8 +115,8 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
-
-
|
119
|
-
-
|
118
|
+
- .gitignore
|
119
|
+
- .travis.yml
|
120
120
|
- Appraisals
|
121
121
|
- Gemfile
|
122
122
|
- LICENSE
|
@@ -167,12 +167,12 @@ require_paths:
|
|
167
167
|
- lib
|
168
168
|
required_ruby_version: !ruby/object:Gem::Requirement
|
169
169
|
requirements:
|
170
|
-
- -
|
170
|
+
- - ! '>='
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: '0'
|
173
173
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
174
|
requirements:
|
175
|
-
- -
|
175
|
+
- - ! '>='
|
176
176
|
- !ruby/object:Gem::Version
|
177
177
|
version: '0'
|
178
178
|
requirements: []
|