Sha256: a4bbed8d3400b35f12afa6fab37add6a419a8d43a325790935b303f05f1b6031
Contents?: true
Size: 1.1 KB
Versions: 2
Compression:
Stored size: 1.1 KB
Contents
require "time_duration/version" module TimeDuration def self.parse(args) Duration.parse(args) end class Duration include Comparable attr_accessor :seconds # TODO: format指定できるようにする def self.parse(time_as_string) hours, minutes = time_as_string.split(':').map(&:to_i) new(hours: hours, minutes: minutes) end def initialize(hours: 0, minutes: 0, seconds: 0) hours = hours.to_i minutes = minutes.to_i seconds = seconds.to_i @seconds = hours * 3600 + minutes * 60 + seconds end def hours minutes / 60 + seconds / 3600 end def minutes (seconds / 60) % 60 end # TODO: format指定できるようにする def to_s "%d:%02d" % [hours, minutes] end def +(time_duration) self.class.new(seconds: seconds + time_duration.seconds) end def -(time_duration) self.class.new(seconds: seconds - time_duration.seconds) end def <=>(time_duration) self.seconds <=> time_duration.seconds end # override def inspect to_s end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
time-duration-0.1.1 | lib/time_duration.rb |
time-duration-0.1.0 | lib/time_duration.rb |