iso8601 0.8.5 → 0.8.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e579f970989da55f7fdabad30b90b62efbccbd4
4
- data.tar.gz: 30dd1505c5fe85a5c8c38a95754c20eb84bdd76a
3
+ metadata.gz: 5334fbf980f84b7381e75406df4aa142096aa195
4
+ data.tar.gz: 60b7dc50b02cbf3814faa7403b6d0c569f0ff611
5
5
  SHA512:
6
- metadata.gz: 3cff9b5a96b76ccdc52abde7cc29d4d565f9e4aa228a194262b65b591c73c158f65897d4407500182a7f4d4b9a8e0a3d7522123e65cbed314e1b4653807933e9
7
- data.tar.gz: 6b652966595cf6e441d5479963b80d418994b9062819c9b84a0130a656c7e4a657dd54515076b3a84592f4c8683f89ab039d8fb0021b25ab5406841cc4e104d2
6
+ metadata.gz: ab405a0c6d9b7c7a82c17d11604ece0155632197ea1a91351df1953ee1aeb787ce7335d84a230604e3de636ef682d8e9dc042df4792ea99027fd5d98d50781e9
7
+ data.tar.gz: 74c27f8c93e799e6de99cd1564a91381d01bfafdf984c86445d03548377d6c3985cbf4b67a78cde4ce1751181126f47de2eae27761bc17af7c882f9176ca639d
@@ -1,3 +1,9 @@
1
+ ## 0.8.6
2
+
3
+ * Fix #26 operations with Date, DateTime and Time with Duration (ex. `ISO8601::DateTime.new('2012-07-07T20:20:20Z') - ISO8601::Duration.new('PT10M')`.)
4
+ * Fix #25 accept time components with timezone and only hour component (ex. `ISO8601::Time.new('T10+01:00')`.)
5
+ * Fix Docker image for testing and inspecting.
6
+
1
7
  ## 0.8.5
2
8
 
3
9
  * Fix `DateTime#hash`
data/Dockerfile CHANGED
@@ -5,6 +5,6 @@ RUN mkdir -p /usr/src/iso8601
5
5
  WORKDIR /usr/src/iso8601
6
6
 
7
7
  COPY . /usr/src/iso8601
8
- RUN bundle update
8
+ RUN bundle install
9
9
 
10
10
  CMD ["bundle", "exec", "rspec"]
data/Makefile CHANGED
@@ -17,4 +17,9 @@ run :
17
17
 
18
18
  shell :
19
19
  docker run -it --rm -v $$(pwd):/usr/src/iso8601 $(IMAGE) \
20
- pry -r ./lib/iso8601
20
+ pry -r ./lib/iso8601
21
+
22
+ gem :
23
+ docker run -t --rm -v $$(pwd):/usr/src/iso8601 $(IMAGE) \
24
+ bundle exec rake build
25
+
data/README.md CHANGED
@@ -19,6 +19,9 @@ times) standard.
19
19
 
20
20
  Check the [changelog](https://github.com/arnau/ISO8601/blob/master/CHANGELOG.md) if you are upgrading from an older version.
21
21
 
22
+ ## Documentation
23
+
24
+ Check the [rubydoc documentation](http://www.rubydoc.info/gems/iso8601).
22
25
 
23
26
  ## Comments about this implementation
24
27
 
@@ -0,0 +1,13 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.0
4
+
5
+ dependencies:
6
+ override:
7
+ - 'rvm-exec 2.1.0 bundle install'
8
+ - 'rvm-exec 2.2.0 bundle install'
9
+
10
+ test:
11
+ override:
12
+ - 'rvm-exec 2.1.0 bundle exec rspec'
13
+ - 'rvm-exec 2.2.0 bundle exec rspec'
@@ -47,6 +47,7 @@ module ISO8601
47
47
  #
48
48
  # @return [ISO8601::Date] New date resulting of the addition
49
49
  def +(other)
50
+ other = other.to_days if other.respond_to?(:to_days)
50
51
  ISO8601::Date.new((@date + other).iso8601)
51
52
  end
52
53
  ##
@@ -56,6 +57,7 @@ module ISO8601
56
57
  #
57
58
  # @return [ISO8601::Date] New date resulting of the substraction
58
59
  def -(other)
60
+ other = other.to_days if other.respond_to?(:to_days)
59
61
  ISO8601::Date.new((@date - other).iso8601)
60
62
  end
61
63
  ##
@@ -30,7 +30,7 @@ module ISO8601
30
30
  #
31
31
  # @param [Numeric] other The seconds to add
32
32
  def +(other)
33
- moment = @date_time.to_time.localtime(zone) + other.round(1)
33
+ moment = @date_time.to_time.localtime(zone) + other.to_f.round(1)
34
34
 
35
35
  self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z'))
36
36
  end
@@ -39,7 +39,7 @@ module ISO8601
39
39
  #
40
40
  # @param [Numeric] other The seconds to substract
41
41
  def -(other)
42
- moment = @date_time.to_time.localtime(zone) - other.round(1)
42
+ moment = @date_time.to_time.localtime(zone) - other.to_f.round(1)
43
43
 
44
44
  self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z'))
45
45
  end
@@ -170,6 +170,21 @@ module ISO8601
170
170
  atoms = [years, months, weeks, days, hours, minutes, seconds]
171
171
  atoms.map(&:to_seconds).reduce(&:+)
172
172
  end
173
+ ##
174
+ # @return [Numeric] The duration in days
175
+ def to_days
176
+ (to_seconds / 86400)
177
+ end
178
+ ##
179
+ # @return [Integer] The integer part of the duration in seconds
180
+ def to_i
181
+ to_seconds.to_i
182
+ end
183
+ ##
184
+ # @return [Float] The duration in seconds coerced to float
185
+ def to_f
186
+ to_seconds.to_f
187
+ end
173
188
 
174
189
  private
175
190
 
@@ -63,7 +63,7 @@ module ISO8601
63
63
  #
64
64
  # @return [ISO8601::Time] New time resulting of the addition
65
65
  def +(other)
66
- moment = @time.to_time.localtime(zone) + other.round(1)
66
+ moment = @time.to_time.localtime(zone) + other.to_f.round(1)
67
67
  base = ::Date.parse(moment.strftime('%Y-%m-%d'))
68
68
 
69
69
  self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
@@ -75,7 +75,7 @@ module ISO8601
75
75
  #
76
76
  # @return [ISO8601::Time] New time resulting of the substraction
77
77
  def -(other)
78
- moment = @time.to_time.localtime(zone) - other.round(1)
78
+ moment = @time.to_time.localtime(zone) - other.to_f.round(1)
79
79
  base = ::Date.parse(moment.strftime('%Y-%m-%d'))
80
80
 
81
81
  self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
@@ -115,22 +115,32 @@ module ISO8601
115
115
  fail ISO8601::Errors::UnknownPattern, @original if hour.nil?
116
116
 
117
117
  @separator = separator
118
+ require_separator = !minute.nil?
118
119
 
119
120
  hour = hour.to_i
120
121
  minute = minute.to_i
121
122
  second = second.nil? ? 0.0 : second.tr(',', '.').to_f
122
123
 
123
- fail ISO8601::Errors::UnknownPattern, @original unless valid_zone?(zone)
124
+ atoms = [hour, minute, second, zone].compact
124
125
 
125
- [hour, minute, second, zone].compact
126
- end
127
126
 
128
- def valid_zone?(zone)
127
+ fail ISO8601::Errors::UnknownPattern, @original unless valid_zone?(zone, require_separator)
128
+
129
+ atoms
130
+ end
131
+ ##
132
+ # @param [String] zone The timezone offset as Z or +-hh[:mm].
133
+ # @param [Boolean] require_separator Flag to determine if the separator
134
+ # consistency check is required as patterns with only hour atom have no
135
+ # separator but the timezone can.
136
+ def valid_zone?(zone, require_separator)
129
137
  zone_regexp = /^(Z|[+-]\d{2}(?:(:?)\d{2})?)$/
130
138
  _, offset, separator = zone_regexp.match(zone).to_a.compact
131
139
 
132
140
  wrong_pattern = !zone.nil? && offset.nil?
133
- invalid_separators = zone.to_s.match(/^[+-]\d{2}:?\d{2}$/) && (@separator != separator)
141
+ if (require_separator)
142
+ invalid_separators = zone.to_s.match(/^[+-]\d{2}:?\d{2}$/) && (@separator != separator)
143
+ end
134
144
 
135
145
  !(wrong_pattern || invalid_separators)
136
146
  end
@@ -1,5 +1,5 @@
1
1
  module ISO8601
2
2
  ##
3
3
  # The gem version
4
- VERSION = '0.8.5'
4
+ VERSION = '0.8.6'
5
5
  end
@@ -54,15 +54,21 @@ describe ISO8601::Date do
54
54
  end
55
55
 
56
56
  describe '#+' do
57
- it "should return the result of the addition" do
57
+ it "should return the result of the addition of a number" do
58
58
  expect((ISO8601::Date.new('2012-07-07') + 7).to_s).to eq('2012-07-14')
59
59
  end
60
+ it "should return the result of the addition of a Duration" do
61
+ expect((ISO8601::Date.new('2012-07-07') + ISO8601::Duration.new('P7D')).to_s).to eq('2012-07-14')
62
+ end
60
63
  end
61
64
 
62
65
  describe '#-' do
63
- it "should return the result of the substraction" do
66
+ it "should return the result of the subtraction" do
64
67
  expect((ISO8601::Date.new('2012-07-07') - 7).to_s).to eq('2012-06-30')
65
68
  end
69
+ it "should return the result of the subtraction" do
70
+ expect((ISO8601::Date.new('2012-07-07') - ISO8601::Duration.new('P7D')).to_s).to eq('2012-06-30')
71
+ end
66
72
  end
67
73
 
68
74
  describe '#to_a' do
@@ -102,7 +102,7 @@ describe ISO8601::DateTime do
102
102
  end
103
103
 
104
104
  describe '#+' do
105
- it "should return the result of the addition" do
105
+ it "should return the result of the addition of a number" 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
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
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')
@@ -111,12 +111,19 @@ describe ISO8601::DateTime do
111
111
  expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).second).to eq(30.5)
112
112
  expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10.09).second).to eq(30.1)
113
113
  end
114
+ it "should return the result of the addition of a Duration" do
115
+ expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') + ISO8601::Duration.new("PT10S")).to_s).to eq('2012-07-07T20:20:30+00:00')
116
+ expect((ISO8601::DateTime.new('2015-07-07T20:20:20Z') + ISO8601::Duration.new("P10D")).to_s).to eq('2015-07-17T20:20:20+00:00')
117
+ end
114
118
  end
115
119
 
116
120
  describe '#-' do
117
- it "should return the result of the substraction" do
121
+ it "should return the result of the subtraction of a number" do
118
122
  expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') - 10).to_s).to eq('2012-07-07T20:20:10+00:00')
119
123
  end
124
+ it "should return the result of the subtraction of a Duration" do
125
+ expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') - ISO8601::Duration.new(10)).to_s).to eq('2012-07-07T20:20:10+00:00')
126
+ end
120
127
  end
121
128
 
122
129
  describe '#to_a' do
@@ -100,7 +100,7 @@ RSpec.describe ISO8601::Duration do
100
100
  expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) - ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
101
101
  end
102
102
 
103
- it "should return the result of the substraction" do
103
+ it "should return the result of the subtraction" do
104
104
  expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S') - ISO8601::Duration.new('PT10S')).to be_an_instance_of(ISO8601::Duration)
105
105
  expect((ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('PT10S')).to_s).to eq('P1Y1M1DT1H1M1S')
106
106
  expect((ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('P1Y1M1DT1H1M11S')).to_s).to eq('PT0S')
@@ -111,6 +111,13 @@ RSpec.describe ISO8601::Duration do
111
111
  end
112
112
  end
113
113
 
114
+ describe "#to_days" do
115
+ it "should return the days of a duration" do
116
+ expect(ISO8601::Duration.new('P1Y', ISO8601::DateTime.new('2010-01-01')).to_days).to eq(365)
117
+ expect(ISO8601::Duration.new('P1D').to_days).to eq(1)
118
+ end
119
+ end
120
+
114
121
  describe '#to_seconds' do
115
122
  context 'positive durations' do
116
123
  it "should return the seconds of a P[n]Y duration in a common year" do
@@ -17,6 +17,8 @@ describe ISO8601::Time do
17
17
 
18
18
  it "should parse any allowed pattern" do
19
19
  expect { ISO8601::Time.new('T10') }.to_not raise_error
20
+ expect { ISO8601::Time.new('T10-00:00') }.to_not raise_error
21
+ expect { ISO8601::Time.new('T10Z') }.to_not raise_error
20
22
  expect { ISO8601::Time.new('T10:30') }.to_not raise_error
21
23
  expect { ISO8601::Time.new('T10:30Z') }.to_not raise_error
22
24
  expect { ISO8601::Time.new('T10:30:12') }.to_not raise_error
@@ -28,6 +30,7 @@ describe ISO8601::Time do
28
30
  expect { ISO8601::Time.new('T103012+04') }.to_not raise_error
29
31
  expect { ISO8601::Time.new('T10:30:12-00:00') }.to_not raise_error
30
32
  expect { ISO8601::Time.new('T16:26:10,5Z') }.to_not raise_error
33
+ expect { ISO8601::Time.new('T10+00:00') }.to_not raise_error
31
34
  end
32
35
 
33
36
  context 'reduced patterns' do
@@ -56,7 +59,7 @@ describe ISO8601::Time do
56
59
  end
57
60
 
58
61
  describe '#+' do
59
- it "should return the result of the addition" do
62
+ it "should return the result of the addition of a number" do
60
63
  expect((ISO8601::Time.new('T20:20:20Z') + 10).to_s).to eq('T20:20:30+00:00')
61
64
  expect((ISO8601::Time.new('T20:20:20.5Z') + 10).to_s).to eq('T20:20:30.5+00:00')
62
65
  expect((ISO8601::Time.new('T20:20:20+02:00') + 10.09).to_s).to eq('T20:20:30.1+02:00')
@@ -65,14 +68,19 @@ describe ISO8601::Time do
65
68
  expect((ISO8601::Time.new('T20:20:20.5Z') + 10).second).to eq(30.5)
66
69
  expect((ISO8601::Time.new('T20:20:20+02:00') + 10.09).second).to eq(30.1)
67
70
  end
68
-
71
+ it "should return the result of the addition of a Duration" do
72
+ expect((ISO8601::Time.new('T20:20:20Z') + ISO8601::Duration.new("PT10S")).to_s).to eq('T20:20:30+00:00')
73
+ end
69
74
  end
70
75
 
71
76
  describe '#-' do
72
- it "should return the result of the substraction" do
77
+ it "should return the result of the subtraction of a number" do
73
78
  expect((ISO8601::Time.new('T20:20:20+01:00') - 10).to_s).to eq('T20:20:10+01:00')
74
79
  expect((ISO8601::Time.new('T20:20:20.11+02:00') - 10).to_s).to eq('T20:20:10.1+02:00')
75
80
  end
81
+ it "should return the result of the subtraction of a Duration" do
82
+ expect((ISO8601::Time.new('T20:20:20+01:00') - ISO8601::Duration.new("PT10S")).to_s).to eq('T20:20:10+01:00')
83
+ end
76
84
  end
77
85
 
78
86
  describe '#to_a' do
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.5
4
+ version: 0.8.6
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-02-15 00:00:00.000000000 Z
11
+ date: 2015-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -59,6 +59,7 @@ files:
59
59
  - README.md
60
60
  - Rakefile
61
61
  - Vagrantfile
62
+ - circle.yml
62
63
  - iso8601.gemspec
63
64
  - lib/iso8601.rb
64
65
  - lib/iso8601/atoms.rb
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  version: '0'
95
96
  requirements: []
96
97
  rubyforge_project: iso8601
97
- rubygems_version: 2.0.14
98
+ rubygems_version: 2.4.5
98
99
  signing_key:
99
100
  specification_version: 4
100
101
  summary: Ruby parser to work with ISO 8601 dateTimes and durations - http://en.wikipedia.org/wiki/ISO_8601