Class: SDL4R::SdlTimeSpan

Inherits:
Object
  • Object
show all
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

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



233
234
235
# File 'lib/sdl4r/sdl_time_span.rb', line 233

def <=>(other)
  @totalMilliseconds <=> other.total_milliseconds
end

#daysObject 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.

Returns:

  • (Boolean)


226
227
228
# File 'lib/sdl4r/sdl_time_span.rb', line 226

def eql?(other)
  other.is_a?(SdlTimeSpan) and @totalMilliseconds == other.total_milliseconds
end

#hashObject

A hashcode based on the canonical string representation.



220
221
222
# File 'lib/sdl4r/sdl_time_span.rb', line 220

def hash
  to_s.hash
end

#hoursObject 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

#millisecondsObject Also known as: msec

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

#minutesObject 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

#negateObject

Returns an new SdlTimeSpan instance that is the opposite of this instance



169
170
171
# File 'lib/sdl4r/sdl_time_span.rb', line 169

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)



178
179
180
# File 'lib/sdl4r/sdl_time_span.rb', line 178

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)



187
188
189
# File 'lib/sdl4r/sdl_time_span.rb', line 187

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)



214
215
216
# File 'lib/sdl4r/sdl_time_span.rb', line 214

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)



196
197
198
# File 'lib/sdl4r/sdl_time_span.rb', line 196

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)



205
206
207
# File 'lib/sdl4r/sdl_time_span.rb', line 205

def roll_seconds(seconds)
  SdlTimeSpan.new(@totalMilliseconds + (seconds * MILLISECONDS_IN_SECOND))
end

#secondsObject 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

#signObject

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(force_show_days = false) ⇒ 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)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/sdl4r/sdl_time_span.rb', line 254

def to_s(force_show_days = false)
  _days = days
  _milliseconds = milliseconds
  
  s = nil
  if _days == 0 and not force_show_days
    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_hoursObject

Get the total number of hours in this time span. For example, if this time span represents two days, this method will return 48.



141
142
143
# File 'lib/sdl4r/sdl_time_span.rb', line 141

def total_hours
  return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_HOUR)
end

#total_millisecondsObject

Get the total number of milliseconds in this time span. For example, if this time span represents 4 seconds, this method will return 4000.



162
163
164
# File 'lib/sdl4r/sdl_time_span.rb', line 162

def total_milliseconds
  return @totalMilliseconds
end

#total_minutesObject

Get the total number of minutes in this time span. For example, if this time span represents two hours, this method will return 120.



148
149
150
# File 'lib/sdl4r/sdl_time_span.rb', line 148

def total_minutes
  return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_MINUTE)
end

#total_secondsObject

Get the total number of seconds in this time span. For example, if this time span represents three minutes, this method will return 180.



155
156
157
# File 'lib/sdl4r/sdl_time_span.rb', line 155

def total_seconds
  return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_SECOND)
end

#usecObject



134
135
136
# File 'lib/sdl4r/sdl_time_span.rb', line 134

def usec
  milliseconds * 1000
end