Module lrs::time

Time handling.

Description

This module provides methods for time handling. The essential types are

Types

KindNameDescription
Struct Clock

A clock that can be used to measure time.

Struct DateTime

An expanded date.

Struct Time

A time offset.

Struct Timer

A timer.

Enum Weekday

A weekday.

Struct Zone

A time zone.

Constants

NameDescription
BOOT

A monotonic clock since some arbitrary point in the past which isn't affected by time jumps and continues to run while the system is suspended.

MONO

A monotonic clock since some arbitrary point in the past which isn't affected by time jumps.

MONO_COARSE

A coarse monotonic clock since some arbitrary point in the past which isn't affected by time jumps.

MONO_RAW

A monotonic clock since some arbitrary point in the past which isn't affected by time jumps or time adjustments.

PROCESS

A clock that measures the CPU time used by this process.

REAL

Real ("wall time") clock that measures the time since 1970-01-01T00:00:00+00:00.

REAL_COARSE

Real coarse ("wall time") clock that measures the time since 1970-01-01T00:00:00+00:00.

THREAD

A clock that measures the CPU time used by this thread.

Examples

The following example prints the current time in the local timezone to stdout:

use lrs::{time};

let now = time::Real::get_time().unwrap();
let local = time::Zone::local().unwrap();
let expanded = local.expand(now);

println!("It is {:02}:{:02}:{:02}", expanded.hour, expanded.minute, expanded.second);

// Example output:
// It is 13:06:27

The next example prints the last time the current time zone was changed when viewed from Tokyo:

use lrs::{file, time};

let info = file::info_no_follow("/etc/localtime").unwrap();
let last_mod = info.last_modification();
let tokyo = time::Zone::load("Asia/Tokyo").unwrap();
let expanded = tokyo.expand(last_mod);

println!("{:?}", expanded);

// Example output:
// 2013-08-06T07:06:55+09:00

The next example creates a timer that starts ticking after five seconds every second, sleeps for ten seconds, and then prints the number of times the timer expired.

use lrs::time::{self, Time};

let timer = time::Real.timer().unwrap();
timer.interval_in(Time::seconds(1), Time::seconds(5)).unwrap();

time::Real.sleep_for(Time::seconds(10)).unwrap();

println!("{:?}", timer.ticks().unwrap());

// Example output:
// 6