Add ability to parse iso8601 durations. #7

Closed
wants to merge 2 commits into
from

Projects

None yet

4 participants

@joevandyk
Duration.new("P1M1DT1M1S")
 => #<Duration:0x007ff9428efd30 @seconds=1, @total=2714461, @weeks=4, @days=3, @hours=10, @minutes=1> 

I'm not an ISO8601 expert, but it appears many implementations ignore
weeks in the format string. The Ruby iso8601 library does, and
postgresql does as well:

select interval '1w';
 interval
----------
 P7D

In order to better operate with those libraries, this library no longer
includes the week in the iso8601 format.

@joevandyk joevandyk Add ability to parse iso8601 durations.
I'm not an ISO8601 expert, but it appears many implementations ignore
weeks in the format string. The Ruby iso8601 library does, and
postgresql does as well:

select interval '1w';
 interval
----------
 P7D

In order to better operate with those libraries, this library no longer
includes the week in the iso8601 format.
60de756
@joevandyk

Actually according to wikipedia http://en.wikipedia.org/wiki/ISO_8601#Durations "Durations are represented by the format P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W".

Note that there's no [n]W in the longer version. So I think that should be removed from the iso8601 output.

@joevandyk joevandyk Add Duration.dump and Duration.load.
Makes Duration compatible with ActiveRecord serialization.
599854f
@joevandyk

As a result of this, we can serialize Durations in and out of a postgresql interval type easily. All that's necessary is to tell postgresql to format the interval in iso8601 format.

test_duration=# select interval '1 hour';
 interval 
----------
 01:00:00
(1 row)

test_duration=# set intervalstyle=iso_8601;
SET

test_duration=# select interval '1 hour';
 interval 
----------
 PT1H
(1 row)

ActiveRecord serialization via Duration.dump and Duration.load takes care of the rest.

class Contract < ActiveRecord::Base
  serialize :duration, Duration
end
@jrochkind

I'm interested in built-in ability to parse ISO8601 durations. Does committer plan to accept this pull request?

@gcirne

Hi,

Sorry for the (really) long delay. Do you mind rebasing against current master?

@peleteiro peleteiro closed this May 22, 2013
@joevandyk

Why'd this get closed?

@joevandyk

New pull request here: #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment