Struct aws_smithy_types::date_time::DateTime
source · pub struct DateTime { /* private fields */ }
Expand description
DateTime in time.
DateTime in time represented as seconds and sub-second nanos since the Unix epoch (January 1, 1970 at midnight UTC/GMT).
This type can be converted to/from the standard library’s SystemTime
:
let the_millennium_as_system_time = SystemTime::try_from(DateTime::from_secs(946_713_600))?;
let now_as_date_time = DateTime::from(SystemTime::now());
The aws-smithy-types-convert
crate
can be used for conversions to/from other libraries, such as
time
or chrono
.
Implementations§
source§impl DateTime
impl DateTime
sourcepub fn from_secs(epoch_seconds: i64) -> Self
pub fn from_secs(epoch_seconds: i64) -> Self
Creates a DateTime
from a number of seconds since the Unix epoch.
sourcepub fn from_millis(epoch_millis: i64) -> DateTime
pub fn from_millis(epoch_millis: i64) -> DateTime
Creates a DateTime
from a number of milliseconds since the Unix epoch.
sourcepub fn from_nanos(epoch_nanos: i128) -> Result<Self, ConversionError>
pub fn from_nanos(epoch_nanos: i128) -> Result<Self, ConversionError>
Creates a DateTime
from a number of nanoseconds since the Unix epoch.
sourcepub fn as_nanos(&self) -> i128
pub fn as_nanos(&self) -> i128
Returns the number of nanoseconds since the Unix epoch that this DateTime
represents.
sourcepub fn from_fractional_secs(epoch_seconds: i64, fraction: f64) -> Self
pub fn from_fractional_secs(epoch_seconds: i64, fraction: f64) -> Self
Creates a DateTime
from a number of seconds and a fractional second since the Unix epoch.
§Example
assert_eq!(
DateTime::from_secs_and_nanos(1, 500_000_000u32),
DateTime::from_fractional_secs(1, 0.5),
);
sourcepub fn from_secs_and_nanos(seconds: i64, subsecond_nanos: u32) -> Self
pub fn from_secs_and_nanos(seconds: i64, subsecond_nanos: u32) -> Self
sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Returns the DateTime
value as an f64
representing the seconds since the Unix epoch.
Note: This conversion will lose precision due to the nature of floating point numbers.
sourcepub fn from_secs_f64(epoch_seconds: f64) -> Self
pub fn from_secs_f64(epoch_seconds: f64) -> Self
Creates a DateTime
from an f64
representing the number of seconds since the Unix epoch.
§Example
assert_eq!(
DateTime::from_fractional_secs(1, 0.5),
DateTime::from_secs_f64(1.5),
);
sourcepub fn from_str(s: &str, format: Format) -> Result<Self, DateTimeParseError>
pub fn from_str(s: &str, format: Format) -> Result<Self, DateTimeParseError>
Parses a DateTime
from a string using the given format
.
sourcepub fn has_subsec_nanos(&self) -> bool
pub fn has_subsec_nanos(&self) -> bool
Returns true if sub-second nanos is greater than zero.
sourcepub fn secs(&self) -> i64
pub fn secs(&self) -> i64
Returns the epoch seconds component of the DateTime
.
Note: this does not include the sub-second nanos.
sourcepub fn set_seconds(&mut self, seconds: i64) -> &mut Self
pub fn set_seconds(&mut self, seconds: i64) -> &mut Self
Set the seconds component of this DateTime
.
sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the sub-second nanos component of the DateTime
.
Note: this does not include the number of seconds since the epoch.
sourcepub fn set_subsec_nanos(&mut self, subsec_nanos: u32) -> &mut Self
pub fn set_subsec_nanos(&mut self, subsec_nanos: u32) -> &mut Self
Set the “sub-second” nanoseconds of this DateTime
.
sourcepub fn to_millis(self) -> Result<i64, ConversionError>
pub fn to_millis(self) -> Result<i64, ConversionError>
Converts the DateTime
to the number of milliseconds since the Unix epoch.
This is fallible since DateTime
holds more precision than an i64
, and will
return a ConversionError
for DateTime
values that can’t be converted.
Trait Implementations§
source§impl From<SystemTime> for DateTime
impl From<SystemTime> for DateTime
source§fn from(time: SystemTime) -> Self
fn from(time: SystemTime) -> Self
source§impl Ord for DateTime
impl Ord for DateTime
source§impl PartialEq for DateTime
impl PartialEq for DateTime
source§impl PartialOrd for DateTime
impl PartialOrd for DateTime
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 moresource§impl TryFrom<DateTime> for SystemTime
impl TryFrom<DateTime> for SystemTime
Tries to convert a DateTime
into a SystemTime
.
This can fail if the the DateTime
value is larger or smaller than what the SystemTime
can represent on the operating system it’s compiled for. On Linux, for example, it will only
fail on Instant::from_secs(i64::MIN)
(with any nanoseconds value). On Windows, however,
Rust’s standard library uses a smaller precision type for SystemTime
, and it will fail
conversion for a much larger range of date-times. This is only an issue if dealing with
date-times beyond several thousands of years from now.
impl Copy for DateTime
impl Eq for DateTime
impl StructuralPartialEq for DateTime
Auto Trait Implementations§
impl Freeze for DateTime
impl RefUnwindSafe for DateTime
impl Send for DateTime
impl Sync for DateTime
impl Unpin for DateTime
impl UnwindSafe for DateTime
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<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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