Struct ScriptTime

Source
pub struct ScriptTime<T = ()> { /* private fields */ }
Expand description

可能帶著資料的時間。 如果有資料,代表「這些資料是新的產生,應儲存起來但還未存」 如果沒資料,就視為上次儲存的一個快照,只記得時間就好,因為我們通常不會需要上一次存下的資料

Implementations§

Source§

impl<T> ScriptTime<T>

Source

pub fn now(data: T) -> Self

Source

pub fn new_or(time: Option<NaiveDateTime>, default: Self) -> Self

Source

pub fn new(time: NaiveDateTime) -> Self

Source

pub fn data(&self) -> Option<&T>

Source

pub fn has_changed(&self) -> bool

Methods from Deref<Target = NaiveDateTime>§

Source

pub const MIN: NaiveDateTime

Source

pub const MAX: NaiveDateTime

Source

pub const UNIX_EPOCH: NaiveDateTime

Source

pub fn date(&self) -> NaiveDate

Retrieves a date component.

§Example
use chrono::NaiveDate;

let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.date(), NaiveDate::from_ymd_opt(2016, 7, 8).unwrap());
Source

pub fn time(&self) -> NaiveTime

Retrieves a time component.

§Example
use chrono::{NaiveDate, NaiveTime};

let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.time(), NaiveTime::from_hms_opt(9, 10, 11).unwrap());
Source

pub fn timestamp(&self) -> i64

👎Deprecated since 0.4.35: use .and_utc().timestamp() instead

Returns the number of non-leap seconds since the midnight on January 1, 1970.

Note that this does not account for the timezone! The true “UNIX timestamp” would count seconds since the midnight UTC on the epoch.

Source

pub fn timestamp_millis(&self) -> i64

👎Deprecated since 0.4.35: use .and_utc().timestamp_millis() instead

Returns the number of non-leap milliseconds since midnight on January 1, 1970.

Note that this does not account for the timezone! The true “UNIX timestamp” would count seconds since the midnight UTC on the epoch.

Source

pub fn timestamp_micros(&self) -> i64

👎Deprecated since 0.4.35: use .and_utc().timestamp_micros() instead

Returns the number of non-leap microseconds since midnight on January 1, 1970.

Note that this does not account for the timezone! The true “UNIX timestamp” would count seconds since the midnight UTC on the epoch.

Source

pub fn timestamp_nanos(&self) -> i64

👎Deprecated since 0.4.31: use .and_utc().timestamp_nanos_opt() instead

Returns the number of non-leap nanoseconds since midnight on January 1, 1970.

Note that this does not account for the timezone! The true “UNIX timestamp” would count seconds since the midnight UTC on the epoch.

§Panics

An i64 with nanosecond precision can span a range of ~584 years. This function panics on an out of range NaiveDateTime.

The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192 and 2262-04-11T23:47:16.854775807.

Source

pub fn timestamp_nanos_opt(&self) -> Option<i64>

👎Deprecated since 0.4.35: use .and_utc().timestamp_nanos_opt() instead

Returns the number of non-leap nanoseconds since midnight on January 1, 1970.

Note that this does not account for the timezone! The true “UNIX timestamp” would count seconds since the midnight UTC on the epoch.

§Errors

An i64 with nanosecond precision can span a range of ~584 years. This function returns None on an out of range NaiveDateTime.

The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192 and 2262-04-11T23:47:16.854775807.

Source

pub fn timestamp_subsec_millis(&self) -> u32

👎Deprecated since 0.4.35: use .and_utc().timestamp_subsec_millis() instead

Returns the number of milliseconds since the last whole non-leap second.

The return value ranges from 0 to 999, or for leap seconds, to 1,999.

Source

pub fn timestamp_subsec_micros(&self) -> u32

👎Deprecated since 0.4.35: use .and_utc().timestamp_subsec_micros() instead

Returns the number of microseconds since the last whole non-leap second.

The return value ranges from 0 to 999,999, or for leap seconds, to 1,999,999.

Source

pub fn timestamp_subsec_nanos(&self) -> u32

👎Deprecated since 0.4.36: use .and_utc().timestamp_subsec_nanos() instead

Returns the number of nanoseconds since the last whole non-leap second.

The return value ranges from 0 to 999,999,999, or for leap seconds, to 1,999,999,999.

Source

pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>,

Formats the combined date and time with the specified formatting items. Otherwise it is the same as the ordinary format method.

The Iterator of items should be Cloneable, since the resulting DelayedFormat value may be formatted multiple times.

§Example
use chrono::format::strftime::StrftimeItems;
use chrono::NaiveDate;

let fmt = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
assert_eq!(dt.format_with_items(fmt.clone()).to_string(), "2015-09-05 23:56:04");
assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", dt.format_with_items(fmt)), "2015-09-05 23:56:04");
Source

pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>

Formats the combined date and time with the specified format string. See the format::strftime module on the supported escape sequences.

This returns a DelayedFormat, which gets converted to a string only when actual formatting happens. You may use the to_string method to get a String, or just feed it into print! and other formatting macros. (In this way it avoids the redundant memory allocation.)

A wrong format string does not issue an error immediately. Rather, converting or formatting the DelayedFormat fails. You are recommended to immediately use DelayedFormat for this reason.

§Example
use chrono::NaiveDate;

let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2015-09-05 23:56:04");
assert_eq!(dt.format("around %l %p on %b %-d").to_string(), "around 11 PM on Sep 5");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", dt.format("%Y-%m-%d %H:%M:%S")), "2015-09-05 23:56:04");
assert_eq!(format!("{}", dt.format("around %l %p on %b %-d")), "around 11 PM on Sep 5");
Source

pub fn and_local_timezone<Tz>(&self, tz: Tz) -> LocalResult<DateTime<Tz>>
where Tz: TimeZone,

Converts the NaiveDateTime into a timezone-aware DateTime<Tz> with the provided time zone.

§Example
use chrono::{FixedOffset, NaiveDate};
let hour = 3600;
let tz = FixedOffset::east_opt(5 * hour).unwrap();
let dt = NaiveDate::from_ymd_opt(2015, 9, 5)
    .unwrap()
    .and_hms_opt(23, 56, 4)
    .unwrap()
    .and_local_timezone(tz)
    .unwrap();
assert_eq!(dt.timezone(), tz);
Source

pub fn and_utc(&self) -> DateTime<Utc>

Converts the NaiveDateTime into the timezone-aware DateTime<Utc>.

§Example
use chrono::{NaiveDate, Utc};
let dt =
    NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap().and_utc();
assert_eq!(dt.timezone(), Utc);

Trait Implementations§

Source§

impl<T: Clone> Clone for ScriptTime<T>

Source§

fn clone(&self) -> ScriptTime<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ScriptTime<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for ScriptTime<T>

Source§

type Target = NaiveDateTime

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> Ord for ScriptTime<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for ScriptTime<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for ScriptTime<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy> Copy for ScriptTime<T>

Source§

impl<T> Eq for ScriptTime<T>

Auto Trait Implementations§

§

impl<T> Freeze for ScriptTime<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ScriptTime<T>
where T: RefUnwindSafe,

§

impl<T> Send for ScriptTime<T>
where T: Send,

§

impl<T> Sync for ScriptTime<T>
where T: Sync,

§

impl<T> Unpin for ScriptTime<T>
where T: Unpin,

§

impl<T> UnwindSafe for ScriptTime<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,