iso8601 0.1.1 → 0.2

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/README.md CHANGED
@@ -14,7 +14,7 @@ Because Durations and DateTime has substract method, Durations has sign to repre
14
14
 
15
15
  ## TODO
16
16
 
17
- * Decimal fraction in dateTime and duration patterns
17
+ * Decimal fraction in dateTime patterns
18
18
  * Recurring time intervals
19
19
  * Ordinal date pattern (YYYY-DDD)
20
20
  * Week date pattern (YYYY-Www-D)
@@ -23,6 +23,7 @@ Because Durations and DateTime has substract method, Durations has sign to repre
23
23
 
24
24
  * [Nick Lynch](https://github.com/njlynch)
25
25
  * [Pelle Braendgaard](https://github.com/pelle)
26
+ * [Takahiro Noda](https://github.com/tnoda)
26
27
 
27
28
  ## Credits
28
29
  Arnau Siches under [LGPL](http://www.gnu.org/licenses/lgpl.html) license. LICENSE file for details.
data/lib/iso8601.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module ISO8601
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2"
3
3
  end
4
4
 
5
5
  require "time"
@@ -4,7 +4,7 @@ module ISO8601
4
4
  class Duration
5
5
  attr_reader :base, :atoms
6
6
  def initialize(duration, base = nil)
7
- @duration = /^(\+|-)?P(((\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?)|(\d+W))$/.match(duration)
7
+ @duration = /^(\+|-)?P(((\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+(?:\.\d+)?S)?)?)|(\d+W))$/.match(duration)
8
8
  @base = base #date base for duration calculations
9
9
  valid_pattern?
10
10
  valid_base?
@@ -104,7 +104,7 @@ module ISO8601
104
104
  days, d_mod = (m_mod / self.days.factor).to_i, (m_mod % self.days.factor)
105
105
  hours, h_mod = (d_mod / self.hours.factor).to_i, (d_mod % self.hours.factor)
106
106
  minutes, mi_mod = (h_mod / self.minutes.factor).to_i, (h_mod % self.minutes.factor)
107
- seconds = mi_mod.to_i
107
+ seconds = mi_mod.div(1) == mi_mod ? mi_mod.to_i : mi_mod.to_f # Coerce to Integer when needed (`PT1S` instead of `PT1.0S`)
108
108
 
109
109
  seconds = (seconds != 0 or (years == 0 and months == 0 and days == 0 and hours == 0 and minutes == 0)) ? "#{seconds}S" : ""
110
110
  minutes = (minutes != 0) ? "#{minutes}M" : ""
@@ -18,6 +18,7 @@ class TestDuration < Test::Unit::TestCase
18
18
  assert_nothing_raised() { ISO8601::Duration.new("PT1H1M") }
19
19
  assert_nothing_raised() { ISO8601::Duration.new("PT1H1S") }
20
20
  assert_nothing_raised() { ISO8601::Duration.new("PT1H1M1S") }
21
+ assert_nothing_raised() { ISO8601::Duration.new("P1Y2M3DT5H20M30.123S")}
21
22
  assert_nothing_raised() { ISO8601::Duration.new("+PT1H1M1S") }
22
23
  assert_nothing_raised() { ISO8601::Duration.new("-PT1H1M1S") }
23
24
  assert_raise(ISO8601::Errors::UnknownPattern) { ISO8601::Duration.new("~PT1H1M1S") }
@@ -70,7 +71,9 @@ class TestDuration < Test::Unit::TestCase
70
71
  assert_equal(27003, ISO8601::Duration.new("PT7H30M3S").to_seconds, "PT[n]H[n]M[n]S form")
71
72
  assert_equal(2040, ISO8601::Duration.new("PT34M").to_seconds, "PT[n]M form")
72
73
  assert_equal(2050, ISO8601::Duration.new("PT34M10S").to_seconds, "PT[n]M[n]S form")
74
+ assert_equal(2050.4, ISO8601::Duration.new("PT34M10.4S").to_seconds, "PT[n]M[n.m]S form")
73
75
  assert_equal(10, ISO8601::Duration.new("PT10S").to_seconds, "PT[n]S form")
76
+ assert_equal(10.5, ISO8601::Duration.new("PT10.5S").to_seconds, "PT[n.m]S form")
74
77
  assert_equal(ISO8601::Duration.new("PT1H15M").to_seconds, ISO8601::Duration.new("PT75M").to_seconds, "PT[n]H[n]M equivalent to PT[n]M")
75
78
  end
76
79
 
@@ -87,5 +90,6 @@ class TestDuration < Test::Unit::TestCase
87
90
 
88
91
  def test_seconds_to_iso
89
92
  assert_equal(ISO8601::Duration.new("P1Y2M3DT4H5M6S").to_seconds, ISO8601::Duration.seconds_to_iso(37065906).to_seconds)
93
+ assert_equal(ISO8601::Duration.new("P1Y2M3DT4H5M6.9S").to_seconds, ISO8601::Duration.seconds_to_iso(37065906.9).to_seconds)
90
94
  end
91
95
  end
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Arnau Siches
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2012-01-30 00:00:00 +01:00
16
+ date: 2012-03-05 00:00:00 +01:00
18
17
  default_executable:
19
18
  dependencies: []
20
19