date_time_precision 0.5.3 → 0.6.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/README.md +17 -8
- data/Rakefile +1 -1
- data/gem_tasks/rspec.rake +18 -10
- data/lib/date_time_precision/format/string.rb +31 -0
- data/lib/date_time_precision/lib.rb +12 -4
- data/lib/date_time_precision/version.rb +1 -1
- data/spec/active_support_spec.rb +65 -0
- data/spec/{date_time_precision/compatibility_spec.rb → compatibility_spec.rb} +0 -0
- data/spec/date_time_precision_spec.rb +177 -0
- data/spec/{date_time_precision/date_time_precision_spec.rb → format_spec.rb} +1 -172
- metadata +11 -8
- data/spec/date_time_precision/active_support_spec.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9e306c98b83f8d976c0f1499b17b3fe57503797
|
4
|
+
data.tar.gz: cf7744903d1a1ea1c3aa8fef6bc98896853bf27a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ec71030888d088622a1a30e92767beb730e10a4168da758b7ac623e188c9d995d9daf5b83b5c402a1106e681bf7be70dd49630e0d870963327aca95cd439500
|
7
|
+
data.tar.gz: b832c62848bae15b69ed22b1c7111d97916458b7f84f0ae8884d00ec35a260e3e95ca482bd1c6166d28f6aecb564ad3c20b988b2e002874facf9c5ed75299ee4
|
data/README.md
CHANGED
@@ -51,6 +51,14 @@ The currently supported formats are Hash, JSON, and ISO 8601.
|
|
51
51
|
|
52
52
|
Examples:
|
53
53
|
|
54
|
+
```ruby
|
55
|
+
# Requires Active Support
|
56
|
+
require 'date_time_precision/format/string'
|
57
|
+
|
58
|
+
Date.new(2000, 5).to_s(:long)
|
59
|
+
# => "May 2000"
|
60
|
+
```
|
61
|
+
|
54
62
|
```ruby
|
55
63
|
require 'date_time_precision/format/hash'
|
56
64
|
|
@@ -100,7 +108,7 @@ modifications to how the core Date/Time classes work. (For example, by allowing
|
|
100
108
|
|
101
109
|
So far, the following gems are on the compatibility watch list:
|
102
110
|
|
103
|
-
- [
|
111
|
+
- [.] [ActiveSupport](https://github.com/rails/rails/tree/master/activesupport)
|
104
112
|
- [x] [Virtus](https://github.com/solnic/virtus)
|
105
113
|
- [x] [Coercible](https://github.com/solnic/coercible)
|
106
114
|
- [ ] [Chronic](https://github.com/mojombo/chronic)
|
@@ -123,14 +131,15 @@ Or install it yourself as:
|
|
123
131
|
|
124
132
|
$ gem install date_time_precision
|
125
133
|
|
126
|
-
## Wishlist
|
134
|
+
## Wishlist / TO-DO
|
127
135
|
|
128
|
-
- [x]
|
129
|
-
- [x]
|
130
|
-
- [x]
|
131
|
-
- [.]
|
132
|
-
- [ ]
|
133
|
-
- [
|
136
|
+
- [x] Time::mktime
|
137
|
+
- [x] Time::utc and Time#utc
|
138
|
+
- [x] Time::local
|
139
|
+
- [.] Correct generation and parsing of the ISO 8601 format, which supports partial dates and times
|
140
|
+
- [ ] The various time zone methods (partially done)
|
141
|
+
- [.] Easy string formatting based on precision
|
142
|
+
- [ ] [ActiveSupport::TimeWithZone](http://apidock.com/rails/ActiveSupport/TimeWithZone)
|
134
143
|
|
135
144
|
## Contributing
|
136
145
|
|
data/Rakefile
CHANGED
data/gem_tasks/rspec.rake
CHANGED
@@ -3,17 +3,25 @@ require 'rspec/core/rake_task'
|
|
3
3
|
desc "Run RSpec"
|
4
4
|
RSpec::Core::RakeTask.new do |t|
|
5
5
|
t.verbose = true
|
6
|
-
t.pattern = "spec/
|
6
|
+
t.pattern = "spec/date_time_precision_spec.rb"
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
namespace :spec do
|
10
|
+
desc "Run format specs"
|
11
|
+
RSpec::Core::RakeTask.new(:format) do |t|
|
12
|
+
t.verbose = true
|
13
|
+
t.pattern = "spec/format_spec.rb"
|
14
|
+
end
|
14
15
|
|
15
|
-
desc "Run
|
16
|
-
RSpec::Core::RakeTask.new(:
|
17
|
-
|
18
|
-
|
16
|
+
desc "Run ActiveSupport specs"
|
17
|
+
RSpec::Core::RakeTask.new(:active_support) do |t|
|
18
|
+
t.verbose = true
|
19
|
+
t.pattern = "spec/active_support_spec.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Run gem compatibility specs"
|
23
|
+
RSpec::Core::RakeTask.new(:compatibility) do |t|
|
24
|
+
t.verbose = true
|
25
|
+
t.pattern = "spec/compatibility_spec.rb"
|
26
|
+
end
|
19
27
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support/core_ext/date/conversions'
|
2
|
+
|
3
|
+
Date::DATE_FORMATS[:long] = lambda do |date|
|
4
|
+
case
|
5
|
+
when date.precision.nil?, date.precision >= DateTimePrecision::DAY
|
6
|
+
date.strftime("%B %e, %Y")
|
7
|
+
when date.precision == DateTimePrecision::MONTH
|
8
|
+
date.strftime("%B %Y")
|
9
|
+
when date.precision == DateTimePrecision::YEAR
|
10
|
+
date.strftime("%Y")
|
11
|
+
else
|
12
|
+
""
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Time::DATE_FORMATS[:long] = lambda do |time|
|
17
|
+
case
|
18
|
+
when time.precision.nil?, time.precision >= DateTimePrecision::MIN
|
19
|
+
time.strftime("%B %d, %Y %H:%M")
|
20
|
+
#when time.precision == DateTimePrecision::HOUR
|
21
|
+
# time.strftime("%B %d, %Y %H")
|
22
|
+
when time.precision >= DateTimePrecision::DAY
|
23
|
+
time.strftime("%B %d, %Y")
|
24
|
+
when time.precision == DateTimePrecision::MONTH
|
25
|
+
time.strftime("%B %Y")
|
26
|
+
when time.precision == DateTimePrecision::YEAR
|
27
|
+
time.strftime("%Y")
|
28
|
+
else
|
29
|
+
""
|
30
|
+
end
|
31
|
+
end
|
@@ -147,17 +147,25 @@ module DateTimePrecision
|
|
147
147
|
[:to_date, :to_time, :to_datetime].each do |conversion_method|
|
148
148
|
orig = :"orig_#{conversion_method}"
|
149
149
|
if base.method_defined?(conversion_method) && !base.instance_methods(false).map(&:to_sym).include?(orig)
|
150
|
-
base.
|
150
|
+
base.class_eval do
|
151
151
|
alias_method orig, conversion_method
|
152
|
-
define_method(conversion_method)
|
152
|
+
define_method(conversion_method) do
|
153
153
|
d = send(orig)
|
154
154
|
d.precision = [self.precision, d.class::MAX_PRECISION].min
|
155
155
|
DATE_ATTRIBUTES.each do |attribute|
|
156
156
|
d.instance_variable_set(:"@#{attribute}_set", self.instance_variable_get(:"@#{attribute}_set"))
|
157
157
|
end
|
158
158
|
d
|
159
|
-
|
160
|
-
|
159
|
+
end
|
160
|
+
end
|
161
|
+
else
|
162
|
+
# Define our own conversion methods by converting to hash first
|
163
|
+
require 'date_time_precision/format/hash'
|
164
|
+
base.class_eval do
|
165
|
+
define_method(conversion_method) do
|
166
|
+
to_h.send(conversion_method)
|
167
|
+
end
|
168
|
+
end
|
161
169
|
end
|
162
170
|
end
|
163
171
|
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
require 'securerandom'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
require 'date_time_precision'
|
7
|
+
|
8
|
+
require 'date_time_precision/format/json'
|
9
|
+
|
10
|
+
describe DateTimePrecision, 'Conversions' do
|
11
|
+
context 'when converting from Date to Time or DateTime' do
|
12
|
+
it 'maintains precision' do
|
13
|
+
d = Date.new(2005, 1)
|
14
|
+
d.precision.should == DateTimePrecision::MONTH
|
15
|
+
d.to_date.precision.should == DateTimePrecision::MONTH
|
16
|
+
d.to_datetime.precision.should == DateTimePrecision::MONTH
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'loses precision when converting from DateTime or Time to Date' do
|
21
|
+
t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
|
22
|
+
t.precision.should == DateTimePrecision::SEC
|
23
|
+
t.to_datetime.precision.should == DateTime::MAX_PRECISION
|
24
|
+
t.to_date.precision.should == DateTimePrecision::DAY
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'converts a date to a hash' do
|
28
|
+
date = Date.new(1999, 10)
|
29
|
+
date.as_json.should == date.to_h
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'retains precision when converting to and from JSON' do
|
33
|
+
date = Date.new(1999, 10)
|
34
|
+
date.precision.should == DateTimePrecision::MONTH
|
35
|
+
json = ActiveSupport::JSON.encode(date)
|
36
|
+
|
37
|
+
date_from_json = ActiveSupport::JSON.decode(json).to_date
|
38
|
+
date_from_json.precision.should == date.precision
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when formatting as a string' do
|
42
|
+
require 'date_time_precision/format/string'
|
43
|
+
|
44
|
+
it 'takes precision into account for the :long format' do
|
45
|
+
Date.new(2000).to_s(:long).should == "2000"
|
46
|
+
Date.new(2000, 8).to_s(:long).should == "August 2000"
|
47
|
+
Date.new(2000, 3, 9).to_s(:long).should == "March 9, 2000"
|
48
|
+
|
49
|
+
DateTime.new(1800).to_s(:long).should == "1800"
|
50
|
+
DateTime.new(1990, 8).to_s(:long).should == "August 1990"
|
51
|
+
DateTime.new(-50, 3, 9).to_s(:long).should == "March 09, -0050"
|
52
|
+
DateTime.new(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
|
53
|
+
DateTime.new(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"
|
54
|
+
|
55
|
+
Time.mktime(1800).to_s(:long).should == "1800"
|
56
|
+
Time::mktime(1990, 8).to_s(:long).should == "August 1990"
|
57
|
+
|
58
|
+
# Every Ruby seems to have a different idea about how to format this exactly
|
59
|
+
Time::mktime(-50, 3, 9).to_s(:long).should match /^March 09, 0*\-0*50$/
|
60
|
+
|
61
|
+
Time::mktime(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
|
62
|
+
Time::mktime(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
File without changes
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'date_time_precision'
|
3
|
+
|
4
|
+
describe DateTimePrecision do
|
5
|
+
context 'Constructors' do
|
6
|
+
it 'has no precision for unspecified date' do
|
7
|
+
d = Date.new
|
8
|
+
d.precision.should == DateTimePrecision::NONE
|
9
|
+
d.year?.should be_false
|
10
|
+
|
11
|
+
dt = DateTime.new
|
12
|
+
dt.precision.should == DateTimePrecision::NONE
|
13
|
+
dt.year?.should be_false
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'has no precision for nil values' do
|
17
|
+
nil.precision.should == DateTimePrecision::NONE
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has year precision when only year is supplied' do
|
21
|
+
d = Date.new(1982)
|
22
|
+
d.precision.should == DateTimePrecision::YEAR
|
23
|
+
d.year?.should be_true
|
24
|
+
d.month?.should be_false
|
25
|
+
d.day?.should be_false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'has month precision when year and month are supplied' do
|
29
|
+
d = Date.new(1982, 11)
|
30
|
+
d.precision.should == DateTimePrecision::MONTH
|
31
|
+
d.year?.should be_true
|
32
|
+
d.month?.should be_true
|
33
|
+
d.day?.should be_false
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'has day precision when year, month, and day are passed in' do
|
37
|
+
dt = DateTime.new(1987,10,19)
|
38
|
+
dt.precision.should == DateTimePrecision::DAY
|
39
|
+
dt.year?.should be_true
|
40
|
+
dt.month?.should be_true
|
41
|
+
dt.day?.should be_true
|
42
|
+
dt.hour?.should be_false
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'has hour precision' do
|
46
|
+
dt = DateTime.new(1970, 1, 2, 3)
|
47
|
+
dt.precision.should == DateTimePrecision::HOUR
|
48
|
+
dt.year?.should be_true
|
49
|
+
dt.month?.should be_true
|
50
|
+
dt.day?.should be_true
|
51
|
+
dt.hour?.should be_true
|
52
|
+
dt.min?.should be_false
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'tracks which attributes were explicitly set separately from precision' do
|
56
|
+
[Date.new(nil, 11, 12), DateTime.new(nil, 10, 11, nil), Time.mktime(nil, 12, 13, nil, 14)].each do |d|
|
57
|
+
d.year?.should be_false
|
58
|
+
d.month?.should be_true
|
59
|
+
d.day?.should be_true
|
60
|
+
d.hour?.should be_false
|
61
|
+
d.min?.should be_true if d.is_a? Time
|
62
|
+
d.precision.should == DateTimePrecision::NONE
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'has max precision for fully specified dates/times' do
|
67
|
+
# Time.new is an alias for Time.now
|
68
|
+
[Time.new, Time.now, DateTime.now, Date.today].each do |t|
|
69
|
+
t.precision.should == t.class::MAX_PRECISION
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'accepts nil values in the constructor' do
|
74
|
+
Date.new(nil).precision.should == DateTimePrecision::NONE
|
75
|
+
Date.new(2000, nil).precision.should == DateTimePrecision::YEAR
|
76
|
+
DateTime.new(2000, 1, nil).precision.should == DateTimePrecision::MONTH
|
77
|
+
Time.mktime(2000, 1, 1, nil, nil).precision.should == DateTimePrecision::DAY
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'Time Zones' do
|
82
|
+
it 'should retain precision when switching to UTC' do
|
83
|
+
Time.mktime(2000).utc.precision.should == DateTimePrecision::YEAR
|
84
|
+
Time.mktime(2000).gmtime.precision.should == DateTimePrecision::YEAR
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should track precision when creating a time in UTC' do
|
88
|
+
Time.utc(1945, 10).precision.should == DateTimePrecision::MONTH
|
89
|
+
Time.gm(1945, 10).precision.should == DateTimePrecision::MONTH
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should track precision when creating a time in the local timezone' do
|
93
|
+
Time.local(2004, 5, 6).precision.should == DateTimePrecision::DAY
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'Parsing' do
|
98
|
+
it 'should have second/frac precision when parsing a timestamp' do
|
99
|
+
t = Time::parse('2000-2-3 00:00:00 UTC')
|
100
|
+
t.precision.should == DateTimePrecision::SEC
|
101
|
+
t.year.should == 2000
|
102
|
+
t.month.should == 2
|
103
|
+
t.day.should == 3
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should have minute precision when seconds are not in the timestamp' do
|
107
|
+
dt = DateTime::parse('2000-1-1 00:00 EST') # => Sat, 01 Jan 2000 00:00:00 -0500
|
108
|
+
dt.precision.should == DateTimePrecision::MIN
|
109
|
+
dt.year.should == 2000
|
110
|
+
dt.day.should == 1
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should have day precision wehn parsing into a Date object' do
|
114
|
+
d = Date::parse('2000-1-1 00:00:00 EST') # => Sat, 01 Jan 2000
|
115
|
+
d.precision.should == DateTimePrecision::DAY
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should have month precision when day is not in the parsed string' do
|
119
|
+
t = Time::parse('January 2000 UTC').utc # => Sat Jan 01 00:00:00 -0800 2000
|
120
|
+
t.precision.should == DateTimePrecision::MONTH
|
121
|
+
t.year.should == 2000
|
122
|
+
t.month.should == 1
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'strptime' do
|
127
|
+
it 'should have day precision when day is specified in date string' do
|
128
|
+
d = Date.strptime('02/09/1968', '%m/%d/%Y')
|
129
|
+
d.precision.should == DateTimePrecision::DAY
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should have minute precision when extracting down to the minute' do
|
133
|
+
dt = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M')
|
134
|
+
dt.precision.should == DateTimePrecision::MIN
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should have second precision when extracting down to the second' do
|
138
|
+
t = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M:%S')
|
139
|
+
t.precision.should == DateTimePrecision::SEC
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'Addition' do
|
144
|
+
it 'should default to max precision when adding or subtracting' do
|
145
|
+
d = Date.new
|
146
|
+
d.precision.should == DateTimePrecision::NONE
|
147
|
+
d += 3
|
148
|
+
d.precision.should == Date::MAX_PRECISION
|
149
|
+
d -= 2
|
150
|
+
d.precision.should == Date::MAX_PRECISION
|
151
|
+
|
152
|
+
dt = DateTime.new
|
153
|
+
dt.precision.should == DateTimePrecision::NONE
|
154
|
+
dt += 3
|
155
|
+
dt.precision.should == DateTime::MAX_PRECISION
|
156
|
+
dt -= 2
|
157
|
+
dt.precision.should == DateTime::MAX_PRECISION
|
158
|
+
|
159
|
+
t = Time::parse('January 2000 UTC').utc
|
160
|
+
t.precision.should == DateTimePrecision::MONTH
|
161
|
+
t += 10
|
162
|
+
t.precision.should == Time::MAX_PRECISION
|
163
|
+
t -= 8
|
164
|
+
t.precision.should == Time::MAX_PRECISION
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context 'Partial Matching' do
|
169
|
+
it 'should match when differing only in day precision' do
|
170
|
+
d1 = Date.new(2001,3,2)
|
171
|
+
d2 = Date.new(2001,3)
|
172
|
+
d1.partial_match?(d2).should be_true
|
173
|
+
d2.partial_match?(d1).should be_true
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
@@ -2,179 +2,8 @@ require 'spec_helper'
|
|
2
2
|
require 'date_time_precision'
|
3
3
|
|
4
4
|
describe DateTimePrecision do
|
5
|
-
context 'Constructors' do
|
6
|
-
it 'has no precision for unspecified date' do
|
7
|
-
d = Date.new
|
8
|
-
d.precision.should == DateTimePrecision::NONE
|
9
|
-
d.year?.should be_false
|
10
|
-
|
11
|
-
dt = DateTime.new
|
12
|
-
dt.precision.should == DateTimePrecision::NONE
|
13
|
-
dt.year?.should be_false
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'has no precision for nil values' do
|
17
|
-
nil.precision.should == DateTimePrecision::NONE
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'has year precision when only year is supplied' do
|
21
|
-
d = Date.new(1982)
|
22
|
-
d.precision.should == DateTimePrecision::YEAR
|
23
|
-
d.year?.should be_true
|
24
|
-
d.month?.should be_false
|
25
|
-
d.day?.should be_false
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'has month precision when year and month are supplied' do
|
29
|
-
d = Date.new(1982, 11)
|
30
|
-
d.precision.should == DateTimePrecision::MONTH
|
31
|
-
d.year?.should be_true
|
32
|
-
d.month?.should be_true
|
33
|
-
d.day?.should be_false
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'has day precision when year, month, and day are passed in' do
|
37
|
-
dt = DateTime.new(1987,10,19)
|
38
|
-
dt.precision.should == DateTimePrecision::DAY
|
39
|
-
dt.year?.should be_true
|
40
|
-
dt.month?.should be_true
|
41
|
-
dt.day?.should be_true
|
42
|
-
dt.hour?.should be_false
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'has hour precision' do
|
46
|
-
dt = DateTime.new(1970, 1, 2, 3)
|
47
|
-
dt.precision.should == DateTimePrecision::HOUR
|
48
|
-
dt.year?.should be_true
|
49
|
-
dt.month?.should be_true
|
50
|
-
dt.day?.should be_true
|
51
|
-
dt.hour?.should be_true
|
52
|
-
dt.min?.should be_false
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'tracks which attributes were explicitly set separately from precision' do
|
56
|
-
[Date.new(nil, 11, 12), DateTime.new(nil, 10, 11, nil), Time.mktime(nil, 12, 13, nil, 14)].each do |d|
|
57
|
-
d.year?.should be_false
|
58
|
-
d.month?.should be_true
|
59
|
-
d.day?.should be_true
|
60
|
-
d.hour?.should be_false
|
61
|
-
d.min?.should be_true if d.is_a? Time
|
62
|
-
d.precision.should == DateTimePrecision::NONE
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'has max precision for fully specified dates/times' do
|
67
|
-
# Time.new is an alias for Time.now
|
68
|
-
[Time.new, Time.now, DateTime.now, Date.today].each do |t|
|
69
|
-
t.precision.should == t.class::MAX_PRECISION
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'accepts nil values in the constructor' do
|
74
|
-
Date.new(nil).precision.should == DateTimePrecision::NONE
|
75
|
-
Date.new(2000, nil).precision.should == DateTimePrecision::YEAR
|
76
|
-
DateTime.new(2000, 1, nil).precision.should == DateTimePrecision::MONTH
|
77
|
-
Time.mktime(2000, 1, 1, nil, nil).precision.should == DateTimePrecision::DAY
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'Time Zones' do
|
82
|
-
it 'should retain precision when switching to UTC' do
|
83
|
-
Time.mktime(2000).utc.precision.should == DateTimePrecision::YEAR
|
84
|
-
Time.mktime(2000).gmtime.precision.should == DateTimePrecision::YEAR
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'should track precision when creating a time in UTC' do
|
88
|
-
Time.utc(1945, 10).precision.should == DateTimePrecision::MONTH
|
89
|
-
Time.gm(1945, 10).precision.should == DateTimePrecision::MONTH
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'should track precision when creating a time in the local timezone' do
|
93
|
-
Time.local(2004, 5, 6).precision.should == DateTimePrecision::DAY
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'Parsing' do
|
98
|
-
it 'should have second/frac precision when parsing a timestamp' do
|
99
|
-
t = Time::parse('2000-2-3 00:00:00 UTC')
|
100
|
-
t.precision.should == DateTimePrecision::SEC
|
101
|
-
t.year.should == 2000
|
102
|
-
t.month.should == 2
|
103
|
-
t.day.should == 3
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should have minute precision when seconds are not in the timestamp' do
|
107
|
-
dt = DateTime::parse('2000-1-1 00:00 EST') # => Sat, 01 Jan 2000 00:00:00 -0500
|
108
|
-
dt.precision.should == DateTimePrecision::MIN
|
109
|
-
dt.year.should == 2000
|
110
|
-
dt.day.should == 1
|
111
|
-
end
|
112
|
-
|
113
|
-
it 'should have day precision wehn parsing into a Date object' do
|
114
|
-
d = Date::parse('2000-1-1 00:00:00 EST') # => Sat, 01 Jan 2000
|
115
|
-
d.precision.should == DateTimePrecision::DAY
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'should have month precision when day is not in the parsed string' do
|
119
|
-
t = Time::parse('January 2000 UTC').utc # => Sat Jan 01 00:00:00 -0800 2000
|
120
|
-
t.precision.should == DateTimePrecision::MONTH
|
121
|
-
t.year.should == 2000
|
122
|
-
t.month.should == 1
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
context 'strptime' do
|
127
|
-
it 'should have day precision when day is specified in date string' do
|
128
|
-
d = Date.strptime('02/09/1968', '%m/%d/%Y')
|
129
|
-
d.precision.should == DateTimePrecision::DAY
|
130
|
-
end
|
131
5
|
|
132
|
-
|
133
|
-
dt = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M')
|
134
|
-
dt.precision.should == DateTimePrecision::MIN
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'should have second precision when extracting down to the second' do
|
138
|
-
t = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M:%S')
|
139
|
-
t.precision.should == DateTimePrecision::SEC
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context 'Addition' do
|
144
|
-
it 'should default to max precision when adding or subtracting' do
|
145
|
-
d = Date.new
|
146
|
-
d.precision.should == DateTimePrecision::NONE
|
147
|
-
d += 3
|
148
|
-
d.precision.should == Date::MAX_PRECISION
|
149
|
-
d -= 2
|
150
|
-
d.precision.should == Date::MAX_PRECISION
|
151
|
-
|
152
|
-
dt = DateTime.new
|
153
|
-
dt.precision.should == DateTimePrecision::NONE
|
154
|
-
dt += 3
|
155
|
-
dt.precision.should == DateTime::MAX_PRECISION
|
156
|
-
dt -= 2
|
157
|
-
dt.precision.should == DateTime::MAX_PRECISION
|
158
|
-
|
159
|
-
t = Time::parse('January 2000 UTC').utc
|
160
|
-
t.precision.should == DateTimePrecision::MONTH
|
161
|
-
t += 10
|
162
|
-
t.precision.should == Time::MAX_PRECISION
|
163
|
-
t -= 8
|
164
|
-
t.precision.should == Time::MAX_PRECISION
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context 'Partial Matching' do
|
169
|
-
it 'should match when differing only in day precision' do
|
170
|
-
d1 = Date.new(2001,3,2)
|
171
|
-
d2 = Date.new(2001,3)
|
172
|
-
d1.partial_match?(d2).should be_true
|
173
|
-
d2.partial_match?(d1).should be_true
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
context 'Formats' do
|
6
|
+
context 'when formatting as' do
|
178
7
|
let(:date) { Date.new(1989, 3, 11) }
|
179
8
|
let(:datetime) { DateTime.new(1989, 3, 11, 8, 30, 15) }
|
180
9
|
let(:time) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.6.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: 2013-
|
11
|
+
date: 2013-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- lib/date_time_precision/format/iso8601.rb
|
120
120
|
- lib/date_time_precision/format/json.rb
|
121
121
|
- lib/date_time_precision/format/nil.rb
|
122
|
+
- lib/date_time_precision/format/string.rb
|
122
123
|
- lib/date_time_precision/lib.rb
|
123
124
|
- lib/date_time_precision/patch.rb
|
124
125
|
- lib/date_time_precision/patch/1.8.7/date.rb
|
@@ -131,9 +132,10 @@ files:
|
|
131
132
|
- lib/date_time_precision/patch/1.9.3/date_time.rb
|
132
133
|
- lib/date_time_precision/patch/1.9.3/time.rb
|
133
134
|
- lib/date_time_precision/version.rb
|
134
|
-
- spec/
|
135
|
-
- spec/
|
136
|
-
- spec/
|
135
|
+
- spec/active_support_spec.rb
|
136
|
+
- spec/compatibility_spec.rb
|
137
|
+
- spec/date_time_precision_spec.rb
|
138
|
+
- spec/format_spec.rb
|
137
139
|
- spec/spec_helper.rb
|
138
140
|
homepage: http://github.com/Spokeo/date_time_precision
|
139
141
|
licenses: []
|
@@ -159,7 +161,8 @@ signing_key:
|
|
159
161
|
specification_version: 4
|
160
162
|
summary: Patches Date, Time, and DateTime ruby classes to keep track of precision
|
161
163
|
test_files:
|
162
|
-
- spec/
|
163
|
-
- spec/
|
164
|
-
- spec/
|
164
|
+
- spec/active_support_spec.rb
|
165
|
+
- spec/compatibility_spec.rb
|
166
|
+
- spec/date_time_precision_spec.rb
|
167
|
+
- spec/format_spec.rb
|
165
168
|
- spec/spec_helper.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'json'
|
3
|
-
require 'securerandom'
|
4
|
-
require 'active_support'
|
5
|
-
|
6
|
-
require 'date_time_precision'
|
7
|
-
|
8
|
-
require 'date_time_precision/format/json'
|
9
|
-
|
10
|
-
describe DateTimePrecision, 'Conversions' do
|
11
|
-
context 'when converting from Date to Time or DateTime' do
|
12
|
-
it 'should maintain precision' do
|
13
|
-
d = Date.new(2005, 1)
|
14
|
-
d.precision.should == DateTimePrecision::MONTH
|
15
|
-
d.to_date.precision.should == DateTimePrecision::MONTH
|
16
|
-
d.to_datetime.precision.should == DateTimePrecision::MONTH
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'will lose precision when converting from DateTime or Time to Date' do
|
21
|
-
t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
|
22
|
-
t.precision.should == DateTimePrecision::SEC
|
23
|
-
t.to_datetime.precision.should == DateTime::MAX_PRECISION
|
24
|
-
t.to_date.precision.should == DateTimePrecision::DAY
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'will convert a date to a hash' do
|
28
|
-
date = Date.new(1999, 10)
|
29
|
-
date.as_json.should == date.to_h
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'will retain precision when converting to and from JSON' do
|
33
|
-
date = Date.new(1999, 10)
|
34
|
-
date.precision.should == DateTimePrecision::MONTH
|
35
|
-
json = ActiveSupport::JSON.encode(date)
|
36
|
-
|
37
|
-
date_from_json = ActiveSupport::JSON.decode(json).to_date
|
38
|
-
date_from_json.precision.should == date.precision
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|