Class Time

extends Object implements Comparable
A wall clock time.
The wall clock time is represented as a Unix time (https://en.wikipedia.org/wiki/Unix_time). That is the time elapsed since 1970-01-01T00:00:00Z.
The time is measured down to nanoseconds.
If you need a decomposed view of a Time, then convert it to a TimeInfo instance with local or utc.

Class summary


epoch --h/int= --m/int= --s/int= --ms/int= --us/int= --ns/int= -> Time
Constructs a time instance from the given parameters.
now -> Time
Constructs a time instance for the current moment in time.
local --year/int --month/int --day/int --h/int= --m/int= --s/int= --ms/int= --us/int= --ns/int= --dst/bool= -> Time
Constructs a time instance in local time.
local year/int month/int day/int h/int= m/int= s/int= --ms/int= --us/int= --ns/int= --dst/bool= -> Time
utc --year/int --month/int --day/int --h/int= --m/int= --s/int= --ms/int= --us/int= --ns/int= -> Time
Constructs a time instance in UTC from the given parameters.
utc year/int month/int day/int h/int= m/int= s/int= --ms/int= --us/int= --ns/int= -> Time
deserialize bytes/Data -> Time
Constructs a time instance from the given bytes.
from-string str/string -> Time
Parses the given str to construct a UTC time instance.
parse str/string [--on-error] -> Time
Deprecated. Use parse str [--if-error] instead.
parse str/string [--if-error] -> Time
Parses the given str to construct a UTC time instance.
parse str/string -> Time
monotonic-us --since-wakeup/bool= -> int
Returns a monotonically increasing microsecond value.

Statics

Constructs a time instance from the given bytes.
This operation is the inverse of to-byte-array.

epoch --h/int=0 --m/int=0 --s/int=0 --ms/int=0 --us/int=0 --ns/int=0 -> Time
Constructs a time instance from the given parameters.

Parses the given str to construct a UTC time instance.
Deprecated. Use parse instead.

local --year/int --month/int --day/int --h/int=0 --m/int=0 --s/int=0 --ms/int=0 --us/int=0 --ns/int=0 --dst/bool=null -> Time
Constructs a time instance in local time.
dst can be used to force daylight saving. This is only interesting when the remaining values are ambiguous. For example, (most of) Europe changed to winter time on October 27 2019, at 3 a.m. The first time Europe observed 3 a.m. the clock was reset to 2 a.m. This means that a time like 2:30 a.m. is ambiguous, since it was observed twice. The dst flag allows to disambiguate these cases. The flag should *not* be used otherwise.

local year/int month/int day/int h/int=0 m/int=0 s/int=0 --ms/int=0 --us/int=0 --ns/int=0 --dst/bool=null -> Time

monotonic-us --since-wakeup/bool=false -> int
Returns a monotonically increasing microsecond value.
If since-wakeup is false, the clock can be used across deep sleeps. Otherwise, the clock is reset to zero when waking up.
The clock is not anchored and thus has no fixed relationship to the current time. Changes to the system time (for example because of an NTP adjustment) do not affect the values returned from this function.

Constructs a time instance for the current moment in time.

parse str/string [--on-error] -> Time
Deprecated. Use parse str [--if-error] instead.

parse str/string [--if-error] -> Time
Parses the given str to construct a UTC time instance.
The str must be in RFC 3339 format, which is a subset of ISO 8601 format.
For example "2019-12-18T06:22:48Z".
Leap seconds are not supported, and lower case 't' and 'z' are not allowed.
Contrary to RFC 3339, this function allows to provide the time without seconds, like "2019-12-18T06:22Z". In this case, the seconds are set to 0.
Calls if-error if there is an error parsing the string. Then returns the result of if-error.

Throws an error if the string cannot be parsed.

utc --year/int --month/int --day/int --h/int=0 --m/int=0 --s/int=0 --ms/int=0 --us/int=0 --ns/int=0 -> Time
Constructs a time instance in UTC from the given parameters.

utc year/int month/int day/int h/int=0 m/int=0 s/int=0 --ms/int=0 --us/int=0 --ns/int=0 -> Time

Methods

Compares this time to the other.
Returns 1 if this time is after (greater than) the other.
Returns 0 if this time is equal to the other.
Returns -1 if this time is before (less than) the other.
Examples

t0 := Time.epoch
t1 := Time.epoch --h=1
t2 := Time.epoch --h=2

t0.compare_to t0  // => 0
t1.compare_to t1  // => 0
t2.compare_to t2  // => 0

t0.compare_to t1  // => -1
t1.compare_to t2  // => -1
t0.compare_to t2  // => -1

t2.compare_to t1  // => 1
t1.compare_to t0  // => 1
t2.compare_to t0  // => 1

compare-to other/Time [--if-equal] -> int
Variant of compare-to other.
Calls if-equal if this time is equal to the other.

The hashcode of this time.

Decomposes this time to a human-friendly version using the local time.

Milliseconds since the epoch 1970-01-01T00:00:00Z.

The nanosecond component of this time.

Nanoseconds since the epoch 1970-01-01T00:00:00Z.

Subtracts the duration from this time.

Adds the duration to this time.

Whether this time is before the other.

Whether this time is before or at the same time as the other.

Whether this time is equal to the other.

Whether this time is after the other.

Whether this time is after or at the same time as the other.

plus --h/int=0 --m/int=0 --s/int=0 --ms/int=0 --us/int=0 --ns/int=0 -> Time
Adds the given parameters.

Seconds since the epoch 1970-01-01T00:00:00Z.

stringify -> any
See super.
Returns the string time in UTC.

to other/Time -> Duration
Computes the duration from this time to the other.

Converts this instance into a byte array.
The returned byte array is a valid input for the constructor Time.deserialize.

Computes the duration from this time to now (Time.now).
Examples

main:
  // In this example, we assume the time is 2021-04-21 15:30:35 UTC.
  time := Time.utc --year=2021 --month=04 --day=21 --h=12 --m=30 --s=35
  print time.to-now  // >> 3h0m0s

  time = Time.utc --year=2021 --month=04 --day=21 --h=18 --m=30 --s=35
  print time.to-now  // >> --3h0m0s

Decomposes this time to a human-friendly version using UTC.