Struct hyper_scripter::script_time::ScriptTime
source · pub struct ScriptTime<T = ()> { /* private fields */ }
Expand description
可能帶著資料的時間。 如果有資料,代表「這些資料是新的產生,應儲存起來但還未存」 如果沒資料,就視為上次儲存的一個快照,只記得時間就好,因為我們通常不會需要上一次存下的資料
Implementations§
source§impl<T> ScriptTime<T>
impl<T> ScriptTime<T>
Methods from Deref<Target = NaiveDateTime>§
sourcepub fn date(&self) -> NaiveDate
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());
sourcepub fn time(&self) -> NaiveTime
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());
sourcepub fn timestamp(&self) -> i64
pub fn timestamp(&self) -> i64
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 980).unwrap();
assert_eq!(dt.timestamp(), 1);
let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_opt(1, 46, 40).unwrap();
assert_eq!(dt.timestamp(), 1_000_000_000);
let dt = NaiveDate::from_ymd_opt(1969, 12, 31).unwrap().and_hms_opt(23, 59, 59).unwrap();
assert_eq!(dt.timestamp(), -1);
let dt = NaiveDate::from_ymd_opt(-1, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
assert_eq!(dt.timestamp(), -62198755200);
sourcepub fn timestamp_millis(&self) -> i64
pub fn timestamp_millis(&self) -> i64
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap();
assert_eq!(dt.timestamp_millis(), 1_444);
let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap();
assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
let dt = NaiveDate::from_ymd_opt(1969, 12, 31).unwrap().and_hms_milli_opt(23, 59, 59, 100).unwrap();
assert_eq!(dt.timestamp_millis(), -900);
sourcepub fn timestamp_micros(&self) -> i64
pub fn timestamp_micros(&self) -> i64
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap();
assert_eq!(dt.timestamp_micros(), 1_000_444);
let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap();
assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
sourcepub fn timestamp_nanos(&self) -> i64
👎Deprecated since 0.4.31: use timestamp_nanos_opt()
instead
pub fn timestamp_nanos(&self) -> i64
timestamp_nanos_opt()
insteadReturns 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:44.0 and 2262-04-11T23:47:16.854775804.
sourcepub fn timestamp_nanos_opt(&self) -> Option<i64>
pub fn timestamp_nanos_opt(&self) -> Option<i64>
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:44.0 and 2262-04-11T23:47:16.854775804.
Example
use chrono::{NaiveDate, NaiveDateTime};
let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).unwrap();
assert_eq!(dt.timestamp_nanos_opt(), Some(1_000_000_444));
let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 40, 555).unwrap();
const A_BILLION: i64 = 1_000_000_000;
let nanos = dt.timestamp_nanos_opt().unwrap();
assert_eq!(nanos, 1_000_000_000_000_000_555);
assert_eq!(
Some(dt),
NaiveDateTime::from_timestamp_opt(nanos / A_BILLION, (nanos % A_BILLION) as u32)
);
sourcepub fn timestamp_subsec_millis(&self) -> u32
pub fn timestamp_subsec_millis(&self) -> u32
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
assert_eq!(dt.timestamp_subsec_millis(), 123);
let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
assert_eq!(dt.timestamp_subsec_millis(), 1_234);
sourcepub fn timestamp_subsec_micros(&self) -> u32
pub fn timestamp_subsec_micros(&self) -> u32
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
assert_eq!(dt.timestamp_subsec_micros(), 123_456);
let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
assert_eq!(dt.timestamp_subsec_micros(), 1_234_567);
sourcepub fn timestamp_subsec_nanos(&self) -> u32
pub fn timestamp_subsec_nanos(&self) -> u32
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.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_nano_opt(9, 10, 11, 123_456_789).unwrap();
assert_eq!(dt.timestamp_subsec_nanos(), 123_456_789);
let dt = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_nano_opt(8, 59, 59, 1_234_567_890).unwrap();
assert_eq!(dt.timestamp_subsec_nanos(), 1_234_567_890);
sourcepub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
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 Clone
able,
since the resulting DelayedFormat
value may be formatted multiple times.
Example
use chrono::NaiveDate;
use chrono::format::strftime::StrftimeItems;
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");
sourcepub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>
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");
sourcepub fn and_local_timezone<Tz>(&self, tz: Tz) -> LocalResult<DateTime<Tz>>where
Tz: TimeZone,
pub fn and_local_timezone<Tz>(&self, tz: Tz) -> LocalResult<DateTime<Tz>>where Tz: TimeZone,
Converts the NaiveDateTime
into the timezone-aware DateTime<Tz>
with the provided timezone, if possible.
This can fail in cases where the local time represented by the NaiveDateTime
is not a valid local timestamp in the target timezone due to an offset transition
for example if the target timezone had a change from +00:00 to +01:00
occuring at 2015-09-05 22:59:59, then a local time of 2015-09-05 23:56:04
could never occur. Similarly, if the offset transitioned in the opposite direction
then there would be two local times of 2015-09-05 23:56:04, one at +00:00 and one
at +01:00.
Example
use chrono::{NaiveDate, FixedOffset};
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);
sourcepub fn and_utc(&self) -> DateTime<Utc>
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);
pub const MIN: NaiveDateTime = _
pub const MAX: NaiveDateTime = _
pub const UNIX_EPOCH: NaiveDateTime = _
Trait Implementations§
source§impl<T: Clone> Clone for ScriptTime<T>
impl<T: Clone> Clone for ScriptTime<T>
source§fn clone(&self) -> ScriptTime<T>
fn clone(&self) -> ScriptTime<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for ScriptTime<T>
impl<T: Debug> Debug for ScriptTime<T>
source§impl<T> Deref for ScriptTime<T>
impl<T> Deref for ScriptTime<T>
source§impl<T> Ord for ScriptTime<T>
impl<T> Ord for ScriptTime<T>
source§impl<T> PartialEq for ScriptTime<T>
impl<T> PartialEq for ScriptTime<T>
source§impl<T> PartialOrd for ScriptTime<T>
impl<T> PartialOrd for ScriptTime<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl<T: Copy> Copy for ScriptTime<T>
impl<T> Eq for ScriptTime<T>
Auto Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.