SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Related Symbols | List of all members
sf::Time Class Reference
System module

Represents a time value. More...

`#include <SFML/System/Time.hpp>`

## Public Member Functions
constexpr | Time ()=default
| Default constructor.
template<typename Rep , typename Period >
constexpr | Time (const std::chrono::duration< Rep, Period > &duration)
| Construct from `std::chrono::duration`
constexpr float | asSeconds () const
| Return the time value as a number of seconds.
constexpr std::int32_t | asMilliseconds () const
| Return the time value as a number of milliseconds.
constexpr std::int64_t | asMicroseconds () const
| Return the time value as a number of microseconds.
constexpr std::chrono::microseconds | toDuration () const
| Return the time value as a `std::chrono::duration`
template<typename Rep , typename Period >
constexpr | operator std::chrono::duration< Rep, Period > () const
| Implicit conversion to `std::chrono::duration`

## Static Public Attributes
static const Time | Zero
| Predefined "zero" time value.

## Related Symbols
(Note that these are not member symbols.)
constexpr Time | seconds (float amount)
| Construct a time value from a number of seconds.
constexpr Time | milliseconds (std::int32_t amount)
| Construct a time value from a number of milliseconds.
constexpr Time | microseconds (std::int64_t amount)
| Construct a time value from a number of microseconds.
constexpr bool | operator== (Time left, Time right)
| Overload of `operator==` to compare two time values.
constexpr bool | operator!= (Time left, Time right)
| Overload of `operator!=` to compare two time values.
constexpr bool | operator< (Time left, Time right)
| Overload of `operator<` to compare two time values.
constexpr bool | operator> (Time left, Time right)
| Overload of `operator>` to compare two time values.
constexpr bool | operator<= (Time left, Time right)
| Overload of `operator<=` to compare two time values.
constexpr bool | operator>= (Time left, Time right)
| Overload of `operator>=` to compare two time values.
constexpr Time | operator- (Time right)
| Overload of unary `operator-` to negate a time value.
constexpr Time | operator+ (Time left, Time right)
| Overload of binary `operator+` to add two time values.
constexpr Time & | operator+= (Time &left, Time right)
| Overload of binary `operator+=` to add/assign two time values.
constexpr Time | operator- (Time left, Time right)
| Overload of binary `operator-` to subtract two time values.
constexpr Time & | operator-= (Time &left, Time right)
| Overload of binary `operator-=` to subtract/assign two time values.
constexpr Time | operator* (Time left, float right)
| Overload of binary `operator*` to scale a time value.
constexpr Time | operator* (Time left, std::int64_t right)
| Overload of binary `operator*` to scale a time value.
constexpr Time | operator* (float left, Time right)
| Overload of binary `operator*` to scale a time value.
constexpr Time | operator* (std::int64_t left, Time right)
| Overload of binary `operator*` to scale a time value.
constexpr Time & | operator*= (Time &left, float right)
| Overload of binary `operator*=` to scale/assign a time value.
constexpr Time & | operator*= (Time &left, std::int64_t right)
| Overload of binary `operator*=` to scale/assign a time value.
constexpr Time | operator/ (Time left, float right)
| Overload of binary `operator/` to scale a time value.
constexpr Time | operator/ (Time left, std::int64_t right)
| Overload of binary `operator/` to scale a time value.
constexpr Time & | operator/= (Time &left, float right)
| Overload of binary `operator/=` to scale/assign a time value.
constexpr Time & | operator/= (Time &left, std::int64_t right)
| Overload of binary `operator/=` to scale/assign a time value.
constexpr float | operator/ (Time left, Time right)
| Overload of binary `operator/` to compute the ratio of two time values.
constexpr Time | operator% (Time left, Time right)
| Overload of binary `operator%` to compute remainder of a time value.
constexpr Time & | operator%= (Time &left, Time right)
| Overload of binary `operator%=` to compute/assign remainder of a time value.

## Detailed Description

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. It even interoperates with the `<chrono>` header. You can construct an `sf::Time` from a `chrono::duration` and read any `sf::Time` as a chrono::duration.

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:

sf::Time t1 = sf::seconds(0.1f);
std::int32_t milli = t1.asMilliseconds(); // 100
sf::Time t2 = sf::milliseconds(30);
std::int64_t micro = t2.asMicroseconds(); // 30000
sf::Time t3 = sf::microseconds(-800000);
float sec = t3.asSeconds(); // -0.8
sf::Time t4 = std::chrono::milliseconds(250);
std::chrono::microseconds micro2 = t4.toDuration(); // 250000us
sf::Time
Represents a time value.
Definition Time.hpp:42
sf::Time::asSeconds
constexpr float asSeconds() const
Return the time value as a number of seconds.
sf::Time::microseconds
constexpr Time microseconds(std::int64_t amount)
Construct a time value from a number of microseconds.
sf::Time::seconds
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
sf::Time::asMicroseconds
constexpr std::int64_t asMicroseconds() const
Return the time value as a number of microseconds.
sf::Time::toDuration
constexpr std::chrono::microseconds toDuration() const
Return the time value as a std::chrono::duration
sf::Time::asMilliseconds
constexpr std::int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
sf::Time::milliseconds
constexpr Time milliseconds(std::int32_t amount)
Construct a time value from a number of milliseconds.
void update(sf::Time elapsed)
{
position += speed * elapsed.asSeconds();
}
update(sf::milliseconds(100));

See also
  `sf::Clock`

Definition at line 41 of file Time.hpp.

## Constructor & Destructor Documentation

## ◆ Time() [1/2]

sf::Time::Time | ( |  | ) |
| constexpr default

Default constructor.

Sets the time value to zero.

## ◆ Time() [2/2]

template<typename Rep , typename Period >

sf::Time::Time | ( | const std::chrono::duration< Rep, Period > & | duration | ) |
| constexpr

Construct from `std::chrono::duration`

## Member Function Documentation

## ◆ asMicroseconds()

std::int64_t sf::Time::asMicroseconds | ( |  | ) | const
| nodiscard constexpr

Return the time value as a number of microseconds.

Returns
  Time in microseconds

See also
  `asSeconds`, `asMilliseconds`

## ◆ asMilliseconds()

std::int32_t sf::Time::asMilliseconds | ( |  | ) | const
| nodiscard constexpr

Return the time value as a number of milliseconds.

Returns
  Time in milliseconds

See also
  `asSeconds`, `asMicroseconds`

## ◆ asSeconds()

float sf::Time::asSeconds | ( |  | ) | const
| nodiscard constexpr

Return the time value as a number of seconds.

Returns
  Time in seconds

See also
  `asMilliseconds`, `asMicroseconds`

## ◆ operator std::chrono::duration< Rep, Period >()

template<typename Rep , typename Period >

sf::Time::operator std::chrono::duration< Rep, Period > | ( |  | ) | const
| constexpr

Implicit conversion to `std::chrono::duration`

Returns
  Duration in microseconds

## ◆ toDuration()

std::chrono::microseconds sf::Time::toDuration | ( |  | ) | const
| nodiscard constexpr

Return the time value as a `std::chrono::duration`

Returns
  Time in microseconds

## Friends And Related Symbol Documentation

## ◆ microseconds()

Time microseconds | ( | std::int64_t | amount | ) |
| related

Construct a time value from a number of microseconds.

Parameters

  amount | Number of microseconds

Returns
  Time value constructed from the amount of microseconds

See also
  `seconds`, `milliseconds`

## ◆ milliseconds()

Time milliseconds | ( | std::int32_t | amount | ) |
| related

Construct a time value from a number of milliseconds.

Parameters

  amount | Number of milliseconds

Returns
  Time value constructed from the amount of milliseconds

See also
  `seconds`, `microseconds`

## ◆ operator!=()

bool operator!= | ( | Time | left,
|  | Time | right )
| related

Overload of `operator!=` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if both time values are different

## ◆ operator%()

Time operator% | ( | Time | left,
|  | Time | right )
| related

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

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `left` modulo `right`

## ◆ operator%=()

Time & operator%= | ( | Time & | left,
|  | Time | right )
| related

Overload of binary `operator%=` to compute/assign remainder of a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `left` modulo `right`

## ◆ operator*() [1/4]

Time operator* | ( | float | left,
|  | Time | right )
| related

Overload of binary `operator*` to scale a time value.

Parameters

  left | Left operand (a number)
  right | Right operand (a time)

Returns
  `left` multiplied by `right`

## ◆ operator*() [2/4]

Time operator* | ( | std::int64_t | left,
|  | Time | right )
| related

Overload of binary `operator*` to scale a time value.

Parameters

  left | Left operand (a number)
  right | Right operand (a time)

Returns
  `left` multiplied by `right`

## ◆ operator*() [3/4]

Time operator* | ( | Time | left,
|  | float | right )
| related

Overload of binary `operator*` to scale a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` multiplied by `right`

## ◆ operator*() [4/4]

Time operator* | ( | Time | left,
|  | std::int64_t | right )
| related

Overload of binary `operator*` to scale a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` multiplied by `right`

## ◆ operator*=() [1/2]

Time & operator*= | ( | Time & | left,
|  | float | right )
| related

Overload of binary `operator*=` to scale/assign a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` multiplied by `right`

## ◆ operator*=() [2/2]

Time & operator*= | ( | Time & | left,
|  | std::int64_t | right )
| related

Overload of binary `operator*=` to scale/assign a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` multiplied by `right`

## ◆ operator+()

Time operator+ | ( | Time | left,
|  | Time | right )
| related

Overload of binary `operator+` to add two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  Sum of the two times values

## ◆ operator+=()

Time & operator+= | ( | Time & | left,
|  | Time | right )
| related

Overload of binary `operator+=` to add/assign two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  Sum of the two times values

## ◆ operator-() [1/2]

Time operator- | ( | Time | left,
|  | Time | right )
| related

Overload of binary `operator-` to subtract two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  Difference of the two times values

## ◆ operator-() [2/2]

Time operator- | ( | Time | right | ) |
| related

Overload of unary `operator-` to negate a time value.

Parameters

  right | Right operand (a time)

Returns
  Opposite of the time value

## ◆ operator-=()

Time & operator-= | ( | Time & | left,
|  | Time | right )
| related

Overload of binary `operator-=` to subtract/assign two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  Difference of the two times values

## ◆ operator/() [1/3]

Time operator/ | ( | Time | left,
|  | float | right )
| related

Overload of binary `operator/` to scale a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` divided by `right`

## ◆ operator/() [2/3]

Time operator/ | ( | Time | left,
|  | std::int64_t | right )
| related

Overload of binary `operator/` to scale a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` divided by `right`

## ◆ operator/() [3/3]

float operator/ | ( | Time | left,
|  | Time | right )
| related

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

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `left` divided by `right`

## ◆ operator/=() [1/2]

Time & operator/= | ( | Time & | left,
|  | float | right )
| related

Overload of binary `operator/=` to scale/assign a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` divided by `right`

## ◆ operator/=() [2/2]

Time & operator/= | ( | Time & | left,
|  | std::int64_t | right )
| related

Overload of binary `operator/=` to scale/assign a time value.

Parameters

  left | Left operand (a time)
  right | Right operand (a number)

Returns
  `left` divided by `right`

## ◆ operator<()

bool operator< | ( | Time | left,
|  | Time | right )
| related

Overload of `operator<` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if `left` is lesser than `right`

## ◆ operator<=()

bool operator<= | ( | Time | left,
|  | Time | right )
| related

Overload of `operator<=` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if `left` is lesser or equal than `right`

## ◆ operator==()

bool operator== | ( | Time | left,
|  | Time | right )
| related

Overload of `operator==` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if both time values are equal

## ◆ operator>()

bool operator> | ( | Time | left,
|  | Time | right )
| related

Overload of `operator>` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if `left` is greater than `right`

## ◆ operator>=()

bool operator>= | ( | Time | left,
|  | Time | right )
| related

Overload of `operator>=` to compare two time values.

Parameters

  left | Left operand (a time)
  right | Right operand (a time)

Returns
  `true` if `left` is greater or equal than `right`

## ◆ seconds()

Time seconds | ( | float | amount | ) |
| related

Construct a time value from a number of seconds.

Parameters

  amount | Number of seconds

Returns
  Time value constructed from the amount of seconds

See also
  `milliseconds`, `microseconds`

## Member Data Documentation

## ◆ Zero

const Time sf::Time::Zero
| static

Predefined "zero" time value.

Definition at line 110 of file Time.hpp.

The documentation for this class was generated from the following file:

- Time.hpp