jiff::span

Enum Relative

Source
enum Relative<'a> {
    Civil(RelativeCivil),
    Zoned(RelativeZoned<'a>),
}
Expand description

An internal abstraction for managing a relative datetime for use in some Span APIs.

This is effectively the same as a SpanRelativeTo, but uses a Cow<Zoned> instead of a &Zoned. This makes it non-Copy, but allows us to craft a more uniform API. (i.e., relative + span = relative instead of `relative

  • span = owned_relativeor whatever.) Note that theCopyimpl onSpanRelativeTomeans it has to accept a&Zoned. It can't ever take a Zoned` since it is non-Copy.

NOTE: Separately from above, I think it’s plausible that this type could be designed a bit differently. Namely, something like this:

struct Relative<'a> {
    tz: Option<&'a TimeZone>,
    dt: DateTime,
    ts: Timestamp,
}

That is, we do zone aware stuff but without an actual Zoned type. But I think in order to make that work, we would need to expose most of the Zoned API as functions on its component types (DateTime, Timestamp and TimeZone). I think we are likely to want to do that for public API reasons, but I’d like to resist it since I think it will add a lot of complexity. Or maybe we need a Unzoned type that is DateTime and Timestamp, but requires passing the time zone in to each of its methods. That might work quite well, even if it was just an internal type.

Anyway, I’m not 100% sure the above would work, but I think it would. It would be nicer because everything would be Copy all the time. We’d never need a Cow<TimeZone> for example, because we never need to change or create a new time zone.

Variants§

Implementations§

Source§

impl<'a> Relative<'a>

Source

fn checked_add(&self, span: Span) -> Result<Relative<'_>, Error>

Adds the given span to this relative datetime.

This defers to either DateTime::checked_add or Zoned::checked_add, depending on the type of relative datetime.

The Relative datetime returned is guaranteed to have the same internal datetie type as self.

§Errors

This returns an error in the same cases as the underlying checked arithmetic APIs. In general, this occurs when adding the given span would result in overflow.

Source

fn checked_add_duration( &self, duration: SignedDuration, ) -> Result<Relative<'_>, Error>

Source

fn until(&self, largest: Unit, other: &Relative<'_>) -> Result<Span, Error>

Returns the span of time from this relative datetime to the one given, with units as large as largest.

§Errors

This returns an error in the same cases as when the underlying DateTime::until or Zoned::until fail. Because this doesn’t set or expose any rounding configuration, this can generally only occur when largest is Unit::Nanosecond and the span of time between self and other is too big to represent as a 64-bit integer nanosecond count.

§Panics

This panics if self and other are different internal datetime types. For example, if self was a civil datetime and other were a zoned datetime.

Source

fn to_nanosecond(&self) -> ri128<{ i128::MIN }, { i128::MAX }>

Converts this relative datetime to a nanosecond in UTC time.

§Errors

If there was a problem doing this conversion, then an error is returned. In practice, this only occurs for a civil datetime near the civil datetime minimum and maximum values.

Source

fn into_relative_span( self, largest: Unit, span: Span, ) -> Result<RelativeSpan<'a>, Error>

Create a balanced span of time relative to this datetime.

The relative span returned has the same internal datetime type (civil or zoned) as this relative datetime.

§Errors

This returns an error when the span in this range cannot be represented. In general, this only occurs when asking for largest units of Unit::Nanosecond and when the span is too big to fit into a 64-bit nanosecond count.

This can also return an error in other extreme cases, such as when adding the given span to this relative datetime results in overflow, or if this relative datetime is a civil datetime and it couldn’t be converted to a timestamp in UTC.

Source

fn round( self, span: Span, smallest: Unit, largest: Unit, increment: ri128<{ i128::MIN }, { i128::MAX }>, mode: RoundMode, ) -> Result<Span, Error>

Rounds the given span using the given rounding configuration.

Trait Implementations§

Source§

impl<'a> Clone for Relative<'a>

Source§

fn clone(&self) -> Relative<'a>

Returns a copy 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<'a> Debug for Relative<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Relative<'a>

§

impl<'a> RefUnwindSafe for Relative<'a>

§

impl<'a> Send for Relative<'a>

§

impl<'a> Sync for Relative<'a>

§

impl<'a> Unpin for Relative<'a>

§

impl<'a> UnwindSafe for Relative<'a>

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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, 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.