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
impl TimeBucket
Sourcepub const fn utc(width: BucketWidth) -> Self
pub const fn utc(width: BucketWidth) -> Self
A bucket of width, read against UTC.
Sourcepub fn with_origin(self, origin: i64) -> Result<Self, BucketParseError>
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
1dbucket 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
1mobucket 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.
Sourcepub fn with_origin_str(self, text: &str) -> Result<Self, BucketParseError>
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.
Sourcepub const fn origin(&self) -> Option<i64>
pub const fn origin(&self) -> Option<i64>
The instant this bucket’s boundaries are aligned to, if any.
Sourcepub fn parse(
width: &str,
timezone: Option<&str>,
) -> Result<Self, BucketParseError>
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.
Sourcepub const fn width(&self) -> BucketWidth
pub const fn width(&self) -> BucketWidth
This bucket’s width.
Sourcepub fn timezone(&self) -> Option<&'static str>
pub fn timezone(&self) -> Option<&'static str>
The IANA zone this bucket is read against, if any.
Sourcepub fn nominal_ns(&self) -> i64
pub fn nominal_ns(&self) -> i64
Roughly how long a bucket is — see BucketWidth::nominal_ns.
Sourcepub fn is_calendar(&self) -> bool
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.
Sourcepub fn next(&self, bucket_start: i64) -> i64
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
impl Clone for TimeBucket
Source§fn clone(&self) -> TimeBucket
fn clone(&self) -> TimeBucket
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more