-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract
Duration#seconds_to_formatted_duration
- Loading branch information
Showing
4 changed files
with
24 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
module TalksHelper | ||
def seconds_to_formatted_duration(seconds) | ||
parts = ActiveSupport::Duration.build(seconds).parts | ||
|
||
values = [parts[:hours], parts.fetch(:minutes, 0), parts.fetch(:seconds, 0)].select(&:present?) | ||
|
||
return "??:??" if values.any?(&:negative?) | ||
|
||
values.map { |value| value.to_s.rjust(2, "0") }.join(":") | ||
Duration.seconds_to_formatted_duration(seconds) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Duration | ||
def self.seconds_to_formatted_duration(seconds) | ||
if seconds.is_a?(Integer) | ||
parts = ActiveSupport::Duration.build(seconds).parts | ||
elsif seconds.is_a?(ActiveSupport::Duration) | ||
parts = seconds.parts | ||
else | ||
raise "seconds (`#{seconds.inspect}`) is not an Integer or ActiveSupport::Duration" | ||
end | ||
|
||
values = [ | ||
parts.fetch(:hours, nil), | ||
parts.fetch(:minutes, 0), | ||
parts.fetch(:seconds, 0) | ||
].compact | ||
|
||
return "??:??" if values.any?(&:negative?) | ||
|
||
values.map { |value| value.to_s.rjust(2, "0") }.join(":") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters