Class: SDL4R::SdlTimeSpan
- Inherits:
-
Object
- Object
- SDL4R::SdlTimeSpan
- Includes:
- Comparable
- Defined in:
- lib/sdl4r/sdl_time_span.rb
Overview
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).
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#days ⇒ Object
(also: #day)
The days component.
-
#eql?(other) ⇒ Boolean
(also: #==)
Tests for equivalence.
-
#hash ⇒ Object
A hashcode based on the canonical string representation.
-
#hours ⇒ Object
(also: #hour)
The hours component.
-
#initialize(*args) ⇒ SdlTimeSpan
constructor
Create an SdlTimeSpan.
-
#milliseconds ⇒ Object
(also: #usec)
The milliseconds component.
-
#minutes ⇒ Object
(also: #min)
The minutes component.
-
#negate ⇒ Object
Returns an new SdlTimeSpan instance that is the opposite of this instance.
-
#roll_days(days) ⇒ Object
Return a new instance with the days adjusted by the given amount.
-
#roll_hours(hours) ⇒ Object
Return a new instance with the hours adjusted by the given amount.
-
#roll_milliseconds(milliseconds) ⇒ Object
Return a new instance with the milliseconds adjusted by the given amount.
-
#roll_minutes(minutes) ⇒ Object
Return a new instance with the minutes adjusted by the given amount.
-
#roll_seconds(seconds) ⇒ Object
Return a new instance with the seconds adjusted by the given amount.
-
#seconds ⇒ Object
(also: #sec)
The seconds component.
-
#sign ⇒ Object
Returns the sign (-1 or +1) of this SdlTimeSpan.
-
#to_s ⇒ Object
Returns an SDL representation of this time span using the format:.
-
#total_hours ⇒ Object
Get the total number of hours in this time span.
-
#total_milliseconds ⇒ Object
Get the total number of milliseconds in this time span.
-
#total_minutes ⇒ Object
Get the total number of minutes in this time span.
-
#total_seconds ⇒ Object
Get the total number of seconds in this time span.
Constructor Details
#initialize(*args) ⇒ SdlTimeSpan
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)
76 77 78 79 80 81 82 |
# 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 |
Instance Method Details
#<=>(other) ⇒ Object
229 230 231 |
# File 'lib/sdl4r/sdl_time_span.rb', line 229 def <=>(other) @totalMilliseconds <=> other.total_milliseconds end |
#days ⇒ Object Also known as: day
The days component.
92 93 94 |
# File 'lib/sdl4r/sdl_time_span.rb', line 92 def days sign * (@totalMilliseconds.abs / MILLISECONDS_IN_DAY) end |
#eql?(other) ⇒ Boolean Also known as: ==
Tests for equivalence.
222 223 224 |
# File 'lib/sdl4r/sdl_time_span.rb', line 222 def eql?(other) other.is_a?(SdlTimeSpan) and @totalMilliseconds == other.total_milliseconds end |
#hash ⇒ Object
A hashcode based on the canonical string representation.
216 217 218 |
# File 'lib/sdl4r/sdl_time_span.rb', line 216 def hash to_s.hash end |
#hours ⇒ Object Also known as: hour
The hours component.
99 100 101 |
# File 'lib/sdl4r/sdl_time_span.rb', line 99 def hours return sign * ((@totalMilliseconds - (days * MILLISECONDS_IN_DAY)).abs / MILLISECONDS_IN_HOUR) end |
#milliseconds ⇒ Object Also known as: usec
The milliseconds component.
125 126 127 128 129 130 131 |
# 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 |
#minutes ⇒ Object Also known as: min
The minutes component.
106 107 108 109 110 |
# File 'lib/sdl4r/sdl_time_span.rb', line 106 def minutes return sign * ((@totalMilliseconds - (days * MILLISECONDS_IN_DAY) - (hours * MILLISECONDS_IN_HOUR)).abs / MILLISECONDS_IN_MINUTE) end |
#negate ⇒ Object
Returns an new SdlTimeSpan instance that is the opposite of this instance
165 166 167 |
# File 'lib/sdl4r/sdl_time_span.rb', line 165 def negate SdlTimeSpan.new(-@totalMilliseconds) end |
#roll_days(days) ⇒ Object
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)
174 175 176 |
# File 'lib/sdl4r/sdl_time_span.rb', line 174 def roll_days(days) SdlTimeSpan.new(@totalMilliseconds + (days * MILLISECONDS_IN_DAY)) end |
#roll_hours(hours) ⇒ Object
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)
183 184 185 |
# File 'lib/sdl4r/sdl_time_span.rb', line 183 def roll_hours(hours) SdlTimeSpan.new(@totalMilliseconds + (hours * MILLISECONDS_IN_HOUR)) end |
#roll_milliseconds(milliseconds) ⇒ Object
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)
210 211 212 |
# File 'lib/sdl4r/sdl_time_span.rb', line 210 def roll_milliseconds(milliseconds) SdlTimeSpan.new(@totalMilliseconds + milliseconds) end |
#roll_minutes(minutes) ⇒ Object
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)
192 193 194 |
# File 'lib/sdl4r/sdl_time_span.rb', line 192 def roll_minutes(minutes) SdlTimeSpan.new(@totalMilliseconds + (minutes * MILLISECONDS_IN_MINUTE)) end |
#roll_seconds(seconds) ⇒ Object
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)
201 202 203 |
# File 'lib/sdl4r/sdl_time_span.rb', line 201 def roll_seconds(seconds) SdlTimeSpan.new(@totalMilliseconds + (seconds * MILLISECONDS_IN_SECOND)) end |
#seconds ⇒ Object Also known as: sec
The seconds component.
115 116 117 118 119 120 |
# File 'lib/sdl4r/sdl_time_span.rb', line 115 def seconds return sign * ((@totalMilliseconds - (days * MILLISECONDS_IN_DAY) - (hours * MILLISECONDS_IN_HOUR) - (minutes * MILLISECONDS_IN_MINUTE)).abs / MILLISECONDS_IN_SECOND) end |
#sign ⇒ Object
Returns the sign (-1 or +1) of this SdlTimeSpan.
86 87 88 |
# File 'lib/sdl4r/sdl_time_span.rb', line 86 def sign @totalMilliseconds <=> 0 end |
#to_s ⇒ Object
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)
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# 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 |
#total_hours ⇒ Object
Get the total number of hours in this time span. For example, if this time span represents two days, this method will return 48.
137 138 139 |
# File 'lib/sdl4r/sdl_time_span.rb', line 137 def total_hours return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_HOUR) end |
#total_milliseconds ⇒ Object
Get the total number of milliseconds in this time span. For example, if this time span represents 4 seconds, this method will return 4000.
158 159 160 |
# File 'lib/sdl4r/sdl_time_span.rb', line 158 def total_milliseconds return @totalMilliseconds end |
#total_minutes ⇒ Object
Get the total number of minutes in this time span. For example, if this time span represents two hours, this method will return 120.
144 145 146 |
# File 'lib/sdl4r/sdl_time_span.rb', line 144 def total_minutes return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_MINUTE) end |
#total_seconds ⇒ Object
Get the total number of seconds in this time span. For example, if this time span represents three minutes, this method will return 180.
151 152 153 |
# File 'lib/sdl4r/sdl_time_span.rb', line 151 def total_seconds return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_SECOND) end |