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).

Methods

<=>   ==   day   days   eql?   hash   hour   hours   milliseconds   min   minutes   negate   new   roll_days   roll_hours   roll_milliseconds   roll_minutes   roll_seconds   sec   seconds   sign   to_s   total_hours   total_milliseconds   total_minutes   total_seconds   usec  

Included Modules

Comparable

Constants

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

Public Class methods

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)

[Source]

# 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

Public Instance methods

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 229
    def <=>(other)
      @totalMilliseconds <=> other.total_milliseconds
    end
==(other)

Alias for eql?

day()

Alias for days

The days component.

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 92
    def days
      sign * (@totalMilliseconds.abs / MILLISECONDS_IN_DAY)
    end

Tests for equivalence.

[Source]

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

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 216
    def hash
      to_s.hash
    end
hour()

Alias for hours

The hours component.

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 99
    def hours
      return sign * ((@totalMilliseconds - (days * MILLISECONDS_IN_DAY)).abs / MILLISECONDS_IN_HOUR)
    end

The milliseconds component.

[Source]

# 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
min()

Alias for minutes

The minutes component.

[Source]

# 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

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 201
    def roll_seconds(seconds)
      SdlTimeSpan.new(@totalMilliseconds + (seconds * MILLISECONDS_IN_SECOND))
    end
sec()

Alias for seconds

The seconds component.

[Source]

# 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

Returns the sign (-1 or +1) of this SdlTimeSpan.

[Source]

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

[Source]

# 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 hours in this time span. For example, if this time span represents two days, this method will return 48.

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 137
    def total_hours
      return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_HOUR)
    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.

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 158
    def total_milliseconds
      return @totalMilliseconds
    end

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

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 144
    def total_minutes
      return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_MINUTE)
    end

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

[Source]

# File lib/sdl4r/sdl_time_span.rb, line 151
    def total_seconds
      return sign * (@totalMilliseconds.abs / MILLISECONDS_IN_SECOND)
    end
usec()

Alias for milliseconds

[Validate]