DateTime

Struct DateTime 

Source
pub struct DateTime<C: Calendar, S: Standard> { /* private fields */ }
Expand description

A calendar date and time, with attosecond precision, representing the time elapsed since the start of the Common Era in a traditional way according to a particular time Standard.

DateTimes are type parameterized by a Calendar type which is either Gregorian or Julian.

Normal ranges for values are as follows:

  • year: any i32 value (-2_147_483_648 .. 2_147_483_647)
  • month: 1 .. 12
  • day: 1 .. 31 (or less in some months)
  • hour: 0 .. 23
  • minute: 0 .. 59
  • second: 0 .. 60 (60 is only used under leap-second time standards)
  • attosecond: 0 .. 999_999_999_999_999_999

Even when years are negative, the other values must remain in the positive ranges as specified (i.e. negative values are not a reflection against zero, but just an extension further backwards)

Zero and Negative years are handled in accordance with ISO 8601, such that year 0 is 1 B.C., and year -1 is 2 B.C., etc. In general:

  • n B.C. is represented by year 1-n
  • Year -y represents year y+1 B.C. (for positive y).

This type is proleptic; that is, it allows values for dates outside the normal usage period of the Calendar.

The oldest date representable is -2147483648-01-01 00:00:00.000000000000000000

The newest date representable is 2147483647-12-31 23:59:59.999999999999999999

Internally this is stored in a packed format and is 128 bits in size.

This represents the same thing that an Instant does, but it makes Calendar data easier to work with, and has such date precomputed and packed within.

Implementations§

Source§

impl<C: Calendar, S: Standard> DateTime<C, S>

Source

pub unsafe fn new_unchecked( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Self

Create a new DateTime with the given parts.

§Safety

Parameter values must be within normal ranges, or else the results are not defined.

Source

pub fn new( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Result<Self, Error>

Create a new DateTime from the given parts.

Values must be within normal ranges. See DateTime for details.

§Errors

Will return Error::RangeError if any input is outside of the normal range (months from 1-12, days from 1-31, hours from 0-23, minutes from 0-59, seconds from 0-60, attoseconds from 0-999_999_999_999_999_999)

Source

pub fn new_bc( bc_year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, attosecond: u64, ) -> Result<Self, Error>

Create a new DateTime from the given parts, with BC years.

Values must be within normal ranges. See DateTime for details.

§Errors

Will return Error::RangeError if any input is outside of the normal range (months from 1-12, days from 1-31, hours from 0-23, minutes from 0-59, seconds from 0-60, attoseconds from 0-999_999_999_999_999_999)

Source

pub fn new_abnormal( year: i32, month: i64, day: i64, hour: i64, minute: i64, second: i64, attosecond: i64, ) -> Self

Create a new DateTime from the given parts.

Values that are out of normal ranges are allowed, including values that are negative. This function will adjust the input your provide into a normal form.

The types we are working with are large i64 types, but they can still overflow. Overflow is not detected or reported (FIXME).

§Panics

Shouldn’t panic but several math assertions may trigger if we have a bug when compiled in development mode.

Source

pub fn from_day_number(day_number: i64) -> Result<Self, Error>

Create a DateTime from a day number (integer).

January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.

Hour, minute, second and attosecond will be zero.

§Errors

Will return a Error::RangeError if day_number is out of range.

Source

pub fn from_day_number_and_fraction( day_number: i64, day_fraction: f64, ) -> Result<Self, Error>

Create a DateTime from a day number (integer) and day fraction (float).

January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.

§Errors

Will return a Error::RangeError if day_number is out of range.

Will return Error::RangeError if day_fraction is <0.0 or >=1.0

§Panics

Panics on assertions that should only fail if there is a bug.

Source

pub fn from_duration_from_epoch(duration: Duration) -> Self

Create a DateTime from a Duration from the calendar epoch (with the calendar epoch represented in time Standard S, such that no time Standard conversions are done here).

Source

pub fn year(&self) -> i32

The year part

Source

pub fn year_bc(&self) -> i32

The year part in BC years

Source

pub fn month(&self) -> u8

The month part. Ranges from 1 .. 12

Source

pub fn month0(&self) -> u8

The month part where January is mapped to 0. Ranges from 0 .. 11

Source

pub fn day(&self) -> u8

The day part. Ranges from 1 .. 31

Source

pub fn day0(&self) -> u8

The day part where the 1st day is mapped to 0. Ranges from 0 .. 30

Source

pub fn hour(&self) -> u8

The hour part. Ranges from 0 .. 23

Source

pub fn minute(&self) -> u8

The minute part. Ranges from 0 .. 59

Source

pub fn second(&self) -> u8

The second part. Ranges from 0 .. 59

Source

pub fn attosecond(&self) -> u64

The attosecond part. Ranges from 0 .. 999_999_999_999_999_999

Source

pub fn date(&self) -> (i32, u8, u8)

The date part

Returns (year, month, day)

Source

pub fn time(&self) -> (u8, u8, u8, u64)

The time part

Returns (hour, minute, second, attosecond)

Source

pub fn set_year(&mut self, year: i32)

Set the year, leaving other fields unchanged

Source

pub fn set_year_bc(&mut self, year_bc: i32)

Set the year with a BC year, leaving other fields unchanged

Source

pub fn set_month(&mut self, month: u8) -> Result<(), Error>

Set the month, leaving other fields unchanged

§Errors

Will return Error::RangeError if month is <1 or >12.

Source

pub fn set_day(&mut self, day: u8) -> Result<(), Error>

Set the day, leaving other fields unchanged

§Errors

Will return Error::RangeError if day is outside of the range of days for the month.

Source

pub fn set_hour(&mut self, hour: u8) -> Result<(), Error>

Set the hour, leaving other fields unchanged

§Errors

Will return Error::RangeError if hour is greater than 23.

Source

pub fn set_minute(&mut self, minute: u8) -> Result<(), Error>

Set the minute, leaving other fields unchanged

§Errors

Will return Error::RangeError if minute is greater than 59.

Source

pub fn set_second(&mut self, second: u8) -> Result<(), Error>

Set the second, leaving other fields unchanged

§Errors

Will return Error::RangeError if second is greater than 60. The second of ‘60’ should only be used for leapsecond situations, but no error is thrown if used otherwise.

Source

pub fn set_attosecond(&mut self, attosecond: u64) -> Result<(), Error>

Set the attosecond, leaving other fields unchanged

§Errors

Will return Error::RangeError if attosecond are out of the proscribed range (more than 1 seconds worth of attoseconds)

Source

pub fn set_date(&mut self, date: (i32, u8, u8)) -> Result<(), Error>

Set the date part (year, month, day)

§Errors

Will return Error::RangeError if any input values are out of the proscribed range

Source

pub fn set_time(&mut self, date: (u8, u8, u8, u64)) -> Result<(), Error>

Set the time part (hour, minute, second, attosecond)

§Errors

Will return Error::RangeError if any input values are out of the proscribed range

Source

pub fn day_number(&self) -> i64

Day number (integer).

January 1st of 1 A.D. (Common Era) is the epoch and has a day number of 0.

§Panics

Will only panic on a bug that caused internal values to get out of range.

Source

pub fn day_fraction(&self) -> f64

Day fraction, fractional part of the day since midnight

This isn’t attosecond accurate because a day contains more attoseconds than can fit in a f64 (which has 52 bits of precision). However, it should be accurate to 10,000 attoseconds.

Source

pub fn duration_from_epoch(&self) -> Duration

Duration from the calendar epoch (with the calendar epoch represented in the time Standard S, such that no time Standard conversions are done here).

§Panics

Only panics if we have an internal data consistency issue

Trait Implementations§

Source§

impl<C: Calendar, S: Standard> Add<Duration> for DateTime<C, S>

Source§

type Output = DateTime<C, S>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> Self

Performs the + operation. Read more
Source§

impl<C: Clone + Calendar, S: Clone + Standard> Clone for DateTime<C, S>

Source§

fn clone(&self) -> DateTime<C, S>

Returns a duplicate 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<C: Calendar, S: Standard> Debug for DateTime<C, S>

Source§

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

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

impl<C: Calendar, S: Standard> Display for DateTime<C, S>

Source§

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

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

impl<C: Calendar, S: Standard> From<DateTime<C, S>> for Instant

Source§

fn from(dt: DateTime<C, S>) -> Self

Converts to this type from the input type.
Source§

impl<C: Calendar, S: Standard> From<Instant> for DateTime<C, S>

Source§

fn from(i: Instant) -> Self

Converts to this type from the input type.
Source§

impl<C: Calendar, S: Standard> Hash for DateTime<C, S>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<C: Calendar, S: Standard> Ord for DateTime<C, S>

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<C: Calendar, S: Standard> PartialEq for DateTime<C, S>

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<C: Calendar, S: Standard> PartialOrd for DateTime<C, S>

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<C: Calendar, S: Standard> Sub<Duration> for DateTime<C, S>

Source§

type Output = DateTime<C, S>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> Self

Performs the - operation. Read more
Source§

impl<C: Calendar, S: Standard> Sub for DateTime<C, S>

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Duration

Performs the - operation. Read more
Source§

impl<S: Standard> TryFrom<DateTime<Gregorian, S>> for DateTime<Julian, S>

Source§

type Error = Error

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

fn try_from(input: DateTime<Gregorian, S>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<S: Standard> TryFrom<DateTime<Julian, S>> for DateTime<Gregorian, S>

Source§

type Error = Error

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

fn try_from(input: DateTime<Julian, S>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<C: Copy + Calendar, S: Copy + Standard> Copy for DateTime<C, S>

Source§

impl<C: Calendar, S: Standard> Eq for DateTime<C, S>

Source§

impl<C: Calendar, S: Standard> Send for DateTime<C, S>

Auto Trait Implementations§

§

impl<C, S> Freeze for DateTime<C, S>

§

impl<C, S> RefUnwindSafe for DateTime<C, S>

§

impl<C, S> Sync for DateTime<C, S>
where C: Sync, S: Sync,

§

impl<C, S> Unpin for DateTime<C, S>
where C: Unpin, S: Unpin,

§

impl<C, S> UnwindSafe for DateTime<C, S>
where C: UnwindSafe, S: 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.