Skip to content

Reorganise and test std::time

Yorick Peterse requested to merge reorganise-std-time into master

std::time is now broken up into two modules:

  1. std::time, which provides time related types.
  2. std::time::duration, which provides the Duration type.

This separation allows for the use of module methods to construct a Duration using different time units, instead of using methods defined on Duration itself. This means that instead of this:

import std::time::Duration

Duration.from_seconds(5)

You now write this:

import std::time::duration

duration.from_seconds(5)

== Renaming MonotonicTime to Instant

MonotonicTime has been renamed to "Instant", which is a bit shorter and much easier to type. A new method has been added as well: "elapsed". This method can be used to measure the time that has elapsed since the Instant was created.

== Adding and subtracting SystemTime and Instant

The methods + and - for SystemTime and Instant no longer accept a ToFloat, instead they require a Duration. Accepting a ToFloat would allow you to add a SystemTime to another SystemTime, or add an Instant to an Instant. Neither make sense, so instead we now require the use of a Duration. This means that instead of this:

import std::time

time.now + 5

You now have to write this:

import std::time
import std::time::duration

time.now + duration.from_seconds(5)

== Tests

Finally, both std::time and std::time::duration have tests. These tests uncovered a few bugs in the implementation of SystemTime, which have been resolved.

Merge request reports