Struct datetime_default::DateTimeDefaultUnix
source · [−]pub struct DateTimeDefaultUnix<Tz, const N: i32 = 0>(_)
where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy;
Expand description
DateTime with UNIX epoch as default.
use chrono::{Utc, TimeZone};
use datetime_default::DateTimeDefaultUnix;
assert_eq!(
DateTimeDefaultUnix::<Utc>::default(),
Utc.datetime_from_str("1970/1/1 00:00:00", "%Y/%m/%d %H:%M:%S").unwrap()
);
Methods from Deref<Target = DateTime<Tz>>
sourcepub fn date(&self) -> Date<Tz>
pub fn date(&self) -> Date<Tz>
Retrieves a date component
Unless you are immediately planning on turning this into a DateTime
with the same Timezone you should use the
date_naive
method.
use chrono::prelude::*;
let date: Date<Utc> = Utc.ymd(2020, 1, 1);
let dt: DateTime<Utc> = date.and_hms(0, 0, 0);
assert_eq!(dt.date(), date);
assert_eq!(dt.date().and_hms(1, 1, 1), date.and_hms(1, 1, 1));
sourcepub fn date_naive(&self) -> NaiveDate
pub fn date_naive(&self) -> NaiveDate
Retrieves the Date without an associated timezone
NaiveDate
is a more well-defined type, and has more traits implemented on it,
so should be preferred to Date
any time you truly want to operate on Dates.
use chrono::prelude::*;
let date: DateTime<Utc> = Utc.ymd(2020, 1, 1).and_hms(0, 0, 0);
let other: DateTime<FixedOffset> = FixedOffset::east(23).ymd(2020, 1, 1).and_hms(0, 0, 0);
assert_eq!(date.date_naive(), other.date_naive());
sourcepub fn time(&self) -> NaiveTime
pub fn time(&self) -> NaiveTime
Retrieves a time component.
Unlike date
, this is not associated to the time zone.
sourcepub fn timestamp(&self) -> i64
pub fn timestamp(&self) -> i64
Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).
sourcepub fn timestamp_millis(&self) -> i64
pub fn timestamp_millis(&self) -> i64
Returns the number of non-leap-milliseconds since January 1, 1970 UTC
Note that this does reduce the number of years that can be represented from ~584 Billion to ~584 Million. (If this is a problem, please file an issue to let me know what domain needs millisecond precision over billions of years, I’m curious.)
Example
use chrono::Utc;
use chrono::TimeZone;
let dt = Utc.ymd(1970, 1, 1).and_hms_milli(0, 0, 1, 444);
assert_eq!(dt.timestamp_millis(), 1_444);
let dt = Utc.ymd(2001, 9, 9).and_hms_milli(1, 46, 40, 555);
assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
sourcepub fn timestamp_micros(&self) -> i64
pub fn timestamp_micros(&self) -> i64
Returns the number of non-leap-microseconds since January 1, 1970 UTC
Note that this does reduce the number of years that can be represented from ~584 Billion to ~584 Thousand. (If this is a problem, please file an issue to let me know what domain needs microsecond precision over millennia, I’m curious.)
Example
use chrono::Utc;
use chrono::TimeZone;
let dt = Utc.ymd(1970, 1, 1).and_hms_micro(0, 0, 1, 444);
assert_eq!(dt.timestamp_micros(), 1_000_444);
let dt = Utc.ymd(2001, 9, 9).and_hms_micro(1, 46, 40, 555);
assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
sourcepub fn timestamp_nanos(&self) -> i64
pub fn timestamp_nanos(&self) -> i64
Returns the number of non-leap-nanoseconds since January 1, 1970 UTC
Note that this does reduce the number of years that can be represented from ~584 Billion to ~584. (If this is a problem, please file an issue to let me know what domain needs nanosecond precision over millennia, I’m curious.)
Example
use chrono::Utc;
use chrono::TimeZone;
let dt = Utc.ymd(1970, 1, 1).and_hms_nano(0, 0, 1, 444);
assert_eq!(dt.timestamp_nanos(), 1_000_000_444);
let dt = Utc.ymd(2001, 9, 9).and_hms_nano(1, 46, 40, 555);
assert_eq!(dt.timestamp_nanos(), 1_000_000_000_000_000_555);
sourcepub fn timestamp_subsec_millis(&self) -> u32
pub fn timestamp_subsec_millis(&self) -> u32
Returns the number of milliseconds since the last second boundary
warning: in event of a leap second, this may exceed 999
note: this is not the number of milliseconds since January 1, 1970 0:00:00 UTC
sourcepub fn timestamp_subsec_micros(&self) -> u32
pub fn timestamp_subsec_micros(&self) -> u32
Returns the number of microseconds since the last second boundary
warning: in event of a leap second, this may exceed 999_999
note: this is not the number of microseconds since January 1, 1970 0:00:00 UTC
sourcepub fn timestamp_subsec_nanos(&self) -> u32
pub fn timestamp_subsec_nanos(&self) -> u32
Returns the number of nanoseconds since the last second boundary
warning: in event of a leap second, this may exceed 999_999_999
note: this is not the number of nanoseconds since January 1, 1970 0:00:00 UTC
sourcepub fn with_timezone<Tz2>(&self, tz: &Tz2) -> DateTime<Tz2>where
Tz2: TimeZone,
pub fn with_timezone<Tz2>(&self, tz: &Tz2) -> DateTime<Tz2>where
Tz2: TimeZone,
Changes the associated time zone.
The returned DateTime
references the same instant of time from the perspective of the provided time zone.
sourcepub fn naive_utc(&self) -> NaiveDateTime
pub fn naive_utc(&self) -> NaiveDateTime
Returns a view to the naive UTC datetime.
sourcepub fn naive_local(&self) -> NaiveDateTime
pub fn naive_local(&self) -> NaiveDateTime
Returns a view to the naive local datetime.
sourcepub fn years_since(&self, base: DateTime<Tz>) -> Option<u32>
pub fn years_since(&self, base: DateTime<Tz>) -> Option<u32>
Retrieve the elapsed years from now to the given DateTime
.
pub const MIN_UTC: DateTime<Utc> = DateTime{ datetime: NaiveDateTime::MIN, offset: Utc,}
pub const MAX_UTC: DateTime<Utc> = DateTime{ datetime: NaiveDateTime::MAX, offset: Utc,}
sourcepub fn to_rfc2822(&self) -> String
pub fn to_rfc2822(&self) -> String
Returns an RFC 2822 date and time string such as Tue, 1 Jul 2003 10:52:37 +0200
.
sourcepub fn to_rfc3339(&self) -> String
pub fn to_rfc3339(&self) -> String
Returns an RFC 3339 and ISO 8601 date and time string such as 1996-12-19T16:39:57-08:00
.
sourcepub fn to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String
pub fn to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String
Return an RFC 3339 and ISO 8601 date and time string with subseconds
formatted as per a SecondsFormat
.
If passed use_z
true and the timezone is UTC (offset 0), use ‘Z’, as
per Fixed::TimezoneOffsetColonZ
If passed use_z
false, use
Fixed::TimezoneOffsetColon
Examples
let dt = Utc.ymd(2018, 1, 26).and_hms_micro(18, 30, 9, 453_829);
assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false),
"2018-01-26T18:30:09.453+00:00");
assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true),
"2018-01-26T18:30:09.453Z");
assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
"2018-01-26T18:30:09Z");
let pst = FixedOffset::east(8 * 60 * 60);
let dt = pst.ymd(2018, 1, 26).and_hms_micro(10, 30, 9, 453_829);
assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
"2018-01-26T10:30:09+08:00");
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.
sourcepub fn format(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>
pub fn format(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>
Formats the combined date and time with the specified format string.
See the crate::format::strftime
module
on the supported escape sequences.
Example
use chrono::prelude::*;
let date_time: DateTime<Utc> = Utc.ymd(2017, 04, 02).and_hms(12, 50, 32);
let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
assert_eq!(formatted, "02/04/2017 12:50");
Trait Implementations
sourceimpl<Tz: Clone, const N: i32> Clone for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: Clone, const N: i32> Clone for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn clone(&self) -> DateTimeDefaultUnix<Tz, N>
fn clone(&self) -> DateTimeDefaultUnix<Tz, N>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl<Tz: Debug, const N: i32> Debug for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: Debug, const N: i32> Debug for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourceimpl<const N: i32> Default for DateTimeDefaultUnix<FixedOffset, N>
impl<const N: i32> Default for DateTimeDefaultUnix<FixedOffset, N>
sourceimpl Default for DateTimeDefaultUnix<Local, 0>
impl Default for DateTimeDefaultUnix<Local, 0>
sourceimpl Default for DateTimeDefaultUnix<Utc, 0>
impl Default for DateTimeDefaultUnix<Utc, 0>
sourceimpl<Tz, const N: i32> Deref for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> Deref for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourceimpl<Tz, const N: i32> From<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> From<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourceimpl<Tz: Hash, const N: i32> Hash for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: Hash, const N: i32> Hash for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourceimpl<Tz: Ord, const N: i32> Ord for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: Ord, const N: i32> Ord for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Ordering
fn cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<Tz, const N: i32> PartialEq<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> PartialEq<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourceimpl<Tz, const N: i32> PartialEq<DateTimeDefaultUnix<Tz, N>> for DateTime<Tz>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> PartialEq<DateTimeDefaultUnix<Tz, N>> for DateTime<Tz>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn eq(&self, other: &DateTimeDefaultUnix<Tz, N>) -> bool
fn eq(&self, other: &DateTimeDefaultUnix<Tz, N>) -> bool
sourceimpl<Tz: PartialEq, const N: i32> PartialEq<DateTimeDefaultUnix<Tz, N>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: PartialEq, const N: i32> PartialEq<DateTimeDefaultUnix<Tz, N>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn eq(&self, other: &DateTimeDefaultUnix<Tz, N>) -> bool
fn eq(&self, other: &DateTimeDefaultUnix<Tz, N>) -> bool
sourceimpl<Tz, const N: i32> PartialOrd<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> PartialOrd<DateTime<Tz>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn partial_cmp(&self, other: &DateTime<Tz>) -> Option<Ordering>
fn partial_cmp(&self, other: &DateTime<Tz>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<Tz, const N: i32> PartialOrd<DateTimeDefaultUnix<Tz, N>> for DateTime<Tz>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz, const N: i32> PartialOrd<DateTimeDefaultUnix<Tz, N>> for DateTime<Tz>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn partial_cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Option<Ordering>
fn partial_cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<Tz: PartialOrd, const N: i32> PartialOrd<DateTimeDefaultUnix<Tz, N>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
impl<Tz: PartialOrd, const N: i32> PartialOrd<DateTimeDefaultUnix<Tz, N>> for DateTimeDefaultUnix<Tz, N>where
Tz: TimeZone,
<Tz as TimeZone>::Offset: Copy,
sourcefn partial_cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Option<Ordering>
fn partial_cmp(&self, other: &DateTimeDefaultUnix<Tz, N>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more