iso8601 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +9 -0
- data/.gitignore +4 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +6 -0
- data/Dockerfile +10 -0
- data/Gemfile +5 -0
- data/Makefile +20 -0
- data/README.md +35 -3
- data/Vagrantfile +41 -0
- data/lib/iso8601/date_time.rb +15 -13
- data/lib/iso8601/time.rb +8 -12
- data/lib/iso8601/version.rb +1 -1
- data/spec/iso8601/date_time_spec.rb +24 -2
- data/spec/iso8601/time_spec.rb +9 -3
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e579f970989da55f7fdabad30b90b62efbccbd4
|
4
|
+
data.tar.gz: 30dd1505c5fe85a5c8c38a95754c20eb84bdd76a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cff9b5a96b76ccdc52abde7cc29d4d565f9e4aa228a194262b65b591c73c158f65897d4407500182a7f4d4b9a8e0a3d7522123e65cbed314e1b4653807933e9
|
7
|
+
data.tar.gz: 6b652966595cf6e441d5479963b80d418994b9062819c9b84a0130a656c7e4a657dd54515076b3a84592f4c8683f89ab039d8fb0021b25ab5406841cc4e104d2
|
data/.editorconfig
ADDED
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Dockerfile
ADDED
data/Gemfile
CHANGED
data/Makefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
TAG ?= latest
|
2
|
+
IMAGE_NAME ?= arnau/iso8601
|
3
|
+
IMAGE = $(IMAGE_NAME):$(TAG)
|
4
|
+
ORPHAN_IMAGES = `docker images -f 'dangling=true'`
|
5
|
+
|
6
|
+
build :
|
7
|
+
docker build -t $(IMAGE) .
|
8
|
+
|
9
|
+
rmo :
|
10
|
+
docker rmi $(ORPHAN_IMAGES)
|
11
|
+
|
12
|
+
rmi :
|
13
|
+
docker rmi $(IMAGE)
|
14
|
+
|
15
|
+
run :
|
16
|
+
docker run -t --rm -v $$(pwd):/usr/src/iso8601 $(IMAGE)
|
17
|
+
|
18
|
+
shell :
|
19
|
+
docker run -it --rm -v $$(pwd):/usr/src/iso8601 $(IMAGE) \
|
20
|
+
pry -r ./lib/iso8601
|
data/README.md
CHANGED
@@ -14,9 +14,7 @@ times) standard.
|
|
14
14
|
|
15
15
|
## Supported versions
|
16
16
|
|
17
|
-
* MRI 1.9.3
|
18
|
-
* MRI 2.0
|
19
|
-
* MRI 2.1
|
17
|
+
* MRI 1.9.3, 2.0, 2.1, 2.2
|
20
18
|
* RBX 2
|
21
19
|
|
22
20
|
Check the [changelog](https://github.com/arnau/ISO8601/blob/master/CHANGELOG.md) if you are upgrading from an older version.
|
@@ -32,6 +30,14 @@ sign to be able to represent negative values:
|
|
32
30
|
(ISO8601::Duration.new('PT10S') - ISO8601::Duration.new('PT12S')).to_s #=> '-PT2S'
|
33
31
|
(ISO8601::Duration.new('-PT10S') + ISO8601::Duration.new('PT12S')).to_s #=> 'PT2S'
|
34
32
|
|
33
|
+
### Fractional seconds precision
|
34
|
+
|
35
|
+
Fractional seconds for `ISO8601::DateTime` and `ISO8601::Time` are rounded to
|
36
|
+
one decimal.
|
37
|
+
|
38
|
+
ISO8601::DateTime.new('2015-02-03T10:11:12.12').second #=> 12.1
|
39
|
+
ISO8601::Time.new('T10:11:12.16').second #=> 12.2
|
40
|
+
|
35
41
|
|
36
42
|
## Differences with core Date, Time and DateTime
|
37
43
|
|
@@ -88,6 +94,32 @@ Week dates raise an error when two digit days provied instead of return monday:
|
|
88
94
|
DateTime.new('2014-W15-02') # => #<Date: 2014-04-07 ((2456755j,0s,0n),+0s,2299161j)>
|
89
95
|
|
90
96
|
|
97
|
+
## Testing
|
98
|
+
|
99
|
+
### Raw
|
100
|
+
|
101
|
+
# Install a Ruby flavour
|
102
|
+
$ bundle install
|
103
|
+
$ bundle exec rspec
|
104
|
+
|
105
|
+
### Docker (experimental)
|
106
|
+
|
107
|
+
This way is in an early stage so for now it's only possible to test one Ruby
|
108
|
+
version (currently Ruby 2.2.)
|
109
|
+
|
110
|
+
# Install Docker
|
111
|
+
$ make build
|
112
|
+
$ make run
|
113
|
+
|
114
|
+
### Vagrant (experimental)
|
115
|
+
|
116
|
+
This way is in an early stage so for now it's only possible to test one Ruby
|
117
|
+
version (currently Ruby 2.2.)
|
118
|
+
|
119
|
+
# Install Vagrant and Virtualbox
|
120
|
+
$ vagrant up mri-2.2
|
121
|
+
|
122
|
+
|
91
123
|
## Contributing
|
92
124
|
|
93
125
|
[Contributors](https://github.com/arnau/ISO8601/graphs/contributors)
|
data/Vagrantfile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
# vi: set ft=ruby :
|
3
|
+
|
4
|
+
Vagrant.require_version '>= 1.6.0'
|
5
|
+
Vagrant.configure(2) do |config|
|
6
|
+
config.vm.define 'iso8601' do |c|
|
7
|
+
c.vm.box = "coreos-alpha"
|
8
|
+
c.vm.box_version = '>= 561.0.0'
|
9
|
+
c.vm.box_url = "http://alpha.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
|
10
|
+
|
11
|
+
c.vm.network :private_network, ip: '10.10.1.100'
|
12
|
+
# Allows dynamic synced folders with docker as it seems Vagrant doesn't
|
13
|
+
# work well with coreOS (?)
|
14
|
+
c.vm.synced_folder '.', '/home/core/share', {
|
15
|
+
id: 'core',
|
16
|
+
nfs: true,
|
17
|
+
mount_options: ['nolock,vers=3,udp']
|
18
|
+
}
|
19
|
+
|
20
|
+
c.vm.provider :virtualbox do |vb|
|
21
|
+
vb.check_guest_additions = false
|
22
|
+
vb.functional_vboxsf = false
|
23
|
+
vb.memory = 1024
|
24
|
+
vb.cpus = 1
|
25
|
+
vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
config.vm.define 'mri-2.2' do |c|
|
30
|
+
c.vm.provider 'docker' do |d|
|
31
|
+
d.vagrant_machine = 'iso8601'
|
32
|
+
d.vagrant_vagrantfile = './Vagrantfile'
|
33
|
+
d.build_dir = '.'
|
34
|
+
d.build_args = ['-t', 'arnau/iso8601:2.2']
|
35
|
+
d.create_args = [
|
36
|
+
'-v', '/home/core/share:/usr/src/iso8601',
|
37
|
+
]
|
38
|
+
d.remains_running = false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/iso8601/date_time.rb
CHANGED
@@ -18,47 +18,49 @@ module ISO8601
|
|
18
18
|
|
19
19
|
attr_reader :second
|
20
20
|
|
21
|
-
FORMAT = '%Y-%m-%dT%H:%M:%S%:z'
|
22
|
-
FORMAT_WITH_FRACTION = '%Y-%m-%dT%H:%M:%S.%2N%:z'
|
23
|
-
|
24
21
|
##
|
25
22
|
# @param [String] date_time The datetime pattern
|
26
23
|
def initialize(date_time)
|
27
24
|
@original = date_time
|
28
25
|
@date_time = parse(date_time)
|
29
|
-
@second = @date_time.second + @date_time.second_fraction.to_f
|
26
|
+
@second = @date_time.second + @date_time.second_fraction.to_f.round(1)
|
30
27
|
end
|
31
28
|
##
|
32
29
|
# Addition
|
33
30
|
#
|
34
31
|
# @param [Numeric] other The seconds to add
|
35
32
|
def +(other)
|
36
|
-
moment = @date_time.to_time.localtime(zone) + other
|
37
|
-
format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION
|
33
|
+
moment = @date_time.to_time.localtime(zone) + other.round(1)
|
38
34
|
|
39
|
-
self.class.new(moment.strftime(
|
35
|
+
self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z'))
|
40
36
|
end
|
41
37
|
##
|
42
38
|
# Substraction
|
43
39
|
#
|
44
40
|
# @param [Numeric] other The seconds to substract
|
45
41
|
def -(other)
|
46
|
-
moment = @date_time.to_time.localtime(zone) - other
|
47
|
-
format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION
|
42
|
+
moment = @date_time.to_time.localtime(zone) - other.round(1)
|
48
43
|
|
49
|
-
self.class.new(moment.strftime(
|
44
|
+
self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z'))
|
50
45
|
end
|
51
46
|
##
|
52
47
|
# Converts DateTime to a formated string
|
53
48
|
def to_s
|
54
|
-
|
55
|
-
|
49
|
+
second_format = (second % 1).zero? ? '%02d' % second : '%04.1f' % second
|
50
|
+
|
51
|
+
"%04d-%02d-%02dT%02d:%02d:#{second_format}#{zone}" % atoms
|
56
52
|
end
|
57
53
|
##
|
58
54
|
# Converts DateTime to an array of atoms.
|
59
55
|
def to_a
|
60
56
|
[year, month, day, hour, minute, second, zone]
|
61
57
|
end
|
58
|
+
alias_method :atoms, :to_a
|
59
|
+
##
|
60
|
+
# Converts DateTime to a floating point number of seconds since the Epoch.
|
61
|
+
def to_f
|
62
|
+
to_time.to_f
|
63
|
+
end
|
62
64
|
##
|
63
65
|
# @param [#hash] other The contrast to compare against
|
64
66
|
#
|
@@ -76,7 +78,7 @@ module ISO8601
|
|
76
78
|
##
|
77
79
|
# @return [Fixnum]
|
78
80
|
def hash
|
79
|
-
[
|
81
|
+
[to_f, self.class].hash
|
80
82
|
end
|
81
83
|
|
82
84
|
private
|
data/lib/iso8601/time.rb
CHANGED
@@ -27,9 +27,6 @@ module ISO8601
|
|
27
27
|
# The original atoms
|
28
28
|
attr_reader :atoms
|
29
29
|
|
30
|
-
FORMAT = 'T%H:%M:%S%:z'
|
31
|
-
FORMAT_WITH_FRACTION = 'T%H:%M:%S.%2N%:z'
|
32
|
-
|
33
30
|
##
|
34
31
|
# @param [String] input The time pattern
|
35
32
|
# @param [Date] base The base date to determine the time
|
@@ -38,7 +35,7 @@ module ISO8601
|
|
38
35
|
@base = base
|
39
36
|
@atoms = atomize(input)
|
40
37
|
@time = compose(@atoms, @base)
|
41
|
-
@second = @time.second + @time.second_fraction.to_f
|
38
|
+
@second = @time.second + @time.second_fraction.to_f.round(1)
|
42
39
|
end
|
43
40
|
##
|
44
41
|
# @param [#hash] other The contrast to compare against
|
@@ -66,11 +63,10 @@ module ISO8601
|
|
66
63
|
#
|
67
64
|
# @return [ISO8601::Time] New time resulting of the addition
|
68
65
|
def +(other)
|
69
|
-
moment = @time.to_time.localtime(zone) + other
|
70
|
-
format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION
|
66
|
+
moment = @time.to_time.localtime(zone) + other.round(1)
|
71
67
|
base = ::Date.parse(moment.strftime('%Y-%m-%d'))
|
72
68
|
|
73
|
-
|
69
|
+
self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
|
74
70
|
end
|
75
71
|
##
|
76
72
|
# Backwards the date the given amount of seconds.
|
@@ -79,17 +75,17 @@ module ISO8601
|
|
79
75
|
#
|
80
76
|
# @return [ISO8601::Time] New time resulting of the substraction
|
81
77
|
def -(other)
|
82
|
-
moment = @time.to_time.localtime(zone) - other
|
83
|
-
format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION
|
78
|
+
moment = @time.to_time.localtime(zone) - other.round(1)
|
84
79
|
base = ::Date.parse(moment.strftime('%Y-%m-%d'))
|
85
80
|
|
86
|
-
|
81
|
+
self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
|
87
82
|
end
|
88
83
|
##
|
89
84
|
# Converts self to a time component representation.
|
90
85
|
def to_s
|
91
|
-
|
92
|
-
|
86
|
+
second_format = (second % 1).zero? ? '%02d' % second : '%04.1f' % second
|
87
|
+
|
88
|
+
"T%02d:%02d:#{second_format}#{zone}" % atoms
|
93
89
|
end
|
94
90
|
##
|
95
91
|
# Converts self to an array of atoms.
|
data/lib/iso8601/version.rb
CHANGED
@@ -104,8 +104,12 @@ describe ISO8601::DateTime do
|
|
104
104
|
describe '#+' do
|
105
105
|
it "should return the result of the addition" do
|
106
106
|
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') + 10).to_s).to eq('2012-07-07T20:20:30+00:00')
|
107
|
-
expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).to_s).to eq('2012-07-07T20:20:30.
|
108
|
-
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10).to_s).to eq('2012-07-07T20:20:30+02:00')
|
107
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).to_s).to eq('2012-07-07T20:20:30.5+00:00')
|
108
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10.09).to_s).to eq('2012-07-07T20:20:30.1+02:00')
|
109
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10.1).to_s).to eq('2012-07-07T20:20:30.1+02:00')
|
110
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10).second).to eq(30)
|
111
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).second).to eq(30.5)
|
112
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10.09).second).to eq(30.1)
|
109
113
|
end
|
110
114
|
end
|
111
115
|
|
@@ -132,4 +136,22 @@ describe ISO8601::DateTime do
|
|
132
136
|
expect(subject.hash).to eq(contrast.hash)
|
133
137
|
end
|
134
138
|
end
|
139
|
+
|
140
|
+
describe '#==' do
|
141
|
+
it "should identify loose precision datetimes" do
|
142
|
+
expect(ISO8601::DateTime.new('2014') == ISO8601::DateTime.new('2014')).to be_truthy
|
143
|
+
expect(ISO8601::DateTime.new('2014') == ISO8601::DateTime.new('2015')).to be_falsy
|
144
|
+
expect(ISO8601::DateTime.new('2014-10') == ISO8601::DateTime.new('2014-11')).to be_falsy
|
145
|
+
expect(ISO8601::DateTime.new('2014-10') == ISO8601::DateTime.new('2014-11')).to be_falsy
|
146
|
+
expect(ISO8601::DateTime.new('2014-10-11T12') == ISO8601::DateTime.new('2014-10-11T13')).to be_falsy
|
147
|
+
expect(ISO8601::DateTime.new('2014-10-11T12:13') == ISO8601::DateTime.new('2014-10-11T12:14')).to be_falsy
|
148
|
+
expect(ISO8601::DateTime.new('2014-10-11T12:13:10') == ISO8601::DateTime.new('2014-10-11T12:13:10.0')).to be_truthy
|
149
|
+
expect(ISO8601::DateTime.new('2014-10-11T12:13:10.1') == ISO8601::DateTime.new('2014-10-11T12:13:10.2')).to be_falsy
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should identify as the same when two dates with different timezones are the same timestamp" do
|
153
|
+
expect(ISO8601::DateTime.new('2014-10-11T12:13:14Z') == ISO8601::DateTime.new('2014-10-11T13:13:14+01:00')).to be_truthy
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
135
157
|
end
|
data/spec/iso8601/time_spec.rb
CHANGED
@@ -57,15 +57,21 @@ describe ISO8601::Time do
|
|
57
57
|
|
58
58
|
describe '#+' do
|
59
59
|
it "should return the result of the addition" do
|
60
|
-
expect((ISO8601::Time.new('T20:20:
|
61
|
-
expect((ISO8601::Time.new('T20:20:20.
|
60
|
+
expect((ISO8601::Time.new('T20:20:20Z') + 10).to_s).to eq('T20:20:30+00:00')
|
61
|
+
expect((ISO8601::Time.new('T20:20:20.5Z') + 10).to_s).to eq('T20:20:30.5+00:00')
|
62
|
+
expect((ISO8601::Time.new('T20:20:20+02:00') + 10.09).to_s).to eq('T20:20:30.1+02:00')
|
63
|
+
expect((ISO8601::Time.new('T20:20:20+02:00') + 10.1).to_s).to eq('T20:20:30.1+02:00')
|
64
|
+
expect((ISO8601::Time.new('T20:20:20+02:00') + 10).second).to eq(30)
|
65
|
+
expect((ISO8601::Time.new('T20:20:20.5Z') + 10).second).to eq(30.5)
|
66
|
+
expect((ISO8601::Time.new('T20:20:20+02:00') + 10.09).second).to eq(30.1)
|
62
67
|
end
|
68
|
+
|
63
69
|
end
|
64
70
|
|
65
71
|
describe '#-' do
|
66
72
|
it "should return the result of the substraction" do
|
67
73
|
expect((ISO8601::Time.new('T20:20:20+01:00') - 10).to_s).to eq('T20:20:10+01:00')
|
68
|
-
expect((ISO8601::Time.new('T20:20:20.11+02:00') - 10).to_s).to eq('T20:20:10.
|
74
|
+
expect((ISO8601::Time.new('T20:20:20.11+02:00') - 10).to_s).to eq('T20:20:10.1+02:00')
|
69
75
|
end
|
70
76
|
end
|
71
77
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iso8601
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arnau Siches
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -47,14 +47,18 @@ executables: []
|
|
47
47
|
extensions: []
|
48
48
|
extra_rdoc_files: []
|
49
49
|
files:
|
50
|
+
- ".editorconfig"
|
50
51
|
- ".gitignore"
|
51
52
|
- ".rubocop.yml"
|
52
53
|
- ".travis.yml"
|
53
54
|
- CHANGELOG.md
|
55
|
+
- Dockerfile
|
54
56
|
- Gemfile
|
55
57
|
- LICENSE
|
58
|
+
- Makefile
|
56
59
|
- README.md
|
57
60
|
- Rakefile
|
61
|
+
- Vagrantfile
|
58
62
|
- iso8601.gemspec
|
59
63
|
- lib/iso8601.rb
|
60
64
|
- lib/iso8601/atoms.rb
|