Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output functionality in ISO 8601 format #38

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The reverse can also be accomplished with the output method. So pass in seconds
=> 4 minutes 30 seconds
>> ChronicDuration.output(270, :format => :chrono)
=> 4:30
>> ChronicDuration.output(270, :format => :iso)
=> "PT4M30S"
>> ChronicDuration.output(1299600, :weeks => true)
=> 2 wks 1 day 1 hr
>> ChronicDuration.output(1299600, :weeks => true, :units => 2)
Expand Down
18 changes: 17 additions & 1 deletion lib/chronic_duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,31 @@ def output(seconds, opts = {})
}.join(divider).gsub(/^(00:)+/, '').gsub(/^0/, '').gsub(/:$/, '')
end
joiner = ''
when :iso
dividers = {
:years => 'Y', :months => 'M', :weeks => 'W', :days => 'D', :hours => 'H', :minutes => 'M', :seconds => 'S',
:iso => true
}
process = lambda do |str|
str.insert(0, 'P')
end
joiner = ''
end

iso_time_part = false
result = [:years, :months, :weeks, :days, :hours, :minutes, :seconds].map do |t|
next if t == :weeks && !opts[:weeks]
num = eval(t.to_s)
num = ("%.#{decimal_places}f" % num) if num.is_a?(Float) && t == :seconds
keep_zero = dividers[:keep_zero]
keep_zero ||= opts[:keep_zero] if t == :seconds
humanize_time_unit( num, dividers[t], dividers[:pluralize], keep_zero )
humanized_num = humanize_time_unit( num, dividers[t], dividers[:pluralize], keep_zero )
if dividers[:iso] && [:hours, :minutes, :seconds].include?(t) && !iso_time_part && !humanized_num.nil?
iso_time_part = true
'T' + humanized_num
else
humanized_num
end
end.compact!

result = result[0...opts[:units]] if opts[:units]
Expand Down
2 changes: 1 addition & 1 deletion lib/chronic_duration/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ChronicDuration
VERSION = '0.10.6'
VERSION = '0.10.7'
end
36 changes: 24 additions & 12 deletions spec/lib/chronic_duration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,79 +86,89 @@
:short => '1m 20s',
:default => '1 min 20 secs',
:long => '1 minute 20 seconds',
:chrono => '1:20'
:chrono => '1:20',
:iso => 'PT1M20S'
},
(60 + 20.51) =>
{
:micro => '1m20.51s',
:short => '1m 20.51s',
:default => '1 min 20.51 secs',
:long => '1 minute 20.51 seconds',
:chrono => '1:20.51'
:chrono => '1:20.51',
:iso => 'PT1M20.51S' # Yuck. FIXME
},
(60 + 20.51928) =>
{
:micro => '1m20.51928s',
:short => '1m 20.51928s',
:default => '1 min 20.51928 secs',
:long => '1 minute 20.51928 seconds',
:chrono => '1:20.51928'
:chrono => '1:20.51928',
:iso => 'PT1M20.51928S' # Yuck. FIXME
},
(4 * 3600 + 60 + 1) =>
{
:micro => '4h1m1s',
:short => '4h 1m 1s',
:default => '4 hrs 1 min 1 sec',
:long => '4 hours 1 minute 1 second',
:chrono => '4:01:01'
:chrono => '4:01:01',
:iso => 'PT4H1M1S'
},
(2 * 3600 + 20 * 60) =>
{
:micro => '2h20m',
:short => '2h 20m',
:default => '2 hrs 20 mins',
:long => '2 hours 20 minutes',
:chrono => '2:20'
:chrono => '2:20',
:iso => 'PT2H20M'
},
(2 * 3600 + 20 * 60) =>
{
:micro => '2h20m',
:short => '2h 20m',
:default => '2 hrs 20 mins',
:long => '2 hours 20 minutes',
:chrono => '2:20:00'
:chrono => '2:20:00',
:iso => 'PT2H20M'
},
(6 * 30 * 24 * 3600 + 24 * 3600) =>
{
:micro => '6mo1d',
:short => '6mo 1d',
:default => '6 mos 1 day',
:long => '6 months 1 day',
:chrono => '6:01:00:00:00' # Yuck. FIXME
:chrono => '6:01:00:00:00', # Yuck. FIXME
:iso => 'P6M1D'
},
(365.25 * 24 * 3600 + 24 * 3600 ).to_i =>
{
:micro => '1y1d',
:short => '1y 1d',
:default => '1 yr 1 day',
:long => '1 year 1 day',
:chrono => '1:00:01:00:00:00'
:chrono => '1:00:01:00:00:00',
:iso => 'P1Y1D'
},
(3 * 365.25 * 24 * 3600 + 24 * 3600 ).to_i =>
{
:micro => '3y1d',
:short => '3y 1d',
:default => '3 yrs 1 day',
:long => '3 years 1 day',
:chrono => '3:00:01:00:00:00'
:chrono => '3:00:01:00:00:00',
:iso => 'P3Y1D'
},
(3600 * 24 * 30 * 18) =>
{
:micro => '18mo',
:short => '18mo',
:default => '18 mos',
:long => '18 months',
:chrono => '18:00:00:00:00'
:chrono => '18:00:00:00:00',
:iso => 'P1Y6M'
}
}

Expand All @@ -177,15 +187,17 @@
:short => '0s',
:default => '0 secs',
:long => '0 seconds',
:chrono => '0'
:chrono => '0',
:iso => 'PT0S'
},
(false) =>
{
:micro => nil,
:short => nil,
:default => nil,
:long => nil,
:chrono => '0'
:chrono => '0',
:iso => 'P' # ??? FIXME
},
}

Expand Down