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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a95ef6cc71f2a2e5af6466a026f86b790f51893
4
- data.tar.gz: 9c656534277c2fd381b03838543b251163f5b274
3
+ metadata.gz: 1e579f970989da55f7fdabad30b90b62efbccbd4
4
+ data.tar.gz: 30dd1505c5fe85a5c8c38a95754c20eb84bdd76a
5
5
  SHA512:
6
- metadata.gz: da5afe9beda6d394dd1a8e827d6fbf229f0c6648dd9493de4e223c23735a7c47757981a320d221d2caec14c97306a7e745ae078eec8495efe97e716b569ba03e
7
- data.tar.gz: e76c0d4bac7906ba82098b4820658c68dca660b5d7493fd00ba5ec111b57b537a4d3c71034afeb70ae9b6090026dace9a6a122980c2da41834f26a1a5ac8c11a
6
+ metadata.gz: 3cff9b5a96b76ccdc52abde7cc29d4d565f9e4aa228a194262b65b591c73c158f65897d4407500182a7f4d4b9a8e0a3d7522123e65cbed314e1b4653807933e9
7
+ data.tar.gz: 6b652966595cf6e441d5479963b80d418994b9062819c9b84a0130a656c7e4a657dd54515076b3a84592f4c8683f89ab039d8fb0021b25ab5406841cc4e104d2
@@ -0,0 +1,9 @@
1
+ [Makefile]
2
+ indent_style = tab
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
data/.gitignore CHANGED
@@ -2,6 +2,10 @@
2
2
  .DS_Store
3
3
  .*.swp
4
4
 
5
+ # development side effects
6
+ .vagrant
7
+ sandbox
8
+
5
9
  # files created by running the specs
6
10
  tmp
7
11
 
@@ -3,6 +3,7 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.2
6
+ - 2.2.0
6
7
  - ruby-head
7
8
  - rbx-2
8
9
  - jruby-head
@@ -1,3 +1,9 @@
1
+ ## 0.8.5
2
+
3
+ * Fix `DateTime#hash`
4
+ * Fix `DateTime#second` and `Time#second` precision. Now it's rounded to the
5
+ first decimal.
6
+
1
7
  ## 0.8.4
2
8
 
3
9
  * Remove unwanted log.
@@ -0,0 +1,10 @@
1
+ FROM ruby:2.2
2
+ MAINTAINER Arnau Siches <asiches@gmail.com>
3
+
4
+ RUN mkdir -p /usr/src/iso8601
5
+ WORKDIR /usr/src/iso8601
6
+
7
+ COPY . /usr/src/iso8601
8
+ RUN bundle update
9
+
10
+ CMD ["bundle", "exec", "rspec"]
data/Gemfile CHANGED
@@ -1,3 +1,8 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ gem 'pry-doc'
8
+ end
@@ -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)
@@ -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
@@ -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(format))
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(format))
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
- format = @date_time.second_fraction.zero? ? FORMAT : FORMAT_WITH_FRACTION
55
- @date_time.strftime(format)
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
- [second, self.class].hash
81
+ [to_f, self.class].hash
80
82
  end
81
83
 
82
84
  private
@@ -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
- ISO8601::Time.new(moment.strftime(format), base)
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
- ISO8601::Time.new(moment.strftime(format), base)
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
- format = @time.second_fraction.zero? ? FORMAT : FORMAT_WITH_FRACTION
92
- @time.strftime(format)
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.
@@ -1,5 +1,5 @@
1
1
  module ISO8601
2
2
  ##
3
3
  # The gem version
4
- VERSION = '0.8.4'
4
+ VERSION = '0.8.5'
5
5
  end
@@ -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.50+00:00')
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
@@ -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:20+02:00') + 10).to_s).to eq('T20:20:30+02:00')
61
- expect((ISO8601::Time.new('T20:20:20.11+02:00') + 10).to_s).to eq('T20:20:30.11+02:00')
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.11+02:00')
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
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-01-23 00:00:00.000000000 Z
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