arnau / ISO8601

Ruby parser to work with ISO8601 dateTimes and durations — http://en.wikipedia.org/wiki/ISO_8601
MIT License
75 stars 15 forks source link

Improve the +/- function of the ISO8601::DateTime #22

Closed runwaldarshu closed 9 years ago

runwaldarshu commented 9 years ago

Currently there is an issue with the library if I try to use floating point calculations with the DateTime object due to format restructing fraction_second to precision of 2 it cases some errors Example:

2.1.5 :001 > require 'iso8601'
 => true
2.1.5 :002 > date1 = ISO8601::DateTime.new('2014')
 => #<ISO8601::DateTime:0x007f91519fd7d0 @original="2014", @date_time=#<DateTime: 2014-01-01T00:00:00+00:00 ((2456659j,0s,0n),+0s,2299161j)>, @second=0.0>
2.1.5 :003 > date1 + 0.1 - 0.1
 => #<ISO8601::DateTime:0x007f91519c67f8 @original="2013-12-31T23:59:59.99+00:00", @date_time=#<DateTime: 2013-12-31T23:59:59+00:00 ((2456658j,86399s,990000000n),+0s,2299161j)>, @second=59.99>
2.1.5 :004 > date1 == (date1 + 0.1 - 0.1)
 => false
2.1.5 :005 >

I am thinking since we allow fraction_second in @seconds for the class if use same from arithmetic calculations that will fix the issue with such cases. OR We can say strictly that in library we only support fraction_second upto 2 digits and then fix seconds to have the same precision.

arnau commented 9 years ago

I'll take a look, thanks!

arnau commented 9 years ago

@runwaldarshu In the end I reduced the fraction seconds to one decimal. Shipped with 0.8.5