Skip to content

struct SF::Time
inherits Struct #

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.

View source

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

View 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

View 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

View 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

View 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

View 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

View source

#- : Time#

Overload of unary - operator to negate a time value

  • right - Right operand (a time)

Returns: Opposite of the time value

View 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

View 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

View 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

View 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 left is lesser than right

View 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 left is lesser or equal than right

View 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

View 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 left is greater than right

View 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 left is greater or equal than right

View source

#as_microseconds : Int64#

Return the time value as a number of microseconds

Returns: Time in microseconds

See also: as_seconds, as_milliseconds

View source

#as_milliseconds : Int32#

Return the time value as a number of milliseconds

Returns: Time in milliseconds

See also: as_seconds, as_microseconds

View source

#as_seconds : Float32#

Return the time value as a number of seconds

Returns: Time in seconds

See also: as_milliseconds, as_microseconds

View source

#dup : Time#

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.

View source