struct SF::Time

Overview

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

Defined in:

system/obj.cr
system/system.cr

Constant Summary

Zero = new

Predefined "zero" time value

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Default constructor

Sets the time value to zero.


[View source]

Instance Method Detail

def !=(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]
def %(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]
def *(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]
def *(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]
def +(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]
def -(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]
def - : Time #

Overload of unary - operator to negate a time value

  • right - Right operand (a time)

Returns: Opposite of the time value


[View source]
def /(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]
def /(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]
def /(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]
def <(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]
def <=(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]
def ==(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]
def >(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]
def >=(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]
def as_microseconds : Int64 #

Return the time value as a number of microseconds

Returns: Time in microseconds

See also: #as_seconds, #as_milliseconds


[View source]
def as_milliseconds : Int32 #

Return the time value as a number of milliseconds

Returns: Time in milliseconds

See also: #as_seconds, #as_microseconds


[View source]
def as_seconds : Float32 #

Return the time value as a number of seconds

Returns: Time in seconds

See also: #as_milliseconds, #as_microseconds


[View source]
def dup : Time #
Description copied from struct Value

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]