Skip to main content

TimeBucket

Struct TimeBucket 

Source
pub struct TimeBucket { /* private fields */ }
Expand description

A bucket width together with the calendar it is read against.

See the module documentation for what each width means.

Implementations§

Source§

impl TimeBucket

Source

pub const fn utc(width: BucketWidth) -> Self

A bucket of width, read against UTC.

Source

pub const fn fixed_ns(ns: i64) -> Self

A fixed span of nanoseconds against UTC.

Source

pub fn fixed(span: Duration) -> Self

A fixed span, from a Duration.

A Duration is a fixed span, so this conversion loses nothing — which is exactly why it cannot produce a calendar bucket. For a day in a zone or a calendar month, say so by name: parse with "1d" or "1mo".

Saturates at i64::MAX nanoseconds, about 292 years.

Source

pub const fn in_zone(self, tz: Tz) -> Self

Read this bucket against an IANA time zone.

Source

pub fn with_origin(self, origin: i64) -> Result<Self, BucketParseError>

Align bucket boundaries to origin instead of the default anchor.

The origin is an instant in epoch nanoseconds; what is taken from it is its local position in this bucket’s zone:

  • a fixed width takes the whole instant, and buckets tile outwards from it in both directions;
  • a day or week width takes the origin’s local time of day, so a 1d bucket runs 06:00 → 06:00 for a shift that starts at six;
  • a month or year width takes its local day of month and time of day, so a 1mo bucket runs from the 15th to the 15th for a billing period that starts then.

The origin may sit before, inside or after the data — only where it falls in the cycle matters, not how far away it is. For a width of one unit that is just the phase, so any date with the right time of day (or day of month) will do. For a multi-unit width it also selects which of the n units opens a bucket: 3mo anchored on January gives calendar quarters, anchored on February gives February–April.

It replaces the default anchor entirely, so a 1w bucket with an origin no longer starts on a Monday unless the origin does.

§Errors

BucketParseError if the origin’s local day of month is 29, 30 or 31 for a month or year width. Those days do not exist in every month, so such a boundary is not a monthly one: it would have to be clamped, and a clamp makes the buckets drift (31 January → 28 February → 28 March) or stop being a fixed day of the month. A month-end boundary is a different question this type does not answer.

Source

pub fn with_origin_str(self, text: &str) -> Result<Self, BucketParseError>

Align boundaries to an origin written as text.

Accepts an RFC 3339 instant (2024-01-15T06:00:00+01:00), or a local wall-clock time read in this bucket’s zone: 2024-01-15, 2024-01-15 06:00:00, or 2024-01-15T06:00:00. A bare date is local midnight.

Reading it in the bucket’s own zone is the point: “the billing month starts on the 15th” means the local 15th, and a caller who writes 2024-01-15 against a Europe/Berlin tier means Berlin’s.

§Errors

BucketParseError if the text is not one of those forms, if it is outside the representable range, or if Self::with_origin refuses the resulting day of month.

Source

pub const fn origin(&self) -> Option<i64>

The instant this bucket’s boundaries are aligned to, if any.

Source

pub fn parse( width: &str, timezone: Option<&str>, ) -> Result<Self, BucketParseError>

Parse a width, and optionally a zone.

§Errors

BucketParseError if the width or the zone name is not understood.

Source

pub const fn width(&self) -> BucketWidth

This bucket’s width.

Source

pub fn timezone(&self) -> Option<&'static str>

The IANA zone this bucket is read against, if any.

Source

pub fn nominal_ns(&self) -> i64

Roughly how long a bucket is — see BucketWidth::nominal_ns.

Source

pub fn is_calendar(&self) -> bool

Whether a bucket’s length can vary between one bucket and the next.

True for a calendar width in any zone (February is short in UTC too), and for a day only where a zone has transitions.

Source

pub fn start_of(&self, ts: i64) -> i64

The start of the bucket containing ts.

Source

pub fn next(&self, bucket_start: i64) -> i64

The start of the bucket after the one starting at bucket_start.

Never bucket_start + width. A calendar bucket’s length depends on which bucket it is: February is three days shorter than March, and a local day is 23 or 25 hours on a transition. Code that adds is code that has assumed the calendar away.

Trait Implementations§

Source§

impl Clone for TimeBucket

Source§

fn clone(&self) -> TimeBucket

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for TimeBucket

Source§

impl Debug for TimeBucket

Source§

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

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

impl<'de> Deserialize<'de> for TimeBucket

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for TimeBucket

Source§

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

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

impl Eq for TimeBucket

Source§

impl PartialEq for TimeBucket

Source§

fn eq(&self, other: &TimeBucket) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for TimeBucket

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for TimeBucket

Auto Trait Implementations§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more