date_time_precision 0.0.1 → 0.1.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.
- data/.travis.yml +10 -0
- data/Gemfile +7 -1
- data/README.md +29 -17
- data/Rakefile +1 -1
- data/{lib/date_time_precision/patch/date.rb → date.rb} +0 -0
- data/{lib/date_time_precision/patch/date_time.rb → date_time.rb} +0 -0
- data/date_time_precision.gemspec +0 -2
- data/gem_tasks/rspec.rake +7 -1
- data/lib/date_time_precision/lib.rb +5 -0
- data/lib/date_time_precision/patch.rb +3 -4
- data/lib/date_time_precision/patch/1.8.7/date.rb +64 -0
- data/lib/date_time_precision/patch/1.8.7/date_time.rb +46 -0
- data/lib/date_time_precision/patch/{time.rb → 1.8.7/time.rb} +0 -0
- data/lib/date_time_precision/patch/1.9.2/date.rb +64 -0
- data/lib/date_time_precision/patch/1.9.2/date_time.rb +46 -0
- data/lib/date_time_precision/patch/1.9.2/time.rb +21 -0
- data/lib/date_time_precision/patch/1.9.3/date.rb +72 -0
- data/lib/date_time_precision/patch/1.9.3/date_time.rb +53 -0
- data/lib/date_time_precision/patch/1.9.3/time.rb +18 -0
- data/lib/date_time_precision/version.rb +1 -1
- data/spec/date_time_precision/{active_support.rb → active_support_spec.rb} +1 -1
- data/spec/date_time_precision/date_time_precision_spec.rb +1 -1
- data/time.rb +21 -0
- metadata +65 -90
- data/.rvmrc +0 -52
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,34 @@
|
|
1
|
-
#
|
1
|
+
# Ruby Date/Time Precision [](https://travis-ci.org/Spokeo/date_time_precision)
|
2
2
|
|
3
3
|
Sometimes it is desirable to manipulate dates or times for which incomplete information is known.
|
4
4
|
For example, one might only know the year, or the year and the month.
|
5
5
|
Unfortunately, Ruby's built-in Date, Time, and DateTime classes do not keep track of the precision of the data.
|
6
6
|
For example:
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
```ruby
|
9
|
+
Date.new.to_s => "-4712-01-01"
|
10
|
+
Date.new(2000).to_s => "2000-01-01"
|
11
|
+
```
|
10
12
|
|
11
13
|
There is no way to tell the difference between January 1, 2000 and a Date where only the year 2000 was known.
|
12
14
|
|
13
15
|
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
|
16
|
+
The behavior of these classes should otherwise remain unchanged.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'date_time_precision'
|
22
|
+
|
23
|
+
d = Date.new(2000)
|
24
|
+
d.precision # => DateTimePrecision::YEAR
|
25
|
+
|
26
|
+
t = Time::parse("2001-05")
|
27
|
+
t.precision # => DateTimePrecision::MONTH
|
28
|
+
t.precision > d.precision # => true
|
29
|
+
```
|
30
|
+
|
31
|
+
The gem adds the following instance methods to Date, Time, and/or DateTime:
|
15
32
|
|
16
33
|
* precision
|
17
34
|
* precision=
|
@@ -23,6 +40,14 @@ The behavior of these classes should remain unchanged, and the following methods
|
|
23
40
|
* min?
|
24
41
|
* sec?
|
25
42
|
|
43
|
+
## Compatibility
|
44
|
+
|
45
|
+
Tested in MRI 1.8.7/1.9.2/1.9.3, REE, JRuby 1.8/1.9, and Rubinius 1.8/1.9.
|
46
|
+
|
47
|
+
Note that starting in MRI 1.9.3, the core Date/Time classes were rewritten in C, making it difficult to
|
48
|
+
override internal functionality. Some functions are now implemented internally and are not exposed.
|
49
|
+
The workaround is inefficient: `#parse` and `#strptime` perform the same work twice, once to get the precision and once inside the original C method.
|
50
|
+
|
26
51
|
## Installation
|
27
52
|
|
28
53
|
Add this line to your application's Gemfile:
|
@@ -37,19 +62,6 @@ Or install it yourself as:
|
|
37
62
|
|
38
63
|
$ gem install date_time_precision
|
39
64
|
|
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
65
|
## Wishlist
|
54
66
|
|
55
67
|
* Support Time::mktime, Time::utc, Time::local
|
data/Rakefile
CHANGED
File without changes
|
File without changes
|
data/date_time_precision.gemspec
CHANGED
@@ -15,10 +15,8 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = DateTimePrecision::VERSION
|
17
17
|
|
18
|
-
gem.required_ruby_version = '>= 1.8.7'
|
19
18
|
gem.add_development_dependency 'rake', '>= 0.9.2'
|
20
19
|
gem.add_development_dependency 'rspec', '~> 2.10.0'
|
21
|
-
gem.add_development_dependency 'activesupport'
|
22
20
|
#gem.add_development_dependency 'ruby-debug'
|
23
21
|
#gem.add_development_dependency 'ruby-debug19'
|
24
22
|
end
|
data/gem_tasks/rspec.rake
CHANGED
@@ -3,5 +3,11 @@ 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 =
|
6
|
+
t.pattern = "spec/date_time_precision/date_time_precision_spec.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run ActiveSupport spec"
|
10
|
+
RSpec::Core::RakeTask.new(:active_support_spec) do |t|
|
11
|
+
t.verbose = true
|
12
|
+
t.pattern = "spec/date_time_precision/active_support_spec.rb"
|
7
13
|
end
|
@@ -107,6 +107,11 @@ module DateTimePrecision
|
|
107
107
|
min_precision = [frags1.length,frags2.length].min
|
108
108
|
frags1.slice(0,min_precision) == frags2.slice(0,min_precision)
|
109
109
|
end
|
110
|
+
|
111
|
+
def precision(val)
|
112
|
+
val = val.take(self::MAX_PRECISION) if val.is_a? Array
|
113
|
+
DateTimePrecision::precision(val)
|
114
|
+
end
|
110
115
|
end
|
111
116
|
|
112
117
|
def self.included(base)
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'date_time_precision/lib'
|
2
|
-
|
3
|
-
require 'date_time_precision/patch/
|
4
|
-
require
|
5
|
-
require 'date_time_precision/patch/nil'
|
2
|
+
|
3
|
+
require 'date_time_precision/patch/nil'
|
4
|
+
Dir["#{File.dirname(__FILE__)}/patch/#{RUBY_VERSION}/*.rb"].each {|f| require f }
|
@@ -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 = 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,46 @@
|
|
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 = valid_civil?(y, m, d, sg)) && (fr = valid_time?(h, min, s))
|
27
|
+
raise ArgumentError, 'invalid date'
|
28
|
+
end
|
29
|
+
if String === of
|
30
|
+
of = Rational(zone_to_diff(of) || 0, 86400)
|
31
|
+
end
|
32
|
+
dt = new!(jd_to_ajd(jd, fr, of), of, sg)
|
33
|
+
dt.precision = precision
|
34
|
+
dt
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self; alias_method :new, :civil end
|
38
|
+
|
39
|
+
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
40
|
+
elem = _strptime(str, fmt)
|
41
|
+
precision = DateTimePrecision::precision(elem)
|
42
|
+
d = new_by_frags(elem, sg)
|
43
|
+
d.precision = precision
|
44
|
+
d
|
45
|
+
end
|
46
|
+
end
|
File without changes
|
@@ -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 = _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,46 @@
|
|
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 = _valid_civil?(y, m, d, sg)) && (fr = _valid_time?(h, min, s))
|
27
|
+
raise ArgumentError, 'invalid date'
|
28
|
+
end
|
29
|
+
if String === of
|
30
|
+
of = Rational(zone_to_diff(of) || 0, 86400)
|
31
|
+
end
|
32
|
+
dt = new!(jd_to_ajd(jd, fr, of), of, sg)
|
33
|
+
dt.precision = precision
|
34
|
+
dt
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self; alias_method :new, :civil end
|
38
|
+
|
39
|
+
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
40
|
+
elem = _strptime(str, fmt)
|
41
|
+
precision = DateTimePrecision::precision(elem)
|
42
|
+
d = new_by_frags(elem, sg)
|
43
|
+
d.precision = precision
|
44
|
+
d
|
45
|
+
end
|
46
|
+
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,72 @@
|
|
1
|
+
require 'date_time_precision/lib'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class Date
|
5
|
+
include DateTimePrecision
|
6
|
+
|
7
|
+
MAX_PRECISION = DateTimePrecision::DAY
|
8
|
+
|
9
|
+
class << self
|
10
|
+
alias_method :new_orig, :new
|
11
|
+
def new(*args)
|
12
|
+
precision = self.precision(args)
|
13
|
+
d = new_orig(*args)
|
14
|
+
d.precision= precision
|
15
|
+
d
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :parse_orig, :parse
|
19
|
+
def parse(str='-4712-01-01T00:00:00+00:00', start=ITALY)
|
20
|
+
comp = !block_given?
|
21
|
+
elem = _parse(str, comp)
|
22
|
+
precision = self.precision(elem)
|
23
|
+
d = parse_orig(str, start)
|
24
|
+
d.precision = precision
|
25
|
+
d
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :civil_orig, :civil
|
29
|
+
def civil(y=nil, m=nil, d=nil, sg=ITALY)
|
30
|
+
vals = [y,m,d]
|
31
|
+
precision = self.precision(vals)
|
32
|
+
unless vals.all?
|
33
|
+
vals = vals.compact
|
34
|
+
vals = vals.concat(NEW_DEFAULTS.slice(vals.length, NEW_DEFAULTS.length - vals.length))
|
35
|
+
end
|
36
|
+
y,m,d = vals
|
37
|
+
|
38
|
+
d = civil_orig(y,m,d,sg)
|
39
|
+
d.precision = precision
|
40
|
+
d
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :strptime_orig, :strptime
|
44
|
+
def strptime(date, format='%F', start=ITALY)
|
45
|
+
elem = _strptime(date, format)
|
46
|
+
precision = self.precision(elem)
|
47
|
+
d = strptime_orig(date, format, start)
|
48
|
+
d.precision = precision
|
49
|
+
d
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
=begin
|
54
|
+
Following code is unnecessary, but keeping it as an example
|
55
|
+
# Return the date as a human-readable string.
|
56
|
+
#
|
57
|
+
# The format used is YYYY-MM-DD, YYYY-MM, or YYYY.
|
58
|
+
def to_s
|
59
|
+
case
|
60
|
+
when self.precision.nil?, self.precision >= DateTimePrecision::DAY
|
61
|
+
format('%.4d-%02d-%02d', year, mon, mday)
|
62
|
+
when self.precision == DateTimePrecision::MONTH
|
63
|
+
format('%.4d-%02d', year, mon)
|
64
|
+
when self.precision == DateTimePrecision::YEAR
|
65
|
+
format('%.4d', year)
|
66
|
+
else
|
67
|
+
'?'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
=end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,53 @@
|
|
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
|
+
class << self
|
10
|
+
alias_method :new_orig, :new
|
11
|
+
def new(*args)
|
12
|
+
precision = self.precision(args)
|
13
|
+
d = new_orig(*args)
|
14
|
+
d.precision= precision
|
15
|
+
d
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :parse_orig, :parse
|
19
|
+
def parse(str='-4712-01-01T00:00:00+00:00', now=self.now)
|
20
|
+
comp = !block_given?
|
21
|
+
elem = _parse(str, comp)
|
22
|
+
precision = self.precision(elem)
|
23
|
+
dt = parse_orig(str, now)
|
24
|
+
dt.precision = precision
|
25
|
+
dt
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :civil_orig, :civil
|
29
|
+
def civil(y=nil, m=nil, d=nil, sg=Date::ITALY)
|
30
|
+
vals = [y,m,d]
|
31
|
+
precision = self.precision(vals)
|
32
|
+
unless vals.all?
|
33
|
+
vals = vals.compact
|
34
|
+
vals = vals.concat(NEW_DEFAULTS.slice(vals.length, NEW_DEFAULTS.length - vals.length))
|
35
|
+
end
|
36
|
+
y,m,d = vals
|
37
|
+
|
38
|
+
dt = civil_orig(y,m,d,sg)
|
39
|
+
dt.precision = precision
|
40
|
+
dt
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :strptime_orig, :strptime
|
44
|
+
def strptime(date, format='%F', start=Date::ITALY)
|
45
|
+
elem = _strptime(date, format)
|
46
|
+
precision = self.precision(elem)
|
47
|
+
d = strptime_orig(date, format, start)
|
48
|
+
d.precision = precision
|
49
|
+
d
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'date_time_precision/lib'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
class Time
|
5
|
+
include DateTimePrecision
|
6
|
+
|
7
|
+
MAX_PRECISION = DateTimePrecision::FRAC
|
8
|
+
|
9
|
+
class << self
|
10
|
+
alias_method :make_time_orig, :make_time
|
11
|
+
def make_time(*args)
|
12
|
+
t = make_time_orig(*args)
|
13
|
+
t.precision = self.precision(args)
|
14
|
+
t
|
15
|
+
end
|
16
|
+
private :make_time
|
17
|
+
end
|
18
|
+
end
|
@@ -15,7 +15,7 @@ describe DateTimePrecision, 'Conversions' do
|
|
15
15
|
it 'will lose precision when converting from DateTime or Time to Date' do
|
16
16
|
t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
|
17
17
|
t.precision.should == DateTimePrecision::SEC
|
18
|
-
t.to_datetime.precision.should ==
|
18
|
+
t.to_datetime.precision.should == DateTime::MAX_PRECISION
|
19
19
|
t.to_date.precision.should == DateTimePrecision::DAY
|
20
20
|
end
|
21
21
|
end
|
@@ -60,7 +60,7 @@ describe DateTimePrecision, 'Constructors' do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
describe DateTimePrecision, 'Parsing' do
|
63
|
-
it 'should have second precision when parsing a timestamp' do
|
63
|
+
it 'should have second/frac precision when parsing a timestamp' do
|
64
64
|
t = Time::parse('2000-2-3 00:00:00 UTC')
|
65
65
|
t.precision.should == DateTimePrecision::SEC
|
66
66
|
t.year.should == 2000
|
data/time.rb
ADDED
@@ -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
|
metadata
CHANGED
@@ -1,136 +1,111 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_time_precision
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- David Butler
|
14
|
-
autorequire:
|
9
|
+
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-12-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: rake
|
23
|
-
|
24
|
-
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.9.2
|
25
21
|
none: false
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
- 9
|
33
|
-
- 2
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
34
26
|
version: 0.9.2
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
35
29
|
type: :development
|
36
|
-
|
37
|
-
- !ruby/object:Gem::Dependency
|
30
|
+
- !ruby/object:Gem::Dependency
|
38
31
|
name: rspec
|
39
|
-
|
40
|
-
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.10.0
|
41
37
|
none: false
|
42
|
-
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
43
40
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
hash: 39
|
46
|
-
segments:
|
47
|
-
- 2
|
48
|
-
- 10
|
49
|
-
- 0
|
41
|
+
- !ruby/object:Gem::Version
|
50
42
|
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
43
|
none: false
|
58
|
-
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
|
-
segments:
|
63
|
-
- 0
|
64
|
-
version: "0"
|
44
|
+
prerelease: false
|
65
45
|
type: :development
|
66
|
-
version_requirements: *id003
|
67
46
|
description: Patches Date, Time, and DateTime ruby classes to keep track of precision
|
68
|
-
email:
|
47
|
+
email:
|
69
48
|
- dwbutler@ucla.edu
|
70
49
|
executables: []
|
71
|
-
|
72
50
|
extensions: []
|
73
|
-
|
74
51
|
extra_rdoc_files: []
|
75
|
-
|
76
|
-
files:
|
52
|
+
files:
|
77
53
|
- .gitignore
|
78
|
-
- .
|
54
|
+
- .travis.yml
|
79
55
|
- Gemfile
|
80
56
|
- LICENSE
|
81
57
|
- README
|
82
58
|
- README.md
|
83
59
|
- Rakefile
|
60
|
+
- date.rb
|
61
|
+
- date_time.rb
|
84
62
|
- date_time_precision.gemspec
|
85
63
|
- gem_tasks/rspec.rake
|
86
64
|
- lib/date_time_precision.rb
|
87
65
|
- lib/date_time_precision/lib.rb
|
88
66
|
- lib/date_time_precision/patch.rb
|
89
|
-
- lib/date_time_precision/patch/date.rb
|
90
|
-
- lib/date_time_precision/patch/date_time.rb
|
67
|
+
- lib/date_time_precision/patch/1.8.7/date.rb
|
68
|
+
- lib/date_time_precision/patch/1.8.7/date_time.rb
|
69
|
+
- lib/date_time_precision/patch/1.8.7/time.rb
|
70
|
+
- lib/date_time_precision/patch/1.9.2/date.rb
|
71
|
+
- lib/date_time_precision/patch/1.9.2/date_time.rb
|
72
|
+
- lib/date_time_precision/patch/1.9.2/time.rb
|
73
|
+
- lib/date_time_precision/patch/1.9.3/date.rb
|
74
|
+
- lib/date_time_precision/patch/1.9.3/date_time.rb
|
75
|
+
- lib/date_time_precision/patch/1.9.3/time.rb
|
91
76
|
- lib/date_time_precision/patch/nil.rb
|
92
|
-
- lib/date_time_precision/patch/time.rb
|
93
77
|
- lib/date_time_precision/version.rb
|
94
|
-
- spec/date_time_precision/
|
78
|
+
- spec/date_time_precision/active_support_spec.rb
|
95
79
|
- spec/date_time_precision/date_time_precision_spec.rb
|
96
80
|
- spec/spec_helper.rb
|
97
|
-
|
81
|
+
- time.rb
|
98
82
|
homepage: http://github.com/Spokeo/date_time_precision
|
99
83
|
licenses: []
|
100
|
-
|
101
|
-
post_install_message:
|
84
|
+
post_install_message:
|
102
85
|
rdoc_options: []
|
103
|
-
|
104
|
-
require_paths:
|
86
|
+
require_paths:
|
105
87
|
- lib
|
106
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: !binary |-
|
93
|
+
MA==
|
107
94
|
none: false
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
- 8
|
115
|
-
- 7
|
116
|
-
version: 1.8.7
|
117
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: !binary |-
|
100
|
+
MA==
|
118
101
|
none: false
|
119
|
-
requirements:
|
120
|
-
- - ">="
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
hash: 3
|
123
|
-
segments:
|
124
|
-
- 0
|
125
|
-
version: "0"
|
126
102
|
requirements: []
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
signing_key:
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.24
|
105
|
+
signing_key:
|
131
106
|
specification_version: 3
|
132
107
|
summary: Patches Date, Time, and DateTime ruby classes to keep track of precision
|
133
|
-
test_files:
|
134
|
-
- spec/date_time_precision/
|
108
|
+
test_files:
|
109
|
+
- spec/date_time_precision/active_support_spec.rb
|
135
110
|
- spec/date_time_precision/date_time_precision_spec.rb
|
136
111
|
- spec/spec_helper.rb
|
data/.rvmrc
DELETED
@@ -1,52 +0,0 @@
|
|
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
|