struct SF::Time
- SF::Time
- Struct
- Value
- Object
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.crsystem/system.cr
Constant Summary
-
Zero =
new
-
Predefined "zero" time value
Constructors
-
.new
Default constructor
Instance Method Summary
-
#!=(right : Time) : Bool
Overload of != operator to compare two time values
-
#%(right : Time) : Time
Overload of binary % operator to compute remainder of a time value
-
#*(right : Int) : Time
Overload of binary * operator to scale a time value
-
#*(right : Number) : Time
Overload of binary * operator to scale a time value
-
#+(right : Time) : Time
Overload of binary + operator to add two time values
-
#-(right : Time) : Time
Overload of binary - operator to subtract two time values
-
#- : Time
Overload of unary - operator to negate a time value
-
#/(right : Int) : Time
Overload of binary / operator to scale a time value
-
#/(right : Number) : Time
Overload of binary / operator to scale a time value
-
#/(right : Time) : Float32
Overload of binary / operator to compute the ratio of two time values
-
#<(right : Time) : Bool
Overload of < operator to compare two time values
-
#<=(right : Time) : Bool
Overload of <= operator to compare two time values
-
#==(right : Time) : Bool
Overload of == operator to compare two time values
-
#>(right : Time) : Bool
Overload of > operator to compare two time values
-
#>=(right : Time) : Bool
Overload of >= operator to compare two time values
-
#as_microseconds : Int64
Return the time value as a number of microseconds
-
#as_milliseconds : Int32
Return the time value as a number of milliseconds
-
#as_seconds : Float32
Return the time value as a number of seconds
-
#dup : Time
Returns a shallow copy of this object.
Constructor Detail
Instance Method Detail
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
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
Overload of binary * operator to scale a time value
- left - Left operand (a time)
- right - Right operand (a number)
Returns: left multiplied by right
Overload of binary * operator to scale a time value
- left - Left operand (a time)
- right - Right operand (a number)
Returns: left multiplied by right
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
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
Overload of unary - operator to negate a time value
- right - Right operand (a time)
Returns: Opposite of the time value
Overload of binary / operator to scale a time value
- left - Left operand (a time)
- right - Right operand (a number)
Returns: left divided by right
Overload of binary / operator to scale a time value
- left - Left operand (a time)
- right - Right operand (a number)
Returns: left divided by right
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
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
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
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
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
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
Return the time value as a number of microseconds
Returns: Time in microseconds
See also: #as_seconds
, #as_milliseconds
Return the time value as a number of milliseconds
Returns: Time in milliseconds
See also: #as_seconds
, #as_microseconds
Return the time value as a number of seconds
Returns: Time in seconds
See also: #as_milliseconds
, #as_microseconds
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.