Struct spinoso_time::Time[][src]

pub struct Time { /* fields omitted */ }
Expand description

Implementation of Ruby Time, a timezone-aware datetime, based on chrono.

Time is represented as:

  • a 64-bit signed integer of seconds since January 1, 1970 UTC (a Unix timestamp).
  • an unsigned 32-bit integer of nanoseconds since the timestamp.
  • An offset from UTC. See Offset for the types of supported offsets.

This data structure allows representing roughly 584 billion years. Unlike MRI, there is no promotion to Bignum or Rational. The maximum granularity of a Time object is nanoseconds.

Time objects are immutable. Date/time value manipulation always returns a new Time object.

Examples

// Create a Time to the current system clock with local offset
let time = Time::now();
assert!(!time.is_utc());
println!("{}", time.is_sunday());
let time = Time::now();
let one_hour_ago: Time = time - (60 * 60);
assert_eq!(time.to_int() - 3600, one_hour_ago.to_int());
assert_eq!(time.nanosecond(), one_hour_ago.nanosecond());

Implementation notes

Time stores raw timestamps and only converts to chrono DateTime for computation. chrono provides an aware datetime view over the raw timestamp.

Implementations

Creates a new Time object for the current time with a local offset.

Examples

use spinoso_time::Time;
let now = Time::new();

Creates a new Time object for the current time with a local offset.

This is same as new.

Examples

use spinoso_time::Time;
let now = Time::now();

Creates a new Time object from the seconds and sub_second_nanos since the Unix EPOCH with a local offset.

Examples

use spinoso_time::Time;
let epoch = Time::at(0, 0);
let epoch_plus_1_nano = Time::at(0, 1);

Returns the year for time (including the century).

Examples

let now = Time::now();
let year = now.year();

Returns the month of the year 1..=12 for time.

Examples

let now = Time::now();
let minute_of_hour = now.minute();

Returns the day of the month 1..=n for time.

Examples

let now = Time::now();
let day_of_month = now.day();

Returns a new Time object, one second later than time.

This method should log a deprecation warning if Time::nanosecond is non-zero.

Examples

let now = Time::now();
let next = now.succ();
assert_eq!(now.to_int() + 1, next.to_int());

Returns an integer representing the day of the year, 1..=366.

This method returns the date’s ordinal day.

Examples

let now = Time::now();
let ordinal = now.year_day();
assert!(ordinal > 0);
assert!(ordinal <= 366);

Returns the hour of the day 0..=23 for time.

Examples

let now = Time::now();
let hour_of_day = now.hour();

Returns the minute of the hour 0..=59 for time.

Examples

let now = Time::now();
let minute_of_hour = now.minute();

Returns the second of the minute 0..=60 for time.

Seconds range from zero to 60 to allow the system to inject leap seconds.

Examples

let now = Time::now();
let second_of_minute = now.second();
if second_of_minute >= 60 {
    // `now` is during a leap second
}

Returns the number of microseconds for time.

This method returns microseconds since the last second (including leap seconds. The range of this value is always from 0 to 999,999.

Examples

let now = Time::now();
let usec_since_last_second = now.second();
if usec_since_last_second >= 1_000_000 {
    // `now` is during a leap second
}

Returns the number of nanoseconds for time.

This method returns nanoseconds since the last second (including leap seconds. The range of this value is always from 0 to 999,999,999.

Examples

let now = Time::now();
let nsec_since_last_second = now.nanosecond();

Implementation notes

The IEEE 754 double is not accurate enough to represent the exact number of nanoseconds since the Unix Epoch. nanosecond is more accurate than to_float.

Returns the fraction for time.

The return value can be a rational number. This result is more accurate than to_float because IEEE 754 double precision is not sufficient to losslessly encode nanosecond fractions of seconds.

Examples

let time = Time::now();
let (sub_second_units, units_per_second) = time.subsec();

Implementation notes

Time has a maximum granularity of nanoseconds, so in practice this method always returns nanoseconds, but the returned tuple accomodates returning any fractional part, such as millis or micros.

Returns true if time occurs during Daylight Saving Time in its time zone.

Implementation notes

This function is not implemented and always returns false.

Returns true if time represents a time in UTC (GMT).

Examples

let local_time = Time::now();
assert!(!local_time.is_utc());
let utc_time = local_time.to_utc();
assert!(utc_time.is_utc());

Returns a new Time object representing time in local time (using the local time zone in effect for this process).

Examples

let local_time = Time::now();
assert!(!local_time.is_utc());

let local_time2 = local_time.to_local();
assert!(!local_time2.is_utc());

let utc_time = local_time.to_utc();
assert!(utc_time.is_utc());

Returns a new Time object representing time in UTC.

Examples

let local_time = Time::now();
assert!(!local_time.is_utc());

let utc_time = local_time.to_utc();
assert!(utc_time.is_utc());

let utc_time2 = utc_time.to_utc();
assert!(utc_time2.is_utc());

Returns the name of the time zone used for time.

Returns an integer representing the day of the week, 0..=6, with Sunday == 0.

Examples

let now = Time::now();
match now {
    time if time.is_sunday() => assert_eq!(time.weekday(), 0),
    time if time.is_monday() => assert_eq!(time.weekday(), 1),
    time if time.is_tuesday() => assert_eq!(time.weekday(), 2),
    time if time.is_wednesday() => assert_eq!(time.weekday(), 3),
    time if time.is_thursday() => assert_eq!(time.weekday(), 4),
    time if time.is_friday() => assert_eq!(time.weekday(), 5),
    time if time.is_saturday() => assert_eq!(time.weekday(), 6),
    _ => {}
}

Returns true if time represents Sunday.

Examples

let now = Time::now();
if now.is_sunday() {
    // go grocery shopping
    assert_eq!(now.weekday(), 0);
}

Returns true if time represents Monday.

Examples

let now = Time::now();
if now.is_monday() {
    // go to work
    assert_eq!(now.weekday(), 1);
}

Returns true if time represents Tuesday.

Examples

let now = Time::now();
if now.is_tuesday() {
    // go to the gym
    assert_eq!(now.weekday(), 2);
}

Returns true if time represents Wednesday.

Examples

let now = Time::now();
if now.is_wednesday() {
    // hump day!
    assert_eq!(now.weekday(), 3);
}

Returns true if time represents Thursday.

Examples

let now = Time::now();
if now.is_thursday() {
    // Chinese food delivery for dinner
    assert_eq!(now.weekday(), 4);
}

Returns true if time represents Friday.

Examples

let now = Time::now();
if now.is_friday() {
    // TGIF
    // Friday, Friday, gotta get down
    assert_eq!(now.weekday(), 5);
}

Returns true if time represents Saturday.

Examples

let now = Time::now();
if now.is_saturday() {
    // hike at the lake
    assert_eq!(now.weekday(), 6);
}

Returns the value of time as a floating point number of seconds since the Unix Epoch.

Examples

let now = Time::now();
let now_f = now.to_float();
let now_i = now.to_int();
assert!(now_i as f64 <= now_f);

Implementation notes

The IEEE 754 double is not accurate enough to represent the exact number of subsecond nanoseconds in the Time.

Returns the value of time as an integer number of seconds since the Unix Epoch.

Examples

let now = Time::now();
let now_f = now.to_float();
let now_i = now.to_int();
assert!(now_i as f64 <= now_f);

Serialize a Time into its components as a ToA.

ToA stores a Time as a ten-element struct of time components: [sec, min, hour, day, month, year, wday, yday, isdst, zone].

The ordering of the properties is important for the Ruby Time API, and is accessible with the ToA::to_tuple method.

Examples

let now = Time::now();
let to_a = now.to_a();
assert_eq!(to_a.sec, now.second());
assert_eq!(to_a.wday, now.weekday());

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The zero-argument Time#new constructor creates a local time set to the current system time.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.