Class | SDL4R::SdlTimeSpan |
In: |
lib/sdl4r/sdl_time_span.rb
|
Parent: | Object |
Represents a period of time (duration) as opposed to a particular moment in time (which would be represented using a Date, DateTime or Time instance).
MILLISECONDS_IN_SECOND | = | 1000 |
MILLISECONDS_IN_MINUTE | = | 60 * MILLISECONDS_IN_SECOND |
MILLISECONDS_IN_HOUR | = | 60 * MILLISECONDS_IN_MINUTE |
MILLISECONDS_IN_DAY | = | 24 * MILLISECONDS_IN_HOUR |
Create an SdlTimeSpan. Note: if the timespan is negative all components should be negative.
SdlTimeSpan.new(days, hours, minutes, seconds = 0, milliseconds = 0)
or
SdlTimeSpan.new(totalMilliseconds)
# File lib/sdl4r/sdl_time_span.rb, line 76 def initialize(*args) if args.length == 1 initialize_total_milliseconds(args[0]) else initialize_days_hours_minutes(*args) end end
# File lib/sdl4r/sdl_time_span.rb, line 229 def <=>(other) @totalMilliseconds <=> other.total_milliseconds end
Tests for equivalence.
# File lib/sdl4r/sdl_time_span.rb, line 222 def eql?(other) other.is_a?(SdlTimeSpan) and @totalMilliseconds == other.total_milliseconds end
A hashcode based on the canonical string representation.
# File lib/sdl4r/sdl_time_span.rb, line 216 def hash to_s.hash end
The milliseconds component.
# File lib/sdl4r/sdl_time_span.rb, line 125 def milliseconds return @totalMilliseconds - (days * MILLISECONDS_IN_DAY) - (hours * MILLISECONDS_IN_HOUR) - (minutes * MILLISECONDS_IN_MINUTE) - (seconds * MILLISECONDS_IN_SECOND) end
Returns an new SdlTimeSpan instance that is the opposite of this instance
# File lib/sdl4r/sdl_time_span.rb, line 165 def negate SdlTimeSpan.new(-@totalMilliseconds) end
Return a new instance with the days adjusted by the given amount. Positive numbers add days. Negative numbers remove days.
days: | The adjustment (days to add or subtract) |
# File lib/sdl4r/sdl_time_span.rb, line 174 def roll_days(days) SdlTimeSpan.new(@totalMilliseconds + (days * MILLISECONDS_IN_DAY)) end
Return a new instance with the hours adjusted by the given amount. Positive numbers add hours. Negative numbers remove hours.
hours: | The adjustment (hours to add or subtract) |
# File lib/sdl4r/sdl_time_span.rb, line 183 def roll_hours(hours) SdlTimeSpan.new(@totalMilliseconds + (hours * MILLISECONDS_IN_HOUR)) end
Return a new instance with the milliseconds adjusted by the given amount. Positive numbers add milliseconds. Negative numbers remove milliseconds.
milliseconds: | The adjustment (milliseconds to add or subtract) |
# File lib/sdl4r/sdl_time_span.rb, line 210 def roll_milliseconds(milliseconds) SdlTimeSpan.new(@totalMilliseconds + milliseconds) end
Return a new instance with the minutes adjusted by the given amount. Positive numbers add minutes. Negative numbers remove minutes.
minutes: | The adjustment (minutes to add or subtract) |
# File lib/sdl4r/sdl_time_span.rb, line 192 def roll_minutes(minutes) SdlTimeSpan.new(@totalMilliseconds + (minutes * MILLISECONDS_IN_MINUTE)) end
Return a new instance with the seconds adjusted by the given amount. Positive numbers add seconds. Negative numbers remove seconds.
seconds: | The adjustment (seconds to add or subtract) |
# File lib/sdl4r/sdl_time_span.rb, line 201 def roll_seconds(seconds) SdlTimeSpan.new(@totalMilliseconds + (seconds * MILLISECONDS_IN_SECOND)) end
Returns the sign (-1 or +1) of this SdlTimeSpan.
# File lib/sdl4r/sdl_time_span.rb, line 86 def sign @totalMilliseconds <=> 0 end
Returns an SDL representation of this time span using the format:
(days:)hours:minutes:seconds(.milliseconds)
(parenthesis indicate optional components)
The days and milliseconds components will not be included if they are set to 0. Days must be suffixed with "d" for clarity.
Hours, minutes, and seconds will be zero paded to two characters.
Examples:
23:13:00 (12 hours and 13 minutes) 24d:12:13:09.234 (24 days, 12 hours, 13 minutes, 9 seconds, 234 milliseconds)
# File lib/sdl4r/sdl_time_span.rb, line 250 def to_s _days = days _milliseconds = milliseconds s = nil if _days == 0 if _milliseconds == 0 s = sprintf("%d:%02d:%02d", hours, minutes.abs, seconds.abs) else s = sprintf("%d:%02d:%02d.%03d", hours, minutes.abs, seconds.abs, _milliseconds.abs) end else if _milliseconds == 0 s = sprintf("%dd:%02d:%02d:%02d", _days, hours.abs, minutes.abs, seconds.abs) else s = sprintf( "%dd:%02d:%02d:%02d.%03d", _days, hours.abs, minutes.abs, seconds.abs, _milliseconds.abs) end end return s end
Get the total number of milliseconds in this time span. For example, if this time span represents 4 seconds, this method will return 4000.
# File lib/sdl4r/sdl_time_span.rb, line 158 def total_milliseconds return @totalMilliseconds end