date_time_precision 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +52 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README +1 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/date_time_precision.gemspec +24 -0
- data/gem_tasks/rspec.rake +7 -0
- data/lib/date_time_precision.rb +1 -0
- data/lib/date_time_precision/lib.rb +131 -0
- data/lib/date_time_precision/patch.rb +5 -0
- data/lib/date_time_precision/patch/date.rb +64 -0
- data/lib/date_time_precision/patch/date_time.rb +47 -0
- data/lib/date_time_precision/patch/nil.rb +7 -0
- data/lib/date_time_precision/patch/time.rb +21 -0
- data/lib/date_time_precision/version.rb +3 -0
- data/spec/date_time_precision/active_support.rb +21 -0
- data/spec/date_time_precision/date_time_precision_spec.rb +140 -0
- data/spec/spec_helper.rb +9 -0
- metadata +136 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.8.7" > .rvmrc
|
9
|
+
environment_id="ruby-1.8.7-p358@date_time_precision"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.13.5 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
if [[ $- == *i* ]] # check for interactive shells
|
29
|
+
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
30
|
+
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
|
31
|
+
fi
|
32
|
+
else
|
33
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
34
|
+
rvm --create use "$environment_id" || {
|
35
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
36
|
+
return 1
|
37
|
+
}
|
38
|
+
fi
|
39
|
+
|
40
|
+
# If you use bundler, this might be useful to you:
|
41
|
+
# if [[ -s Gemfile ]] && {
|
42
|
+
# ! builtin command -v bundle >/dev/null ||
|
43
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
44
|
+
# }
|
45
|
+
# then
|
46
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
47
|
+
# gem install bundler
|
48
|
+
# fi
|
49
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
50
|
+
# then
|
51
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
52
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Butler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
README.md
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# DateTimePrecision
|
2
|
+
|
3
|
+
Sometimes it is desirable to manipulate dates or times for which incomplete information is known.
|
4
|
+
For example, one might only know the year, or the year and the month.
|
5
|
+
Unfortunately, Ruby's built-in Date, Time, and DateTime classes do not keep track of the precision of the data.
|
6
|
+
For example:
|
7
|
+
|
8
|
+
Date.new.to_s => "-4712-01-01"
|
9
|
+
Date.new(2000).to_s => "2000-01-01"
|
10
|
+
|
11
|
+
There is no way to tell the difference between January 1, 2000 and a Date where only the year 2000 was known.
|
12
|
+
|
13
|
+
The DateTimePrecision gem patches the Date, Time, DateTime, and NilClass classes to keep track of precision.
|
14
|
+
The behavior of these classes should remain unchanged, and the following methods are now available:
|
15
|
+
|
16
|
+
* precision
|
17
|
+
* precision=
|
18
|
+
* partial_match?
|
19
|
+
* year?
|
20
|
+
* month?
|
21
|
+
* day?
|
22
|
+
* hour?
|
23
|
+
* min?
|
24
|
+
* sec?
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
gem 'date_time_precision'
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install date_time_precision
|
39
|
+
|
40
|
+
Tested in Ruby 1.8.7, 1.9.1, and 1.9.2
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
require 'date_time_precision'
|
45
|
+
|
46
|
+
d = Date.new(2000)
|
47
|
+
d.precision # => DateTimePrecision::YEAR
|
48
|
+
|
49
|
+
t = Time::parse("2001-05")
|
50
|
+
t.precision # => DateTimePrecision::MONTH
|
51
|
+
t.precision > d.precision # => true
|
52
|
+
|
53
|
+
## Wishlist
|
54
|
+
|
55
|
+
* Support Time::mktime, Time::utc, Time::local
|
56
|
+
* Support easy string formatting based on precision
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/date_time_precision/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["David Butler"]
|
6
|
+
gem.email = ["dwbutler@ucla.edu"]
|
7
|
+
gem.description = %q{Patches Date, Time, and DateTime ruby classes to keep track of precision}
|
8
|
+
gem.summary = %q{Patches Date, Time, and DateTime ruby classes to keep track of precision}
|
9
|
+
gem.homepage = "http://github.com/Spokeo/date_time_precision"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "date_time_precision"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = DateTimePrecision::VERSION
|
17
|
+
|
18
|
+
gem.required_ruby_version = '>= 1.8.7'
|
19
|
+
gem.add_development_dependency 'rake', '>= 0.9.2'
|
20
|
+
gem.add_development_dependency 'rspec', '~> 2.10.0'
|
21
|
+
gem.add_development_dependency 'activesupport'
|
22
|
+
#gem.add_development_dependency 'ruby-debug'
|
23
|
+
#gem.add_development_dependency 'ruby-debug19'
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'date_time_precision/patch'
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module DateTimePrecision
|
2
|
+
unless constants.include? "NONE"
|
3
|
+
FRAC = 7
|
4
|
+
SEC = 6
|
5
|
+
MIN = 5
|
6
|
+
HOUR = 4
|
7
|
+
DAY = 3
|
8
|
+
MONTH = 2
|
9
|
+
YEAR = 1
|
10
|
+
NONE = 0
|
11
|
+
|
12
|
+
# Default values for y,m,d,h,m,s,frac
|
13
|
+
NEW_DEFAULTS = [-4712,1,1,0,0,0,0]
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the precision for this Date/Time object, or the
|
17
|
+
# maximum precision if none was specified
|
18
|
+
def precision
|
19
|
+
@precision = self.class::MAX_PRECISION unless @precision
|
20
|
+
return @precision
|
21
|
+
end
|
22
|
+
|
23
|
+
def precision=(prec)
|
24
|
+
@precision = [prec,self.class::MAX_PRECISION].min
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.precision(val)
|
28
|
+
case val
|
29
|
+
when Date,Time,DateTime
|
30
|
+
val.precision
|
31
|
+
when Hash
|
32
|
+
case
|
33
|
+
when val[:sec_frac]
|
34
|
+
FRAC
|
35
|
+
when val[:sec]
|
36
|
+
SEC
|
37
|
+
when val[:min]
|
38
|
+
MIN
|
39
|
+
when val[:hour]
|
40
|
+
HOUR
|
41
|
+
when val[:mday]
|
42
|
+
DAY
|
43
|
+
when val[:mon]
|
44
|
+
MONTH
|
45
|
+
when val[:year]
|
46
|
+
YEAR
|
47
|
+
else
|
48
|
+
NONE
|
49
|
+
end
|
50
|
+
when Array
|
51
|
+
val.compact.length
|
52
|
+
else
|
53
|
+
NONE
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def subsec?
|
58
|
+
return self.precision >= FRAC
|
59
|
+
end
|
60
|
+
|
61
|
+
def sec?
|
62
|
+
return self.precision >= SEC
|
63
|
+
end
|
64
|
+
|
65
|
+
def min?
|
66
|
+
return self.precision >= MIN
|
67
|
+
end
|
68
|
+
|
69
|
+
def hour?
|
70
|
+
return self.precision >= HOUR
|
71
|
+
end
|
72
|
+
|
73
|
+
def year?
|
74
|
+
return self.precision >= YEAR
|
75
|
+
end
|
76
|
+
|
77
|
+
def month?
|
78
|
+
return self.precision >= MONTH
|
79
|
+
end
|
80
|
+
alias_method :mon?, :month?
|
81
|
+
|
82
|
+
def day?
|
83
|
+
return self.precision >= DAY
|
84
|
+
end
|
85
|
+
|
86
|
+
def fragments
|
87
|
+
frags = []
|
88
|
+
frags << self.year if self.year?
|
89
|
+
frags << self.month if self.month?
|
90
|
+
frags << self.day if self.day?
|
91
|
+
frags << self.hour if self.hour?
|
92
|
+
frags << self.min if self.min?
|
93
|
+
frags << self.sec if self.sec?
|
94
|
+
frags
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns true if dates partially match (i.e. one is a partial date of the other)
|
98
|
+
def partial_match?(date2)
|
99
|
+
self.class::partial_match?(self, date2)
|
100
|
+
end
|
101
|
+
|
102
|
+
module ClassMethods
|
103
|
+
def partial_match?(date1, date2)
|
104
|
+
return true if date1.nil? or date2.nil?
|
105
|
+
frags1 = date1.fragments
|
106
|
+
frags2 = date2.fragments
|
107
|
+
min_precision = [frags1.length,frags2.length].min
|
108
|
+
frags1.slice(0,min_precision) == frags2.slice(0,min_precision)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.included(base)
|
113
|
+
# Redefine any conversion methods so precision is preserved
|
114
|
+
[:to_date, :to_time, :to_datetime].each do |m|
|
115
|
+
orig = :"orig_#{m}"
|
116
|
+
if base.method_defined?(m) && !base.method_defined?(orig)
|
117
|
+
base.class_exec {
|
118
|
+
alias_method orig, m
|
119
|
+
define_method(m) {
|
120
|
+
d = send(orig)
|
121
|
+
d.precision = [self.precision, d.class::MAX_PRECISION].min
|
122
|
+
d
|
123
|
+
}
|
124
|
+
}
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Extend with this module's class methods
|
129
|
+
base.extend(ClassMethods)
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'date_time_precision/lib'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class Date
|
5
|
+
include DateTimePrecision
|
6
|
+
|
7
|
+
MAX_PRECISION = DateTimePrecision::DAY
|
8
|
+
|
9
|
+
def self.parse(str='-4712-01-01T00:00:00+00:00', comp=false, sg=ITALY)
|
10
|
+
elem = _parse(str, comp)
|
11
|
+
precision = DateTimePrecision::precision(elem)
|
12
|
+
d = new_by_frags(elem, sg)
|
13
|
+
d.precision = precision
|
14
|
+
d
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
18
|
+
elem = _strptime(str, fmt)
|
19
|
+
precision = DateTimePrecision::precision(elem)
|
20
|
+
d = new_by_frags(elem, sg)
|
21
|
+
d.precision = precision
|
22
|
+
d
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.civil(y=nil, m=nil, d=nil, sg=ITALY)
|
26
|
+
vals = [y,m,d]
|
27
|
+
precision = DateTimePrecision::precision(vals)
|
28
|
+
unless vals.all?
|
29
|
+
vals = vals.compact
|
30
|
+
vals = vals.concat(NEW_DEFAULTS.slice(vals.length, NEW_DEFAULTS.length - vals.length))
|
31
|
+
end
|
32
|
+
y,m,d = vals
|
33
|
+
|
34
|
+
unless jd = (private_methods.include?(:"_valid_civil?") ? _valid_civil?(y, m, d, sg) : valid_civil?(y, m, d, sg))
|
35
|
+
raise ArgumentError, 'invalid date'
|
36
|
+
end
|
37
|
+
|
38
|
+
d = new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
39
|
+
d.precision = precision
|
40
|
+
d
|
41
|
+
end
|
42
|
+
|
43
|
+
class << self; alias_method :new, :civil end
|
44
|
+
|
45
|
+
=begin
|
46
|
+
Following code is unneccessary, but keeping it as an example
|
47
|
+
# Return the date as a human-readable string.
|
48
|
+
#
|
49
|
+
# The format used is YYYY-MM-DD, YYYY-MM, or YYYY.
|
50
|
+
def to_s
|
51
|
+
case
|
52
|
+
when self.precision.nil?, self.precision >= DateTimePrecision::DAY
|
53
|
+
format('%.4d-%02d-%02d', year, mon, mday)
|
54
|
+
when self.precision == DateTimePrecision::MONTH
|
55
|
+
format('%.4d-%02d', year, mon)
|
56
|
+
when self.precision == DateTimePrecision::YEAR
|
57
|
+
format('%.4d', year)
|
58
|
+
else
|
59
|
+
'?'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
=end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'date_time_precision/lib'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class DateTime < Date
|
5
|
+
include DateTimePrecision
|
6
|
+
|
7
|
+
MAX_PRECISION = DateTimePrecision::SEC
|
8
|
+
|
9
|
+
def self.parse(str='-4712-01-01T00:00:00+00:00', comp=false, sg=ITALY)
|
10
|
+
elem = _parse(str, comp)
|
11
|
+
precision = DateTimePrecision::precision(elem)
|
12
|
+
dt = new_by_frags(elem, sg)
|
13
|
+
dt.precision = precision
|
14
|
+
dt
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.civil(y=nil, m=nil, d=nil, h=nil, min=nil, s=nil, of=0, sg=ITALY)
|
18
|
+
vals = [y,m,d,h,min,s]
|
19
|
+
precision = DateTimePrecision::precision(vals)
|
20
|
+
unless vals.all?
|
21
|
+
vals = vals.compact
|
22
|
+
vals = vals.concat(NEW_DEFAULTS.slice(vals.length, NEW_DEFAULTS.length - vals.length))
|
23
|
+
end
|
24
|
+
y,m,d,h,min,s = vals
|
25
|
+
|
26
|
+
unless (jd = (private_methods.include?(:"_valid_civil?") ? _valid_civil?(y, m, d, sg) : valid_civil?(y, m, d, sg))) &&
|
27
|
+
(fr = (private_methods.include?(:"_valid_time?") ? _valid_time?(h, min, s) : valid_time?(h, min, s)))
|
28
|
+
raise ArgumentError, 'invalid date'
|
29
|
+
end
|
30
|
+
if String === of
|
31
|
+
of = Rational(zone_to_diff(of) || 0, 86400)
|
32
|
+
end
|
33
|
+
dt = new!(jd_to_ajd(jd, fr, of), of, sg)
|
34
|
+
dt.precision = precision
|
35
|
+
dt
|
36
|
+
end
|
37
|
+
|
38
|
+
class << self; alias_method :new, :civil end
|
39
|
+
|
40
|
+
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
41
|
+
elem = _strptime(str, fmt)
|
42
|
+
precision = DateTimePrecision::precision(elem)
|
43
|
+
d = new_by_frags(elem, sg)
|
44
|
+
d.precision = precision
|
45
|
+
d
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'date_time_precision/lib'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
class Time
|
5
|
+
include DateTimePrecision
|
6
|
+
|
7
|
+
MAX_PRECISION = DateTimePrecision::SEC
|
8
|
+
|
9
|
+
def self.parse(date, now=self.now)
|
10
|
+
d = Date._parse(date, false)
|
11
|
+
year = d[:year]
|
12
|
+
year = yield(year) if year && block_given?
|
13
|
+
t = make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
|
14
|
+
t.precision = DateTimePrecision::precision(d)
|
15
|
+
t
|
16
|
+
end
|
17
|
+
|
18
|
+
#def self.strptime(str='-4712-01-01', fmt='%F', sg=Date::ITALY)
|
19
|
+
# DateTime.strptime(str, fmt, sg).to_time
|
20
|
+
#end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_support'
|
3
|
+
require 'date_time_precision'
|
4
|
+
|
5
|
+
describe DateTimePrecision, 'Conversions' do
|
6
|
+
context 'when converting from Date to Time or DateTime' do
|
7
|
+
it 'should maintain precision' do
|
8
|
+
d = Date.new(2005, 1, 2)
|
9
|
+
d.precision.should == DateTimePrecision::DAY
|
10
|
+
d.to_date.precision.should == DateTimePrecision::DAY
|
11
|
+
d.to_datetime.precision.should == DateTimePrecision::DAY
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'will lose precision when converting from DateTime or Time to Date' do
|
16
|
+
t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
|
17
|
+
t.precision.should == DateTimePrecision::SEC
|
18
|
+
t.to_datetime.precision.should == DateTimePrecision::SEC
|
19
|
+
t.to_date.precision.should == DateTimePrecision::DAY
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'date_time_precision'
|
3
|
+
|
4
|
+
describe DateTimePrecision, 'Constructors' do
|
5
|
+
it 'has no precision for unspecified date' do
|
6
|
+
d = Date.new
|
7
|
+
d.precision.should == DateTimePrecision::NONE
|
8
|
+
d.year?.should be_false
|
9
|
+
|
10
|
+
dt = DateTime.new
|
11
|
+
dt.precision.should == DateTimePrecision::NONE
|
12
|
+
dt.year?.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'has no precision for nil values' do
|
16
|
+
nil.precision.should == DateTimePrecision::NONE
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'has year precision when only year is supplied' do
|
20
|
+
d = Date.new(1982)
|
21
|
+
d.precision.should == DateTimePrecision::YEAR
|
22
|
+
d.year?.should be_true
|
23
|
+
d.month?.should be_false
|
24
|
+
d.day?.should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has month precision when year and month are supplied' do
|
28
|
+
d = Date.new(1982, 11)
|
29
|
+
d.precision.should == DateTimePrecision::MONTH
|
30
|
+
d.year?.should be_true
|
31
|
+
d.month?.should be_true
|
32
|
+
d.day?.should be_false
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'has day precision when year, month, and day are passed in' do
|
36
|
+
dt = DateTime.new(1987,10,19)
|
37
|
+
dt.precision.should == DateTimePrecision::DAY
|
38
|
+
dt.year?.should be_true
|
39
|
+
dt.month?.should be_true
|
40
|
+
dt.day?.should be_true
|
41
|
+
dt.hour?.should be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'has hour precision' do
|
45
|
+
dt = DateTime.new(1970, 1, 2, 3)
|
46
|
+
dt.precision.should == DateTimePrecision::HOUR
|
47
|
+
dt.year?.should be_true
|
48
|
+
dt.month?.should be_true
|
49
|
+
dt.day?.should be_true
|
50
|
+
dt.hour?.should be_true
|
51
|
+
dt.min?.should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should have max precision for fully specified dates/times' do
|
55
|
+
# Time.new is an alias for Time.now
|
56
|
+
[Time.new, Time.now, DateTime.now, Date.today].each do |t|
|
57
|
+
t.precision.should == t.class::MAX_PRECISION
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe DateTimePrecision, 'Parsing' do
|
63
|
+
it 'should have second precision when parsing a timestamp' do
|
64
|
+
t = Time::parse('2000-2-3 00:00:00 UTC')
|
65
|
+
t.precision.should == DateTimePrecision::SEC
|
66
|
+
t.year.should == 2000
|
67
|
+
t.month.should == 2
|
68
|
+
t.day.should == 3
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should have minute precision when seconds are not in the timestamp' do
|
72
|
+
dt = DateTime::parse('2000-1-1 00:00 EST') # => Sat, 01 Jan 2000 00:00:00 -0500
|
73
|
+
dt.precision.should == DateTimePrecision::MIN
|
74
|
+
dt.year.should == 2000
|
75
|
+
dt.day.should == 1
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should have day precision wehn parsing into a Date object' do
|
79
|
+
d = Date::parse('2000-1-1 00:00:00 EST') # => Sat, 01 Jan 2000
|
80
|
+
d.precision.should == DateTimePrecision::DAY
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should have month precision when day is not in the parsed string' do
|
84
|
+
t = Time::parse('January 2000 UTC').utc # => Sat Jan 01 00:00:00 -0800 2000
|
85
|
+
t.precision.should == DateTimePrecision::MONTH
|
86
|
+
t.year.should == 2000
|
87
|
+
t.month.should == 1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe DateTimePrecision, 'strptime' do
|
92
|
+
it 'should have day precision when day is specified in date string' do
|
93
|
+
d = Date.strptime('02/09/1968', '%m/%d/%Y')
|
94
|
+
d.precision.should == DateTimePrecision::DAY
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should have minute precision when extracting down to the minute' do
|
98
|
+
dt = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M')
|
99
|
+
dt.precision.should == DateTimePrecision::MIN
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should have second precision when extracting down to the second' do
|
103
|
+
t = DateTime.strptime('2011-02-03 15:14:52','%Y-%m-%d %H:%M:%S')
|
104
|
+
t.precision.should == DateTimePrecision::SEC
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe DateTimePrecision, 'Addition' do
|
109
|
+
it 'will default to max precision when adding or subtracting' do
|
110
|
+
d = Date.new
|
111
|
+
d.precision.should == DateTimePrecision::NONE
|
112
|
+
d += 3
|
113
|
+
d.precision.should == Date::MAX_PRECISION
|
114
|
+
d -= 2
|
115
|
+
d.precision.should == Date::MAX_PRECISION
|
116
|
+
|
117
|
+
dt = DateTime.new
|
118
|
+
dt.precision.should == DateTimePrecision::NONE
|
119
|
+
dt += 3
|
120
|
+
dt.precision.should == DateTime::MAX_PRECISION
|
121
|
+
dt -= 2
|
122
|
+
dt.precision.should == DateTime::MAX_PRECISION
|
123
|
+
|
124
|
+
t = Time::parse('January 2000 UTC').utc
|
125
|
+
t.precision.should == DateTimePrecision::MONTH
|
126
|
+
t += 10
|
127
|
+
t.precision.should == Time::MAX_PRECISION
|
128
|
+
t -= 8
|
129
|
+
t.precision.should == Time::MAX_PRECISION
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe DateTimePrecision, 'Partial Matching' do
|
134
|
+
it 'should match when differing only in day precision' do
|
135
|
+
d1 = Date.new(2001,3,2)
|
136
|
+
d2 = Date.new(2001,3)
|
137
|
+
d1.partial_match?(d2).should be_true
|
138
|
+
d2.partial_match?(d1).should be_true
|
139
|
+
end
|
140
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_time_precision
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Butler
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-06-14 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rake
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 63
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 9
|
33
|
+
- 2
|
34
|
+
version: 0.9.2
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 39
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 10
|
49
|
+
- 0
|
50
|
+
version: 2.10.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: activesupport
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
description: Patches Date, Time, and DateTime ruby classes to keep track of precision
|
68
|
+
email:
|
69
|
+
- dwbutler@ucla.edu
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .rvmrc
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- README
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- date_time_precision.gemspec
|
85
|
+
- gem_tasks/rspec.rake
|
86
|
+
- lib/date_time_precision.rb
|
87
|
+
- lib/date_time_precision/lib.rb
|
88
|
+
- lib/date_time_precision/patch.rb
|
89
|
+
- lib/date_time_precision/patch/date.rb
|
90
|
+
- lib/date_time_precision/patch/date_time.rb
|
91
|
+
- lib/date_time_precision/patch/nil.rb
|
92
|
+
- lib/date_time_precision/patch/time.rb
|
93
|
+
- lib/date_time_precision/version.rb
|
94
|
+
- spec/date_time_precision/active_support.rb
|
95
|
+
- spec/date_time_precision/date_time_precision_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
has_rdoc: true
|
98
|
+
homepage: http://github.com/Spokeo/date_time_precision
|
99
|
+
licenses: []
|
100
|
+
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 57
|
112
|
+
segments:
|
113
|
+
- 1
|
114
|
+
- 8
|
115
|
+
- 7
|
116
|
+
version: 1.8.7
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
hash: 3
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
requirements: []
|
127
|
+
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 1.3.7
|
130
|
+
signing_key:
|
131
|
+
specification_version: 3
|
132
|
+
summary: Patches Date, Time, and DateTime ruby classes to keep track of precision
|
133
|
+
test_files:
|
134
|
+
- spec/date_time_precision/active_support.rb
|
135
|
+
- spec/date_time_precision/date_time_precision_spec.rb
|
136
|
+
- spec/spec_helper.rb
|