pub(crate) struct Increment {
unit: Unit,
value: i32,
}Expand description
A representation of a rounding increment in a particular unit.
This implements the core rounding interface of Jiff, shared across all
types. The constructors on Increment know which units and increments
are valid for a particular rounding configuration. For example, only
Increment::for_span supports any unit value. All other constructors
have some kind of limitation (e.g., you can’t round a date to the nearest
month).
For most cases, rounding is done via Increment::round. This takes a
quantity in a number of nanoseconds and rounds it to the nearest increment
(after converting the increment to nanoseconds as well, based on the unit
provided). This API makes it difficult for callers to get rounding wrong.
They just need to use the right constructor and provide a quantity in units
of nanoseconds.
For lower level needs, one can use RoundMode::round_by_duration directly.
This doesn’t know about any units other than nanoseconds. It’s useful in
contexts when dealing with variable length units and there are no rules
for what is a valid increment.
Fields§
§unit: Unit§value: i32Implementations§
Source§impl Increment
impl Increment
Sourcepub(crate) fn for_span(unit: Unit, value: i64) -> Result<Increment, Error>
pub(crate) fn for_span(unit: Unit, value: i64) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a Span.
Sourcepub(crate) fn for_signed_duration(
unit: Unit,
value: i64,
) -> Result<Increment, Error>
pub(crate) fn for_signed_duration( unit: Unit, value: i64, ) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a
SignedDuration.
Sourcepub(crate) fn for_offset(unit: Unit, value: i64) -> Result<Increment, Error>
pub(crate) fn for_offset(unit: Unit, value: i64) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a
tz::Offset.
Sourcepub(crate) fn for_datetime(unit: Unit, value: i64) -> Result<Increment, Error>
pub(crate) fn for_datetime(unit: Unit, value: i64) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a
civil::DateTime or a Zoned.
Sourcepub(crate) fn for_time(unit: Unit, value: i64) -> Result<Increment, Error>
pub(crate) fn for_time(unit: Unit, value: i64) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a
civil::Time.
Sourcepub(crate) fn for_timestamp(unit: Unit, value: i64) -> Result<Increment, Error>
pub(crate) fn for_timestamp(unit: Unit, value: i64) -> Result<Increment, Error>
Return a rounding increment suitable for use when rounding a
civil::Timestamp.
Sourcepub(crate) fn round(
&self,
mode: RoundMode,
quantity: SignedDuration,
) -> Result<SignedDuration, Error>
pub(crate) fn round( &self, mode: RoundMode, quantity: SignedDuration, ) -> Result<SignedDuration, Error>
Rounds the given quantity to the nearest increment value (in terms of the units given to this increment’s constructor). Rounding down or up is determined by the mode given.
Source§impl Increment
Lower level constructors that require the caller to know the precise
limits or maximums. These should generally stay unexported.
impl Increment
Lower level constructors that require the caller to know the precise limits or maximums. These should generally stay unexported.
Sourcefn for_limits(
unit: Unit,
value: i64,
limits: &[MustDivide],
) -> Result<Increment, Error>
fn for_limits( unit: Unit, value: i64, limits: &[MustDivide], ) -> Result<Increment, Error>
Validate the increment value for the given unit and limits.
Specifically, this ensures that rounding to the given unit is
supported and that the specific value is less than to the
corresponding limit.
Sourcefn for_maximums(
unit: Unit,
value: i64,
maximums: &[MustDivide],
) -> Result<Increment, Error>
fn for_maximums( unit: Unit, value: i64, maximums: &[MustDivide], ) -> Result<Increment, Error>
Validate the increment value for the given unit and maximums.
Specifically, this ensures that rounding to the given unit is
supported and that the specific value is less than or equal to the
corresponding maximum.