Time

Trait Time 

Source
pub trait Time {
Show 26 methods // Required methods fn now() -> Self; fn strptime<T: ToString, G: ToString>(s: T, format: G) -> Self; fn unix(&self) -> i64; fn unix_ms(&self) -> i64; fn strftime(&self, format: &str) -> String; // Provided methods fn windows_ns(&self) -> i64 { ... } fn webkit(&self) -> i64 { ... } fn mac_os(&self) -> i64 { ... } fn mac_os_cfa(&self) -> i64 { ... } fn sas_4gl(&self) -> i64 { ... } fn epoch(&self) -> i64 { ... } fn pretty(&self) -> String { ... } fn iso8601(&self) -> String { ... } fn rfc3339(&self) -> String { ... } fn tz_offset(&self) -> String { ... } fn tz_enum(&self) -> Option<Tz> { ... } fn change_tz<T: ToString>(&self, offset: T) -> Self where Self: Sized { ... } fn local(&self) -> Self where Self: Sized { ... } fn add_seconds(&self, duration: i64) -> Self where Self: Sized { ... } fn add_minutes(&self, minutes: i64) -> Self where Self: Sized { ... } fn add_hours(&self, hours: i64) -> Self where Self: Sized { ... } fn add_days(&self, days: i64) -> Self where Self: Sized { ... } fn add_weeks(&self, weeks: i64) -> Self where Self: Sized { ... } fn past_future<T: Time>(&self, other: &T) -> RelativeTime { ... } fn add_duration<T: ImplsDuration>(&self, duration: T) -> Self where Self: Sized { ... } fn cast<T: Time>(&self) -> T where Self: Sized { ... }
}
Expand description

Implements the core functionality of the library

The conversion methods from struct to various timestamps do support negatives where needed (everything but windows_ns as it uses the same epoch as we do)

Note that while all the examples use System time, as Ntp is not guaranteed to be included, Ntp can be used in exactly the same way in every one of these examples, as it too implements the Time trait.

Required Methods§

Source

fn now() -> Self

Get current time, returning the relevant struct

§Examples
use thetime::{System, Time};
println!("{} says the system, but {} says the server", System::now(), System::now());
Source

fn strptime<T: ToString, G: ToString>(s: T, format: G) -> Self

Parse a string into a time struct

§Examples
use thetime::{System, Time};
println!("The time was {}", System::strptime("2015-01-18 23:16:09", "%Y-%m-%d %H:%M:%S"));
Source

fn unix(&self) -> i64

Get the time in seconds since Unix epoch

§Examples
use thetime::{System, Time};
println!("{} seconds since Unix epoch", System::now().unix());
println!("{} seconds since Unix epoch from pool.ntp.org", System::now().unix());
Source

fn unix_ms(&self) -> i64

Get the time in milliseconds since Unix epoch

§Examples
use thetime::{System, Time};
println!("{} milliseconds since Unix epoch", System::now().unix_ms());
println!("{} milliseconds since Unix epoch from pool.ntp.org", System::now().unix_ms());
Source

fn strftime(&self, format: &str) -> String

Format the time according to the given format string

§Examples
use thetime::{System, Time};
println!("{}", System::now().strftime("%Y-%m-%d %H:%M:%S"));
println!("{}", System::now().strftime("%Y-%B-%d %H:%M:%S"));

Provided Methods§

Source

fn windows_ns(&self) -> i64

Gets the time in nanoseconds (approximate) since Windows epoch (1601-01-01 00:00:00)

§Examples
use thetime::{System, Time};
println!("{} nanoseconds since Windows epoch", System::now().windows_ns());
println!("{} nanoseconds since Windows epoch from pool.ntp.org", System::now().windows_ns());
Source

fn webkit(&self) -> i64

Gets the time in microseconds (approximate) since Webkit epoch (1601-01-01 00:00:00)

§Examples
use thetime::{System, Time};
println!("{} microseconds since Webkit epoch", System::now().webkit());
println!("{} microseconds since Webkit epoch from pool.ntp.org", System::now().webkit());
Source

fn mac_os(&self) -> i64

Get the time in seconds since the Mac OS epoch (1904-01-01 00:00:00)

§Examples
use thetime::{System, Time};
println!("{} seconds since Mac OS epoch", System::now().mac_os());
println!("{} seconds since Mac OS epoch from pool.ntp.org", System::now().mac_os());
Source

fn mac_os_cfa(&self) -> i64

Get the time in seconds since the Mac OS Absolute epoch (2001-01-01 00:00:00)

§Examples
use thetime::{System, Time};
println!("{} seconds since Mac OS Absolute epoch", System::now().mac_os_cfa());
println!("{} seconds since Mac OS Absolute epoch from pool.ntp.org", System::now().mac_os_cfa());
Source

fn sas_4gl(&self) -> i64

Get the time in seconds since the SAS 4GL epoch (1960-01-01 00:00:00)

§Examples
use thetime::{System, Time};
println!("{} seconds since SAS 4GL epoch", System::now().sas_4gl());
println!("{} seconds since SAS 4GL epoch from pool.ntp.org", System::now().sas_4gl());
Source

fn epoch(&self) -> i64

Get the time since the epoch we use (1601-01-01 00:00:00). we use this for full compataibility with Windows

§Examples
use thetime::{System, Time};
println!("{} milliseconds since the epoch we use", System::now().epoch());
println!("{} milliseconds since the epoch we use from pool.ntp.org", System::now().epoch());
Source

fn pretty(&self) -> String

pretty print the time object

§Examples
use thetime::{System, Time, StrTime};
let date2017 = "2017-01-01 00:00:00".parse_time::<System>("%Y-%m-%d %H:%M:%S");
println!("2017 - {}", date2017.pretty());
assert_eq!(date2017.pretty(), "2017-01-01 00:00:00");
Source

fn iso8601(&self) -> String

Returns the date formatted in ISO8601 format

§Examples
use thetime::{System, Time};
println!("{}", System::now().iso8601());
println!("{}", System::now().iso8601());
Source

fn rfc3339(&self) -> String

Returns the date formatted in RFC3339 format

§Examples
use thetime::{System, Time};
println!("{}", System::now().rfc3339());
println!("{}", System::now().rfc3339());
Source

fn tz_offset(&self) -> String

Gets the timezone offset in the format HH:MM

§Examples
use thetime::{System, Time};
println!("{}", System::now().tz_offset());
println!("{}", System::now().tz_offset());
Source

fn tz_enum(&self) -> Option<Tz>

Represents the timezone as an enum

§Examples
use thetime::{System, Time};
println!("{:?}", System::now().tz_enum());
println!("{:?}", System::now().tz_enum());
Source

fn change_tz<T: ToString>(&self, offset: T) -> Self
where Self: Sized,

Changes the timezone offset of the time object, where offset is in the form “+|-[0-5][0-9]:[0-5][0-9]” Note that this change is relative to UTC, not the current timezone

§Examples
use thetime::{System, Time};
println!("{}", System::now().change_tz("+01:00"));
println!("{}", System::now().change_tz("-01:00"));
Source

fn local(&self) -> Self
where Self: Sized,

Changes the timezone offset of the time object to the local timezone

§Examples
use thetime::{System, Time};
println!("{}", System::now().local());
println!("{}", System::now().local());
Source

fn add_seconds(&self, duration: i64) -> Self
where Self: Sized,

add an amount in seconds to a time object

§Examples
use thetime::{System, Time};
println!("{}", System::now().add_seconds(3600));
println!("{}", System::now().add_seconds(3600));
Source

fn add_minutes(&self, minutes: i64) -> Self
where Self: Sized,

add an amount in minutes to a time object

§Examples
use thetime::{System, Time};
println!("{}", System::now().add_minutes(60));
println!("{}", System::now().add_minutes(-60));
Source

fn add_hours(&self, hours: i64) -> Self
where Self: Sized,

add an amount in hours to a time object

§Examples
use thetime::{System, Time};
println!("{}", System::now().add_minutes(60));
println!("{}", System::now().add_minutes(24));
Source

fn add_days(&self, days: i64) -> Self
where Self: Sized,

add an amount in days to a time object

§Examples
use thetime::{System, Time};
println!("{}", System::now().add_days(7));
println!("{}", System::now().add_days(24));
Source

fn add_weeks(&self, weeks: i64) -> Self
where Self: Sized,

add an amount in weeks to a time object we stop at weeks to avoid potential leap year confusion

§Examples
use thetime::{System, Time};
println!("{}", System::now().add_weeks(7));
println!("{}", System::now().add_weeks(52));
Source

fn past_future<T: Time>(&self, other: &T) -> RelativeTime

determine whether a time object is in the past, present or future

§Examples
use thetime::{System, Time};
let x = System::now();
let y = System::now();
println!("{} is in the {}", x, x.past_future(&y));
println!("{} is in the {}", y, y.past_future(&x));
Source

fn add_duration<T: ImplsDuration>(&self, duration: T) -> Self
where Self: Sized,

add a duration to a time object

§Examples
use thetime::{System, Time, ImplsDuration};
let x = System::now();
println!("{}", x.add_duration(chrono::Duration::seconds(3600)));
Source

fn cast<T: Time>(&self) -> T
where Self: Sized,

cast a time object to another time object

§Examples
use thetime::{System, Ntp, Time};
let x = System::now();
println!("{}", x.cast::<Ntp>());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§