#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![forbid(unsafe_code)]
#![allow(unused_attributes)]
#![warn(absolute_paths_not_starting_with_crate)]
#![warn(elided_lifetimes_in_paths)]
#![warn(explicit_outlives_requirements)]
#![warn(meta_variable_misuse)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(non_ascii_idents)]
#![warn(noop_method_call)]
#![warn(single_use_lifetimes)]
#![warn(trivial_casts)]
#![warn(unreachable_pub)]
#![warn(unused_extern_crates)]
#![warn(unused_lifetimes)]
#![warn(unused_results)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg_attr(
any(
target_os = "dragonfly",
target_os = "emscripten",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "openbsd",
target_os = "redox",
target_os = "wasi",
),
path = "impl_rustix.rs"
)]
mod platform;
use core::convert::{TryFrom, TryInto};
use core::fmt;
use core::time::Duration;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UtcTime {
secs: i64,
nanos: u32,
}
impl UtcTime {
pub fn as_secs(self) -> i64 {
self.secs
}
pub fn as_millis(self) -> i128 {
(self.secs as i128 * 1_000) + (self.nanos as i128 / 1_000_000)
}
pub fn as_micros(self) -> i128 {
(self.secs as i128 * 1_000_000) + (self.nanos as i128 / 1_000)
}
pub fn as_nanos(self) -> i128 {
(self.secs as i128 * 1_000_000_000) + (self.nanos as i128)
}
pub fn subsec_millis(self) -> u32 {
self.nanos / 1_000_000
}
pub fn subsec_micros(self) -> u32 {
self.nanos / 1_000
}
pub fn subsec_nanos(self) -> u32 {
self.nanos
}
}
pub fn utcnow() -> Result<UtcTime> {
platform::utcnow()
}
impl TryFrom<UtcTime> for Duration {
type Error = NegativeTime;
#[cfg(feature = "std")]
fn try_from(value: UtcTime) -> Result<Self, NegativeTime> {
Ok(Duration::new(
value.secs.try_into().map_err(|_| NegativeTime)?,
value.nanos,
))
}
}
impl TryFrom<UtcTime> for std::time::SystemTime {
type Error = NegativeTime;
#[cfg(feature = "std")]
fn try_from(value: UtcTime) -> Result<Self, NegativeTime> {
let duration: Duration = value.try_into()?;
Ok(std::time::SystemTime::UNIX_EPOCH + duration)
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug)]
#[allow(missing_copy_implementations)]
#[non_exhaustive]
pub enum Error {
OsError(crate::platform::OsError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::OsError(err) => err.fmt(f),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::OsError(err) => err.source(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NegativeTime;
impl fmt::Display for NegativeTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("cannot convert a negative UtcTime")
}
}