struct

SF::Time

Inherits Struct / Value / Object

Represents a time value

SF::Time encapsulates a time value in a flexible way. It allows to define a time value either as a number of seconds, milliseconds or microseconds. It also works the other way round: you can read a time value as either a number of seconds, milliseconds or microseconds.

By using such a flexible interface, the API doesn't impose any fixed type or resolution for time values, and let the user choose its own favorite representation.

Time values support the usual mathematical operations: you can add or subtract two times, multiply or divide a time by a number, compare two times, etc.

Since they represent a time span and not an absolute time value, times can also be negative.

Usage example:

t1 = SF.seconds(0.1)
milli = t1.as_milliseconds # 100

t2 = SF.milliseconds(30)
micro = t2.as_microseconds # 30000

t3 = SF.microseconds(-800000)
sec = t3.as_seconds # -0.8
def update(elapsed : SF::Time)
  @position += @speed * elapsed.as_seconds
end

update(SF.milliseconds(100))

See also: SF::Clock

Constants

Zero = new

Predefined "zero" time value

Constructors

new

Default constructor

Sets the time value to zero.

Source

Instance methods

!=(right : Time) : Bool

Overload of != operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if both time values are different

Source
%(right : Time) : Time

Overload of binary % operator to compute remainder of a time value

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: left modulo right

Source
*(right : Int) : Time

Overload of binary * operator to scale a time value

  • left - Left operand (a time)
  • right - Right operand (a number)

Returns: left multiplied by right

Source
*(right : Number) : Time

Overload of binary * operator to scale a time value

  • left - Left operand (a time)
  • right - Right operand (a number)

Returns: left multiplied by right

Source
+(right : Time) : Time

Overload of binary + operator to add two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: Sum of the two times values

Source
-(right : Time) : Time

Overload of binary - operator to subtract two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: Difference of the two times values

Source
-

Overload of unary - operator to negate a time value

  • right - Right operand (a time)

Returns: Opposite of the time value

Source
/(right : Int) : Time

Overload of binary / operator to scale a time value

  • left - Left operand (a time)
  • right - Right operand (a number)

Returns: left divided by right

Source
/(right : Number) : Time

Overload of binary / operator to scale a time value

  • left - Left operand (a time)
  • right - Right operand (a number)

Returns: left divided by right

Source
/(right : Time) : Float32

Overload of binary / operator to compute the ratio of two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: left divided by right

Source
<(right : Time) : Bool

Overload of &lt; operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if left is lesser than right

Source
<=(right : Time) : Bool

Overload of &lt;= operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if left is lesser or equal than right

Source
==(right : Time) : Bool

Overload of == operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if both time values are equal

Source
>(right : Time) : Bool

Overload of &gt; operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if left is greater than right

Source
>=(right : Time) : Bool

Overload of &gt;= operator to compare two time values

  • left - Left operand (a time)
  • right - Right operand (a time)

Returns: True if left is greater or equal than right

Source
as_microseconds

Return the time value as a number of microseconds

Returns: Time in microseconds

See also: as_seconds, as_milliseconds

Source
as_milliseconds

Return the time value as a number of milliseconds

Returns: Time in milliseconds

See also: as_seconds, as_microseconds

Source
as_seconds

Return the time value as a number of seconds

Returns: Time in seconds

See also: as_milliseconds, as_microseconds

Source
dup

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.

Source