jiff/zoned.rs
1use core::time::Duration as UnsignedDuration;
2
3use crate::{
4 civil::{
5 Date, DateTime, DateTimeRound, DateTimeWith, Era, ISOWeekDate, Time,
6 Weekday,
7 },
8 duration::{Duration, SDuration},
9 error::{zoned::Error as E, Error, ErrorContext},
10 fmt::{
11 self,
12 temporal::{self, DEFAULT_DATETIME_PARSER},
13 },
14 tz::{AmbiguousOffset, Disambiguation, Offset, OffsetConflict, TimeZone},
15 util::{b, round::Increment},
16 RoundMode, SignedDuration, Span, SpanRound, Timestamp, Unit,
17};
18
19/// A time zone aware instant in time.
20///
21/// A `Zoned` value can be thought of as the combination of following types,
22/// all rolled into one:
23///
24/// * A [`Timestamp`] for indicating the precise instant in time.
25/// * A [`DateTime`] for indicating the "civil" calendar date and clock time.
26/// * A [`TimeZone`] for indicating how to apply time zone transitions while
27/// performing arithmetic.
28///
29/// In particular, a `Zoned` is specifically designed for dealing with
30/// datetimes in a time zone aware manner. Here are some highlights:
31///
32/// * Arithmetic automatically adjusts for daylight saving time (DST), using
33/// the rules defined by [RFC 5545].
34/// * Creating new `Zoned` values from other `Zoned` values via [`Zoned::with`]
35/// by changing clock time (e.g., `02:30`) can do so without worrying that the
36/// time will be invalid due to DST transitions.
37/// * An approximate superset of the [`DateTime`] API is offered on `Zoned`,
38/// but where each of its operations take time zone into account when
39/// appropriate. For example, [`DateTime::start_of_day`] always returns a
40/// datetime set to midnight, but [`Zoned::start_of_day`] returns the first
41/// instant of a day, which might not be midnight if there is a time zone
42/// transition at midnight.
43/// * When using a `Zoned`, it is easy to switch between civil datetime (the
44/// day you see on the calendar and the time you see on the clock) and Unix
45/// time (a precise instant in time). Indeed, a `Zoned` can be losslessy
46/// converted to any other datetime type in this crate: [`Timestamp`],
47/// [`DateTime`], [`Date`] and [`Time`].
48/// * A `Zoned` value can be losslessly serialized and deserialized, via
49/// [serde], by adhering to [RFC 8536]. An example of a serialized zoned
50/// datetime is `2024-07-04T08:39:00-04:00[America/New_York]`.
51/// * Since a `Zoned` stores a [`TimeZone`] itself, multiple time zone aware
52/// operations can be chained together without repeatedly specifying the time
53/// zone.
54///
55/// [RFC 5545]: https://datatracker.ietf.org/doc/html/rfc5545
56/// [RFC 8536]: https://datatracker.ietf.org/doc/html/rfc8536
57/// [serde]: https://serde.rs/
58///
59/// # Parsing and printing
60///
61/// The `Zoned` type provides convenient trait implementations of
62/// [`std::str::FromStr`] and [`std::fmt::Display`]:
63///
64/// ```
65/// use jiff::Zoned;
66///
67/// let zdt: Zoned = "2024-06-19 15:22[America/New_York]".parse()?;
68/// // Notice that the second component and the offset have both been added.
69/// assert_eq!(zdt.to_string(), "2024-06-19T15:22:00-04:00[America/New_York]");
70///
71/// // While in the above case the datetime is unambiguous, in some cases, it
72/// // can be ambiguous. In these cases, an offset is required to correctly
73/// // roundtrip a zoned datetime. For example, on 2024-11-03 in New York, the
74/// // 1 o'clock hour was repeated twice, corresponding to the end of daylight
75/// // saving time.
76/// //
77/// // So because of the ambiguity, this time could be in offset -04 (the first
78/// // time 1 o'clock is on the clock) or it could be -05 (the second time
79/// // 1 o'clock is on the clock, corresponding to the end of DST).
80/// //
81/// // By default, parsing uses a "compatible" strategy for resolving all cases
82/// // of ambiguity: in forward transitions (gaps), the later time is selected.
83/// // And in backward transitions (folds), the earlier time is selected.
84/// let zdt: Zoned = "2024-11-03 01:30[America/New_York]".parse()?;
85/// // As we can see, since this was a fold, the earlier time was selected
86/// // because the -04 offset is the first time 1 o'clock appears on the clock.
87/// assert_eq!(zdt.to_string(), "2024-11-03T01:30:00-04:00[America/New_York]");
88/// // But if we changed the offset and re-serialized, the only thing that
89/// // changes is, indeed, the offset. This demonstrates that the offset is
90/// // key to ensuring lossless serialization.
91/// let zdt = zdt.with().offset(jiff::tz::offset(-5)).build()?;
92/// assert_eq!(zdt.to_string(), "2024-11-03T01:30:00-05:00[America/New_York]");
93///
94/// # Ok::<(), Box<dyn std::error::Error>>(())
95/// ```
96///
97/// A `Zoned` can also be parsed from just a time zone aware date (but the
98/// time zone annotation is still required). In this case, the time is set to
99/// midnight:
100///
101/// ```
102/// use jiff::Zoned;
103///
104/// let zdt: Zoned = "2024-06-19[America/New_York]".parse()?;
105/// assert_eq!(zdt.to_string(), "2024-06-19T00:00:00-04:00[America/New_York]");
106/// // ... although it isn't always midnight, in the case of a time zone
107/// // transition at midnight!
108/// let zdt: Zoned = "2015-10-18[America/Sao_Paulo]".parse()?;
109/// assert_eq!(zdt.to_string(), "2015-10-18T01:00:00-02:00[America/Sao_Paulo]");
110///
111/// # Ok::<(), Box<dyn std::error::Error>>(())
112/// ```
113///
114/// For more information on the specific format supported, see the
115/// [`fmt::temporal`](crate::fmt::temporal) module documentation.
116///
117/// # Leap seconds
118///
119/// Jiff does not support leap seconds. Jiff behaves as if they don't exist.
120/// The only exception is that if one parses a datetime with a second component
121/// of `60`, then it is automatically constrained to `59`:
122///
123/// ```
124/// use jiff::{civil::date, Zoned};
125///
126/// let zdt: Zoned = "2016-12-31 23:59:60[Australia/Tasmania]".parse()?;
127/// assert_eq!(zdt.datetime(), date(2016, 12, 31).at(23, 59, 59, 0));
128///
129/// # Ok::<(), Box<dyn std::error::Error>>(())
130/// ```
131///
132/// # Comparisons
133///
134/// The `Zoned` type provides both `Eq` and `Ord` trait implementations to
135/// facilitate easy comparisons. When a zoned datetime `zdt1` occurs before a
136/// zoned datetime `zdt2`, then `zdt1 < zdt2`. For example:
137///
138/// ```
139/// use jiff::civil::date;
140///
141/// let zdt1 = date(2024, 3, 11).at(1, 25, 15, 0).in_tz("America/New_York")?;
142/// let zdt2 = date(2025, 1, 31).at(0, 30, 0, 0).in_tz("America/New_York")?;
143/// assert!(zdt1 < zdt2);
144///
145/// # Ok::<(), Box<dyn std::error::Error>>(())
146/// ```
147///
148/// Note that `Zoned` comparisons only consider the precise instant in time.
149/// The civil datetime or even the time zone are completely ignored. So it's
150/// possible for a zoned datetime to be less than another even if it's civil
151/// datetime is bigger:
152///
153/// ```
154/// use jiff::civil::date;
155///
156/// let zdt1 = date(2024, 7, 4).at(12, 0, 0, 0).in_tz("America/New_York")?;
157/// let zdt2 = date(2024, 7, 4).at(11, 0, 0, 0).in_tz("America/Los_Angeles")?;
158/// assert!(zdt1 < zdt2);
159/// // But if we only compare civil datetime, the result is flipped:
160/// assert!(zdt1.datetime() > zdt2.datetime());
161///
162/// # Ok::<(), Box<dyn std::error::Error>>(())
163/// ```
164///
165/// The same applies for equality as well. Two `Zoned` values are equal, even
166/// if they have different time zones, when the instant in time is identical:
167///
168/// ```
169/// use jiff::civil::date;
170///
171/// let zdt1 = date(2024, 7, 4).at(12, 0, 0, 0).in_tz("America/New_York")?;
172/// let zdt2 = date(2024, 7, 4).at(9, 0, 0, 0).in_tz("America/Los_Angeles")?;
173/// assert_eq!(zdt1, zdt2);
174///
175/// # Ok::<(), Box<dyn std::error::Error>>(())
176/// ```
177///
178/// (Note that this is different from
179/// [Temporal's `ZonedDateTime.equals`][temporal-equals] comparison, which will
180/// take time zone into account for equality. This is because `Eq` and `Ord`
181/// trait implementations must be consistent in Rust. If you need Temporal's
182/// behavior, then use `zdt1 == zdt2 && zdt1.time_zone() == zdt2.time_zone()`.)
183///
184/// [temporal-equals]: https://tc39.es/proposal-temporal/docs/zoneddatetime.html#equals
185///
186/// # Arithmetic
187///
188/// This type provides routines for adding and subtracting spans of time, as
189/// well as computing the span of time between two `Zoned` values. These
190/// operations take time zones into account.
191///
192/// For adding or subtracting spans of time, one can use any of the following
193/// routines:
194///
195/// * [`Zoned::checked_add`] or [`Zoned::checked_sub`] for checked
196/// arithmetic.
197/// * [`Zoned::saturating_add`] or [`Zoned::saturating_sub`] for
198/// saturating arithmetic.
199///
200/// Additionally, checked arithmetic is available via the `Add` and `Sub`
201/// trait implementations. When the result overflows, a panic occurs.
202///
203/// ```
204/// use jiff::{civil::date, ToSpan};
205///
206/// let start = date(2024, 2, 25).at(15, 45, 0, 0).in_tz("America/New_York")?;
207/// // `Zoned` doesn't implement `Copy`, so you'll want to use `&start` instead
208/// // of `start` if you want to keep using it after arithmetic.
209/// let one_week_later = start + 1.weeks();
210/// assert_eq!(one_week_later.datetime(), date(2024, 3, 3).at(15, 45, 0, 0));
211///
212/// # Ok::<(), Box<dyn std::error::Error>>(())
213/// ```
214///
215/// One can compute the span of time between two zoned datetimes using either
216/// [`Zoned::until`] or [`Zoned::since`]. It's also possible to subtract
217/// two `Zoned` values directly via a `Sub` trait implementation:
218///
219/// ```
220/// use jiff::{civil::date, ToSpan};
221///
222/// let zdt1 = date(2024, 5, 3).at(23, 30, 0, 0).in_tz("America/New_York")?;
223/// let zdt2 = date(2024, 2, 25).at(7, 0, 0, 0).in_tz("America/New_York")?;
224/// assert_eq!(zdt1 - zdt2, 1647.hours().minutes(30).fieldwise());
225///
226/// # Ok::<(), Box<dyn std::error::Error>>(())
227/// ```
228///
229/// The `until` and `since` APIs are polymorphic and allow re-balancing and
230/// rounding the span returned. For example, the default largest unit is hours
231/// (as exemplified above), but we can ask for bigger units:
232///
233/// ```
234/// use jiff::{civil::date, ToSpan, Unit};
235///
236/// let zdt1 = date(2024, 5, 3).at(23, 30, 0, 0).in_tz("America/New_York")?;
237/// let zdt2 = date(2024, 2, 25).at(7, 0, 0, 0).in_tz("America/New_York")?;
238/// assert_eq!(
239/// zdt1.since((Unit::Year, &zdt2))?,
240/// 2.months().days(7).hours(16).minutes(30).fieldwise(),
241/// );
242///
243/// # Ok::<(), Box<dyn std::error::Error>>(())
244/// ```
245///
246/// Or even round the span returned:
247///
248/// ```
249/// use jiff::{civil::date, RoundMode, ToSpan, Unit, ZonedDifference};
250///
251/// let zdt1 = date(2024, 5, 3).at(23, 30, 0, 0).in_tz("America/New_York")?;
252/// let zdt2 = date(2024, 2, 25).at(7, 0, 0, 0).in_tz("America/New_York")?;
253/// assert_eq!(
254/// zdt1.since(
255/// ZonedDifference::new(&zdt2)
256/// .smallest(Unit::Day)
257/// .largest(Unit::Year),
258/// )?,
259/// 2.months().days(7).fieldwise(),
260/// );
261/// // `ZonedDifference` uses truncation as a rounding mode by default,
262/// // but you can set the rounding mode to break ties away from zero:
263/// assert_eq!(
264/// zdt1.since(
265/// ZonedDifference::new(&zdt2)
266/// .smallest(Unit::Day)
267/// .largest(Unit::Year)
268/// .mode(RoundMode::HalfExpand),
269/// )?,
270/// // Rounds up to 8 days.
271/// 2.months().days(8).fieldwise(),
272/// );
273///
274/// # Ok::<(), Box<dyn std::error::Error>>(())
275/// ```
276///
277/// # Rounding
278///
279/// A `Zoned` can be rounded based on a [`ZonedRound`] configuration of
280/// smallest units, rounding increment and rounding mode. Here's an example
281/// showing how to round to the nearest third hour:
282///
283/// ```
284/// use jiff::{civil::date, Unit, ZonedRound};
285///
286/// let zdt = date(2024, 6, 19)
287/// .at(16, 27, 29, 999_999_999)
288/// .in_tz("America/New_York")?;
289/// assert_eq!(
290/// zdt.round(ZonedRound::new().smallest(Unit::Hour).increment(3))?,
291/// date(2024, 6, 19).at(15, 0, 0, 0).in_tz("America/New_York")?,
292/// );
293/// // Or alternatively, make use of the `From<(Unit, i64)> for ZonedRound`
294/// // trait implementation:
295/// assert_eq!(
296/// zdt.round((Unit::Hour, 3))?,
297/// date(2024, 6, 19).at(15, 0, 0, 0).in_tz("America/New_York")?,
298/// );
299///
300/// # Ok::<(), Box<dyn std::error::Error>>(())
301/// ```
302///
303/// See [`Zoned::round`] for more details.
304#[derive(Clone)]
305pub struct Zoned {
306 inner: ZonedInner,
307}
308
309/// The representation of a `Zoned`.
310///
311/// This uses 4 different things: a timestamp, a datetime, an offset and a
312/// time zone. This in turn makes `Zoned` a bit beefy (40 bytes on x86-64),
313/// but I think this is probably the right trade off. (At time of writing,
314/// 2024-07-04.)
315///
316/// Technically speaking, the only essential fields here are timestamp and time
317/// zone. The datetime and offset can both be unambiguously _computed_ from the
318/// combination of a timestamp and a time zone. Indeed, just the timestamp and
319/// the time zone was my initial representation. But as I developed the API of
320/// this type, it became clearer that we should probably store the datetime and
321/// offset as well.
322///
323/// The main issue here is that in order to compute the datetime from a
324/// timestamp and a time zone, you need to do two things:
325///
326/// 1. First, compute the offset. This means doing a binary search on the TZif
327/// data for the transition (or closest transition) matching the timestamp.
328/// 2. Second, use the offset (from UTC) to convert the timestamp into a civil
329/// datetime. This involves a "Unix time to Unix epoch days" conversion that
330/// requires some heavy arithmetic.
331///
332/// So if we don't store the datetime or offset, then we need to compute them
333/// any time we need them. And the Temporal design really pushes heavily in
334/// favor of treating the "instant in time" and "civil datetime" as two sides
335/// to the same coin. That means users are very encouraged to just use whatever
336/// they need. So if we are always computing the offset and datetime whenever
337/// we need them, we're potentially punishing users for working with civil
338/// datetimes. It just doesn't feel like the right trade-off.
339///
340/// Instead, my idea here is that, ultimately, `Zoned` is meant to provide
341/// a one-stop shop for "doing the right thing." Presenting that unified
342/// abstraction comes with costs. And that if we want to expose cheaper ways
343/// of performing at least some of the operations on `Zoned` by making fewer
344/// assumptions, then we should probably endeavor to do that by exposing a
345/// lower level API. I'm not sure what that would look like, so I think it
346/// should be driven by use cases.
347///
348/// Some other things I considered:
349///
350/// * Use `Zoned(Arc<ZonedInner>)` to make `Zoned` pointer-sized. But I didn't
351/// like this because it implies creating any new `Zoned` value requires an
352/// allocation. Since a `TimeZone` internally uses an `Arc`, all it requires
353/// today is a chunky memcpy and an atomic ref count increment.
354/// * Use `OnceLock` shenanigans for the datetime and offset fields. This would
355/// make `Zoned` even beefier and I wasn't totally clear how much this would
356/// save us. And it would impose some (probably small) cost on every datetime
357/// or offset access.
358/// * Use a radically different design that permits a `Zoned` to be `Copy`.
359/// I personally find it deeply annoying that `Zoned` is both the "main"
360/// datetime type in Jiff and also the only one that doesn't implement `Copy`.
361/// I explored some designs, but I couldn't figure out how to make it work in
362/// a satisfying way. The main issue here is `TimeZone`. A `TimeZone` is a huge
363/// chunk of data and the ergonomics of the `Zoned` API require being able to
364/// access a `TimeZone` without the caller providing it explicitly. So to me,
365/// the only real alternative here is to use some kind of integer handle into
366/// a global time zone database. But now you all of a sudden need to worry
367/// about synchronization for every time zone access and plausibly also garbage
368/// collection. And this also complicates matters for using custom time zone
369/// databases. So I ultimately came down on "Zoned is not Copy" as the least
370/// awful choice. *heavy sigh*
371#[derive(Clone)]
372struct ZonedInner {
373 timestamp: Timestamp,
374 datetime: DateTime,
375 offset: Offset,
376 time_zone: TimeZone,
377}
378
379impl Zoned {
380 /// The default `Zoned` value.
381 ///
382 /// This is pre-computed as a constant instead of just doing
383 /// `Zoned::new(Timestamp::default(), TimeZone::UTC)`. I had
384 /// thought the compiler wouldn't be able to optimized away that
385 /// `Zoned::new` call, but it looks like it did. However, this is a
386 /// more robust way to guarantee that `Zoned::default()` is always
387 /// fast.
388 const DEFAULT: Zoned = Zoned {
389 inner: ZonedInner {
390 timestamp: Timestamp::UNIX_EPOCH,
391 datetime: DateTime::constant(1970, 1, 1, 0, 0, 0, 0),
392 offset: Offset::UTC,
393 time_zone: TimeZone::UTC,
394 },
395 };
396
397 /// Returns the current system time in this system's time zone.
398 ///
399 /// If the system's time zone could not be found, then
400 /// [`TimeZone::unknown`] is used instead. When this happens, a `WARN`
401 /// level log message will be emitted. (To see it, one will need to install
402 /// a logger that is compatible with the `log` crate and enable Jiff's
403 /// `logging` Cargo feature.)
404 ///
405 /// To create a `Zoned` value for the current time in a particular
406 /// time zone other than the system default time zone, use
407 /// `Timestamp::now().to_zoned(time_zone)`. In particular, using
408 /// [`Timestamp::now`] avoids the work required to fetch the system time
409 /// zone if you did `Zoned::now().with_time_zone(time_zone)`.
410 ///
411 /// # Panics
412 ///
413 /// This panics if the system clock is set to a time value outside of the
414 /// range `-009999-01-01T00:00:00Z..=9999-12-31T11:59:59.999999999Z`. The
415 /// justification here is that it is reasonable to expect the system clock
416 /// to be set to a somewhat sane, if imprecise, value.
417 ///
418 /// If you want to get the current Unix time fallibly, use
419 /// [`Zoned::try_from`] with a `std::time::SystemTime` as input.
420 ///
421 /// This may also panic when `SystemTime::now()` itself panics. The most
422 /// common context in which this happens is on the `wasm32-unknown-unknown`
423 /// target. If you're using that target in the context of the web (for
424 /// example, via `wasm-pack`), and you're an application, then you should
425 /// enable Jiff's `js` feature. This will automatically instruct Jiff in
426 /// this very specific circumstance to execute JavaScript code to determine
427 /// the current time from the web browser.
428 ///
429 /// # Example
430 ///
431 /// ```
432 /// use jiff::{Timestamp, Zoned};
433 ///
434 /// assert!(Zoned::now().timestamp() > Timestamp::UNIX_EPOCH);
435 /// ```
436 #[cfg(feature = "std")]
437 #[inline]
438 pub fn now() -> Zoned {
439 Zoned::try_from(crate::now::system_time())
440 .expect("system time is valid")
441 }
442
443 /// Creates a new `Zoned` value from a specific instant in a particular
444 /// time zone. The time zone determines how to render the instant in time
445 /// into civil time. (Also known as "clock," "wall," "local" or "naive"
446 /// time.)
447 ///
448 /// To create a new zoned datetime from another with a particular field
449 /// value, use the methods on [`ZonedWith`] via [`Zoned::with`].
450 ///
451 /// # Construction from civil time
452 ///
453 /// A `Zoned` value can also be created from a civil time via the following
454 /// methods:
455 ///
456 /// * [`DateTime::in_tz`] does a Time Zone Database lookup given a time
457 /// zone name string.
458 /// * [`DateTime::to_zoned`] accepts a `TimeZone`.
459 /// * [`Date::in_tz`] does a Time Zone Database lookup given a time zone
460 /// name string and attempts to use midnight as the clock time.
461 /// * [`Date::to_zoned`] accepts a `TimeZone` and attempts to use midnight
462 /// as the clock time.
463 ///
464 /// Whenever one is converting from civil time to a zoned
465 /// datetime, it is possible for the civil time to be ambiguous.
466 /// That is, it might be a clock reading that could refer to
467 /// multiple possible instants in time, or it might be a clock
468 /// reading that never exists. The above routines will use a
469 /// [`Disambiguation::Compatible`]
470 /// strategy to automatically resolve these corner cases.
471 ///
472 /// If one wants to control how ambiguity is resolved (including
473 /// by returning an error), use [`TimeZone::to_ambiguous_zoned`]
474 /// and select the desired strategy via a method on
475 /// [`AmbiguousZoned`](crate::tz::AmbiguousZoned).
476 ///
477 /// # Example: What was the civil time in Tasmania at the Unix epoch?
478 ///
479 /// ```
480 /// use jiff::{tz::TimeZone, Timestamp, Zoned};
481 ///
482 /// let tz = TimeZone::get("Australia/Tasmania")?;
483 /// let zdt = Zoned::new(Timestamp::UNIX_EPOCH, tz);
484 /// assert_eq!(
485 /// zdt.to_string(),
486 /// "1970-01-01T11:00:00+11:00[Australia/Tasmania]",
487 /// );
488 ///
489 /// # Ok::<(), Box<dyn std::error::Error>>(())
490 /// ```
491 ///
492 /// # Example: What was the civil time in New York when World War 1 ended?
493 ///
494 /// ```
495 /// use jiff::civil::date;
496 ///
497 /// let zdt1 = date(1918, 11, 11).at(11, 0, 0, 0).in_tz("Europe/Paris")?;
498 /// let zdt2 = zdt1.in_tz("America/New_York")?;
499 /// assert_eq!(
500 /// zdt2.to_string(),
501 /// "1918-11-11T06:00:00-05:00[America/New_York]",
502 /// );
503 ///
504 /// # Ok::<(), Box<dyn std::error::Error>>(())
505 /// ```
506 #[inline]
507 pub fn new(timestamp: Timestamp, time_zone: TimeZone) -> Zoned {
508 let offset = time_zone.to_offset(timestamp);
509 let datetime = offset.to_datetime(timestamp);
510 let inner = ZonedInner { timestamp, datetime, offset, time_zone };
511 Zoned { inner }
512 }
513
514 /// A crate internal constructor for building a `Zoned` from its
515 /// constituent parts.
516 ///
517 /// This should basically never be exposed, because it can be quite tricky
518 /// to get the parts correct.
519 ///
520 /// See `civil::DateTime::to_zoned` for a use case for this routine. (Why
521 /// do you think? Perf!)
522 #[inline]
523 pub(crate) fn from_parts(
524 timestamp: Timestamp,
525 time_zone: TimeZone,
526 offset: Offset,
527 datetime: DateTime,
528 ) -> Zoned {
529 let inner = ZonedInner { timestamp, datetime, offset, time_zone };
530 Zoned { inner }
531 }
532
533 /// Create a builder for constructing a new `Zoned` from the fields of
534 /// this zoned datetime.
535 ///
536 /// See the methods on [`ZonedWith`] for the different ways one can set
537 /// the fields of a new `Zoned`.
538 ///
539 /// Note that this doesn't support changing the time zone. If you want a
540 /// `Zoned` value of the same instant but in a different time zone, use
541 /// [`Zoned::in_tz`] or [`Zoned::with_time_zone`]. If you want a `Zoned`
542 /// value of the same civil datetime (assuming it isn't ambiguous) but in
543 /// a different time zone, then use [`Zoned::datetime`] followed by
544 /// [`DateTime::in_tz`] or [`DateTime::to_zoned`].
545 ///
546 /// # Example
547 ///
548 /// The builder ensures one can chain together the individual components
549 /// of a zoned datetime without it failing at an intermediate step. For
550 /// example, if you had a date of `2024-10-31T00:00:00[America/New_York]`
551 /// and wanted to change both the day and the month, and each setting was
552 /// validated independent of the other, you would need to be careful to set
553 /// the day first and then the month. In some cases, you would need to set
554 /// the month first and then the day!
555 ///
556 /// But with the builder, you can set values in any order:
557 ///
558 /// ```
559 /// use jiff::civil::date;
560 ///
561 /// let zdt1 = date(2024, 10, 31).at(0, 0, 0, 0).in_tz("America/New_York")?;
562 /// let zdt2 = zdt1.with().month(11).day(30).build()?;
563 /// assert_eq!(
564 /// zdt2,
565 /// date(2024, 11, 30).at(0, 0, 0, 0).in_tz("America/New_York")?,
566 /// );
567 ///
568 /// let zdt1 = date(2024, 4, 30).at(0, 0, 0, 0).in_tz("America/New_York")?;
569 /// let zdt2 = zdt1.with().day(31).month(7).build()?;
570 /// assert_eq!(
571 /// zdt2,
572 /// date(2024, 7, 31).at(0, 0, 0, 0).in_tz("America/New_York")?,
573 /// );
574 ///
575 /// # Ok::<(), Box<dyn std::error::Error>>(())
576 /// ```
577 #[inline]
578 pub fn with(&self) -> ZonedWith {
579 ZonedWith::new(self.clone())
580 }
581
582 /// Return a new zoned datetime with precisely the same instant in a
583 /// different time zone.
584 ///
585 /// The zoned datetime returned is guaranteed to have an equivalent
586 /// [`Timestamp`]. However, its civil [`DateTime`] may be different.
587 ///
588 /// # Example: What was the civil time in New York when World War 1 ended?
589 ///
590 /// ```
591 /// use jiff::{civil::date, tz::TimeZone};
592 ///
593 /// let from = TimeZone::get("Europe/Paris")?;
594 /// let to = TimeZone::get("America/New_York")?;
595 /// let zdt1 = date(1918, 11, 11).at(11, 0, 0, 0).to_zoned(from)?;
596 /// // Switch zdt1 to a different time zone, but keeping the same instant
597 /// // in time. The civil time changes, but not the instant!
598 /// let zdt2 = zdt1.with_time_zone(to);
599 /// assert_eq!(
600 /// zdt2.to_string(),
601 /// "1918-11-11T06:00:00-05:00[America/New_York]",
602 /// );
603 ///
604 /// # Ok::<(), Box<dyn std::error::Error>>(())
605 /// ```
606 #[inline]
607 pub fn with_time_zone(&self, time_zone: TimeZone) -> Zoned {
608 Zoned::new(self.timestamp(), time_zone)
609 }
610
611 /// Return a new zoned datetime with precisely the same instant in a
612 /// different time zone.
613 ///
614 /// The zoned datetime returned is guaranteed to have an equivalent
615 /// [`Timestamp`]. However, its civil [`DateTime`] may be different.
616 ///
617 /// The name given is resolved to a [`TimeZone`] by using the default
618 /// [`TimeZoneDatabase`](crate::tz::TimeZoneDatabase) created by
619 /// [`tz::db`](crate::tz::db). Indeed, this is a convenience function for
620 /// [`DateTime::to_zoned`] where the time zone database lookup is done
621 /// automatically.
622 ///
623 /// # Errors
624 ///
625 /// This returns an error when the given time zone name could not be found
626 /// in the default time zone database.
627 ///
628 /// # Example: What was the civil time in New York when World War 1 ended?
629 ///
630 /// ```
631 /// use jiff::civil::date;
632 ///
633 /// let zdt1 = date(1918, 11, 11).at(11, 0, 0, 0).in_tz("Europe/Paris")?;
634 /// // Switch zdt1 to a different time zone, but keeping the same instant
635 /// // in time. The civil time changes, but not the instant!
636 /// let zdt2 = zdt1.in_tz("America/New_York")?;
637 /// assert_eq!(
638 /// zdt2.to_string(),
639 /// "1918-11-11T06:00:00-05:00[America/New_York]",
640 /// );
641 ///
642 /// # Ok::<(), Box<dyn std::error::Error>>(())
643 /// ```
644 #[inline]
645 pub fn in_tz(&self, name: &str) -> Result<Zoned, Error> {
646 let tz = crate::tz::db().get(name)?;
647 Ok(self.with_time_zone(tz))
648 }
649
650 /// Returns the time zone attached to this [`Zoned`] value.
651 ///
652 /// A time zone is more than just an offset. A time zone is a series of
653 /// rules for determining the civil time for a corresponding instant.
654 /// Indeed, a zoned datetime uses its time zone to perform zone-aware
655 /// arithmetic, rounding and serialization.
656 ///
657 /// # Example
658 ///
659 /// ```
660 /// use jiff::Zoned;
661 ///
662 /// let zdt: Zoned = "2024-07-03 14:31[america/new_york]".parse()?;
663 /// assert_eq!(zdt.time_zone().iana_name(), Some("America/New_York"));
664 ///
665 /// # Ok::<(), Box<dyn std::error::Error>>(())
666 /// ```
667 #[inline]
668 pub fn time_zone(&self) -> &TimeZone {
669 &self.inner.time_zone
670 }
671
672 /// Returns the year for this zoned datetime.
673 ///
674 /// The value returned is guaranteed to be in the range `-9999..=9999`.
675 ///
676 /// # Example
677 ///
678 /// ```
679 /// use jiff::civil::date;
680 ///
681 /// let zdt1 = date(2024, 3, 9).at(7, 30, 0, 0).in_tz("America/New_York")?;
682 /// assert_eq!(zdt1.year(), 2024);
683 ///
684 /// let zdt2 = date(-2024, 3, 9).at(7, 30, 0, 0).in_tz("America/New_York")?;
685 /// assert_eq!(zdt2.year(), -2024);
686 ///
687 /// let zdt3 = date(0, 3, 9).at(7, 30, 0, 0).in_tz("America/New_York")?;
688 /// assert_eq!(zdt3.year(), 0);
689 ///
690 /// # Ok::<(), Box<dyn std::error::Error>>(())
691 /// ```
692 #[inline]
693 pub fn year(&self) -> i16 {
694 self.date().year()
695 }
696
697 /// Returns the year and its era.
698 ///
699 /// This crate specifically allows years to be negative or `0`, where as
700 /// years written for the Gregorian calendar are always positive and
701 /// greater than `0`. In the Gregorian calendar, the era labels `BCE` and
702 /// `CE` are used to disambiguate between years less than or equal to `0`
703 /// and years greater than `0`, respectively.
704 ///
705 /// The crate is designed this way so that years in the latest era (that
706 /// is, `CE`) are aligned with years in this crate.
707 ///
708 /// The year returned is guaranteed to be in the range `1..=10000`.
709 ///
710 /// # Example
711 ///
712 /// ```
713 /// use jiff::civil::{Era, date};
714 ///
715 /// let zdt = date(2024, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
716 /// assert_eq!(zdt.era_year(), (2024, Era::CE));
717 ///
718 /// let zdt = date(1, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
719 /// assert_eq!(zdt.era_year(), (1, Era::CE));
720 ///
721 /// let zdt = date(0, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
722 /// assert_eq!(zdt.era_year(), (1, Era::BCE));
723 ///
724 /// let zdt = date(-1, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
725 /// assert_eq!(zdt.era_year(), (2, Era::BCE));
726 ///
727 /// let zdt = date(-10, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
728 /// assert_eq!(zdt.era_year(), (11, Era::BCE));
729 ///
730 /// let zdt = date(-9_999, 10, 3).at(7, 30, 0, 0).in_tz("America/New_York")?;
731 /// assert_eq!(zdt.era_year(), (10_000, Era::BCE));
732 ///
733 /// # Ok::<(), Box<dyn std::error::Error>>(())
734 /// ```
735 #[inline]
736 pub fn era_year(&self) -> (i16, Era) {
737 self.date().era_year()
738 }
739
740 /// Returns the month for this zoned datetime.
741 ///
742 /// The value returned is guaranteed to be in the range `1..=12`.
743 ///
744 /// # Example
745 ///
746 /// ```
747 /// use jiff::civil::date;
748 ///
749 /// let zdt = date(2024, 3, 9).at(7, 30, 0, 0).in_tz("America/New_York")?;
750 /// assert_eq!(zdt.month(), 3);
751 ///
752 /// # Ok::<(), Box<dyn std::error::Error>>(())
753 /// ```
754 #[inline]
755 pub fn month(&self) -> i8 {
756 self.date().month()
757 }
758
759 /// Returns the day for this zoned datetime.
760 ///
761 /// The value returned is guaranteed to be in the range `1..=31`.
762 ///
763 /// # Example
764 ///
765 /// ```
766 /// use jiff::civil::date;
767 ///
768 /// let zdt = date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?;
769 /// assert_eq!(zdt.day(), 29);
770 ///
771 /// # Ok::<(), Box<dyn std::error::Error>>(())
772 /// ```
773 #[inline]
774 pub fn day(&self) -> i8 {
775 self.date().day()
776 }
777
778 /// Returns the "hour" component of this zoned datetime.
779 ///
780 /// The value returned is guaranteed to be in the range `0..=23`.
781 ///
782 /// # Example
783 ///
784 /// ```
785 /// use jiff::civil::date;
786 ///
787 /// let zdt = date(2000, 1, 2)
788 /// .at(3, 4, 5, 123_456_789)
789 /// .in_tz("America/New_York")?;
790 /// assert_eq!(zdt.hour(), 3);
791 ///
792 /// # Ok::<(), Box<dyn std::error::Error>>(())
793 /// ```
794 #[inline]
795 pub fn hour(&self) -> i8 {
796 self.time().hour()
797 }
798
799 /// Returns the "minute" component of this zoned datetime.
800 ///
801 /// The value returned is guaranteed to be in the range `0..=59`.
802 ///
803 /// # Example
804 ///
805 /// ```
806 /// use jiff::civil::date;
807 ///
808 /// let zdt = date(2000, 1, 2)
809 /// .at(3, 4, 5, 123_456_789)
810 /// .in_tz("America/New_York")?;
811 /// assert_eq!(zdt.minute(), 4);
812 ///
813 /// # Ok::<(), Box<dyn std::error::Error>>(())
814 /// ```
815 #[inline]
816 pub fn minute(&self) -> i8 {
817 self.time().minute()
818 }
819
820 /// Returns the "second" component of this zoned datetime.
821 ///
822 /// The value returned is guaranteed to be in the range `0..=59`.
823 ///
824 /// # Example
825 ///
826 /// ```
827 /// use jiff::civil::date;
828 ///
829 /// let zdt = date(2000, 1, 2)
830 /// .at(3, 4, 5, 123_456_789)
831 /// .in_tz("America/New_York")?;
832 /// assert_eq!(zdt.second(), 5);
833 ///
834 /// # Ok::<(), Box<dyn std::error::Error>>(())
835 /// ```
836 #[inline]
837 pub fn second(&self) -> i8 {
838 self.time().second()
839 }
840
841 /// Returns the "millisecond" component of this zoned datetime.
842 ///
843 /// The value returned is guaranteed to be in the range `0..=999`.
844 ///
845 /// # Example
846 ///
847 /// ```
848 /// use jiff::civil::date;
849 ///
850 /// let zdt = date(2000, 1, 2)
851 /// .at(3, 4, 5, 123_456_789)
852 /// .in_tz("America/New_York")?;
853 /// assert_eq!(zdt.millisecond(), 123);
854 ///
855 /// # Ok::<(), Box<dyn std::error::Error>>(())
856 /// ```
857 #[inline]
858 pub fn millisecond(&self) -> i16 {
859 self.time().millisecond()
860 }
861
862 /// Returns the "microsecond" component of this zoned datetime.
863 ///
864 /// The value returned is guaranteed to be in the range `0..=999`.
865 ///
866 /// # Example
867 ///
868 /// ```
869 /// use jiff::civil::date;
870 ///
871 /// let zdt = date(2000, 1, 2)
872 /// .at(3, 4, 5, 123_456_789)
873 /// .in_tz("America/New_York")?;
874 /// assert_eq!(zdt.microsecond(), 456);
875 ///
876 /// # Ok::<(), Box<dyn std::error::Error>>(())
877 /// ```
878 #[inline]
879 pub fn microsecond(&self) -> i16 {
880 self.time().microsecond()
881 }
882
883 /// Returns the "nanosecond" component of this zoned datetime.
884 ///
885 /// The value returned is guaranteed to be in the range `0..=999`.
886 ///
887 /// # Example
888 ///
889 /// ```
890 /// use jiff::civil::date;
891 ///
892 /// let zdt = date(2000, 1, 2)
893 /// .at(3, 4, 5, 123_456_789)
894 /// .in_tz("America/New_York")?;
895 /// assert_eq!(zdt.nanosecond(), 789);
896 ///
897 /// # Ok::<(), Box<dyn std::error::Error>>(())
898 /// ```
899 #[inline]
900 pub fn nanosecond(&self) -> i16 {
901 self.time().nanosecond()
902 }
903
904 /// Returns the fractional nanosecond for this `Zoned` value.
905 ///
906 /// If you want to set this value on `Zoned`, then use
907 /// [`ZonedWith::subsec_nanosecond`] via [`Zoned::with`].
908 ///
909 /// The value returned is guaranteed to be in the range `0..=999_999_999`.
910 ///
911 /// Note that this returns the fractional second associated with the civil
912 /// time on this `Zoned` value. This is distinct from the fractional
913 /// second on the underlying timestamp. A timestamp, for example, may be
914 /// negative to indicate time before the Unix epoch. But a civil datetime
915 /// can only have a negative year, while the remaining values are all
916 /// semantically positive. See the examples below for how this can manifest
917 /// in practice.
918 ///
919 /// # Example
920 ///
921 /// This shows the relationship between constructing a `Zoned` value
922 /// with routines like `with().millisecond()` and accessing the entire
923 /// fractional part as a nanosecond:
924 ///
925 /// ```
926 /// use jiff::civil::date;
927 ///
928 /// let zdt1 = date(2000, 1, 2)
929 /// .at(3, 4, 5, 123_456_789)
930 /// .in_tz("America/New_York")?;
931 /// assert_eq!(zdt1.subsec_nanosecond(), 123_456_789);
932 ///
933 /// let zdt2 = zdt1.with().millisecond(333).build()?;
934 /// assert_eq!(zdt2.subsec_nanosecond(), 333_456_789);
935 ///
936 /// # Ok::<(), Box<dyn std::error::Error>>(())
937 /// ```
938 ///
939 /// # Example: nanoseconds from a timestamp
940 ///
941 /// This shows how the fractional nanosecond part of a `Zoned` value
942 /// manifests from a specific timestamp.
943 ///
944 /// ```
945 /// use jiff::Timestamp;
946 ///
947 /// // 1,234 nanoseconds after the Unix epoch.
948 /// let zdt = Timestamp::new(0, 1_234)?.in_tz("UTC")?;
949 /// assert_eq!(zdt.subsec_nanosecond(), 1_234);
950 /// // N.B. The timestamp's fractional second and the civil datetime's
951 /// // fractional second happen to be equal here:
952 /// assert_eq!(zdt.timestamp().subsec_nanosecond(), 1_234);
953 ///
954 /// # Ok::<(), Box<dyn std::error::Error>>(())
955 /// ```
956 ///
957 /// # Example: fractional seconds can differ between timestamps and civil time
958 ///
959 /// This shows how a timestamp can have a different fractional second
960 /// value than its corresponding `Zoned` value because of how the sign
961 /// is handled:
962 ///
963 /// ```
964 /// use jiff::{civil, Timestamp};
965 ///
966 /// // 1,234 nanoseconds before the Unix epoch.
967 /// let zdt = Timestamp::new(0, -1_234)?.in_tz("UTC")?;
968 /// // The timestamp's fractional second is what was given:
969 /// assert_eq!(zdt.timestamp().subsec_nanosecond(), -1_234);
970 /// // But the civil datetime's fractional second is equal to
971 /// // `1_000_000_000 - 1_234`. This is because civil datetimes
972 /// // represent times in strictly positive values, like it
973 /// // would read on a clock.
974 /// assert_eq!(zdt.subsec_nanosecond(), 999998766);
975 /// // Looking at the other components of the time value might help.
976 /// assert_eq!(zdt.hour(), 23);
977 /// assert_eq!(zdt.minute(), 59);
978 /// assert_eq!(zdt.second(), 59);
979 ///
980 /// # Ok::<(), Box<dyn std::error::Error>>(())
981 /// ```
982 #[inline]
983 pub fn subsec_nanosecond(&self) -> i32 {
984 self.time().subsec_nanosecond()
985 }
986
987 /// Returns the weekday corresponding to this zoned datetime.
988 ///
989 /// # Example
990 ///
991 /// ```
992 /// use jiff::civil::{Weekday, date};
993 ///
994 /// // The Unix epoch was on a Thursday.
995 /// let zdt = date(1970, 1, 1).at(7, 30, 0, 0).in_tz("America/New_York")?;
996 /// assert_eq!(zdt.weekday(), Weekday::Thursday);
997 /// // One can also get the weekday as an offset in a variety of schemes.
998 /// assert_eq!(zdt.weekday().to_monday_zero_offset(), 3);
999 /// assert_eq!(zdt.weekday().to_monday_one_offset(), 4);
1000 /// assert_eq!(zdt.weekday().to_sunday_zero_offset(), 4);
1001 /// assert_eq!(zdt.weekday().to_sunday_one_offset(), 5);
1002 ///
1003 /// # Ok::<(), Box<dyn std::error::Error>>(())
1004 /// ```
1005 #[inline]
1006 pub fn weekday(&self) -> Weekday {
1007 self.date().weekday()
1008 }
1009
1010 /// Returns the ordinal day of the year that this zoned datetime resides
1011 /// in.
1012 ///
1013 /// For leap years, this always returns a value in the range `1..=366`.
1014 /// Otherwise, the value is in the range `1..=365`.
1015 ///
1016 /// # Example
1017 ///
1018 /// ```
1019 /// use jiff::civil::date;
1020 ///
1021 /// let zdt = date(2006, 8, 24).at(7, 30, 0, 0).in_tz("America/New_York")?;
1022 /// assert_eq!(zdt.day_of_year(), 236);
1023 ///
1024 /// let zdt = date(2023, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?;
1025 /// assert_eq!(zdt.day_of_year(), 365);
1026 ///
1027 /// let zdt = date(2024, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?;
1028 /// assert_eq!(zdt.day_of_year(), 366);
1029 ///
1030 /// # Ok::<(), Box<dyn std::error::Error>>(())
1031 /// ```
1032 #[inline]
1033 pub fn day_of_year(&self) -> i16 {
1034 self.date().day_of_year()
1035 }
1036
1037 /// Returns the ordinal day of the year that this zoned datetime resides
1038 /// in, but ignores leap years.
1039 ///
1040 /// That is, the range of possible values returned by this routine is
1041 /// `1..=365`, even if this date resides in a leap year. If this date is
1042 /// February 29, then this routine returns `None`.
1043 ///
1044 /// The value `365` always corresponds to the last day in the year,
1045 /// December 31, even for leap years.
1046 ///
1047 /// # Example
1048 ///
1049 /// ```
1050 /// use jiff::civil::date;
1051 ///
1052 /// let zdt = date(2006, 8, 24).at(7, 30, 0, 0).in_tz("America/New_York")?;
1053 /// assert_eq!(zdt.day_of_year_no_leap(), Some(236));
1054 ///
1055 /// let zdt = date(2023, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?;
1056 /// assert_eq!(zdt.day_of_year_no_leap(), Some(365));
1057 ///
1058 /// let zdt = date(2024, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?;
1059 /// assert_eq!(zdt.day_of_year_no_leap(), Some(365));
1060 ///
1061 /// let zdt = date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?;
1062 /// assert_eq!(zdt.day_of_year_no_leap(), None);
1063 ///
1064 /// # Ok::<(), Box<dyn std::error::Error>>(())
1065 /// ```
1066 #[inline]
1067 pub fn day_of_year_no_leap(&self) -> Option<i16> {
1068 self.date().day_of_year_no_leap()
1069 }
1070
1071 /// Returns the beginning of the day, corresponding to `00:00:00` civil
1072 /// time, that this datetime resides in.
1073 ///
1074 /// While in nearly all cases the time returned will be `00:00:00`, it is
1075 /// possible for the time to be different from midnight if there is a time
1076 /// zone transition at midnight.
1077 ///
1078 /// # Example
1079 ///
1080 /// ```
1081 /// use jiff::{civil::date, Zoned};
1082 ///
1083 /// let zdt = date(2015, 10, 18).at(12, 0, 0, 0).in_tz("America/New_York")?;
1084 /// assert_eq!(
1085 /// zdt.start_of_day()?.to_string(),
1086 /// "2015-10-18T00:00:00-04:00[America/New_York]",
1087 /// );
1088 ///
1089 /// # Ok::<(), Box<dyn std::error::Error>>(())
1090 /// ```
1091 ///
1092 /// # Example: start of day may not be midnight
1093 ///
1094 /// In some time zones, gap transitions may begin at midnight. This implies
1095 /// that `00:xx:yy` does not exist on a clock in that time zone for that
1096 /// day.
1097 ///
1098 /// ```
1099 /// use jiff::{civil::date, Zoned};
1100 ///
1101 /// let zdt = date(2015, 10, 18).at(12, 0, 0, 0).in_tz("America/Sao_Paulo")?;
1102 /// assert_eq!(
1103 /// zdt.start_of_day()?.to_string(),
1104 /// // not midnight!
1105 /// "2015-10-18T01:00:00-02:00[America/Sao_Paulo]",
1106 /// );
1107 ///
1108 /// # Ok::<(), Box<dyn std::error::Error>>(())
1109 /// ```
1110 ///
1111 /// # Example: error because of overflow
1112 ///
1113 /// In some cases, it's possible for `Zoned` value to be able to represent
1114 /// an instant in time later in the day for a particular time zone, but not
1115 /// earlier in the day. This can only occur near the minimum datetime value
1116 /// supported by Jiff.
1117 ///
1118 /// ```
1119 /// use jiff::{civil::date, tz::{TimeZone, Offset}, Zoned};
1120 ///
1121 /// // While -9999-01-03T04:00:00+25:59:59 is representable as a Zoned
1122 /// // value, the start of the corresponding day is not!
1123 /// let tz = TimeZone::fixed(Offset::MAX);
1124 /// let zdt = date(-9999, 1, 3).at(4, 0, 0, 0).to_zoned(tz.clone())?;
1125 /// assert!(zdt.start_of_day().is_err());
1126 /// // The next day works fine since -9999-01-04T00:00:00+25:59:59 is
1127 /// // representable.
1128 /// let zdt = date(-9999, 1, 4).at(15, 0, 0, 0).to_zoned(tz)?;
1129 /// assert_eq!(
1130 /// zdt.start_of_day()?.datetime(),
1131 /// date(-9999, 1, 4).at(0, 0, 0, 0),
1132 /// );
1133 ///
1134 /// # Ok::<(), Box<dyn std::error::Error>>(())
1135 /// ```
1136 #[inline]
1137 pub fn start_of_day(&self) -> Result<Zoned, Error> {
1138 self.datetime().start_of_day().to_zoned(self.time_zone().clone())
1139 }
1140
1141 /// Returns the end of the day, corresponding to `23:59:59.999999999` civil
1142 /// time, that this datetime resides in.
1143 ///
1144 /// While in nearly all cases the time returned will be
1145 /// `23:59:59.999999999`, it is possible for the time to be different if
1146 /// there is a time zone transition covering that time.
1147 ///
1148 /// # Example
1149 ///
1150 /// ```
1151 /// use jiff::civil::date;
1152 ///
1153 /// let zdt = date(2024, 7, 3)
1154 /// .at(7, 30, 10, 123_456_789)
1155 /// .in_tz("America/New_York")?;
1156 /// assert_eq!(
1157 /// zdt.end_of_day()?,
1158 /// date(2024, 7, 3)
1159 /// .at(23, 59, 59, 999_999_999)
1160 /// .in_tz("America/New_York")?,
1161 /// );
1162 ///
1163 /// # Ok::<(), Box<dyn std::error::Error>>(())
1164 /// ```
1165 ///
1166 /// # Example: error because of overflow
1167 ///
1168 /// In some cases, it's possible for `Zoned` value to be able to represent
1169 /// an instant in time earlier in the day for a particular time zone, but
1170 /// not later in the day. This can only occur near the maximum datetime
1171 /// value supported by Jiff.
1172 ///
1173 /// ```
1174 /// use jiff::{civil::date, tz::{TimeZone, Offset}, Zoned};
1175 ///
1176 /// // While 9999-12-30T01:30-04 is representable as a Zoned
1177 /// // value, the start of the corresponding day is not!
1178 /// let tz = TimeZone::get("America/New_York")?;
1179 /// let zdt = date(9999, 12, 30).at(1, 30, 0, 0).to_zoned(tz.clone())?;
1180 /// assert!(zdt.end_of_day().is_err());
1181 /// // The previous day works fine since 9999-12-29T23:59:59.999999999-04
1182 /// // is representable.
1183 /// let zdt = date(9999, 12, 29).at(1, 30, 0, 0).to_zoned(tz.clone())?;
1184 /// assert_eq!(
1185 /// zdt.end_of_day()?,
1186 /// date(9999, 12, 29)
1187 /// .at(23, 59, 59, 999_999_999)
1188 /// .in_tz("America/New_York")?,
1189 /// );
1190 ///
1191 /// # Ok::<(), Box<dyn std::error::Error>>(())
1192 /// ```
1193 #[inline]
1194 pub fn end_of_day(&self) -> Result<Zoned, Error> {
1195 let end_of_civil_day = self.datetime().end_of_day();
1196 let ambts = self.time_zone().to_ambiguous_timestamp(end_of_civil_day);
1197 // I'm not sure if there are any real world cases where this matters,
1198 // but this is basically the reverse of `compatible`, so we write
1199 // it out ourselves. Basically, if the last civil datetime is in a
1200 // gap, then we want the earlier instant since the later instant must
1201 // necessarily be in the next day. And if the last civil datetime is
1202 // in a fold, then we want the later instant since both the earlier
1203 // and later instants are in the same calendar day and the later one
1204 // must be, well, later. In contrast, compatible mode takes the later
1205 // instant in a gap and the earlier instant in a fold. So we flip that
1206 // here.
1207 let offset = match ambts.offset() {
1208 AmbiguousOffset::Unambiguous { offset } => offset,
1209 AmbiguousOffset::Gap { after, .. } => after,
1210 AmbiguousOffset::Fold { after, .. } => after,
1211 };
1212 offset
1213 .to_timestamp(end_of_civil_day)
1214 .map(|ts| ts.to_zoned(self.time_zone().clone()))
1215 }
1216
1217 /// Returns the first date of the month that this zoned datetime resides
1218 /// in.
1219 ///
1220 /// In most cases, the time in the zoned datetime returned remains
1221 /// unchanged. In some cases, the time may change if the time
1222 /// on the previous date was unambiguous (always true, since a
1223 /// `Zoned` is a precise instant in time) and the same clock time
1224 /// on the returned zoned datetime is ambiguous. In this case, the
1225 /// [`Disambiguation::Compatible`]
1226 /// strategy will be used to turn it into a precise instant. If you want to
1227 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1228 /// to get the civil datetime, then use [`DateTime::first_of_month`],
1229 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1230 /// disambiguation strategy.
1231 ///
1232 /// # Example
1233 ///
1234 /// ```
1235 /// use jiff::civil::date;
1236 ///
1237 /// let zdt = date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?;
1238 /// assert_eq!(
1239 /// zdt.first_of_month()?,
1240 /// date(2024, 2, 1).at(7, 30, 0, 0).in_tz("America/New_York")?,
1241 /// );
1242 ///
1243 /// # Ok::<(), Box<dyn std::error::Error>>(())
1244 /// ```
1245 #[inline]
1246 pub fn first_of_month(&self) -> Result<Zoned, Error> {
1247 self.datetime().first_of_month().to_zoned(self.time_zone().clone())
1248 }
1249
1250 /// Returns the last date of the month that this zoned datetime resides in.
1251 ///
1252 /// In most cases, the time in the zoned datetime returned remains
1253 /// unchanged. In some cases, the time may change if the time
1254 /// on the previous date was unambiguous (always true, since a
1255 /// `Zoned` is a precise instant in time) and the same clock time
1256 /// on the returned zoned datetime is ambiguous. In this case, the
1257 /// [`Disambiguation::Compatible`]
1258 /// strategy will be used to turn it into a precise instant. If you want to
1259 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1260 /// to get the civil datetime, then use [`DateTime::last_of_month`],
1261 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1262 /// disambiguation strategy.
1263 ///
1264 /// # Example
1265 ///
1266 /// ```
1267 /// use jiff::civil::date;
1268 ///
1269 /// let zdt = date(2024, 2, 5).at(7, 30, 0, 0).in_tz("America/New_York")?;
1270 /// assert_eq!(
1271 /// zdt.last_of_month()?,
1272 /// date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?,
1273 /// );
1274 ///
1275 /// # Ok::<(), Box<dyn std::error::Error>>(())
1276 /// ```
1277 #[inline]
1278 pub fn last_of_month(&self) -> Result<Zoned, Error> {
1279 self.datetime().last_of_month().to_zoned(self.time_zone().clone())
1280 }
1281
1282 /// Returns the ordinal number of the last day in the month in which this
1283 /// zoned datetime resides.
1284 ///
1285 /// This is phrased as "the ordinal number of the last day" instead of "the
1286 /// number of days" because some months may be missing days due to time
1287 /// zone transitions. However, this is extraordinarily rare.
1288 ///
1289 /// This is guaranteed to always return one of the following values,
1290 /// depending on the year and the month: 28, 29, 30 or 31.
1291 ///
1292 /// # Example
1293 ///
1294 /// ```
1295 /// use jiff::civil::date;
1296 ///
1297 /// let zdt = date(2024, 2, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1298 /// assert_eq!(zdt.days_in_month(), 29);
1299 ///
1300 /// let zdt = date(2023, 2, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1301 /// assert_eq!(zdt.days_in_month(), 28);
1302 ///
1303 /// let zdt = date(2024, 8, 15).at(7, 30, 0, 0).in_tz("America/New_York")?;
1304 /// assert_eq!(zdt.days_in_month(), 31);
1305 ///
1306 /// # Ok::<(), Box<dyn std::error::Error>>(())
1307 /// ```
1308 ///
1309 /// # Example: count of days in month
1310 ///
1311 /// In `Pacific/Apia`, December 2011 did not have a December 30. Instead,
1312 /// the calendar [skipped from December 29 right to December 31][samoa].
1313 ///
1314 /// If you really do need the count of days in a month in a time zone
1315 /// aware fashion, then it's possible to achieve through arithmetic:
1316 ///
1317 /// ```
1318 /// use jiff::{civil::date, RoundMode, ToSpan, Unit, ZonedDifference};
1319 ///
1320 /// let first_of_month = date(2011, 12, 1).in_tz("Pacific/Apia")?;
1321 /// assert_eq!(first_of_month.days_in_month(), 31);
1322 /// let one_month_later = first_of_month.checked_add(1.month())?;
1323 ///
1324 /// let options = ZonedDifference::new(&one_month_later)
1325 /// .largest(Unit::Hour)
1326 /// .smallest(Unit::Hour)
1327 /// .mode(RoundMode::HalfExpand);
1328 /// let span = first_of_month.until(options)?;
1329 /// let days = ((span.get_hours() as f64) / 24.0).round() as i64;
1330 /// // Try the above in a different time zone, like America/New_York, and
1331 /// // you'll get 31 here.
1332 /// assert_eq!(days, 30);
1333 ///
1334 /// # Ok::<(), Box<dyn std::error::Error>>(())
1335 /// ```
1336 ///
1337 /// [samoa]: https://en.wikipedia.org/wiki/Time_in_Samoa#2011_time_zone_change
1338 #[inline]
1339 pub fn days_in_month(&self) -> i8 {
1340 self.date().days_in_month()
1341 }
1342
1343 /// Returns the first date of the year that this zoned datetime resides in.
1344 ///
1345 /// In most cases, the time in the zoned datetime returned remains
1346 /// unchanged. In some cases, the time may change if the time
1347 /// on the previous date was unambiguous (always true, since a
1348 /// `Zoned` is a precise instant in time) and the same clock time
1349 /// on the returned zoned datetime is ambiguous. In this case, the
1350 /// [`Disambiguation::Compatible`]
1351 /// strategy will be used to turn it into a precise instant. If you want to
1352 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1353 /// to get the civil datetime, then use [`DateTime::first_of_year`],
1354 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1355 /// disambiguation strategy.
1356 ///
1357 /// # Example
1358 ///
1359 /// ```
1360 /// use jiff::civil::date;
1361 ///
1362 /// let zdt = date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?;
1363 /// assert_eq!(
1364 /// zdt.first_of_year()?,
1365 /// date(2024, 1, 1).at(7, 30, 0, 0).in_tz("America/New_York")?,
1366 /// );
1367 ///
1368 /// # Ok::<(), Box<dyn std::error::Error>>(())
1369 /// ```
1370 #[inline]
1371 pub fn first_of_year(&self) -> Result<Zoned, Error> {
1372 self.datetime().first_of_year().to_zoned(self.time_zone().clone())
1373 }
1374
1375 /// Returns the last date of the year that this zoned datetime resides in.
1376 ///
1377 /// In most cases, the time in the zoned datetime returned remains
1378 /// unchanged. In some cases, the time may change if the time
1379 /// on the previous date was unambiguous (always true, since a
1380 /// `Zoned` is a precise instant in time) and the same clock time
1381 /// on the returned zoned datetime is ambiguous. In this case, the
1382 /// [`Disambiguation::Compatible`]
1383 /// strategy will be used to turn it into a precise instant. If you want to
1384 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1385 /// to get the civil datetime, then use [`DateTime::last_of_year`],
1386 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1387 /// disambiguation strategy.
1388 ///
1389 /// # Example
1390 ///
1391 /// ```
1392 /// use jiff::civil::date;
1393 ///
1394 /// let zdt = date(2024, 2, 5).at(7, 30, 0, 0).in_tz("America/New_York")?;
1395 /// assert_eq!(
1396 /// zdt.last_of_year()?,
1397 /// date(2024, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?,
1398 /// );
1399 ///
1400 /// # Ok::<(), Box<dyn std::error::Error>>(())
1401 /// ```
1402 #[inline]
1403 pub fn last_of_year(&self) -> Result<Zoned, Error> {
1404 self.datetime().last_of_year().to_zoned(self.time_zone().clone())
1405 }
1406
1407 /// Returns the ordinal number of the last day in the year in which this
1408 /// zoned datetime resides.
1409 ///
1410 /// This is phrased as "the ordinal number of the last day" instead of "the
1411 /// number of days" because some years may be missing days due to time
1412 /// zone transitions. However, this is extraordinarily rare.
1413 ///
1414 /// This is guaranteed to always return either `365` or `366`.
1415 ///
1416 /// # Example
1417 ///
1418 /// ```
1419 /// use jiff::civil::date;
1420 ///
1421 /// let zdt = date(2024, 7, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1422 /// assert_eq!(zdt.days_in_year(), 366);
1423 ///
1424 /// let zdt = date(2023, 7, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1425 /// assert_eq!(zdt.days_in_year(), 365);
1426 ///
1427 /// # Ok::<(), Box<dyn std::error::Error>>(())
1428 /// ```
1429 #[inline]
1430 pub fn days_in_year(&self) -> i16 {
1431 self.date().days_in_year()
1432 }
1433
1434 /// Returns true if and only if the year in which this zoned datetime
1435 /// resides is a leap year.
1436 ///
1437 /// # Example
1438 ///
1439 /// ```
1440 /// use jiff::civil::date;
1441 ///
1442 /// let zdt = date(2024, 1, 1).at(7, 30, 0, 0).in_tz("America/New_York")?;
1443 /// assert!(zdt.in_leap_year());
1444 ///
1445 /// let zdt = date(2023, 12, 31).at(7, 30, 0, 0).in_tz("America/New_York")?;
1446 /// assert!(!zdt.in_leap_year());
1447 ///
1448 /// # Ok::<(), Box<dyn std::error::Error>>(())
1449 /// ```
1450 #[inline]
1451 pub fn in_leap_year(&self) -> bool {
1452 self.date().in_leap_year()
1453 }
1454
1455 /// Returns the zoned datetime with a date immediately following this one.
1456 ///
1457 /// In most cases, the time in the zoned datetime returned remains
1458 /// unchanged. In some cases, the time may change if the time
1459 /// on the previous date was unambiguous (always true, since a
1460 /// `Zoned` is a precise instant in time) and the same clock time
1461 /// on the returned zoned datetime is ambiguous. In this case, the
1462 /// [`Disambiguation::Compatible`]
1463 /// strategy will be used to turn it into a precise instant. If you want to
1464 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1465 /// to get the civil datetime, then use [`DateTime::tomorrow`],
1466 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1467 /// disambiguation strategy.
1468 ///
1469 /// # Errors
1470 ///
1471 /// This returns an error when one day following this zoned datetime would
1472 /// exceed the maximum `Zoned` value.
1473 ///
1474 /// # Example
1475 ///
1476 /// ```
1477 /// use jiff::{civil::date, Timestamp};
1478 ///
1479 /// let zdt = date(2024, 2, 28).at(7, 30, 0, 0).in_tz("America/New_York")?;
1480 /// assert_eq!(
1481 /// zdt.tomorrow()?,
1482 /// date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?,
1483 /// );
1484 ///
1485 /// // The max doesn't have a tomorrow.
1486 /// assert!(Timestamp::MAX.in_tz("America/New_York")?.tomorrow().is_err());
1487 ///
1488 /// # Ok::<(), Box<dyn std::error::Error>>(())
1489 /// ```
1490 ///
1491 /// # Example: ambiguous datetimes are automatically resolved
1492 ///
1493 /// ```
1494 /// use jiff::{civil::date, Timestamp};
1495 ///
1496 /// let zdt = date(2024, 3, 9).at(2, 30, 0, 0).in_tz("America/New_York")?;
1497 /// assert_eq!(
1498 /// zdt.tomorrow()?,
1499 /// date(2024, 3, 10).at(3, 30, 0, 0).in_tz("America/New_York")?,
1500 /// );
1501 ///
1502 /// # Ok::<(), Box<dyn std::error::Error>>(())
1503 /// ```
1504 #[inline]
1505 pub fn tomorrow(&self) -> Result<Zoned, Error> {
1506 self.datetime().tomorrow()?.to_zoned(self.time_zone().clone())
1507 }
1508
1509 /// Returns the zoned datetime with a date immediately preceding this one.
1510 ///
1511 /// In most cases, the time in the zoned datetime returned remains
1512 /// unchanged. In some cases, the time may change if the time
1513 /// on the previous date was unambiguous (always true, since a
1514 /// `Zoned` is a precise instant in time) and the same clock time
1515 /// on the returned zoned datetime is ambiguous. In this case, the
1516 /// [`Disambiguation::Compatible`]
1517 /// strategy will be used to turn it into a precise instant. If you want to
1518 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1519 /// to get the civil datetime, then use [`DateTime::yesterday`],
1520 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1521 /// disambiguation strategy.
1522 ///
1523 /// # Errors
1524 ///
1525 /// This returns an error when one day preceding this zoned datetime would
1526 /// be less than the minimum `Zoned` value.
1527 ///
1528 /// # Example
1529 ///
1530 /// ```
1531 /// use jiff::{civil::date, Timestamp};
1532 ///
1533 /// let zdt = date(2024, 3, 1).at(7, 30, 0, 0).in_tz("America/New_York")?;
1534 /// assert_eq!(
1535 /// zdt.yesterday()?,
1536 /// date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?,
1537 /// );
1538 ///
1539 /// // The min doesn't have a yesterday.
1540 /// assert!(Timestamp::MIN.in_tz("America/New_York")?.yesterday().is_err());
1541 ///
1542 /// # Ok::<(), Box<dyn std::error::Error>>(())
1543 /// ```
1544 ///
1545 /// # Example: ambiguous datetimes are automatically resolved
1546 ///
1547 /// ```
1548 /// use jiff::{civil::date, Timestamp};
1549 ///
1550 /// let zdt = date(2024, 11, 4).at(1, 30, 0, 0).in_tz("America/New_York")?;
1551 /// assert_eq!(
1552 /// zdt.yesterday()?.to_string(),
1553 /// // Consistent with the "compatible" disambiguation strategy, the
1554 /// // "first" 1 o'clock hour is selected. You can tell this because
1555 /// // the offset is -04, which corresponds to DST time in New York.
1556 /// // The second 1 o'clock hour would have offset -05.
1557 /// "2024-11-03T01:30:00-04:00[America/New_York]",
1558 /// );
1559 ///
1560 /// # Ok::<(), Box<dyn std::error::Error>>(())
1561 /// ```
1562 #[inline]
1563 pub fn yesterday(&self) -> Result<Zoned, Error> {
1564 self.datetime().yesterday()?.to_zoned(self.time_zone().clone())
1565 }
1566
1567 /// Returns the "nth" weekday from the beginning or end of the month in
1568 /// which this zoned datetime resides.
1569 ///
1570 /// The `nth` parameter can be positive or negative. A positive value
1571 /// computes the "nth" weekday from the beginning of the month. A negative
1572 /// value computes the "nth" weekday from the end of the month. So for
1573 /// example, use `-1` to "find the last weekday" in this date's month.
1574 ///
1575 /// In most cases, the time in the zoned datetime returned remains
1576 /// unchanged. In some cases, the time may change if the time
1577 /// on the previous date was unambiguous (always true, since a
1578 /// `Zoned` is a precise instant in time) and the same clock time
1579 /// on the returned zoned datetime is ambiguous. In this case, the
1580 /// [`Disambiguation::Compatible`]
1581 /// strategy will be used to turn it into a precise instant. If you want to
1582 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1583 /// to get the civil datetime, then use [`DateTime::nth_weekday_of_month`],
1584 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1585 /// disambiguation strategy.
1586 ///
1587 /// # Errors
1588 ///
1589 /// This returns an error when `nth` is `0`, or if it is `5` or `-5` and
1590 /// there is no 5th weekday from the beginning or end of the month. This
1591 /// could also return an error if the corresponding datetime could not be
1592 /// represented as an instant for this `Zoned`'s time zone. (This can only
1593 /// happen close the boundaries of an [`Timestamp`].)
1594 ///
1595 /// # Example
1596 ///
1597 /// This shows how to get the nth weekday in a month, starting from the
1598 /// beginning of the month:
1599 ///
1600 /// ```
1601 /// use jiff::civil::{Weekday, date};
1602 ///
1603 /// let zdt = date(2017, 3, 1).at(7, 30, 0, 0).in_tz("America/New_York")?;
1604 /// let second_friday = zdt.nth_weekday_of_month(2, Weekday::Friday)?;
1605 /// assert_eq!(
1606 /// second_friday,
1607 /// date(2017, 3, 10).at(7, 30, 0, 0).in_tz("America/New_York")?,
1608 /// );
1609 ///
1610 /// # Ok::<(), Box<dyn std::error::Error>>(())
1611 /// ```
1612 ///
1613 /// This shows how to do the reverse of the above. That is, the nth _last_
1614 /// weekday in a month:
1615 ///
1616 /// ```
1617 /// use jiff::civil::{Weekday, date};
1618 ///
1619 /// let zdt = date(2024, 3, 1).at(7, 30, 0, 0).in_tz("America/New_York")?;
1620 /// let last_thursday = zdt.nth_weekday_of_month(-1, Weekday::Thursday)?;
1621 /// assert_eq!(
1622 /// last_thursday,
1623 /// date(2024, 3, 28).at(7, 30, 0, 0).in_tz("America/New_York")?,
1624 /// );
1625 ///
1626 /// let second_last_thursday = zdt.nth_weekday_of_month(
1627 /// -2,
1628 /// Weekday::Thursday,
1629 /// )?;
1630 /// assert_eq!(
1631 /// second_last_thursday,
1632 /// date(2024, 3, 21).at(7, 30, 0, 0).in_tz("America/New_York")?,
1633 /// );
1634 ///
1635 /// # Ok::<(), Box<dyn std::error::Error>>(())
1636 /// ```
1637 ///
1638 /// This routine can return an error if there isn't an `nth` weekday
1639 /// for this month. For example, March 2024 only has 4 Mondays:
1640 ///
1641 /// ```
1642 /// use jiff::civil::{Weekday, date};
1643 ///
1644 /// let zdt = date(2024, 3, 25).at(7, 30, 0, 0).in_tz("America/New_York")?;
1645 /// let fourth_monday = zdt.nth_weekday_of_month(4, Weekday::Monday)?;
1646 /// assert_eq!(
1647 /// fourth_monday,
1648 /// date(2024, 3, 25).at(7, 30, 0, 0).in_tz("America/New_York")?,
1649 /// );
1650 /// // There is no 5th Monday.
1651 /// assert!(zdt.nth_weekday_of_month(5, Weekday::Monday).is_err());
1652 /// // Same goes for counting backwards.
1653 /// assert!(zdt.nth_weekday_of_month(-5, Weekday::Monday).is_err());
1654 ///
1655 /// # Ok::<(), Box<dyn std::error::Error>>(())
1656 /// ```
1657 #[inline]
1658 pub fn nth_weekday_of_month(
1659 &self,
1660 nth: i8,
1661 weekday: Weekday,
1662 ) -> Result<Zoned, Error> {
1663 self.datetime()
1664 .nth_weekday_of_month(nth, weekday)?
1665 .to_zoned(self.time_zone().clone())
1666 }
1667
1668 /// Returns the "nth" weekday from this zoned datetime, not including
1669 /// itself.
1670 ///
1671 /// The `nth` parameter can be positive or negative. A positive value
1672 /// computes the "nth" weekday starting at the day after this date and
1673 /// going forwards in time. A negative value computes the "nth" weekday
1674 /// starting at the day before this date and going backwards in time.
1675 ///
1676 /// For example, if this zoned datetime's weekday is a Sunday and the first
1677 /// Sunday is asked for (that is, `zdt.nth_weekday(1, Weekday::Sunday)`),
1678 /// then the result is a week from this zoned datetime corresponding to the
1679 /// following Sunday.
1680 ///
1681 /// In most cases, the time in the zoned datetime returned remains
1682 /// unchanged. In some cases, the time may change if the time
1683 /// on the previous date was unambiguous (always true, since a
1684 /// `Zoned` is a precise instant in time) and the same clock time
1685 /// on the returned zoned datetime is ambiguous. In this case, the
1686 /// [`Disambiguation::Compatible`]
1687 /// strategy will be used to turn it into a precise instant. If you want to
1688 /// use a different disambiguation strategy, then use [`Zoned::datetime`]
1689 /// to get the civil datetime, then use [`DateTime::nth_weekday`],
1690 /// then use [`TimeZone::to_ambiguous_zoned`] and apply your preferred
1691 /// disambiguation strategy.
1692 ///
1693 /// # Errors
1694 ///
1695 /// This returns an error when `nth` is `0`, or if it would otherwise
1696 /// result in a date that overflows the minimum/maximum values of
1697 /// `Zoned`.
1698 ///
1699 /// # Example
1700 ///
1701 /// This example shows how to find the "nth" weekday going forwards in
1702 /// time:
1703 ///
1704 /// ```
1705 /// use jiff::civil::{Weekday, date};
1706 ///
1707 /// // Use a Sunday in March as our start date.
1708 /// let zdt = date(2024, 3, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1709 /// assert_eq!(zdt.weekday(), Weekday::Sunday);
1710 ///
1711 /// // The first next Monday is tomorrow!
1712 /// let next_monday = zdt.nth_weekday(1, Weekday::Monday)?;
1713 /// assert_eq!(
1714 /// next_monday,
1715 /// date(2024, 3, 11).at(7, 30, 0, 0).in_tz("America/New_York")?,
1716 /// );
1717 ///
1718 /// // But the next Sunday is a week away, because this doesn't
1719 /// // include the current weekday.
1720 /// let next_sunday = zdt.nth_weekday(1, Weekday::Sunday)?;
1721 /// assert_eq!(
1722 /// next_sunday,
1723 /// date(2024, 3, 17).at(7, 30, 0, 0).in_tz("America/New_York")?,
1724 /// );
1725 ///
1726 /// // "not this Thursday, but next Thursday"
1727 /// let next_next_thursday = zdt.nth_weekday(2, Weekday::Thursday)?;
1728 /// assert_eq!(
1729 /// next_next_thursday,
1730 /// date(2024, 3, 21).at(7, 30, 0, 0).in_tz("America/New_York")?,
1731 /// );
1732 ///
1733 /// # Ok::<(), Box<dyn std::error::Error>>(())
1734 /// ```
1735 ///
1736 /// This example shows how to find the "nth" weekday going backwards in
1737 /// time:
1738 ///
1739 /// ```
1740 /// use jiff::civil::{Weekday, date};
1741 ///
1742 /// // Use a Sunday in March as our start date.
1743 /// let zdt = date(2024, 3, 10).at(7, 30, 0, 0).in_tz("America/New_York")?;
1744 /// assert_eq!(zdt.weekday(), Weekday::Sunday);
1745 ///
1746 /// // "last Saturday" was yesterday!
1747 /// let last_saturday = zdt.nth_weekday(-1, Weekday::Saturday)?;
1748 /// assert_eq!(
1749 /// last_saturday,
1750 /// date(2024, 3, 9).at(7, 30, 0, 0).in_tz("America/New_York")?,
1751 /// );
1752 ///
1753 /// // "last Sunday" was a week ago.
1754 /// let last_sunday = zdt.nth_weekday(-1, Weekday::Sunday)?;
1755 /// assert_eq!(
1756 /// last_sunday,
1757 /// date(2024, 3, 3).at(7, 30, 0, 0).in_tz("America/New_York")?,
1758 /// );
1759 ///
1760 /// // "not last Thursday, but the one before"
1761 /// let prev_prev_thursday = zdt.nth_weekday(-2, Weekday::Thursday)?;
1762 /// assert_eq!(
1763 /// prev_prev_thursday,
1764 /// date(2024, 2, 29).at(7, 30, 0, 0).in_tz("America/New_York")?,
1765 /// );
1766 ///
1767 /// # Ok::<(), Box<dyn std::error::Error>>(())
1768 /// ```
1769 ///
1770 /// This example shows that overflow results in an error in either
1771 /// direction:
1772 ///
1773 /// ```
1774 /// use jiff::{civil::Weekday, Timestamp};
1775 ///
1776 /// let zdt = Timestamp::MAX.in_tz("America/New_York")?;
1777 /// assert_eq!(zdt.weekday(), Weekday::Thursday);
1778 /// assert!(zdt.nth_weekday(1, Weekday::Saturday).is_err());
1779 ///
1780 /// let zdt = Timestamp::MIN.in_tz("America/New_York")?;
1781 /// assert_eq!(zdt.weekday(), Weekday::Monday);
1782 /// assert!(zdt.nth_weekday(-1, Weekday::Sunday).is_err());
1783 ///
1784 /// # Ok::<(), Box<dyn std::error::Error>>(())
1785 /// ```
1786 ///
1787 /// # Example: getting the start of the week
1788 ///
1789 /// Given a date, one can use `nth_weekday` to determine the start of the
1790 /// week in which the date resides in. This might vary based on whether
1791 /// the weeks start on Sunday or Monday. This example shows how to handle
1792 /// both.
1793 ///
1794 /// ```
1795 /// use jiff::civil::{Weekday, date};
1796 ///
1797 /// let zdt = date(2024, 3, 15).at(7, 30, 0, 0).in_tz("America/New_York")?;
1798 /// // For weeks starting with Sunday.
1799 /// let start_of_week = zdt.tomorrow()?.nth_weekday(-1, Weekday::Sunday)?;
1800 /// assert_eq!(
1801 /// start_of_week,
1802 /// date(2024, 3, 10).at(7, 30, 0, 0).in_tz("America/New_York")?,
1803 /// );
1804 /// // For weeks starting with Monday.
1805 /// let start_of_week = zdt.tomorrow()?.nth_weekday(-1, Weekday::Monday)?;
1806 /// assert_eq!(
1807 /// start_of_week,
1808 /// date(2024, 3, 11).at(7, 30, 0, 0).in_tz("America/New_York")?,
1809 /// );
1810 ///
1811 /// # Ok::<(), Box<dyn std::error::Error>>(())
1812 /// ```
1813 ///
1814 /// In the above example, we first get the date after the current one
1815 /// because `nth_weekday` does not consider itself when counting. This
1816 /// works as expected even at the boundaries of a week:
1817 ///
1818 /// ```
1819 /// use jiff::civil::{Time, Weekday, date};
1820 ///
1821 /// // The start of the week.
1822 /// let zdt = date(2024, 3, 10).at(0, 0, 0, 0).in_tz("America/New_York")?;
1823 /// let start_of_week = zdt.tomorrow()?.nth_weekday(-1, Weekday::Sunday)?;
1824 /// assert_eq!(
1825 /// start_of_week,
1826 /// date(2024, 3, 10).at(0, 0, 0, 0).in_tz("America/New_York")?,
1827 /// );
1828 /// // The end of the week.
1829 /// let zdt = date(2024, 3, 16)
1830 /// .at(23, 59, 59, 999_999_999)
1831 /// .in_tz("America/New_York")?;
1832 /// let start_of_week = zdt
1833 /// .tomorrow()?
1834 /// .nth_weekday(-1, Weekday::Sunday)?
1835 /// .with().time(Time::midnight()).build()?;
1836 /// assert_eq!(
1837 /// start_of_week,
1838 /// date(2024, 3, 10).at(0, 0, 0, 0).in_tz("America/New_York")?,
1839 /// );
1840 ///
1841 /// # Ok::<(), Box<dyn std::error::Error>>(())
1842 /// ```
1843 #[inline]
1844 pub fn nth_weekday(
1845 &self,
1846 nth: i32,
1847 weekday: Weekday,
1848 ) -> Result<Zoned, Error> {
1849 self.datetime()
1850 .nth_weekday(nth, weekday)?
1851 .to_zoned(self.time_zone().clone())
1852 }
1853
1854 /// Returns the precise instant in time referred to by this zoned datetime.
1855 ///
1856 /// # Example
1857 ///
1858 /// ```
1859 /// use jiff::civil::date;
1860 ///
1861 /// let zdt = date(2024, 3, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1862 /// assert_eq!(zdt.timestamp().as_second(), 1_710_456_300);
1863 ///
1864 /// # Ok::<(), Box<dyn std::error::Error>>(())
1865 /// ```
1866 #[inline]
1867 pub fn timestamp(&self) -> Timestamp {
1868 self.inner.timestamp
1869 }
1870
1871 /// Returns the civil datetime component of this zoned datetime.
1872 ///
1873 /// # Example
1874 ///
1875 /// ```
1876 /// use jiff::civil::date;
1877 ///
1878 /// let zdt = date(2024, 3, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1879 /// assert_eq!(zdt.datetime(), date(2024, 3, 14).at(18, 45, 0, 0));
1880 ///
1881 /// # Ok::<(), Box<dyn std::error::Error>>(())
1882 /// ```
1883 #[inline]
1884 pub fn datetime(&self) -> DateTime {
1885 self.inner.datetime
1886 }
1887
1888 /// Returns the civil date component of this zoned datetime.
1889 ///
1890 /// # Example
1891 ///
1892 /// ```
1893 /// use jiff::civil::date;
1894 ///
1895 /// let zdt = date(2024, 3, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1896 /// assert_eq!(zdt.date(), date(2024, 3, 14));
1897 ///
1898 /// # Ok::<(), Box<dyn std::error::Error>>(())
1899 /// ```
1900 #[inline]
1901 pub fn date(&self) -> Date {
1902 self.datetime().date()
1903 }
1904
1905 /// Returns the civil time component of this zoned datetime.
1906 ///
1907 /// # Example
1908 ///
1909 /// ```
1910 /// use jiff::civil::{date, time};
1911 ///
1912 /// let zdt = date(2024, 3, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1913 /// assert_eq!(zdt.time(), time(18, 45, 0, 0));
1914 ///
1915 /// # Ok::<(), Box<dyn std::error::Error>>(())
1916 /// ```
1917 #[inline]
1918 pub fn time(&self) -> Time {
1919 self.datetime().time()
1920 }
1921
1922 /// Construct a civil [ISO 8601 week date] from this zoned datetime.
1923 ///
1924 /// The [`ISOWeekDate`] type describes itself in more detail, but in
1925 /// brief, the ISO week date calendar system eschews months in favor of
1926 /// weeks.
1927 ///
1928 /// This routine is equivalent to
1929 /// [`ISOWeekDate::from_date(zdt.date())`](ISOWeekDate::from_date).
1930 ///
1931 /// [ISO 8601 week date]: https://en.wikipedia.org/wiki/ISO_week_date
1932 ///
1933 /// # Example
1934 ///
1935 /// This shows a number of examples demonstrating the conversion from a
1936 /// Gregorian date to an ISO 8601 week date:
1937 ///
1938 /// ```
1939 /// use jiff::civil::{Date, Time, Weekday, date};
1940 ///
1941 /// let zdt = date(1995, 1, 1).at(18, 45, 0, 0).in_tz("US/Eastern")?;
1942 /// let weekdate = zdt.iso_week_date();
1943 /// assert_eq!(weekdate.year(), 1994);
1944 /// assert_eq!(weekdate.week(), 52);
1945 /// assert_eq!(weekdate.weekday(), Weekday::Sunday);
1946 ///
1947 /// let zdt = date(1996, 12, 31).at(18, 45, 0, 0).in_tz("US/Eastern")?;
1948 /// let weekdate = zdt.iso_week_date();
1949 /// assert_eq!(weekdate.year(), 1997);
1950 /// assert_eq!(weekdate.week(), 1);
1951 /// assert_eq!(weekdate.weekday(), Weekday::Tuesday);
1952 ///
1953 /// let zdt = date(2019, 12, 30).at(18, 45, 0, 0).in_tz("US/Eastern")?;
1954 /// let weekdate = zdt.iso_week_date();
1955 /// assert_eq!(weekdate.year(), 2020);
1956 /// assert_eq!(weekdate.week(), 1);
1957 /// assert_eq!(weekdate.weekday(), Weekday::Monday);
1958 ///
1959 /// let zdt = date(2024, 3, 9).at(18, 45, 0, 0).in_tz("US/Eastern")?;
1960 /// let weekdate = zdt.iso_week_date();
1961 /// assert_eq!(weekdate.year(), 2024);
1962 /// assert_eq!(weekdate.week(), 10);
1963 /// assert_eq!(weekdate.weekday(), Weekday::Saturday);
1964 ///
1965 /// # Ok::<(), Box<dyn std::error::Error>>(())
1966 /// ```
1967 #[inline]
1968 pub fn iso_week_date(self) -> ISOWeekDate {
1969 self.date().iso_week_date()
1970 }
1971
1972 /// Returns the time zone offset of this zoned datetime.
1973 ///
1974 /// # Example
1975 ///
1976 /// ```
1977 /// use jiff::civil::date;
1978 ///
1979 /// let zdt = date(2024, 2, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1980 /// // -05 because New York is in "standard" time at this point.
1981 /// assert_eq!(zdt.offset(), jiff::tz::offset(-5));
1982 ///
1983 /// let zdt = date(2024, 7, 14).at(18, 45, 0, 0).in_tz("America/New_York")?;
1984 /// // But we get -04 once "summer" or "daylight saving time" starts.
1985 /// assert_eq!(zdt.offset(), jiff::tz::offset(-4));
1986 ///
1987 /// # Ok::<(), Box<dyn std::error::Error>>(())
1988 /// ```
1989 #[inline]
1990 pub fn offset(&self) -> Offset {
1991 self.inner.offset
1992 }
1993
1994 /// Add the given span of time to this zoned datetime. If the sum would
1995 /// overflow the minimum or maximum zoned datetime values, then an error is
1996 /// returned.
1997 ///
1998 /// This operation accepts three different duration types: [`Span`],
1999 /// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
2000 /// `From` trait implementations for the [`ZonedArithmetic`] type.
2001 ///
2002 /// # Properties
2003 ///
2004 /// This routine is _not_ reversible because some additions may
2005 /// be ambiguous. For example, adding `1 month` to the zoned
2006 /// datetime `2024-03-31T00:00:00[America/New_York]` will produce
2007 /// `2024-04-30T00:00:00[America/New_York]` since April has
2008 /// only 30 days in a month. Moreover, subtracting `1 month`
2009 /// from `2024-04-30T00:00:00[America/New_York]` will produce
2010 /// `2024-03-30T00:00:00[America/New_York]`, which is not the date we
2011 /// started with.
2012 ///
2013 /// A similar argument applies for days, since with zoned datetimes,
2014 /// different days can be different lengths.
2015 ///
2016 /// If spans of time are limited to units of hours (or less), then this
2017 /// routine _is_ reversible. This also implies that all operations with a
2018 /// [`SignedDuration`] or a [`std::time::Duration`] are reversible.
2019 ///
2020 /// # Errors
2021 ///
2022 /// If the span added to this zoned datetime would result in a zoned
2023 /// datetime that exceeds the range of a `Zoned`, then this will return an
2024 /// error.
2025 ///
2026 /// # Example
2027 ///
2028 /// This shows a few examples of adding spans of time to various zoned
2029 /// datetimes. We make use of the [`ToSpan`](crate::ToSpan) trait for
2030 /// convenient creation of spans.
2031 ///
2032 /// ```
2033 /// use jiff::{civil::date, ToSpan};
2034 ///
2035 /// let zdt = date(1995, 12, 7)
2036 /// .at(3, 24, 30, 3_500)
2037 /// .in_tz("America/New_York")?;
2038 /// let got = zdt.checked_add(20.years().months(4).nanoseconds(500))?;
2039 /// assert_eq!(
2040 /// got,
2041 /// date(2016, 4, 7).at(3, 24, 30, 4_000).in_tz("America/New_York")?,
2042 /// );
2043 ///
2044 /// let zdt = date(2019, 1, 31).at(15, 30, 0, 0).in_tz("America/New_York")?;
2045 /// let got = zdt.checked_add(1.months())?;
2046 /// assert_eq!(
2047 /// got,
2048 /// date(2019, 2, 28).at(15, 30, 0, 0).in_tz("America/New_York")?,
2049 /// );
2050 ///
2051 /// # Ok::<(), Box<dyn std::error::Error>>(())
2052 /// ```
2053 ///
2054 /// # Example: available via addition operator
2055 ///
2056 /// This routine can be used via the `+` operator. Note though that if it
2057 /// fails, it will result in a panic. Note that we use `&zdt + ...` instead
2058 /// of `zdt + ...` since `Add` is implemented for `&Zoned` and not `Zoned`.
2059 /// This is because `Zoned` is not `Copy`.
2060 ///
2061 /// ```
2062 /// use jiff::{civil::date, ToSpan};
2063 ///
2064 /// let zdt = date(1995, 12, 7)
2065 /// .at(3, 24, 30, 3_500)
2066 /// .in_tz("America/New_York")?;
2067 /// let got = &zdt + 20.years().months(4).nanoseconds(500);
2068 /// assert_eq!(
2069 /// got,
2070 /// date(2016, 4, 7).at(3, 24, 30, 4_000).in_tz("America/New_York")?,
2071 /// );
2072 ///
2073 /// # Ok::<(), Box<dyn std::error::Error>>(())
2074 /// ```
2075 ///
2076 /// # Example: zone aware arithmetic
2077 ///
2078 /// This example demonstrates the difference between "add 1 day" and
2079 /// "add 24 hours." In the former case, 1 day might not correspond to 24
2080 /// hours if there is a time zone transition in the intervening period.
2081 /// However, adding 24 hours always means adding exactly 24 hours.
2082 ///
2083 /// ```
2084 /// use jiff::{civil::date, ToSpan};
2085 ///
2086 /// let zdt = date(2024, 3, 10).at(0, 0, 0, 0).in_tz("America/New_York")?;
2087 ///
2088 /// let one_day_later = zdt.checked_add(1.day())?;
2089 /// assert_eq!(
2090 /// one_day_later.to_string(),
2091 /// "2024-03-11T00:00:00-04:00[America/New_York]",
2092 /// );
2093 ///
2094 /// let twenty_four_hours_later = zdt.checked_add(24.hours())?;
2095 /// assert_eq!(
2096 /// twenty_four_hours_later.to_string(),
2097 /// "2024-03-11T01:00:00-04:00[America/New_York]",
2098 /// );
2099 ///
2100 /// # Ok::<(), Box<dyn std::error::Error>>(())
2101 /// ```
2102 ///
2103 /// # Example: automatic disambiguation
2104 ///
2105 /// This example demonstrates what happens when adding a span
2106 /// of time results in an ambiguous zoned datetime. Zone aware
2107 /// arithmetic uses automatic disambiguation corresponding to the
2108 /// [`Disambiguation::Compatible`]
2109 /// strategy for resolving an ambiguous datetime to a precise instant.
2110 /// For example, in the case below, there is a gap in the clocks for 1
2111 /// hour starting at `2024-03-10 02:00:00` in `America/New_York`. The
2112 /// "compatible" strategy chooses the later time in a gap:.
2113 ///
2114 /// ```
2115 /// use jiff::{civil::date, ToSpan};
2116 ///
2117 /// let zdt = date(2024, 3, 9).at(2, 30, 0, 0).in_tz("America/New_York")?;
2118 /// let one_day_later = zdt.checked_add(1.day())?;
2119 /// assert_eq!(
2120 /// one_day_later.to_string(),
2121 /// "2024-03-10T03:30:00-04:00[America/New_York]",
2122 /// );
2123 ///
2124 /// # Ok::<(), Box<dyn std::error::Error>>(())
2125 /// ```
2126 ///
2127 /// And this example demonstrates the "compatible" strategy when arithmetic
2128 /// results in an ambiguous datetime in a fold. In this case, we make use
2129 /// of the fact that the 1 o'clock hour was repeated on `2024-11-03`.
2130 ///
2131 /// ```
2132 /// use jiff::{civil::date, ToSpan};
2133 ///
2134 /// let zdt = date(2024, 11, 2).at(1, 30, 0, 0).in_tz("America/New_York")?;
2135 /// let one_day_later = zdt.checked_add(1.day())?;
2136 /// assert_eq!(
2137 /// one_day_later.to_string(),
2138 /// // This corresponds to the first iteration of the 1 o'clock hour,
2139 /// // i.e., when DST is still in effect. It's the earlier time.
2140 /// "2024-11-03T01:30:00-04:00[America/New_York]",
2141 /// );
2142 ///
2143 /// # Ok::<(), Box<dyn std::error::Error>>(())
2144 /// ```
2145 ///
2146 /// # Example: negative spans are supported
2147 ///
2148 /// ```
2149 /// use jiff::{civil::date, ToSpan};
2150 ///
2151 /// let zdt = date(2024, 3, 31)
2152 /// .at(19, 5, 59, 999_999_999)
2153 /// .in_tz("America/New_York")?;
2154 /// assert_eq!(
2155 /// zdt.checked_add(-1.months())?,
2156 /// date(2024, 2, 29).
2157 /// at(19, 5, 59, 999_999_999)
2158 /// .in_tz("America/New_York")?,
2159 /// );
2160 ///
2161 /// # Ok::<(), Box<dyn std::error::Error>>(())
2162 /// ```
2163 ///
2164 /// # Example: error on overflow
2165 ///
2166 /// ```
2167 /// use jiff::{civil::date, ToSpan};
2168 ///
2169 /// let zdt = date(2024, 3, 31).at(13, 13, 13, 13).in_tz("America/New_York")?;
2170 /// assert!(zdt.checked_add(9000.years()).is_err());
2171 /// assert!(zdt.checked_add(-19000.years()).is_err());
2172 ///
2173 /// # Ok::<(), Box<dyn std::error::Error>>(())
2174 /// ```
2175 ///
2176 /// # Example: adding absolute durations
2177 ///
2178 /// This shows how to add signed and unsigned absolute durations to a
2179 /// `Zoned`.
2180 ///
2181 /// ```
2182 /// use std::time::Duration;
2183 ///
2184 /// use jiff::{civil::date, SignedDuration};
2185 ///
2186 /// let zdt = date(2024, 2, 29).at(0, 0, 0, 0).in_tz("US/Eastern")?;
2187 ///
2188 /// let dur = SignedDuration::from_hours(25);
2189 /// assert_eq!(
2190 /// zdt.checked_add(dur)?,
2191 /// date(2024, 3, 1).at(1, 0, 0, 0).in_tz("US/Eastern")?,
2192 /// );
2193 /// assert_eq!(
2194 /// zdt.checked_add(-dur)?,
2195 /// date(2024, 2, 27).at(23, 0, 0, 0).in_tz("US/Eastern")?,
2196 /// );
2197 ///
2198 /// let dur = Duration::from_secs(25 * 60 * 60);
2199 /// assert_eq!(
2200 /// zdt.checked_add(dur)?,
2201 /// date(2024, 3, 1).at(1, 0, 0, 0).in_tz("US/Eastern")?,
2202 /// );
2203 /// // One cannot negate an unsigned duration,
2204 /// // but you can subtract it!
2205 /// assert_eq!(
2206 /// zdt.checked_sub(dur)?,
2207 /// date(2024, 2, 27).at(23, 0, 0, 0).in_tz("US/Eastern")?,
2208 /// );
2209 ///
2210 /// # Ok::<(), Box<dyn std::error::Error>>(())
2211 /// ```
2212 #[inline]
2213 pub fn checked_add<A: Into<ZonedArithmetic>>(
2214 &self,
2215 duration: A,
2216 ) -> Result<Zoned, Error> {
2217 self.clone().checked_add_consuming(duration)
2218 }
2219
2220 /// Like `checked_add`, but consumes `self` and thus avoids cloning
2221 /// the `TimeZone`.
2222 ///
2223 /// This is currently only accessible via the `impl Add<...> for Zoned`
2224 /// trait implementation.
2225 #[inline]
2226 fn checked_add_consuming<A: Into<ZonedArithmetic>>(
2227 self,
2228 duration: A,
2229 ) -> Result<Zoned, Error> {
2230 let duration: ZonedArithmetic = duration.into();
2231 duration.checked_add(self)
2232 }
2233
2234 #[inline]
2235 fn checked_add_span(self, span: &Span) -> Result<Zoned, Error> {
2236 let span_calendar = span.only_calendar();
2237 // If our duration only consists of "time" (hours, minutes, etc), then
2238 // we can short-circuit and do timestamp math. This also avoids dealing
2239 // with ambiguity and time zone bullshit.
2240 if span_calendar.is_zero() {
2241 return self
2242 .timestamp()
2243 .checked_add(span)
2244 .map(|ts| ts.to_zoned(self.time_zone().clone()))
2245 .context(E::AddTimestamp);
2246 }
2247 let span_time = span.only_time();
2248 let dt = self
2249 .datetime()
2250 .checked_add(span_calendar)
2251 .context(E::AddDateTime)?;
2252
2253 let tz = self.inner.time_zone;
2254 let mut ts = tz
2255 .to_ambiguous_timestamp(dt)
2256 .compatible()
2257 .context(E::ConvertDateTimeToTimestamp)?;
2258 ts = ts.checked_add(span_time).context(E::AddTimestamp)?;
2259 Ok(ts.to_zoned(tz))
2260 }
2261
2262 #[inline]
2263 fn checked_add_duration(
2264 self,
2265 duration: SignedDuration,
2266 ) -> Result<Zoned, Error> {
2267 self.timestamp()
2268 .checked_add(duration)
2269 .map(|ts| ts.to_zoned(self.inner.time_zone))
2270 }
2271
2272 /// This routine is identical to [`Zoned::checked_add`] with the
2273 /// duration negated.
2274 ///
2275 /// # Errors
2276 ///
2277 /// This has the same error conditions as [`Zoned::checked_add`].
2278 ///
2279 /// # Example
2280 ///
2281 /// This routine can be used via the `-` operator. Note though that if it
2282 /// fails, it will result in a panic. Note that we use `&zdt - ...` instead
2283 /// of `zdt - ...` since `Sub` is implemented for `&Zoned` and not `Zoned`.
2284 /// This is because `Zoned` is not `Copy`.
2285 ///
2286 /// ```
2287 /// use std::time::Duration;
2288 ///
2289 /// use jiff::{civil::date, SignedDuration, ToSpan};
2290 ///
2291 /// let zdt = date(1995, 12, 7)
2292 /// .at(3, 24, 30, 3_500)
2293 /// .in_tz("America/New_York")?;
2294 /// let got = &zdt - 20.years().months(4).nanoseconds(500);
2295 /// assert_eq!(
2296 /// got,
2297 /// date(1975, 8, 7).at(3, 24, 30, 3_000).in_tz("America/New_York")?,
2298 /// );
2299 ///
2300 /// let dur = SignedDuration::new(24 * 60 * 60, 500);
2301 /// assert_eq!(
2302 /// &zdt - dur,
2303 /// date(1995, 12, 6).at(3, 24, 30, 3_000).in_tz("America/New_York")?,
2304 /// );
2305 ///
2306 /// let dur = Duration::new(24 * 60 * 60, 500);
2307 /// assert_eq!(
2308 /// &zdt - dur,
2309 /// date(1995, 12, 6).at(3, 24, 30, 3_000).in_tz("America/New_York")?,
2310 /// );
2311 ///
2312 /// # Ok::<(), Box<dyn std::error::Error>>(())
2313 /// ```
2314 #[inline]
2315 pub fn checked_sub<A: Into<ZonedArithmetic>>(
2316 &self,
2317 duration: A,
2318 ) -> Result<Zoned, Error> {
2319 self.clone().checked_sub_consuming(duration)
2320 }
2321
2322 /// Like `checked_sub`, but consumes `self` and thus avoids cloning
2323 /// the `TimeZone`.
2324 ///
2325 /// This is currently only accessible via the `impl Sub<...> for Zoned`
2326 /// trait implementation.
2327 #[inline]
2328 fn checked_sub_consuming<A: Into<ZonedArithmetic>>(
2329 self,
2330 duration: A,
2331 ) -> Result<Zoned, Error> {
2332 let duration: ZonedArithmetic = duration.into();
2333 duration.checked_neg().and_then(|za| za.checked_add(self))
2334 }
2335
2336 /// This routine is identical to [`Zoned::checked_add`], except the
2337 /// result saturates on overflow. That is, instead of overflow, either
2338 /// [`Timestamp::MIN`] or [`Timestamp::MAX`] (in this `Zoned` value's time
2339 /// zone) is returned.
2340 ///
2341 /// # Properties
2342 ///
2343 /// The properties of this routine are identical to [`Zoned::checked_add`],
2344 /// except that if saturation occurs, then the result is not reversible.
2345 ///
2346 /// # Example
2347 ///
2348 /// ```
2349 /// use jiff::{civil::date, SignedDuration, Timestamp, ToSpan};
2350 ///
2351 /// let zdt = date(2024, 3, 31).at(13, 13, 13, 13).in_tz("America/New_York")?;
2352 /// assert_eq!(Timestamp::MAX, zdt.saturating_add(9000.years()).timestamp());
2353 /// assert_eq!(Timestamp::MIN, zdt.saturating_add(-19000.years()).timestamp());
2354 /// assert_eq!(Timestamp::MAX, zdt.saturating_add(SignedDuration::MAX).timestamp());
2355 /// assert_eq!(Timestamp::MIN, zdt.saturating_add(SignedDuration::MIN).timestamp());
2356 /// assert_eq!(Timestamp::MAX, zdt.saturating_add(std::time::Duration::MAX).timestamp());
2357 ///
2358 /// # Ok::<(), Box<dyn std::error::Error>>(())
2359 /// ```
2360 #[inline]
2361 pub fn saturating_add<A: Into<ZonedArithmetic>>(
2362 &self,
2363 duration: A,
2364 ) -> Zoned {
2365 let duration: ZonedArithmetic = duration.into();
2366 self.checked_add(duration).unwrap_or_else(|_| {
2367 let ts = if duration.is_negative() {
2368 Timestamp::MIN
2369 } else {
2370 Timestamp::MAX
2371 };
2372 ts.to_zoned(self.time_zone().clone())
2373 })
2374 }
2375
2376 /// This routine is identical to [`Zoned::saturating_add`] with the span
2377 /// parameter negated.
2378 ///
2379 /// # Example
2380 ///
2381 /// ```
2382 /// use jiff::{civil::date, SignedDuration, Timestamp, ToSpan};
2383 ///
2384 /// let zdt = date(2024, 3, 31).at(13, 13, 13, 13).in_tz("America/New_York")?;
2385 /// assert_eq!(Timestamp::MIN, zdt.saturating_sub(19000.years()).timestamp());
2386 /// assert_eq!(Timestamp::MAX, zdt.saturating_sub(-9000.years()).timestamp());
2387 /// assert_eq!(Timestamp::MIN, zdt.saturating_sub(SignedDuration::MAX).timestamp());
2388 /// assert_eq!(Timestamp::MAX, zdt.saturating_sub(SignedDuration::MIN).timestamp());
2389 /// assert_eq!(Timestamp::MIN, zdt.saturating_sub(std::time::Duration::MAX).timestamp());
2390 ///
2391 /// # Ok::<(), Box<dyn std::error::Error>>(())
2392 /// ```
2393 #[inline]
2394 pub fn saturating_sub<A: Into<ZonedArithmetic>>(
2395 &self,
2396 duration: A,
2397 ) -> Zoned {
2398 let duration: ZonedArithmetic = duration.into();
2399 let Ok(duration) = duration.checked_neg() else {
2400 return Timestamp::MIN.to_zoned(self.time_zone().clone());
2401 };
2402 self.saturating_add(duration)
2403 }
2404
2405 /// Returns a span representing the elapsed time from this zoned datetime
2406 /// until the given `other` zoned datetime.
2407 ///
2408 /// When `other` occurs before this datetime, then the span returned will
2409 /// be negative.
2410 ///
2411 /// Depending on the input provided, the span returned is rounded. It may
2412 /// also be balanced up to bigger units than the default. By default, the
2413 /// span returned is balanced such that the biggest possible unit is hours.
2414 /// This default is an API guarantee. Users can rely on the default not
2415 /// returning any calendar units in the default configuration.
2416 ///
2417 /// This operation is configured by providing a [`ZonedDifference`]
2418 /// value. Since this routine accepts anything that implements
2419 /// `Into<ZonedDifference>`, once can pass a `&Zoned` directly.
2420 /// One can also pass a `(Unit, &Zoned)`, where `Unit` is treated as
2421 /// [`ZonedDifference::largest`].
2422 ///
2423 /// # Properties
2424 ///
2425 /// It is guaranteed that if the returned span is subtracted from `other`,
2426 /// and if no rounding is requested, and if the largest unit requested
2427 /// is at most `Unit::Hour`, then the original zoned datetime will be
2428 /// returned.
2429 ///
2430 /// This routine is equivalent to `self.since(other).map(|span| -span)`
2431 /// if no rounding options are set. If rounding options are set, then
2432 /// it's equivalent to
2433 /// `self.since(other_without_rounding_options).map(|span| -span)`,
2434 /// followed by a call to [`Span::round`] with the appropriate rounding
2435 /// options set. This is because the negation of a span can result in
2436 /// different rounding results depending on the rounding mode.
2437 ///
2438 /// # Errors
2439 ///
2440 /// An error can occur in the following scenarios:
2441 ///
2442 /// * When the requested configuration would result in a span that is
2443 /// beyond allowable limits. For example, the nanosecond component of a
2444 /// span cannot represent the span of time between the minimum and maximum
2445 /// zoned datetime supported by Jiff. Therefore, if one requests a span
2446 /// with its largest unit set to [`Unit::Nanosecond`], then it's possible
2447 /// for this routine to fail.
2448 /// * When `ZonedDifference` is misconfigured. For example, if the smallest
2449 /// unit provided is bigger than the largest unit.
2450 /// * When units greater than `Unit::Hour` are requested _and_ if the time
2451 /// zones in the provided zoned datetimes are distinct. (See [`TimeZone`]'s
2452 /// section on equality for details on how equality is determined.) This
2453 /// error occurs because the length of a day may vary depending on the time
2454 /// zone. To work around this restriction, convert one or both of the zoned
2455 /// datetimes into the same time zone.
2456 ///
2457 /// It is guaranteed that if one provides a datetime with the default
2458 /// [`ZonedDifference`] configuration, then this routine will never
2459 /// fail.
2460 ///
2461 /// # Example
2462 ///
2463 /// ```
2464 /// use jiff::{civil::date, ToSpan};
2465 ///
2466 /// let earlier = date(2006, 8, 24).at(22, 30, 0, 0).in_tz("America/New_York")?;
2467 /// let later = date(2019, 1, 31).at(21, 0, 0, 0).in_tz("America/New_York")?;
2468 /// assert_eq!(
2469 /// earlier.until(&later)?,
2470 /// 109_031.hours().minutes(30).fieldwise(),
2471 /// );
2472 ///
2473 /// // Flipping the dates is fine, but you'll get a negative span.
2474 /// assert_eq!(
2475 /// later.until(&earlier)?,
2476 /// -109_031.hours().minutes(30).fieldwise(),
2477 /// );
2478 ///
2479 /// # Ok::<(), Box<dyn std::error::Error>>(())
2480 /// ```
2481 ///
2482 /// # Example: using bigger units
2483 ///
2484 /// This example shows how to expand the span returned to bigger units.
2485 /// This makes use of a `From<(Unit, &Zoned)> for ZonedDifference`
2486 /// trait implementation.
2487 ///
2488 /// ```
2489 /// use jiff::{civil::date, Unit, ToSpan};
2490 ///
2491 /// let zdt1 = date(1995, 12, 07).at(3, 24, 30, 3500).in_tz("America/New_York")?;
2492 /// let zdt2 = date(2019, 01, 31).at(15, 30, 0, 0).in_tz("America/New_York")?;
2493 ///
2494 /// // The default limits durations to using "hours" as the biggest unit.
2495 /// let span = zdt1.until(&zdt2)?;
2496 /// assert_eq!(span.to_string(), "PT202956H5M29.9999965S");
2497 ///
2498 /// // But we can ask for units all the way up to years.
2499 /// let span = zdt1.until((Unit::Year, &zdt2))?;
2500 /// assert_eq!(format!("{span:#}"), "23y 1mo 24d 12h 5m 29s 999ms 996µs 500ns");
2501 /// # Ok::<(), Box<dyn std::error::Error>>(())
2502 /// ```
2503 ///
2504 /// # Example: rounding the result
2505 ///
2506 /// This shows how one might find the difference between two zoned
2507 /// datetimes and have the result rounded such that sub-seconds are
2508 /// removed.
2509 ///
2510 /// In this case, we need to hand-construct a [`ZonedDifference`]
2511 /// in order to gain full configurability.
2512 ///
2513 /// ```
2514 /// use jiff::{civil::date, Unit, ToSpan, ZonedDifference};
2515 ///
2516 /// let zdt1 = date(1995, 12, 07).at(3, 24, 30, 3500).in_tz("America/New_York")?;
2517 /// let zdt2 = date(2019, 01, 31).at(15, 30, 0, 0).in_tz("America/New_York")?;
2518 ///
2519 /// let span = zdt1.until(
2520 /// ZonedDifference::from(&zdt2).smallest(Unit::Second),
2521 /// )?;
2522 /// assert_eq!(format!("{span:#}"), "202956h 5m 29s");
2523 ///
2524 /// // We can combine smallest and largest units too!
2525 /// let span = zdt1.until(
2526 /// ZonedDifference::from(&zdt2)
2527 /// .smallest(Unit::Second)
2528 /// .largest(Unit::Year),
2529 /// )?;
2530 /// assert_eq!(span.to_string(), "P23Y1M24DT12H5M29S");
2531 ///
2532 /// # Ok::<(), Box<dyn std::error::Error>>(())
2533 /// ```
2534 ///
2535 /// # Example: units biggers than days inhibit reversibility
2536 ///
2537 /// If you ask for units bigger than hours, then adding the span returned
2538 /// to the `other` zoned datetime is not guaranteed to result in the
2539 /// original zoned datetime. For example:
2540 ///
2541 /// ```
2542 /// use jiff::{civil::date, Unit, ToSpan};
2543 ///
2544 /// let zdt1 = date(2024, 3, 2).at(0, 0, 0, 0).in_tz("America/New_York")?;
2545 /// let zdt2 = date(2024, 5, 1).at(0, 0, 0, 0).in_tz("America/New_York")?;
2546 ///
2547 /// let span = zdt1.until((Unit::Month, &zdt2))?;
2548 /// assert_eq!(span, 1.month().days(29).fieldwise());
2549 /// let maybe_original = zdt2.checked_sub(span)?;
2550 /// // Not the same as the original datetime!
2551 /// assert_eq!(
2552 /// maybe_original,
2553 /// date(2024, 3, 3).at(0, 0, 0, 0).in_tz("America/New_York")?,
2554 /// );
2555 ///
2556 /// // But in the default configuration, hours are always the biggest unit
2557 /// // and reversibility is guaranteed.
2558 /// let span = zdt1.until(&zdt2)?;
2559 /// assert_eq!(span.to_string(), "PT1439H");
2560 /// let is_original = zdt2.checked_sub(span)?;
2561 /// assert_eq!(is_original, zdt1);
2562 ///
2563 /// # Ok::<(), Box<dyn std::error::Error>>(())
2564 /// ```
2565 ///
2566 /// This occurs because spans are added as if by adding the biggest units
2567 /// first, and then the smaller units. Because months vary in length,
2568 /// their meaning can change depending on how the span is added. In this
2569 /// case, adding one month to `2024-03-02` corresponds to 31 days, but
2570 /// subtracting one month from `2024-05-01` corresponds to 30 days.
2571 #[inline]
2572 pub fn until<'a, A: Into<ZonedDifference<'a>>>(
2573 &self,
2574 other: A,
2575 ) -> Result<Span, Error> {
2576 let args: ZonedDifference = other.into();
2577 let span = args.until_with_largest_unit(self)?;
2578 if args.rounding_may_change_span() {
2579 span.round(args.round.relative(self))
2580 } else {
2581 Ok(span)
2582 }
2583 }
2584
2585 /// This routine is identical to [`Zoned::until`], but the order of the
2586 /// parameters is flipped.
2587 ///
2588 /// # Errors
2589 ///
2590 /// This has the same error conditions as [`Zoned::until`].
2591 ///
2592 /// # Example
2593 ///
2594 /// This routine can be used via the `-` operator. Since the default
2595 /// configuration is used and because a `Span` can represent the difference
2596 /// between any two possible zoned datetimes, it will never panic. Note
2597 /// that we use `&zdt1 - &zdt2` instead of `zdt1 - zdt2` since `Sub` is
2598 /// implemented for `&Zoned` and not `Zoned`. This is because `Zoned` is
2599 /// not `Copy`.
2600 ///
2601 /// ```
2602 /// use jiff::{civil::date, ToSpan};
2603 ///
2604 /// let earlier = date(2006, 8, 24).at(22, 30, 0, 0).in_tz("America/New_York")?;
2605 /// let later = date(2019, 1, 31).at(21, 0, 0, 0).in_tz("America/New_York")?;
2606 /// assert_eq!(&later - &earlier, 109_031.hours().minutes(30).fieldwise());
2607 ///
2608 /// # Ok::<(), Box<dyn std::error::Error>>(())
2609 /// ```
2610 #[inline]
2611 pub fn since<'a, A: Into<ZonedDifference<'a>>>(
2612 &self,
2613 other: A,
2614 ) -> Result<Span, Error> {
2615 let args: ZonedDifference = other.into();
2616 let span = -args.until_with_largest_unit(self)?;
2617 if args.rounding_may_change_span() {
2618 span.round(args.round.relative(self))
2619 } else {
2620 Ok(span)
2621 }
2622 }
2623
2624 /// Returns an absolute duration representing the elapsed time from this
2625 /// zoned datetime until the given `other` zoned datetime.
2626 ///
2627 /// When `other` occurs before this zoned datetime, then the duration
2628 /// returned will be negative.
2629 ///
2630 /// Unlike [`Zoned::until`], this always returns a duration
2631 /// corresponding to a 96-bit integer of nanoseconds between two
2632 /// zoned datetimes.
2633 ///
2634 /// # Fallibility
2635 ///
2636 /// This routine never panics or returns an error. Since there are no
2637 /// configuration options that can be incorrectly provided, no error is
2638 /// possible when calling this routine. In contrast, [`Zoned::until`]
2639 /// can return an error in some cases due to misconfiguration. But like
2640 /// this routine, [`Zoned::until`] never panics or returns an error in
2641 /// its default configuration.
2642 ///
2643 /// # When should I use this versus [`Zoned::until`]?
2644 ///
2645 /// See the type documentation for [`SignedDuration`] for the section on
2646 /// when one should use [`Span`] and when one should use `SignedDuration`.
2647 /// In short, use `Span` (and therefore `Timestamp::until`) unless you have
2648 /// a specific reason to do otherwise.
2649 ///
2650 /// # Example
2651 ///
2652 /// ```
2653 /// use jiff::{civil::date, SignedDuration};
2654 ///
2655 /// let earlier = date(2006, 8, 24).at(22, 30, 0, 0).in_tz("US/Eastern")?;
2656 /// let later = date(2019, 1, 31).at(21, 0, 0, 0).in_tz("US/Eastern")?;
2657 /// assert_eq!(
2658 /// earlier.duration_until(&later),
2659 /// SignedDuration::from_hours(109_031) + SignedDuration::from_mins(30),
2660 /// );
2661 ///
2662 /// // Flipping the dates is fine, but you'll get a negative span.
2663 /// assert_eq!(
2664 /// later.duration_until(&earlier),
2665 /// -SignedDuration::from_hours(109_031) + -SignedDuration::from_mins(30),
2666 /// );
2667 ///
2668 /// # Ok::<(), Box<dyn std::error::Error>>(())
2669 /// ```
2670 ///
2671 /// # Example: difference with [`Zoned::until`]
2672 ///
2673 /// The main difference between this routine and `Zoned::until` is that
2674 /// the latter can return units other than a 96-bit integer of nanoseconds.
2675 /// While a 96-bit integer of nanoseconds can be converted into other units
2676 /// like hours, this can only be done for uniform units. (Uniform units are
2677 /// units for which each individual unit always corresponds to the same
2678 /// elapsed time regardless of the datetime it is relative to.) This can't
2679 /// be done for units like years, months or days.
2680 ///
2681 /// ```
2682 /// use jiff::{civil::date, SignedDuration, Span, SpanRound, ToSpan, Unit};
2683 ///
2684 /// let zdt1 = date(2024, 3, 10).at(0, 0, 0, 0).in_tz("US/Eastern")?;
2685 /// let zdt2 = date(2024, 3, 11).at(0, 0, 0, 0).in_tz("US/Eastern")?;
2686 ///
2687 /// let span = zdt1.until((Unit::Day, &zdt2))?;
2688 /// assert_eq!(format!("{span:#}"), "1d");
2689 ///
2690 /// let duration = zdt1.duration_until(&zdt2);
2691 /// // This day was only 23 hours long!
2692 /// assert_eq!(duration, SignedDuration::from_hours(23));
2693 /// // There's no way to extract years, months or days from the signed
2694 /// // duration like one might extract hours (because every hour
2695 /// // is the same length). Instead, you actually have to convert
2696 /// // it to a span and then balance it by providing a relative date!
2697 /// let options = SpanRound::new().largest(Unit::Day).relative(&zdt1);
2698 /// let span = Span::try_from(duration)?.round(options)?;
2699 /// assert_eq!(format!("{span:#}"), "1d");
2700 ///
2701 /// # Ok::<(), Box<dyn std::error::Error>>(())
2702 /// ```
2703 ///
2704 /// # Example: getting an unsigned duration
2705 ///
2706 /// If you're looking to find the duration between two zoned datetimes as
2707 /// a [`std::time::Duration`], you'll need to use this method to get a
2708 /// [`SignedDuration`] and then convert it to a `std::time::Duration`:
2709 ///
2710 /// ```
2711 /// use std::time::Duration;
2712 ///
2713 /// use jiff::civil::date;
2714 ///
2715 /// let zdt1 = date(2024, 7, 1).at(0, 0, 0, 0).in_tz("US/Eastern")?;
2716 /// let zdt2 = date(2024, 8, 1).at(0, 0, 0, 0).in_tz("US/Eastern")?;
2717 /// let duration = Duration::try_from(zdt1.duration_until(&zdt2))?;
2718 /// assert_eq!(duration, Duration::from_secs(31 * 24 * 60 * 60));
2719 ///
2720 /// // Note that unsigned durations cannot represent all
2721 /// // possible differences! If the duration would be negative,
2722 /// // then the conversion fails:
2723 /// assert!(Duration::try_from(zdt2.duration_until(&zdt1)).is_err());
2724 ///
2725 /// # Ok::<(), Box<dyn std::error::Error>>(())
2726 /// ```
2727 #[inline]
2728 pub fn duration_until(&self, other: &Zoned) -> SignedDuration {
2729 SignedDuration::zoned_until(self, other)
2730 }
2731
2732 /// This routine is identical to [`Zoned::duration_until`], but the
2733 /// order of the parameters is flipped.
2734 ///
2735 /// # Example
2736 ///
2737 /// ```
2738 /// use jiff::{civil::date, SignedDuration};
2739 ///
2740 /// let earlier = date(2006, 8, 24).at(22, 30, 0, 0).in_tz("US/Eastern")?;
2741 /// let later = date(2019, 1, 31).at(21, 0, 0, 0).in_tz("US/Eastern")?;
2742 /// assert_eq!(
2743 /// later.duration_since(&earlier),
2744 /// SignedDuration::from_hours(109_031) + SignedDuration::from_mins(30),
2745 /// );
2746 ///
2747 /// # Ok::<(), Box<dyn std::error::Error>>(())
2748 /// ```
2749 #[inline]
2750 pub fn duration_since(&self, other: &Zoned) -> SignedDuration {
2751 SignedDuration::zoned_until(other, self)
2752 }
2753
2754 /// Rounds this zoned datetime according to the [`ZonedRound`]
2755 /// configuration given.
2756 ///
2757 /// The principal option is [`ZonedRound::smallest`], which allows one to
2758 /// configure the smallest units in the returned zoned datetime. Rounding
2759 /// is what determines whether that unit should keep its current value
2760 /// or whether it should be incremented. Moreover, the amount it should
2761 /// be incremented can be configured via [`ZonedRound::increment`].
2762 /// Finally, the rounding strategy itself can be configured via
2763 /// [`ZonedRound::mode`].
2764 ///
2765 /// Note that this routine is generic and accepts anything that
2766 /// implements `Into<ZonedRound>`. Some notable implementations are:
2767 ///
2768 /// * `From<Unit> for ZonedRound`, which will automatically create a
2769 /// `ZonedRound::new().smallest(unit)` from the unit provided.
2770 /// * `From<(Unit, i64)> for ZonedRound`, which will automatically
2771 /// create a `ZonedRound::new().smallest(unit).increment(number)` from
2772 /// the unit and increment provided.
2773 ///
2774 /// # Errors
2775 ///
2776 /// This returns an error if the smallest unit configured on the given
2777 /// [`ZonedRound`] is bigger than days. An error is also returned if
2778 /// the rounding increment is greater than 1 when the units are days.
2779 /// (Currently, rounding to the nearest week, month or year is not
2780 /// supported.)
2781 ///
2782 /// When the smallest unit is less than days, the rounding increment must
2783 /// divide evenly into the next highest unit after the smallest unit
2784 /// configured (and must not be equivalent to it). For example, if the
2785 /// smallest unit is [`Unit::Nanosecond`], then *some* of the valid values
2786 /// for the rounding increment are `1`, `2`, `4`, `5`, `100` and `500`.
2787 /// Namely, any integer that divides evenly into `1,000` nanoseconds since
2788 /// there are `1,000` nanoseconds in the next highest unit (microseconds).
2789 ///
2790 /// This can also return an error in some cases where rounding would
2791 /// require arithmetic that exceeds the maximum zoned datetime value.
2792 ///
2793 /// # Example
2794 ///
2795 /// This is a basic example that demonstrates rounding a zoned datetime
2796 /// to the nearest day. This also demonstrates calling this method with
2797 /// the smallest unit directly, instead of constructing a `ZonedRound`
2798 /// manually.
2799 ///
2800 /// ```
2801 /// use jiff::{civil::date, Unit};
2802 ///
2803 /// // rounds up
2804 /// let zdt = date(2024, 6, 19).at(15, 0, 0, 0).in_tz("America/New_York")?;
2805 /// assert_eq!(
2806 /// zdt.round(Unit::Day)?,
2807 /// date(2024, 6, 20).at(0, 0, 0, 0).in_tz("America/New_York")?,
2808 /// );
2809 ///
2810 /// // rounds down
2811 /// let zdt = date(2024, 6, 19).at(10, 0, 0, 0).in_tz("America/New_York")?;
2812 /// assert_eq!(
2813 /// zdt.round(Unit::Day)?,
2814 /// date(2024, 6, 19).at(0, 0, 0, 0).in_tz("America/New_York")?,
2815 /// );
2816 ///
2817 /// # Ok::<(), Box<dyn std::error::Error>>(())
2818 /// ```
2819 ///
2820 /// # Example: changing the rounding mode
2821 ///
2822 /// The default rounding mode is [`RoundMode::HalfExpand`], which
2823 /// breaks ties by rounding away from zero. But other modes like
2824 /// [`RoundMode::Trunc`] can be used too:
2825 ///
2826 /// ```
2827 /// use jiff::{civil::date, RoundMode, Unit, Zoned, ZonedRound};
2828 ///
2829 /// let zdt = date(2024, 6, 19).at(15, 0, 0, 0).in_tz("America/New_York")?;
2830 /// assert_eq!(
2831 /// zdt.round(Unit::Day)?,
2832 /// date(2024, 6, 20).at(0, 0, 0, 0).in_tz("America/New_York")?,
2833 /// );
2834 /// // The default will round up to the next day for any time past noon (as
2835 /// // shown above), but using truncation rounding will always round down.
2836 /// assert_eq!(
2837 /// zdt.round(
2838 /// ZonedRound::new().smallest(Unit::Day).mode(RoundMode::Trunc),
2839 /// )?,
2840 /// date(2024, 6, 19).at(0, 0, 0, 0).in_tz("America/New_York")?,
2841 /// );
2842 ///
2843 /// # Ok::<(), Box<dyn std::error::Error>>(())
2844 /// ```
2845 ///
2846 /// # Example: rounding to the nearest 5 minute increment
2847 ///
2848 /// ```
2849 /// use jiff::{civil::date, Unit};
2850 ///
2851 /// // rounds down
2852 /// let zdt = date(2024, 6, 19)
2853 /// .at(15, 27, 29, 999_999_999)
2854 /// .in_tz("America/New_York")?;
2855 /// assert_eq!(
2856 /// zdt.round((Unit::Minute, 5))?,
2857 /// date(2024, 6, 19).at(15, 25, 0, 0).in_tz("America/New_York")?,
2858 /// );
2859 /// // rounds up
2860 /// let zdt = date(2024, 6, 19)
2861 /// .at(15, 27, 30, 0)
2862 /// .in_tz("America/New_York")?;
2863 /// assert_eq!(
2864 /// zdt.round((Unit::Minute, 5))?,
2865 /// date(2024, 6, 19).at(15, 30, 0, 0).in_tz("America/New_York")?,
2866 /// );
2867 ///
2868 /// # Ok::<(), Box<dyn std::error::Error>>(())
2869 /// ```
2870 ///
2871 /// # Example: behavior near time zone transitions
2872 ///
2873 /// When rounding this zoned datetime near time zone transitions (such as
2874 /// DST), the "sensible" thing is done by default. Namely, rounding will
2875 /// jump to the closest instant, even if the change in civil clock time is
2876 /// large. For example, when rounding up into a gap, the civil clock time
2877 /// will jump over the gap, but the corresponding change in the instant is
2878 /// as one might expect:
2879 ///
2880 /// ```
2881 /// use jiff::{Unit, Zoned};
2882 ///
2883 /// let zdt1: Zoned = "2024-03-10T01:59:00-05[America/New_York]".parse()?;
2884 /// let zdt2 = zdt1.round(Unit::Hour)?;
2885 /// assert_eq!(
2886 /// zdt2.to_string(),
2887 /// "2024-03-10T03:00:00-04:00[America/New_York]",
2888 /// );
2889 ///
2890 /// # Ok::<(), Box<dyn std::error::Error>>(())
2891 /// ```
2892 ///
2893 /// Similarly, when rounding inside a fold, rounding will respect whether
2894 /// it's the first or second time the clock has repeated the hour. For the
2895 /// DST transition in New York on `2024-11-03` from offset `-04` to `-05`,
2896 /// here is an example that rounds the first 1 o'clock hour:
2897 ///
2898 /// ```
2899 /// use jiff::{Unit, Zoned};
2900 ///
2901 /// let zdt1: Zoned = "2024-11-03T01:59:01-04[America/New_York]".parse()?;
2902 /// let zdt2 = zdt1.round(Unit::Minute)?;
2903 /// assert_eq!(
2904 /// zdt2.to_string(),
2905 /// "2024-11-03T01:59:00-04:00[America/New_York]",
2906 /// );
2907 ///
2908 /// # Ok::<(), Box<dyn std::error::Error>>(())
2909 /// ```
2910 ///
2911 /// And now the second 1 o'clock hour. Notice how the rounded result stays
2912 /// in the second 1 o'clock hour.
2913 ///
2914 /// ```
2915 /// use jiff::{Unit, Zoned};
2916 ///
2917 /// let zdt1: Zoned = "2024-11-03T01:59:01-05[America/New_York]".parse()?;
2918 /// let zdt2 = zdt1.round(Unit::Minute)?;
2919 /// assert_eq!(
2920 /// zdt2.to_string(),
2921 /// "2024-11-03T01:59:00-05:00[America/New_York]",
2922 /// );
2923 ///
2924 /// # Ok::<(), Box<dyn std::error::Error>>(())
2925 /// ```
2926 ///
2927 /// # Example: rounding to nearest day takes length of day into account
2928 ///
2929 /// Some days are shorter than 24 hours, and so rounding down will occur
2930 /// even when the time is past noon:
2931 ///
2932 /// ```
2933 /// use jiff::{Unit, Zoned};
2934 ///
2935 /// let zdt1: Zoned = "2025-03-09T12:15-04[America/New_York]".parse()?;
2936 /// let zdt2 = zdt1.round(Unit::Day)?;
2937 /// assert_eq!(
2938 /// zdt2.to_string(),
2939 /// "2025-03-09T00:00:00-05:00[America/New_York]",
2940 /// );
2941 ///
2942 /// // For 23 hour days, 12:30 is the tipping point to round up in the
2943 /// // default rounding configuration:
2944 /// let zdt1: Zoned = "2025-03-09T12:30-04[America/New_York]".parse()?;
2945 /// let zdt2 = zdt1.round(Unit::Day)?;
2946 /// assert_eq!(
2947 /// zdt2.to_string(),
2948 /// "2025-03-10T00:00:00-04:00[America/New_York]",
2949 /// );
2950 ///
2951 /// # Ok::<(), Box<dyn std::error::Error>>(())
2952 /// ```
2953 ///
2954 /// And some days are longer than 24 hours, and so rounding _up_ will occur
2955 /// even when the time is before noon:
2956 ///
2957 /// ```
2958 /// use jiff::{Unit, Zoned};
2959 ///
2960 /// let zdt1: Zoned = "2025-11-02T11:45-05[America/New_York]".parse()?;
2961 /// let zdt2 = zdt1.round(Unit::Day)?;
2962 /// assert_eq!(
2963 /// zdt2.to_string(),
2964 /// "2025-11-03T00:00:00-05:00[America/New_York]",
2965 /// );
2966 ///
2967 /// // For 25 hour days, 11:30 is the tipping point to round up in the
2968 /// // default rounding configuration. So 11:29 will round down:
2969 /// let zdt1: Zoned = "2025-11-02T11:29-05[America/New_York]".parse()?;
2970 /// let zdt2 = zdt1.round(Unit::Day)?;
2971 /// assert_eq!(
2972 /// zdt2.to_string(),
2973 /// "2025-11-02T00:00:00-04:00[America/New_York]",
2974 /// );
2975 ///
2976 /// # Ok::<(), Box<dyn std::error::Error>>(())
2977 /// ```
2978 ///
2979 /// # Example: overflow error
2980 ///
2981 /// This example demonstrates that it's possible for this operation to
2982 /// result in an error from zoned datetime arithmetic overflow.
2983 ///
2984 /// ```
2985 /// use jiff::{Timestamp, Unit};
2986 ///
2987 /// let zdt = Timestamp::MAX.in_tz("America/New_York")?;
2988 /// assert!(zdt.round(Unit::Day).is_err());
2989 ///
2990 /// # Ok::<(), Box<dyn std::error::Error>>(())
2991 /// ```
2992 ///
2993 /// This occurs because rounding to the nearest day for the maximum
2994 /// timestamp would result in rounding up to the next day. But the next day
2995 /// is greater than the maximum, and so this returns an error.
2996 #[inline]
2997 pub fn round<R: Into<ZonedRound>>(
2998 &self,
2999 options: R,
3000 ) -> Result<Zoned, Error> {
3001 let options: ZonedRound = options.into();
3002 options.round(self)
3003 }
3004
3005 /// Return an iterator of periodic zoned datetimes determined by the given
3006 /// span.
3007 ///
3008 /// The given span may be negative, in which case, the iterator will move
3009 /// backwards through time. The iterator won't stop until either the span
3010 /// itself overflows, or it would otherwise exceed the minimum or maximum
3011 /// `Zoned` value.
3012 ///
3013 /// When the given span is positive, the zoned datetimes yielded are
3014 /// monotonically increasing. When the given span is negative, the zoned
3015 /// datetimes yielded as monotonically decreasing. When the given span is
3016 /// zero, then all values yielded are identical and the time series is
3017 /// infinite.
3018 ///
3019 /// # Example: when to check a glucose monitor
3020 ///
3021 /// When my cat had diabetes, my veterinarian installed a glucose monitor
3022 /// and instructed me to scan it about every 5 hours. This example lists
3023 /// all of the times I needed to scan it for the 2 days following its
3024 /// installation:
3025 ///
3026 /// ```
3027 /// use jiff::{civil::datetime, ToSpan};
3028 ///
3029 /// let start = datetime(2023, 7, 15, 16, 30, 0, 0).in_tz("America/New_York")?;
3030 /// let end = start.checked_add(2.days())?;
3031 /// let mut scan_times = vec![];
3032 /// for zdt in start.series(5.hours()).take_while(|zdt| zdt <= end) {
3033 /// scan_times.push(zdt.datetime());
3034 /// }
3035 /// assert_eq!(scan_times, vec![
3036 /// datetime(2023, 7, 15, 16, 30, 0, 0),
3037 /// datetime(2023, 7, 15, 21, 30, 0, 0),
3038 /// datetime(2023, 7, 16, 2, 30, 0, 0),
3039 /// datetime(2023, 7, 16, 7, 30, 0, 0),
3040 /// datetime(2023, 7, 16, 12, 30, 0, 0),
3041 /// datetime(2023, 7, 16, 17, 30, 0, 0),
3042 /// datetime(2023, 7, 16, 22, 30, 0, 0),
3043 /// datetime(2023, 7, 17, 3, 30, 0, 0),
3044 /// datetime(2023, 7, 17, 8, 30, 0, 0),
3045 /// datetime(2023, 7, 17, 13, 30, 0, 0),
3046 /// ]);
3047 ///
3048 /// # Ok::<(), Box<dyn std::error::Error>>(())
3049 /// ```
3050 ///
3051 /// # Example: behavior during daylight saving time transitions
3052 ///
3053 /// Even when there is a daylight saving time transition, the time series
3054 /// returned handles it correctly by continuing to move forward.
3055 ///
3056 /// This first example shows what happens when there is a gap in time (it
3057 /// is automatically skipped):
3058 ///
3059 /// ```
3060 /// use jiff::{civil::date, ToSpan};
3061 ///
3062 /// let zdt = date(2025, 3, 9).at(1, 0, 0, 0).in_tz("America/New_York")?;
3063 /// let mut it = zdt.series(30.minutes());
3064 ///
3065 /// assert_eq!(
3066 /// it.next().map(|zdt| zdt.to_string()),
3067 /// Some("2025-03-09T01:00:00-05:00[America/New_York]".to_string()),
3068 /// );
3069 /// assert_eq!(
3070 /// it.next().map(|zdt| zdt.to_string()),
3071 /// Some("2025-03-09T01:30:00-05:00[America/New_York]".to_string()),
3072 /// );
3073 /// assert_eq!(
3074 /// it.next().map(|zdt| zdt.to_string()),
3075 /// Some("2025-03-09T03:00:00-04:00[America/New_York]".to_string()),
3076 /// );
3077 /// assert_eq!(
3078 /// it.next().map(|zdt| zdt.to_string()),
3079 /// Some("2025-03-09T03:30:00-04:00[America/New_York]".to_string()),
3080 /// );
3081 ///
3082 /// # Ok::<(), Box<dyn std::error::Error>>(())
3083 /// ```
3084 ///
3085 /// And similarly, when there is a fold in time, the fold is repeated:
3086 ///
3087 /// ```
3088 /// use jiff::{civil::date, ToSpan};
3089 ///
3090 /// let zdt = date(2025, 11, 2).at(0, 30, 0, 0).in_tz("America/New_York")?;
3091 /// let mut it = zdt.series(30.minutes());
3092 ///
3093 /// assert_eq!(
3094 /// it.next().map(|zdt| zdt.to_string()),
3095 /// Some("2025-11-02T00:30:00-04:00[America/New_York]".to_string()),
3096 /// );
3097 /// assert_eq!(
3098 /// it.next().map(|zdt| zdt.to_string()),
3099 /// Some("2025-11-02T01:00:00-04:00[America/New_York]".to_string()),
3100 /// );
3101 /// assert_eq!(
3102 /// it.next().map(|zdt| zdt.to_string()),
3103 /// Some("2025-11-02T01:30:00-04:00[America/New_York]".to_string()),
3104 /// );
3105 /// assert_eq!(
3106 /// it.next().map(|zdt| zdt.to_string()),
3107 /// Some("2025-11-02T01:00:00-05:00[America/New_York]".to_string()),
3108 /// );
3109 /// assert_eq!(
3110 /// it.next().map(|zdt| zdt.to_string()),
3111 /// Some("2025-11-02T01:30:00-05:00[America/New_York]".to_string()),
3112 /// );
3113 /// assert_eq!(
3114 /// it.next().map(|zdt| zdt.to_string()),
3115 /// Some("2025-11-02T02:00:00-05:00[America/New_York]".to_string()),
3116 /// );
3117 ///
3118 /// # Ok::<(), Box<dyn std::error::Error>>(())
3119 /// ```
3120 ///
3121 /// # Example: ensures values are monotonically increasing (or decreasing)
3122 ///
3123 /// Because of odd time zone transitions, it's possible that adding
3124 /// different calendar units to the same zoned datetime will yield the
3125 /// same result. For example, `2011-12-30` did not exist on the clocks
3126 /// in the `Pacific/Apia` time zone. (Because Samoa switched sides of the
3127 /// International Date Line.) This means that adding `1 day` to
3128 /// `2011-12-29` yields the same result as adding `2 days`:
3129 ///
3130 /// ```
3131 /// use jiff::{civil, ToSpan};
3132 ///
3133 /// let zdt = civil::date(2011, 12, 29).in_tz("Pacific/Apia")?;
3134 /// assert_eq!(
3135 /// zdt.checked_add(1.day())?.to_string(),
3136 /// "2011-12-31T00:00:00+14:00[Pacific/Apia]",
3137 /// );
3138 /// assert_eq!(
3139 /// zdt.checked_add(2.days())?.to_string(),
3140 /// "2011-12-31T00:00:00+14:00[Pacific/Apia]",
3141 /// );
3142 /// assert_eq!(
3143 /// zdt.checked_add(3.days())?.to_string(),
3144 /// "2012-01-01T00:00:00+14:00[Pacific/Apia]",
3145 /// );
3146 ///
3147 /// # Ok::<(), Box<dyn std::error::Error>>(())
3148 /// ```
3149 ///
3150 /// This might lead one to believe that `Zoned::series` could emit the
3151 /// same instant twice. But it takes this into account and ensures all
3152 /// values occur after the previous value (or before if the `Span` given
3153 /// is negative):
3154 ///
3155 /// ```
3156 /// use jiff::{civil::date, ToSpan};
3157 ///
3158 /// let zdt = date(2011, 12, 28).in_tz("Pacific/Apia")?;
3159 /// let mut it = zdt.series(1.day());
3160 ///
3161 /// assert_eq!(
3162 /// it.next().map(|zdt| zdt.to_string()),
3163 /// Some("2011-12-28T00:00:00-10:00[Pacific/Apia]".to_string()),
3164 /// );
3165 /// assert_eq!(
3166 /// it.next().map(|zdt| zdt.to_string()),
3167 /// Some("2011-12-29T00:00:00-10:00[Pacific/Apia]".to_string()),
3168 /// );
3169 /// assert_eq!(
3170 /// it.next().map(|zdt| zdt.to_string()),
3171 /// Some("2011-12-31T00:00:00+14:00[Pacific/Apia]".to_string()),
3172 /// );
3173 /// assert_eq!(
3174 /// it.next().map(|zdt| zdt.to_string()),
3175 /// Some("2012-01-01T00:00:00+14:00[Pacific/Apia]".to_string()),
3176 /// );
3177 ///
3178 /// # Ok::<(), Box<dyn std::error::Error>>(())
3179 /// ```
3180 ///
3181 /// And similarly for a negative `Span`:
3182 ///
3183 /// ```
3184 /// use jiff::{civil::date, ToSpan};
3185 ///
3186 /// let zdt = date(2012, 1, 1).in_tz("Pacific/Apia")?;
3187 /// let mut it = zdt.series(-1.day());
3188 ///
3189 /// assert_eq!(
3190 /// it.next().map(|zdt| zdt.to_string()),
3191 /// Some("2012-01-01T00:00:00+14:00[Pacific/Apia]".to_string()),
3192 /// );
3193 /// assert_eq!(
3194 /// it.next().map(|zdt| zdt.to_string()),
3195 /// Some("2011-12-31T00:00:00+14:00[Pacific/Apia]".to_string()),
3196 /// );
3197 /// assert_eq!(
3198 /// it.next().map(|zdt| zdt.to_string()),
3199 /// Some("2011-12-29T00:00:00-10:00[Pacific/Apia]".to_string()),
3200 /// );
3201 /// assert_eq!(
3202 /// it.next().map(|zdt| zdt.to_string()),
3203 /// Some("2011-12-28T00:00:00-10:00[Pacific/Apia]".to_string()),
3204 /// );
3205 ///
3206 /// # Ok::<(), Box<dyn std::error::Error>>(())
3207 /// ```
3208 ///
3209 /// An exception to this is if a zero `Span` is provided. Then all values
3210 /// emitted are necessarily equivalent:
3211 ///
3212 /// ```
3213 /// use jiff::{civil::date, ToSpan};
3214 ///
3215 /// let zdt = date(2011, 12, 28).in_tz("Pacific/Apia")?;
3216 /// let mut it = zdt.series(0.days());
3217 ///
3218 /// assert_eq!(
3219 /// it.next().map(|zdt| zdt.to_string()),
3220 /// Some("2011-12-28T00:00:00-10:00[Pacific/Apia]".to_string()),
3221 /// );
3222 /// assert_eq!(
3223 /// it.next().map(|zdt| zdt.to_string()),
3224 /// Some("2011-12-28T00:00:00-10:00[Pacific/Apia]".to_string()),
3225 /// );
3226 ///
3227 /// # Ok::<(), Box<dyn std::error::Error>>(())
3228 /// ```
3229 #[inline]
3230 pub fn series(&self, period: Span) -> ZonedSeries {
3231 ZonedSeries { start: self.clone(), prev: None, period, step: 0 }
3232 }
3233
3234 #[inline]
3235 fn into_parts(self) -> (Timestamp, DateTime, Offset, TimeZone) {
3236 let inner = self.inner;
3237 let ZonedInner { timestamp, datetime, offset, time_zone } = inner;
3238 (timestamp, datetime, offset, time_zone)
3239 }
3240}
3241
3242/// Parsing and formatting using a "printf"-style API.
3243impl Zoned {
3244 /// Parses a zoned datetime in `input` matching the given `format`.
3245 ///
3246 /// The format string uses a "printf"-style API where conversion
3247 /// specifiers can be used as place holders to match components of
3248 /// a datetime. For details on the specifiers supported, see the
3249 /// [`fmt::strtime`] module documentation.
3250 ///
3251 /// # Warning
3252 ///
3253 /// The `strtime` module APIs do not require an IANA time zone identifier
3254 /// to parse a `Zoned`. If one is not used, then if you format a zoned
3255 /// datetime in a time zone like `America/New_York` and then parse it back
3256 /// again, the zoned datetime you get back will be a "fixed offset" zoned
3257 /// datetime. This in turn means it will not perform daylight saving time
3258 /// safe arithmetic.
3259 ///
3260 /// However, the `%Q` directive may be used to both format and parse an
3261 /// IANA time zone identifier. It is strongly recommended to use this
3262 /// directive whenever one is formatting or parsing `Zoned` values.
3263 ///
3264 /// # Errors
3265 ///
3266 /// This returns an error when parsing failed. This might happen because
3267 /// the format string itself was invalid, or because the input didn't match
3268 /// the format string.
3269 ///
3270 /// This also returns an error if there wasn't sufficient information to
3271 /// construct a zoned datetime. For example, if an offset wasn't parsed.
3272 ///
3273 /// # Example
3274 ///
3275 /// This example shows how to parse a zoned datetime:
3276 ///
3277 /// ```
3278 /// use jiff::Zoned;
3279 ///
3280 /// let zdt = Zoned::strptime("%F %H:%M %:Q", "2024-07-14 21:14 US/Eastern")?;
3281 /// assert_eq!(zdt.to_string(), "2024-07-14T21:14:00-04:00[US/Eastern]");
3282 ///
3283 /// # Ok::<(), Box<dyn std::error::Error>>(())
3284 /// ```
3285 #[inline]
3286 pub fn strptime(
3287 format: impl AsRef<[u8]>,
3288 input: impl AsRef<[u8]>,
3289 ) -> Result<Zoned, Error> {
3290 fmt::strtime::parse(format, input).and_then(|tm| tm.to_zoned())
3291 }
3292
3293 /// Formats this zoned datetime according to the given `format`.
3294 ///
3295 /// The format string uses a "printf"-style API where conversion
3296 /// specifiers can be used as place holders to format components of
3297 /// a datetime. For details on the specifiers supported, see the
3298 /// [`fmt::strtime`] module documentation.
3299 ///
3300 /// # Warning
3301 ///
3302 /// The `strtime` module APIs do not require an IANA time zone identifier
3303 /// to parse a `Zoned`. If one is not used, then if you format a zoned
3304 /// datetime in a time zone like `America/New_York` and then parse it back
3305 /// again, the zoned datetime you get back will be a "fixed offset" zoned
3306 /// datetime. This in turn means it will not perform daylight saving time
3307 /// safe arithmetic.
3308 ///
3309 /// However, the `%Q` directive may be used to both format and parse an
3310 /// IANA time zone identifier. It is strongly recommended to use this
3311 /// directive whenever one is formatting or parsing `Zoned` values since
3312 /// it permits correctly round-tripping `Zoned` values.
3313 ///
3314 /// # Errors and panics
3315 ///
3316 /// While this routine itself does not error or panic, using the value
3317 /// returned may result in a panic if formatting fails. See the
3318 /// documentation on [`fmt::strtime::Display`] for more information.
3319 ///
3320 /// To format in a way that surfaces errors without panicking, use either
3321 /// [`fmt::strtime::format`] or [`fmt::strtime::BrokenDownTime::format`].
3322 ///
3323 /// # Example
3324 ///
3325 /// While the output of the Unix `date` command is likely locale specific,
3326 /// this is what it looks like on my system:
3327 ///
3328 /// ```
3329 /// use jiff::civil::date;
3330 ///
3331 /// let zdt = date(2024, 7, 15).at(16, 24, 59, 0).in_tz("America/New_York")?;
3332 /// let string = zdt.strftime("%a %b %e %I:%M:%S %p %Z %Y").to_string();
3333 /// assert_eq!(string, "Mon Jul 15 04:24:59 PM EDT 2024");
3334 ///
3335 /// # Ok::<(), Box<dyn std::error::Error>>(())
3336 /// ```
3337 #[inline]
3338 pub fn strftime<'f, F: 'f + ?Sized + AsRef<[u8]>>(
3339 &self,
3340 format: &'f F,
3341 ) -> fmt::strtime::Display<'f> {
3342 fmt::strtime::Display { fmt: format.as_ref(), tm: self.into() }
3343 }
3344}
3345
3346impl Default for Zoned {
3347 #[inline]
3348 fn default() -> Zoned {
3349 Zoned::DEFAULT
3350 }
3351}
3352
3353/// Converts a `Zoned` datetime into a human readable datetime string.
3354///
3355/// (This `Debug` representation currently emits the same string as the
3356/// `Display` representation, but this is not a guarantee.)
3357///
3358/// Options currently supported:
3359///
3360/// * [`std::fmt::Formatter::precision`] can be set to control the precision
3361/// of the fractional second component.
3362///
3363/// # Example
3364///
3365/// ```
3366/// use jiff::civil::date;
3367///
3368/// let zdt = date(2024, 6, 15).at(7, 0, 0, 123_000_000).in_tz("US/Eastern")?;
3369/// assert_eq!(
3370/// format!("{zdt:.6?}"),
3371/// "2024-06-15T07:00:00.123000-04:00[US/Eastern]",
3372/// );
3373/// // Precision values greater than 9 are clamped to 9.
3374/// assert_eq!(
3375/// format!("{zdt:.300?}"),
3376/// "2024-06-15T07:00:00.123000000-04:00[US/Eastern]",
3377/// );
3378/// // A precision of 0 implies the entire fractional
3379/// // component is always truncated.
3380/// assert_eq!(
3381/// format!("{zdt:.0?}"),
3382/// "2024-06-15T07:00:00-04:00[US/Eastern]",
3383/// );
3384///
3385/// # Ok::<(), Box<dyn std::error::Error>>(())
3386/// ```
3387impl core::fmt::Debug for Zoned {
3388 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
3389 core::fmt::Display::fmt(self, f)
3390 }
3391}
3392
3393/// Converts a `Zoned` datetime into a RFC 9557 compliant string.
3394///
3395/// # Formatting options supported
3396///
3397/// * [`std::fmt::Formatter::precision`] can be set to control the precision
3398/// of the fractional second component. When not set, the minimum precision
3399/// required to losslessly render the value is used.
3400///
3401/// # Example
3402///
3403/// This shows the default rendering:
3404///
3405/// ```
3406/// use jiff::civil::date;
3407///
3408/// // No fractional seconds:
3409/// let zdt = date(2024, 6, 15).at(7, 0, 0, 0).in_tz("US/Eastern")?;
3410/// assert_eq!(format!("{zdt}"), "2024-06-15T07:00:00-04:00[US/Eastern]");
3411///
3412/// // With fractional seconds:
3413/// let zdt = date(2024, 6, 15).at(7, 0, 0, 123_000_000).in_tz("US/Eastern")?;
3414/// assert_eq!(format!("{zdt}"), "2024-06-15T07:00:00.123-04:00[US/Eastern]");
3415///
3416/// # Ok::<(), Box<dyn std::error::Error>>(())
3417/// ```
3418///
3419/// # Example: setting the precision
3420///
3421/// ```
3422/// use jiff::civil::date;
3423///
3424/// let zdt = date(2024, 6, 15).at(7, 0, 0, 123_000_000).in_tz("US/Eastern")?;
3425/// assert_eq!(
3426/// format!("{zdt:.6}"),
3427/// "2024-06-15T07:00:00.123000-04:00[US/Eastern]",
3428/// );
3429/// // Precision values greater than 9 are clamped to 9.
3430/// assert_eq!(
3431/// format!("{zdt:.300}"),
3432/// "2024-06-15T07:00:00.123000000-04:00[US/Eastern]",
3433/// );
3434/// // A precision of 0 implies the entire fractional
3435/// // component is always truncated.
3436/// assert_eq!(
3437/// format!("{zdt:.0}"),
3438/// "2024-06-15T07:00:00-04:00[US/Eastern]",
3439/// );
3440///
3441/// # Ok::<(), Box<dyn std::error::Error>>(())
3442/// ```
3443impl core::fmt::Display for Zoned {
3444 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
3445 use crate::fmt::StdFmtWrite;
3446
3447 let precision =
3448 f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
3449 temporal::DateTimePrinter::new()
3450 .precision(precision)
3451 .print_zoned(self, StdFmtWrite(f))
3452 .map_err(|_| core::fmt::Error)
3453 }
3454}
3455
3456/// Parses a zoned timestamp from the Temporal datetime format.
3457///
3458/// See the [`fmt::temporal`](crate::fmt::temporal) for more information on
3459/// the precise format.
3460///
3461/// Note that this is only enabled when the `std` feature
3462/// is enabled because it requires access to a global
3463/// [`TimeZoneDatabase`](crate::tz::TimeZoneDatabase).
3464impl core::str::FromStr for Zoned {
3465 type Err = Error;
3466
3467 fn from_str(string: &str) -> Result<Zoned, Error> {
3468 DEFAULT_DATETIME_PARSER.parse_zoned(string)
3469 }
3470}
3471
3472impl Eq for Zoned {}
3473
3474impl PartialEq for Zoned {
3475 #[inline]
3476 fn eq(&self, rhs: &Zoned) -> bool {
3477 self.timestamp().eq(&rhs.timestamp())
3478 }
3479}
3480
3481impl<'a> PartialEq<Zoned> for &'a Zoned {
3482 #[inline]
3483 fn eq(&self, rhs: &Zoned) -> bool {
3484 (**self).eq(rhs)
3485 }
3486}
3487
3488impl Ord for Zoned {
3489 #[inline]
3490 fn cmp(&self, rhs: &Zoned) -> core::cmp::Ordering {
3491 self.timestamp().cmp(&rhs.timestamp())
3492 }
3493}
3494
3495impl PartialOrd for Zoned {
3496 #[inline]
3497 fn partial_cmp(&self, rhs: &Zoned) -> Option<core::cmp::Ordering> {
3498 Some(self.cmp(rhs))
3499 }
3500}
3501
3502impl<'a> PartialOrd<Zoned> for &'a Zoned {
3503 #[inline]
3504 fn partial_cmp(&self, rhs: &Zoned) -> Option<core::cmp::Ordering> {
3505 (**self).partial_cmp(rhs)
3506 }
3507}
3508
3509impl core::hash::Hash for Zoned {
3510 #[inline]
3511 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
3512 self.timestamp().hash(state);
3513 }
3514}
3515
3516#[cfg(feature = "std")]
3517impl TryFrom<std::time::SystemTime> for Zoned {
3518 type Error = Error;
3519
3520 #[inline]
3521 fn try_from(system_time: std::time::SystemTime) -> Result<Zoned, Error> {
3522 let timestamp = Timestamp::try_from(system_time)?;
3523 Ok(Zoned::new(timestamp, TimeZone::system()))
3524 }
3525}
3526
3527#[cfg(feature = "std")]
3528impl From<Zoned> for std::time::SystemTime {
3529 #[inline]
3530 fn from(time: Zoned) -> std::time::SystemTime {
3531 time.timestamp().into()
3532 }
3533}
3534
3535#[cfg(feature = "std")]
3536impl<'a> From<&'a Zoned> for std::time::SystemTime {
3537 #[inline]
3538 fn from(time: &'a Zoned) -> std::time::SystemTime {
3539 time.timestamp().into()
3540 }
3541}
3542
3543/// Adds a span of time to a zoned datetime.
3544///
3545/// This uses checked arithmetic and panics on overflow. To handle overflow
3546/// without panics, use [`Zoned::checked_add`].
3547///
3548/// Using this implementation will result in consuming the `Zoned` value. Since
3549/// it is not `Copy`, this will prevent further use. If this is undesirable,
3550/// consider using the trait implementation for `&Zoned`, `Zoned::checked_add`
3551/// or cloning the `Zoned` value.
3552impl<'a> core::ops::Add<Span> for Zoned {
3553 type Output = Zoned;
3554
3555 #[inline]
3556 fn add(self, rhs: Span) -> Zoned {
3557 self.checked_add_consuming(rhs)
3558 .expect("adding span to zoned datetime overflowed")
3559 }
3560}
3561
3562/// Adds a span of time to a borrowed zoned datetime.
3563///
3564/// This uses checked arithmetic and panics on overflow. To handle overflow
3565/// without panics, use [`Zoned::checked_add`].
3566impl<'a> core::ops::Add<Span> for &'a Zoned {
3567 type Output = Zoned;
3568
3569 #[inline]
3570 fn add(self, rhs: Span) -> Zoned {
3571 self.checked_add(rhs)
3572 .expect("adding span to zoned datetime overflowed")
3573 }
3574}
3575
3576/// Adds a span of time to a zoned datetime in place.
3577///
3578/// This uses checked arithmetic and panics on overflow. To handle overflow
3579/// without panics, use [`Zoned::checked_add`].
3580impl core::ops::AddAssign<Span> for Zoned {
3581 #[inline]
3582 fn add_assign(&mut self, rhs: Span) {
3583 *self = core::mem::take(self) + rhs;
3584 }
3585}
3586
3587/// Subtracts a span of time from a zoned datetime.
3588///
3589/// This uses checked arithmetic and panics on overflow. To handle overflow
3590/// without panics, use [`Zoned::checked_sub`].
3591///
3592/// Using this implementation will result in consuming the `Zoned` value. Since
3593/// it is not `Copy`, this will prevent further use. If this is undesirable,
3594/// consider using the trait implementation for `&Zoned`, `Zoned::checked_sub`
3595/// or cloning the `Zoned` value.
3596impl<'a> core::ops::Sub<Span> for Zoned {
3597 type Output = Zoned;
3598
3599 #[inline]
3600 fn sub(self, rhs: Span) -> Zoned {
3601 self.checked_sub_consuming(rhs)
3602 .expect("subtracting span from zoned datetime overflowed")
3603 }
3604}
3605
3606/// Subtracts a span of time from a borrowed zoned datetime.
3607///
3608/// This uses checked arithmetic and panics on overflow. To handle overflow
3609/// without panics, use [`Zoned::checked_sub`].
3610impl<'a> core::ops::Sub<Span> for &'a Zoned {
3611 type Output = Zoned;
3612
3613 #[inline]
3614 fn sub(self, rhs: Span) -> Zoned {
3615 self.checked_sub(rhs)
3616 .expect("subtracting span from zoned datetime overflowed")
3617 }
3618}
3619
3620/// Subtracts a span of time from a zoned datetime in place.
3621///
3622/// This uses checked arithmetic and panics on overflow. To handle overflow
3623/// without panics, use [`Zoned::checked_sub`].
3624impl core::ops::SubAssign<Span> for Zoned {
3625 #[inline]
3626 fn sub_assign(&mut self, rhs: Span) {
3627 *self = core::mem::take(self) - rhs;
3628 }
3629}
3630
3631/// Computes the span of time between two zoned datetimes.
3632///
3633/// This will return a negative span when the zoned datetime being subtracted
3634/// is greater.
3635///
3636/// Since this uses the default configuration for calculating a span between
3637/// two zoned datetimes (no rounding and largest units is hours), this will
3638/// never panic or fail in any way. It is guaranteed that the largest non-zero
3639/// unit in the `Span` returned will be hours.
3640///
3641/// To configure the largest unit or enable rounding, use [`Zoned::since`].
3642///
3643/// Using this implementation will result in consuming the `Zoned` value. Since
3644/// it is not `Copy`, this will prevent further use. If this is undesirable,
3645/// consider using the trait implementation for `&Zoned`, `Zoned::since`,
3646/// `Zoned::until` or cloning the `Zoned` value.
3647impl core::ops::Sub for Zoned {
3648 type Output = Span;
3649
3650 #[inline]
3651 fn sub(self, rhs: Zoned) -> Span {
3652 (&self).sub(&rhs)
3653 }
3654}
3655
3656/// Computes the span of time between two borrowed zoned datetimes.
3657///
3658/// This will return a negative span when the zoned datetime being subtracted
3659/// is greater.
3660///
3661/// Since this uses the default configuration for calculating a span between
3662/// two zoned datetimes (no rounding and largest units is hours), this will
3663/// never panic or fail in any way. It is guaranteed that the largest non-zero
3664/// unit in the `Span` returned will be hours.
3665///
3666/// To configure the largest unit or enable rounding, use [`Zoned::since`].
3667impl<'a> core::ops::Sub for &'a Zoned {
3668 type Output = Span;
3669
3670 #[inline]
3671 fn sub(self, rhs: &'a Zoned) -> Span {
3672 self.since(rhs).expect("since never fails when given Zoned")
3673 }
3674}
3675
3676/// Adds a signed duration of time to a zoned datetime.
3677///
3678/// This uses checked arithmetic and panics on overflow. To handle overflow
3679/// without panics, use [`Zoned::checked_add`].
3680///
3681/// Using this implementation will result in consuming the `Zoned` value. Since
3682/// it is not `Copy`, this will prevent further use. If this is undesirable,
3683/// consider using the trait implementation for `&Zoned`, `Zoned::checked_add`
3684/// or cloning the `Zoned` value.
3685impl core::ops::Add<SignedDuration> for Zoned {
3686 type Output = Zoned;
3687
3688 #[inline]
3689 fn add(self, rhs: SignedDuration) -> Zoned {
3690 self.checked_add_consuming(rhs)
3691 .expect("adding signed duration to zoned datetime overflowed")
3692 }
3693}
3694
3695/// Adds a signed duration of time to a borrowed zoned datetime.
3696///
3697/// This uses checked arithmetic and panics on overflow. To handle overflow
3698/// without panics, use [`Zoned::checked_add`].
3699impl<'a> core::ops::Add<SignedDuration> for &'a Zoned {
3700 type Output = Zoned;
3701
3702 #[inline]
3703 fn add(self, rhs: SignedDuration) -> Zoned {
3704 self.checked_add(rhs)
3705 .expect("adding signed duration to zoned datetime overflowed")
3706 }
3707}
3708
3709/// Adds a signed duration of time to a zoned datetime in place.
3710///
3711/// This uses checked arithmetic and panics on overflow. To handle overflow
3712/// without panics, use [`Zoned::checked_add`].
3713impl core::ops::AddAssign<SignedDuration> for Zoned {
3714 #[inline]
3715 fn add_assign(&mut self, rhs: SignedDuration) {
3716 *self = core::mem::take(self) + rhs;
3717 }
3718}
3719
3720/// Subtracts a signed duration of time from a zoned datetime.
3721///
3722/// This uses checked arithmetic and panics on overflow. To handle overflow
3723/// without panics, use [`Zoned::checked_sub`].
3724///
3725/// Using this implementation will result in consuming the `Zoned` value. Since
3726/// it is not `Copy`, this will prevent further use. If this is undesirable,
3727/// consider using the trait implementation for `&Zoned`, `Zoned::checked_sub`
3728/// or cloning the `Zoned` value.
3729impl core::ops::Sub<SignedDuration> for Zoned {
3730 type Output = Zoned;
3731
3732 #[inline]
3733 fn sub(self, rhs: SignedDuration) -> Zoned {
3734 self.checked_sub_consuming(rhs).expect(
3735 "subtracting signed duration from zoned datetime overflowed",
3736 )
3737 }
3738}
3739
3740/// Subtracts a signed duration of time from a borrowed zoned datetime.
3741///
3742/// This uses checked arithmetic and panics on overflow. To handle overflow
3743/// without panics, use [`Zoned::checked_sub`].
3744impl<'a> core::ops::Sub<SignedDuration> for &'a Zoned {
3745 type Output = Zoned;
3746
3747 #[inline]
3748 fn sub(self, rhs: SignedDuration) -> Zoned {
3749 self.checked_sub(rhs).expect(
3750 "subtracting signed duration from zoned datetime overflowed",
3751 )
3752 }
3753}
3754
3755/// Subtracts a signed duration of time from a zoned datetime in place.
3756///
3757/// This uses checked arithmetic and panics on overflow. To handle overflow
3758/// without panics, use [`Zoned::checked_sub`].
3759impl core::ops::SubAssign<SignedDuration> for Zoned {
3760 #[inline]
3761 fn sub_assign(&mut self, rhs: SignedDuration) {
3762 *self = core::mem::take(self) - rhs;
3763 }
3764}
3765
3766/// Adds an unsigned duration of time to a zoned datetime.
3767///
3768/// This uses checked arithmetic and panics on overflow. To handle overflow
3769/// without panics, use [`Zoned::checked_add`].
3770///
3771/// Using this implementation will result in consuming the `Zoned` value. Since
3772/// it is not `Copy`, this will prevent further use. If this is undesirable,
3773/// consider using the trait implementation for `&Zoned`, `Zoned::checked_add`
3774/// or cloning the `Zoned` value.
3775impl core::ops::Add<UnsignedDuration> for Zoned {
3776 type Output = Zoned;
3777
3778 #[inline]
3779 fn add(self, rhs: UnsignedDuration) -> Zoned {
3780 self.checked_add_consuming(rhs)
3781 .expect("adding unsigned duration to zoned datetime overflowed")
3782 }
3783}
3784
3785/// Adds an unsigned duration of time to a borrowed zoned datetime.
3786///
3787/// This uses checked arithmetic and panics on overflow. To handle overflow
3788/// without panics, use [`Zoned::checked_add`].
3789impl<'a> core::ops::Add<UnsignedDuration> for &'a Zoned {
3790 type Output = Zoned;
3791
3792 #[inline]
3793 fn add(self, rhs: UnsignedDuration) -> Zoned {
3794 self.checked_add(rhs)
3795 .expect("adding unsigned duration to zoned datetime overflowed")
3796 }
3797}
3798
3799/// Adds an unsigned duration of time to a zoned datetime in place.
3800///
3801/// This uses checked arithmetic and panics on overflow. To handle overflow
3802/// without panics, use [`Zoned::checked_add`].
3803impl core::ops::AddAssign<UnsignedDuration> for Zoned {
3804 #[inline]
3805 fn add_assign(&mut self, rhs: UnsignedDuration) {
3806 *self = core::mem::take(self) + rhs;
3807 }
3808}
3809
3810/// Subtracts an unsigned duration of time from a zoned datetime.
3811///
3812/// This uses checked arithmetic and panics on overflow. To handle overflow
3813/// without panics, use [`Zoned::checked_sub`].
3814///
3815/// Using this implementation will result in consuming the `Zoned` value. Since
3816/// it is not `Copy`, this will prevent further use. If this is undesirable,
3817/// consider using the trait implementation for `&Zoned`, `Zoned::checked_sub`
3818/// or cloning the `Zoned` value.
3819impl core::ops::Sub<UnsignedDuration> for Zoned {
3820 type Output = Zoned;
3821
3822 #[inline]
3823 fn sub(self, rhs: UnsignedDuration) -> Zoned {
3824 self.checked_sub_consuming(rhs).expect(
3825 "subtracting unsigned duration from zoned datetime overflowed",
3826 )
3827 }
3828}
3829
3830/// Subtracts an unsigned duration of time from a borrowed zoned datetime.
3831///
3832/// This uses checked arithmetic and panics on overflow. To handle overflow
3833/// without panics, use [`Zoned::checked_sub`].
3834impl<'a> core::ops::Sub<UnsignedDuration> for &'a Zoned {
3835 type Output = Zoned;
3836
3837 #[inline]
3838 fn sub(self, rhs: UnsignedDuration) -> Zoned {
3839 self.checked_sub(rhs).expect(
3840 "subtracting unsigned duration from zoned datetime overflowed",
3841 )
3842 }
3843}
3844
3845/// Subtracts an unsigned duration of time from a zoned datetime in place.
3846///
3847/// This uses checked arithmetic and panics on overflow. To handle overflow
3848/// without panics, use [`Zoned::checked_sub`].
3849impl core::ops::SubAssign<UnsignedDuration> for Zoned {
3850 #[inline]
3851 fn sub_assign(&mut self, rhs: UnsignedDuration) {
3852 *self = core::mem::take(self) - rhs;
3853 }
3854}
3855
3856#[cfg(feature = "serde")]
3857impl serde_core::Serialize for Zoned {
3858 #[inline]
3859 fn serialize<S: serde_core::Serializer>(
3860 &self,
3861 serializer: S,
3862 ) -> Result<S::Ok, S::Error> {
3863 serializer.collect_str(self)
3864 }
3865}
3866
3867#[cfg(feature = "serde")]
3868impl<'de> serde_core::Deserialize<'de> for Zoned {
3869 #[inline]
3870 fn deserialize<D: serde_core::Deserializer<'de>>(
3871 deserializer: D,
3872 ) -> Result<Zoned, D::Error> {
3873 use serde_core::de;
3874
3875 struct ZonedVisitor;
3876
3877 impl<'de> de::Visitor<'de> for ZonedVisitor {
3878 type Value = Zoned;
3879
3880 fn expecting(
3881 &self,
3882 f: &mut core::fmt::Formatter,
3883 ) -> core::fmt::Result {
3884 f.write_str("a zoned datetime string")
3885 }
3886
3887 #[inline]
3888 fn visit_bytes<E: de::Error>(
3889 self,
3890 value: &[u8],
3891 ) -> Result<Zoned, E> {
3892 DEFAULT_DATETIME_PARSER
3893 .parse_zoned(value)
3894 .map_err(de::Error::custom)
3895 }
3896
3897 #[inline]
3898 fn visit_str<E: de::Error>(self, value: &str) -> Result<Zoned, E> {
3899 self.visit_bytes(value.as_bytes())
3900 }
3901 }
3902
3903 deserializer.deserialize_str(ZonedVisitor)
3904 }
3905}
3906
3907#[cfg(test)]
3908impl quickcheck::Arbitrary for Zoned {
3909 fn arbitrary(g: &mut quickcheck::Gen) -> Zoned {
3910 let timestamp = Timestamp::arbitrary(g);
3911 let tz = TimeZone::UTC; // TODO: do something better here?
3912 Zoned::new(timestamp, tz)
3913 }
3914
3915 fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Self>> {
3916 let timestamp = self.timestamp();
3917 alloc::boxed::Box::new(
3918 timestamp
3919 .shrink()
3920 .map(|timestamp| Zoned::new(timestamp, TimeZone::UTC)),
3921 )
3922 }
3923}
3924
3925/// An iterator over periodic zoned datetimes, created by [`Zoned::series`].
3926///
3927/// It is exhausted when the next value would exceed the limits of a [`Span`]
3928/// or [`Zoned`] value.
3929///
3930/// This iterator is created by [`Zoned::series`].
3931#[derive(Clone, Debug)]
3932pub struct ZonedSeries {
3933 start: Zoned,
3934 prev: Option<Timestamp>,
3935 period: Span,
3936 step: i64,
3937}
3938
3939impl Iterator for ZonedSeries {
3940 type Item = Zoned;
3941
3942 #[inline]
3943 fn next(&mut self) -> Option<Zoned> {
3944 // This loop is necessary because adding, e.g., `N * 1 day` may not
3945 // always result in a timestamp that is strictly greater than
3946 // `(N-1) * 1 day`. For example, `Pacific/Apia` never had `2011-12-30`
3947 // on their clocks. So adding `1 day` to `2011-12-29` yields the same
3948 // value as adding `2 days` (that is, `2011-12-31`).
3949 //
3950 // This may seem odd, but Temporal has the same behavior (as of
3951 // 2025-10-15):
3952 //
3953 // >>> zdt = Temporal.ZonedDateTime.from("2011-12-29[Pacific/Apia]")
3954 // Object { … }
3955 // >>> zdt.toString()
3956 // "2011-12-29T00:00:00-10:00[Pacific/Apia]"
3957 // >>> zdt.add({days: 1}).toString()
3958 // "2011-12-31T00:00:00+14:00[Pacific/Apia]"
3959 // >>> zdt.add({days: 2}).toString()
3960 // "2011-12-31T00:00:00+14:00[Pacific/Apia]"
3961 //
3962 // Since we are generating a time series specifically here, it seems
3963 // weird to yield two results that are equivalent instants in time.
3964 // So we use a loop here to guarantee that every instant yielded is
3965 // always strictly *after* the previous instant yielded.
3966 loop {
3967 let span = self.period.checked_mul(self.step).ok()?;
3968 self.step = self.step.checked_add(1)?;
3969 let zdt = self.start.checked_add(span).ok()?;
3970 if self.prev.map_or(true, |prev| {
3971 if self.period.is_positive() {
3972 prev < zdt.timestamp()
3973 } else if self.period.is_negative() {
3974 prev > zdt.timestamp()
3975 } else {
3976 assert!(self.period.is_zero());
3977 // In the case of a zero span, the caller has clearly
3978 // opted into an infinite repeating sequence.
3979 true
3980 }
3981 }) {
3982 self.prev = Some(zdt.timestamp());
3983 return Some(zdt);
3984 }
3985 }
3986 }
3987}
3988
3989impl core::iter::FusedIterator for ZonedSeries {}
3990
3991/// Options for [`Timestamp::checked_add`] and [`Timestamp::checked_sub`].
3992///
3993/// This type provides a way to ergonomically add one of a few different
3994/// duration types to a [`Timestamp`].
3995///
3996/// The main way to construct values of this type is with its `From` trait
3997/// implementations:
3998///
3999/// * `From<Span> for ZonedArithmetic` adds (or subtracts) the given span
4000/// to the receiver timestamp.
4001/// * `From<SignedDuration> for ZonedArithmetic` adds (or subtracts)
4002/// the given signed duration to the receiver timestamp.
4003/// * `From<std::time::Duration> for ZonedArithmetic` adds (or subtracts)
4004/// the given unsigned duration to the receiver timestamp.
4005///
4006/// # Example
4007///
4008/// ```
4009/// use std::time::Duration;
4010///
4011/// use jiff::{SignedDuration, Timestamp, ToSpan};
4012///
4013/// let ts: Timestamp = "2024-02-28T00:00:00Z".parse()?;
4014/// assert_eq!(
4015/// ts.checked_add(48.hours())?,
4016/// "2024-03-01T00:00:00Z".parse()?,
4017/// );
4018/// assert_eq!(
4019/// ts.checked_add(SignedDuration::from_hours(48))?,
4020/// "2024-03-01T00:00:00Z".parse()?,
4021/// );
4022/// assert_eq!(
4023/// ts.checked_add(Duration::from_secs(48 * 60 * 60))?,
4024/// "2024-03-01T00:00:00Z".parse()?,
4025/// );
4026///
4027/// # Ok::<(), Box<dyn std::error::Error>>(())
4028/// ```
4029#[derive(Clone, Copy, Debug)]
4030pub struct ZonedArithmetic {
4031 duration: Duration,
4032}
4033
4034impl ZonedArithmetic {
4035 #[inline]
4036 fn checked_add(self, zdt: Zoned) -> Result<Zoned, Error> {
4037 match self.duration.to_signed()? {
4038 SDuration::Span(span) => zdt.checked_add_span(span),
4039 SDuration::Absolute(sdur) => zdt.checked_add_duration(sdur),
4040 }
4041 }
4042
4043 #[inline]
4044 fn checked_neg(self) -> Result<ZonedArithmetic, Error> {
4045 let duration = self.duration.checked_neg()?;
4046 Ok(ZonedArithmetic { duration })
4047 }
4048
4049 #[inline]
4050 fn is_negative(&self) -> bool {
4051 self.duration.is_negative()
4052 }
4053}
4054
4055impl From<Span> for ZonedArithmetic {
4056 fn from(span: Span) -> ZonedArithmetic {
4057 let duration = Duration::from(span);
4058 ZonedArithmetic { duration }
4059 }
4060}
4061
4062impl From<SignedDuration> for ZonedArithmetic {
4063 fn from(sdur: SignedDuration) -> ZonedArithmetic {
4064 let duration = Duration::from(sdur);
4065 ZonedArithmetic { duration }
4066 }
4067}
4068
4069impl From<UnsignedDuration> for ZonedArithmetic {
4070 fn from(udur: UnsignedDuration) -> ZonedArithmetic {
4071 let duration = Duration::from(udur);
4072 ZonedArithmetic { duration }
4073 }
4074}
4075
4076impl<'a> From<&'a Span> for ZonedArithmetic {
4077 fn from(span: &'a Span) -> ZonedArithmetic {
4078 ZonedArithmetic::from(*span)
4079 }
4080}
4081
4082impl<'a> From<&'a SignedDuration> for ZonedArithmetic {
4083 fn from(sdur: &'a SignedDuration) -> ZonedArithmetic {
4084 ZonedArithmetic::from(*sdur)
4085 }
4086}
4087
4088impl<'a> From<&'a UnsignedDuration> for ZonedArithmetic {
4089 fn from(udur: &'a UnsignedDuration) -> ZonedArithmetic {
4090 ZonedArithmetic::from(*udur)
4091 }
4092}
4093
4094/// Options for [`Zoned::since`] and [`Zoned::until`].
4095///
4096/// This type provides a way to configure the calculation of spans between two
4097/// [`Zoned`] values. In particular, both `Zoned::since` and `Zoned::until`
4098/// accept anything that implements `Into<ZonedDifference>`. There are a few
4099/// key trait implementations that make this convenient:
4100///
4101/// * `From<&Zoned> for ZonedDifference` will construct a configuration
4102/// consisting of just the zoned datetime. So for example, `zdt1.since(zdt2)`
4103/// returns the span from `zdt2` to `zdt1`.
4104/// * `From<(Unit, &Zoned)>` is a convenient way to specify the largest units
4105/// that should be present on the span returned. By default, the largest units
4106/// are days. Using this trait implementation is equivalent to
4107/// `ZonedDifference::new(&zdt).largest(unit)`.
4108///
4109/// One can also provide a `ZonedDifference` value directly. Doing so
4110/// is necessary to use the rounding features of calculating a span. For
4111/// example, setting the smallest unit (defaults to [`Unit::Nanosecond`]), the
4112/// rounding mode (defaults to [`RoundMode::Trunc`]) and the rounding increment
4113/// (defaults to `1`). The defaults are selected such that no rounding occurs.
4114///
4115/// Rounding a span as part of calculating it is provided as a convenience.
4116/// Callers may choose to round the span as a distinct step via
4117/// [`Span::round`], but callers may need to provide a reference date
4118/// for rounding larger units. By coupling rounding with routines like
4119/// [`Zoned::since`], the reference date can be set automatically based on
4120/// the input to `Zoned::since`.
4121///
4122/// # Example
4123///
4124/// This example shows how to round a span between two zoned datetimes to the
4125/// nearest half-hour, with ties breaking away from zero.
4126///
4127/// ```
4128/// use jiff::{RoundMode, ToSpan, Unit, Zoned, ZonedDifference};
4129///
4130/// let zdt1 = "2024-03-15 08:14:00.123456789[America/New_York]".parse::<Zoned>()?;
4131/// let zdt2 = "2030-03-22 15:00[America/New_York]".parse::<Zoned>()?;
4132/// let span = zdt1.until(
4133/// ZonedDifference::new(&zdt2)
4134/// .smallest(Unit::Minute)
4135/// .largest(Unit::Year)
4136/// .mode(RoundMode::HalfExpand)
4137/// .increment(30),
4138/// )?;
4139/// assert_eq!(span, 6.years().days(7).hours(7).fieldwise());
4140///
4141/// # Ok::<(), Box<dyn std::error::Error>>(())
4142/// ```
4143#[derive(Clone, Copy, Debug)]
4144pub struct ZonedDifference<'a> {
4145 zoned: &'a Zoned,
4146 round: SpanRound<'static>,
4147}
4148
4149impl<'a> ZonedDifference<'a> {
4150 /// Create a new default configuration for computing the span between the
4151 /// given zoned datetime and some other zoned datetime (specified as the
4152 /// receiver in [`Zoned::since`] or [`Zoned::until`]).
4153 #[inline]
4154 pub fn new(zoned: &'a Zoned) -> ZonedDifference<'a> {
4155 // We use truncation rounding by default since it seems that's
4156 // what is generally expected when computing the difference between
4157 // datetimes.
4158 //
4159 // See: https://github.com/tc39/proposal-temporal/issues/1122
4160 let round = SpanRound::new().mode(RoundMode::Trunc);
4161 ZonedDifference { zoned, round }
4162 }
4163
4164 /// Set the smallest units allowed in the span returned.
4165 ///
4166 /// When a largest unit is not specified and the smallest unit is hours
4167 /// or greater, then the largest unit is automatically set to be equal to
4168 /// the smallest unit.
4169 ///
4170 /// # Errors
4171 ///
4172 /// The smallest units must be no greater than the largest units. If this
4173 /// is violated, then computing a span with this configuration will result
4174 /// in an error.
4175 ///
4176 /// # Example
4177 ///
4178 /// This shows how to round a span between two zoned datetimes to the
4179 /// nearest number of weeks.
4180 ///
4181 /// ```
4182 /// use jiff::{RoundMode, ToSpan, Unit, Zoned, ZonedDifference};
4183 ///
4184 /// let zdt1 = "2024-03-15 08:14[America/New_York]".parse::<Zoned>()?;
4185 /// let zdt2 = "2030-11-22 08:30[America/New_York]".parse::<Zoned>()?;
4186 /// let span = zdt1.until(
4187 /// ZonedDifference::new(&zdt2)
4188 /// .smallest(Unit::Week)
4189 /// .largest(Unit::Week)
4190 /// .mode(RoundMode::HalfExpand),
4191 /// )?;
4192 /// assert_eq!(format!("{span:#}"), "349w");
4193 ///
4194 /// # Ok::<(), Box<dyn std::error::Error>>(())
4195 /// ```
4196 #[inline]
4197 pub fn smallest(self, unit: Unit) -> ZonedDifference<'a> {
4198 ZonedDifference { round: self.round.smallest(unit), ..self }
4199 }
4200
4201 /// Set the largest units allowed in the span returned.
4202 ///
4203 /// When a largest unit is not specified and the smallest unit is hours
4204 /// or greater, then the largest unit is automatically set to be equal to
4205 /// the smallest unit. Otherwise, when the largest unit is not specified,
4206 /// it is set to hours.
4207 ///
4208 /// Once a largest unit is set, there is no way to change this rounding
4209 /// configuration back to using the "automatic" default. Instead, callers
4210 /// must create a new configuration.
4211 ///
4212 /// # Errors
4213 ///
4214 /// The largest units, when set, must be at least as big as the smallest
4215 /// units (which defaults to [`Unit::Nanosecond`]). If this is violated,
4216 /// then computing a span with this configuration will result in an error.
4217 ///
4218 /// # Example
4219 ///
4220 /// This shows how to round a span between two zoned datetimes to units no
4221 /// bigger than seconds.
4222 ///
4223 /// ```
4224 /// use jiff::{ToSpan, Unit, Zoned, ZonedDifference};
4225 ///
4226 /// let zdt1 = "2024-03-15 08:14[America/New_York]".parse::<Zoned>()?;
4227 /// let zdt2 = "2030-11-22 08:30[America/New_York]".parse::<Zoned>()?;
4228 /// let span = zdt1.until(
4229 /// ZonedDifference::new(&zdt2).largest(Unit::Second),
4230 /// )?;
4231 /// assert_eq!(span.to_string(), "PT211079760S");
4232 ///
4233 /// # Ok::<(), Box<dyn std::error::Error>>(())
4234 /// ```
4235 #[inline]
4236 pub fn largest(self, unit: Unit) -> ZonedDifference<'a> {
4237 ZonedDifference { round: self.round.largest(unit), ..self }
4238 }
4239
4240 /// Set the rounding mode.
4241 ///
4242 /// This defaults to [`RoundMode::Trunc`] since it's plausible that
4243 /// rounding "up" in the context of computing the span between
4244 /// two zoned datetimes could be surprising in a number of cases. The
4245 /// [`RoundMode::HalfExpand`] mode corresponds to typical rounding you
4246 /// might have learned about in school. But a variety of other rounding
4247 /// modes exist.
4248 ///
4249 /// # Example
4250 ///
4251 /// This shows how to always round "up" towards positive infinity.
4252 ///
4253 /// ```
4254 /// use jiff::{RoundMode, ToSpan, Unit, Zoned, ZonedDifference};
4255 ///
4256 /// let zdt1 = "2024-03-15 08:10[America/New_York]".parse::<Zoned>()?;
4257 /// let zdt2 = "2024-03-15 08:11[America/New_York]".parse::<Zoned>()?;
4258 /// let span = zdt1.until(
4259 /// ZonedDifference::new(&zdt2)
4260 /// .smallest(Unit::Hour)
4261 /// .mode(RoundMode::Ceil),
4262 /// )?;
4263 /// // Only one minute elapsed, but we asked to always round up!
4264 /// assert_eq!(span, 1.hour().fieldwise());
4265 ///
4266 /// // Since `Ceil` always rounds toward positive infinity, the behavior
4267 /// // flips for a negative span.
4268 /// let span = zdt1.since(
4269 /// ZonedDifference::new(&zdt2)
4270 /// .smallest(Unit::Hour)
4271 /// .mode(RoundMode::Ceil),
4272 /// )?;
4273 /// assert_eq!(span, 0.hour().fieldwise());
4274 ///
4275 /// # Ok::<(), Box<dyn std::error::Error>>(())
4276 /// ```
4277 #[inline]
4278 pub fn mode(self, mode: RoundMode) -> ZonedDifference<'a> {
4279 ZonedDifference { round: self.round.mode(mode), ..self }
4280 }
4281
4282 /// Set the rounding increment for the smallest unit.
4283 ///
4284 /// The default value is `1`. Other values permit rounding the smallest
4285 /// unit to the nearest integer increment specified. For example, if the
4286 /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
4287 /// `30` would result in rounding in increments of a half hour. That is,
4288 /// the only minute value that could result would be `0` or `30`.
4289 ///
4290 /// # Errors
4291 ///
4292 /// When the smallest unit is less than days, the rounding increment must
4293 /// divide evenly into the next highest unit after the smallest unit
4294 /// configured (and must not be equivalent to it). For example, if the
4295 /// smallest unit is [`Unit::Nanosecond`], then *some* of the valid values
4296 /// for the rounding increment are `1`, `2`, `4`, `5`, `100` and `500`.
4297 /// Namely, any integer that divides evenly into `1,000` nanoseconds since
4298 /// there are `1,000` nanoseconds in the next highest unit (microseconds).
4299 ///
4300 /// In all cases, the increment must be greater than zero and less than
4301 /// or equal to `1_000_000_000`.
4302 ///
4303 /// The error will occur when computing the span, and not when setting
4304 /// the increment here.
4305 ///
4306 /// # Example
4307 ///
4308 /// This shows how to round the span between two zoned datetimes to the
4309 /// nearest 5 minute increment.
4310 ///
4311 /// ```
4312 /// use jiff::{RoundMode, ToSpan, Unit, Zoned, ZonedDifference};
4313 ///
4314 /// let zdt1 = "2024-03-15 08:19[America/New_York]".parse::<Zoned>()?;
4315 /// let zdt2 = "2024-03-15 12:52[America/New_York]".parse::<Zoned>()?;
4316 /// let span = zdt1.until(
4317 /// ZonedDifference::new(&zdt2)
4318 /// .smallest(Unit::Minute)
4319 /// .increment(5)
4320 /// .mode(RoundMode::HalfExpand),
4321 /// )?;
4322 /// assert_eq!(format!("{span:#}"), "4h 35m");
4323 ///
4324 /// # Ok::<(), Box<dyn std::error::Error>>(())
4325 /// ```
4326 #[inline]
4327 pub fn increment(self, increment: i64) -> ZonedDifference<'a> {
4328 ZonedDifference { round: self.round.increment(increment), ..self }
4329 }
4330
4331 /// Returns true if and only if this configuration could change the span
4332 /// via rounding.
4333 #[inline]
4334 fn rounding_may_change_span(&self) -> bool {
4335 self.round.rounding_may_change_span()
4336 }
4337
4338 /// Returns the span of time from `dt1` to the datetime in this
4339 /// configuration. The biggest units allowed are determined by the
4340 /// `smallest` and `largest` settings, but defaults to `Unit::Day`.
4341 #[inline]
4342 fn until_with_largest_unit(&self, zdt1: &Zoned) -> Result<Span, Error> {
4343 let zdt2 = self.zoned;
4344
4345 let sign = b::Sign::from_ordinals(zdt2, zdt1);
4346 if sign.is_zero() {
4347 return Ok(Span::new());
4348 }
4349
4350 let largest = self
4351 .round
4352 .get_largest()
4353 .unwrap_or_else(|| self.round.get_smallest().max(Unit::Hour));
4354 if largest < Unit::Day {
4355 return zdt1.timestamp().until((largest, zdt2.timestamp()));
4356 }
4357 if zdt1.time_zone() != zdt2.time_zone() {
4358 return Err(Error::from(E::MismatchTimeZoneUntil { largest }));
4359 }
4360 let tz = zdt1.time_zone();
4361
4362 let (dt1, mut dt2) = (zdt1.datetime(), zdt2.datetime());
4363
4364 let mut day_correct: i32 = 0;
4365 if b::Sign::from_ordinals(dt1.time(), dt2.time()) == sign {
4366 day_correct += 1;
4367 }
4368
4369 let mut mid = dt2
4370 .date()
4371 .checked_add(Span::new().days(day_correct * -sign))
4372 .context(E::AddDays)?
4373 .to_datetime(dt1.time());
4374 let mut zmid: Zoned = mid
4375 .to_zoned(tz.clone())
4376 .context(E::ConvertIntermediateDatetime)?;
4377 if b::Sign::from_ordinals(zdt2, &zmid) == -sign {
4378 if sign.is_negative() {
4379 // FIXME
4380 panic!("this should be an error");
4381 }
4382 day_correct += 1;
4383 mid = dt2
4384 .date()
4385 .checked_add(Span::new().days(day_correct * -sign))
4386 .context(E::AddDays)?
4387 .to_datetime(dt1.time());
4388 zmid = mid
4389 .to_zoned(tz.clone())
4390 .context(E::ConvertIntermediateDatetime)?;
4391 if b::Sign::from_ordinals(zdt2, &zmid) == -sign {
4392 // FIXME
4393 panic!("this should be an error too");
4394 }
4395 }
4396 let remainder =
4397 zdt2.timestamp().as_duration() - zmid.timestamp().as_duration();
4398 dt2 = mid;
4399
4400 let date_span = dt1.date().until((largest, dt2.date()))?;
4401 Ok(Span::from_invariant_duration(Unit::Hour, remainder)
4402 .expect("difference between time always fits in span")
4403 .years(date_span.get_years())
4404 .months(date_span.get_months())
4405 .weeks(date_span.get_weeks())
4406 .days(date_span.get_days()))
4407 }
4408}
4409
4410impl<'a> From<&'a Zoned> for ZonedDifference<'a> {
4411 #[inline]
4412 fn from(zdt: &'a Zoned) -> ZonedDifference<'a> {
4413 ZonedDifference::new(zdt)
4414 }
4415}
4416
4417impl<'a> From<(Unit, &'a Zoned)> for ZonedDifference<'a> {
4418 #[inline]
4419 fn from((largest, zdt): (Unit, &'a Zoned)) -> ZonedDifference<'a> {
4420 ZonedDifference::new(zdt).largest(largest)
4421 }
4422}
4423
4424/// Options for [`Zoned::round`].
4425///
4426/// This type provides a way to configure the rounding of a zoned datetime. In
4427/// particular, `Zoned::round` accepts anything that implements the
4428/// `Into<ZonedRound>` trait. There are some trait implementations that
4429/// therefore make calling `Zoned::round` in some common cases more
4430/// ergonomic:
4431///
4432/// * `From<Unit> for ZonedRound` will construct a rounding
4433/// configuration that rounds to the unit given. Specifically,
4434/// `ZonedRound::new().smallest(unit)`.
4435/// * `From<(Unit, i64)> for ZonedRound` is like the one above, but also
4436/// specifies the rounding increment for [`ZonedRound::increment`].
4437///
4438/// Note that in the default configuration, no rounding occurs.
4439///
4440/// # Example
4441///
4442/// This example shows how to round a zoned datetime to the nearest second:
4443///
4444/// ```
4445/// use jiff::{civil::date, Unit, Zoned};
4446///
4447/// let zdt: Zoned = "2024-06-20 16:24:59.5[America/New_York]".parse()?;
4448/// assert_eq!(
4449/// zdt.round(Unit::Second)?,
4450/// // The second rounds up and causes minutes to increase.
4451/// date(2024, 6, 20).at(16, 25, 0, 0).in_tz("America/New_York")?,
4452/// );
4453///
4454/// # Ok::<(), Box<dyn std::error::Error>>(())
4455/// ```
4456///
4457/// The above makes use of the fact that `Unit` implements
4458/// `Into<ZonedRound>`. If you want to change the rounding mode to, say,
4459/// truncation, then you'll need to construct a `ZonedRound` explicitly
4460/// since there are no convenience `Into` trait implementations for
4461/// [`RoundMode`].
4462///
4463/// ```
4464/// use jiff::{civil::date, RoundMode, Unit, Zoned, ZonedRound};
4465///
4466/// let zdt: Zoned = "2024-06-20 16:24:59.5[America/New_York]".parse()?;
4467/// assert_eq!(
4468/// zdt.round(
4469/// ZonedRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
4470/// )?,
4471/// // The second just gets truncated as if it wasn't there.
4472/// date(2024, 6, 20).at(16, 24, 59, 0).in_tz("America/New_York")?,
4473/// );
4474///
4475/// # Ok::<(), Box<dyn std::error::Error>>(())
4476/// ```
4477#[derive(Clone, Copy, Debug)]
4478pub struct ZonedRound {
4479 round: DateTimeRound,
4480}
4481
4482impl ZonedRound {
4483 /// Create a new default configuration for rounding a [`Zoned`].
4484 #[inline]
4485 pub fn new() -> ZonedRound {
4486 ZonedRound { round: DateTimeRound::new() }
4487 }
4488
4489 /// Set the smallest units allowed in the zoned datetime returned after
4490 /// rounding.
4491 ///
4492 /// Any units below the smallest configured unit will be used, along
4493 /// with the rounding increment and rounding mode, to determine
4494 /// the value of the smallest unit. For example, when rounding
4495 /// `2024-06-20T03:25:30[America/New_York]` to the nearest minute, the `30`
4496 /// second unit will result in rounding the minute unit of `25` up to `26`
4497 /// and zeroing out everything below minutes.
4498 ///
4499 /// This defaults to [`Unit::Nanosecond`].
4500 ///
4501 /// # Errors
4502 ///
4503 /// The smallest units must be no greater than [`Unit::Day`]. And when the
4504 /// smallest unit is `Unit::Day`, the rounding increment must be equal to
4505 /// `1`. Otherwise an error will be returned from [`Zoned::round`].
4506 ///
4507 /// # Example
4508 ///
4509 /// ```
4510 /// use jiff::{civil::date, Unit, ZonedRound};
4511 ///
4512 /// let zdt = date(2024, 6, 20).at(3, 25, 30, 0).in_tz("America/New_York")?;
4513 /// assert_eq!(
4514 /// zdt.round(ZonedRound::new().smallest(Unit::Minute))?,
4515 /// date(2024, 6, 20).at(3, 26, 0, 0).in_tz("America/New_York")?,
4516 /// );
4517 /// // Or, utilize the `From<Unit> for ZonedRound` impl:
4518 /// assert_eq!(
4519 /// zdt.round(Unit::Minute)?,
4520 /// date(2024, 6, 20).at(3, 26, 0, 0).in_tz("America/New_York")?,
4521 /// );
4522 ///
4523 /// # Ok::<(), Box<dyn std::error::Error>>(())
4524 /// ```
4525 #[inline]
4526 pub fn smallest(self, unit: Unit) -> ZonedRound {
4527 ZonedRound { round: self.round.smallest(unit) }
4528 }
4529
4530 /// Set the rounding mode.
4531 ///
4532 /// This defaults to [`RoundMode::HalfExpand`], which rounds away from
4533 /// zero. It matches the kind of rounding you might have been taught in
4534 /// school.
4535 ///
4536 /// # Example
4537 ///
4538 /// This shows how to always round zoned datetimes up towards positive
4539 /// infinity.
4540 ///
4541 /// ```
4542 /// use jiff::{civil::date, RoundMode, Unit, Zoned, ZonedRound};
4543 ///
4544 /// let zdt: Zoned = "2024-06-20 03:25:01[America/New_York]".parse()?;
4545 /// assert_eq!(
4546 /// zdt.round(
4547 /// ZonedRound::new()
4548 /// .smallest(Unit::Minute)
4549 /// .mode(RoundMode::Ceil),
4550 /// )?,
4551 /// date(2024, 6, 20).at(3, 26, 0, 0).in_tz("America/New_York")?,
4552 /// );
4553 ///
4554 /// # Ok::<(), Box<dyn std::error::Error>>(())
4555 /// ```
4556 #[inline]
4557 pub fn mode(self, mode: RoundMode) -> ZonedRound {
4558 ZonedRound { round: self.round.mode(mode) }
4559 }
4560
4561 /// Set the rounding increment for the smallest unit.
4562 ///
4563 /// The default value is `1`. Other values permit rounding the smallest
4564 /// unit to the nearest integer increment specified. For example, if the
4565 /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
4566 /// `30` would result in rounding in increments of a half hour. That is,
4567 /// the only minute value that could result would be `0` or `30`.
4568 ///
4569 /// # Errors
4570 ///
4571 /// When the smallest unit is `Unit::Day`, then the rounding increment must
4572 /// be `1` or else [`Zoned::round`] will return an error.
4573 ///
4574 /// For other units, the rounding increment must divide evenly into the
4575 /// next highest unit above the smallest unit set. The rounding increment
4576 /// must also not be equal to the next highest unit. For example, if the
4577 /// smallest unit is [`Unit::Nanosecond`], then *some* of the valid values
4578 /// for the rounding increment are `1`, `2`, `4`, `5`, `100` and `500`.
4579 /// Namely, any integer that divides evenly into `1,000` nanoseconds since
4580 /// there are `1,000` nanoseconds in the next highest unit (microseconds).
4581 ///
4582 /// In all cases, the increment must be greater than zero and less than or
4583 /// equal to `1_000_000_000`.
4584 ///
4585 /// # Example
4586 ///
4587 /// This example shows how to round a zoned datetime to the nearest 10
4588 /// minute increment.
4589 ///
4590 /// ```
4591 /// use jiff::{civil::date, RoundMode, Unit, Zoned, ZonedRound};
4592 ///
4593 /// let zdt: Zoned = "2024-06-20 03:24:59[America/New_York]".parse()?;
4594 /// assert_eq!(
4595 /// zdt.round((Unit::Minute, 10))?,
4596 /// date(2024, 6, 20).at(3, 20, 0, 0).in_tz("America/New_York")?,
4597 /// );
4598 ///
4599 /// # Ok::<(), Box<dyn std::error::Error>>(())
4600 /// ```
4601 #[inline]
4602 pub fn increment(self, increment: i64) -> ZonedRound {
4603 ZonedRound { round: self.round.increment(increment) }
4604 }
4605
4606 /// Does the actual rounding.
4607 ///
4608 /// Most of the work is farmed out to civil datetime rounding.
4609 pub(crate) fn round(&self, zdt: &Zoned) -> Result<Zoned, Error> {
4610 let start = zdt.datetime();
4611 if self.round.get_smallest() == Unit::Day {
4612 return self.round_days(zdt);
4613 }
4614 let end = self.round.round(start)?;
4615 // Like in the ZonedWith API, in order to avoid small changes to clock
4616 // time hitting a 1 hour disambiguation shift, we use offset conflict
4617 // resolution to do our best to "prefer" the offset we already have.
4618 let amb = OffsetConflict::PreferOffset.resolve(
4619 end,
4620 zdt.offset(),
4621 zdt.time_zone().clone(),
4622 )?;
4623 amb.compatible()
4624 }
4625
4626 /// Does rounding when the smallest unit is equal to days. We don't reuse
4627 /// civil datetime rounding for this since the length of a day for a zoned
4628 /// datetime might not be 24 hours.
4629 ///
4630 /// Ref: https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.round
4631 fn round_days(&self, zdt: &Zoned) -> Result<Zoned, Error> {
4632 debug_assert_eq!(self.round.get_smallest(), Unit::Day);
4633
4634 // Rounding by days requires an increment of 1. We just re-use the
4635 // civil datetime rounding checks, which has the same constraint.
4636 Increment::for_datetime(Unit::Day, self.round.get_increment())?;
4637
4638 // FIXME: We should be doing this with a &TimeZone, but will need a
4639 // refactor so that we do zone-aware arithmetic using just a Timestamp
4640 // and a &TimeZone. Fixing just this should just be some minor annoying
4641 // work. The grander refactor is something like an `Unzoned` type, but
4642 // I'm not sure that's really worth it. ---AG
4643 let start = zdt.start_of_day().context(E::FailedStartOfDay)?;
4644 let end = start.tomorrow().context(E::FailedLengthOfDay)?;
4645 // I don't believe this is actually possible, since adding 1 day should
4646 // always advance the underlying timestamp by some amount. On the
4647 // other hand, it's somewhat tricky to reason about this because of the
4648 // impact of time zone transition data on the length of a day. So we
4649 // conservatively report an error here.
4650 //
4651 // (The specific problem is that if `day_length` is zero, then our
4652 // rounding API will panic because it doesn't know what to do with a
4653 // zero increment.)
4654 if start.timestamp() == end.timestamp() {
4655 return Err(Error::from(E::FailedLengthOfDay));
4656 }
4657 let day_length =
4658 end.timestamp().as_duration() - start.timestamp().as_duration();
4659 let progress =
4660 zdt.timestamp().as_duration() - start.timestamp().as_duration();
4661 let rounded =
4662 self.round.get_mode().round_by_duration(progress, day_length)?;
4663 let nanos = start
4664 .timestamp()
4665 .as_duration()
4666 .checked_add(rounded)
4667 .ok_or(E::FailedSpanNanoseconds)?;
4668 Ok(Timestamp::from_duration(nanos)?.to_zoned(zdt.time_zone().clone()))
4669 }
4670}
4671
4672impl Default for ZonedRound {
4673 #[inline]
4674 fn default() -> ZonedRound {
4675 ZonedRound::new()
4676 }
4677}
4678
4679impl From<Unit> for ZonedRound {
4680 #[inline]
4681 fn from(unit: Unit) -> ZonedRound {
4682 ZonedRound::default().smallest(unit)
4683 }
4684}
4685
4686impl From<(Unit, i64)> for ZonedRound {
4687 #[inline]
4688 fn from((unit, increment): (Unit, i64)) -> ZonedRound {
4689 ZonedRound::from(unit).increment(increment)
4690 }
4691}
4692
4693/// A builder for setting the fields on a [`Zoned`].
4694///
4695/// This builder is constructed via [`Zoned::with`].
4696///
4697/// # Example
4698///
4699/// The builder ensures one can chain together the individual components of a
4700/// zoned datetime without it failing at an intermediate step. For example,
4701/// if you had a date of `2024-10-31T00:00:00[America/New_York]` and wanted
4702/// to change both the day and the month, and each setting was validated
4703/// independent of the other, you would need to be careful to set the day first
4704/// and then the month. In some cases, you would need to set the month first
4705/// and then the day!
4706///
4707/// But with the builder, you can set values in any order:
4708///
4709/// ```
4710/// use jiff::civil::date;
4711///
4712/// let zdt1 = date(2024, 10, 31).at(0, 0, 0, 0).in_tz("America/New_York")?;
4713/// let zdt2 = zdt1.with().month(11).day(30).build()?;
4714/// assert_eq!(
4715/// zdt2,
4716/// date(2024, 11, 30).at(0, 0, 0, 0).in_tz("America/New_York")?,
4717/// );
4718///
4719/// let zdt1 = date(2024, 4, 30).at(0, 0, 0, 0).in_tz("America/New_York")?;
4720/// let zdt2 = zdt1.with().day(31).month(7).build()?;
4721/// assert_eq!(
4722/// zdt2,
4723/// date(2024, 7, 31).at(0, 0, 0, 0).in_tz("America/New_York")?,
4724/// );
4725///
4726/// # Ok::<(), Box<dyn std::error::Error>>(())
4727/// ```
4728#[derive(Clone, Debug)]
4729pub struct ZonedWith {
4730 original: Zoned,
4731 datetime_with: DateTimeWith,
4732 offset: Option<Offset>,
4733 disambiguation: Disambiguation,
4734 offset_conflict: OffsetConflict,
4735}
4736
4737impl ZonedWith {
4738 #[inline]
4739 fn new(original: Zoned) -> ZonedWith {
4740 let datetime_with = original.datetime().with();
4741 ZonedWith {
4742 original,
4743 datetime_with,
4744 offset: None,
4745 disambiguation: Disambiguation::default(),
4746 offset_conflict: OffsetConflict::PreferOffset,
4747 }
4748 }
4749
4750 /// Create a new `Zoned` from the fields set on this configuration.
4751 ///
4752 /// An error occurs when the fields combine to an invalid zoned datetime.
4753 ///
4754 /// For any fields not set on this configuration, the values are taken from
4755 /// the [`Zoned`] that originally created this configuration. When no
4756 /// values are set, this routine is guaranteed to succeed and will always
4757 /// return the original zoned datetime without modification.
4758 ///
4759 /// # Example
4760 ///
4761 /// This creates a zoned datetime corresponding to the last day in the year
4762 /// at noon:
4763 ///
4764 /// ```
4765 /// use jiff::civil::date;
4766 ///
4767 /// let zdt = date(2023, 1, 1).at(12, 0, 0, 0).in_tz("America/New_York")?;
4768 /// assert_eq!(
4769 /// zdt.with().day_of_year_no_leap(365).build()?,
4770 /// date(2023, 12, 31).at(12, 0, 0, 0).in_tz("America/New_York")?,
4771 /// );
4772 ///
4773 /// // It also works with leap years for the same input:
4774 /// let zdt = date(2024, 1, 1).at(12, 0, 0, 0).in_tz("America/New_York")?;
4775 /// assert_eq!(
4776 /// zdt.with().day_of_year_no_leap(365).build()?,
4777 /// date(2024, 12, 31).at(12, 0, 0, 0).in_tz("America/New_York")?,
4778 /// );
4779 ///
4780 /// # Ok::<(), Box<dyn std::error::Error>>(())
4781 /// ```
4782 ///
4783 /// # Example: error for invalid zoned datetime
4784 ///
4785 /// If the fields combine to form an invalid datetime, then an error is
4786 /// returned:
4787 ///
4788 /// ```
4789 /// use jiff::civil::date;
4790 ///
4791 /// let zdt = date(2024, 11, 30).at(15, 30, 0, 0).in_tz("America/New_York")?;
4792 /// assert!(zdt.with().day(31).build().is_err());
4793 ///
4794 /// let zdt = date(2024, 2, 29).at(15, 30, 0, 0).in_tz("America/New_York")?;
4795 /// assert!(zdt.with().year(2023).build().is_err());
4796 ///
4797 /// # Ok::<(), Box<dyn std::error::Error>>(())
4798 /// ```
4799 #[inline]
4800 pub fn build(self) -> Result<Zoned, Error> {
4801 let dt = self.datetime_with.build()?;
4802 let (_, _, offset, time_zone) = self.original.into_parts();
4803 let offset = self.offset.unwrap_or(offset);
4804 let ambiguous = self.offset_conflict.resolve(dt, offset, time_zone)?;
4805 ambiguous.disambiguate(self.disambiguation)
4806 }
4807
4808 /// Set the year, month and day fields via the `Date` given.
4809 ///
4810 /// This overrides any previous year, month or day settings.
4811 ///
4812 /// # Example
4813 ///
4814 /// This shows how to create a new zoned datetime with a different date:
4815 ///
4816 /// ```
4817 /// use jiff::civil::date;
4818 ///
4819 /// let zdt1 = date(2005, 11, 5).at(15, 30, 0, 0).in_tz("America/New_York")?;
4820 /// let zdt2 = zdt1.with().date(date(2017, 10, 31)).build()?;
4821 /// // The date changes but the time remains the same.
4822 /// assert_eq!(
4823 /// zdt2,
4824 /// date(2017, 10, 31).at(15, 30, 0, 0).in_tz("America/New_York")?,
4825 /// );
4826 ///
4827 /// # Ok::<(), Box<dyn std::error::Error>>(())
4828 /// ```
4829 #[inline]
4830 pub fn date(self, date: Date) -> ZonedWith {
4831 ZonedWith { datetime_with: self.datetime_with.date(date), ..self }
4832 }
4833
4834 /// Set the hour, minute, second, millisecond, microsecond and nanosecond
4835 /// fields via the `Time` given.
4836 ///
4837 /// This overrides any previous hour, minute, second, millisecond,
4838 /// microsecond, nanosecond or subsecond nanosecond settings.
4839 ///
4840 /// # Example
4841 ///
4842 /// This shows how to create a new zoned datetime with a different time:
4843 ///
4844 /// ```
4845 /// use jiff::civil::{date, time};
4846 ///
4847 /// let zdt1 = date(2005, 11, 5).at(15, 30, 0, 0).in_tz("America/New_York")?;
4848 /// let zdt2 = zdt1.with().time(time(23, 59, 59, 123_456_789)).build()?;
4849 /// // The time changes but the date remains the same.
4850 /// assert_eq!(
4851 /// zdt2,
4852 /// date(2005, 11, 5)
4853 /// .at(23, 59, 59, 123_456_789)
4854 /// .in_tz("America/New_York")?,
4855 /// );
4856 ///
4857 /// # Ok::<(), Box<dyn std::error::Error>>(())
4858 /// ```
4859 #[inline]
4860 pub fn time(self, time: Time) -> ZonedWith {
4861 ZonedWith { datetime_with: self.datetime_with.time(time), ..self }
4862 }
4863
4864 /// Set the year field on a [`Zoned`].
4865 ///
4866 /// One can access this value via [`Zoned::year`].
4867 ///
4868 /// This overrides any previous year settings.
4869 ///
4870 /// # Errors
4871 ///
4872 /// This returns an error when [`ZonedWith::build`] is called if the
4873 /// given year is outside the range `-9999..=9999`. This can also return an
4874 /// error if the resulting date is otherwise invalid.
4875 ///
4876 /// # Example
4877 ///
4878 /// This shows how to create a new zoned datetime with a different year:
4879 ///
4880 /// ```
4881 /// use jiff::civil::date;
4882 ///
4883 /// let zdt1 = date(2005, 11, 5).at(15, 30, 0, 0).in_tz("America/New_York")?;
4884 /// assert_eq!(zdt1.year(), 2005);
4885 /// let zdt2 = zdt1.with().year(2007).build()?;
4886 /// assert_eq!(zdt2.year(), 2007);
4887 ///
4888 /// # Ok::<(), Box<dyn std::error::Error>>(())
4889 /// ```
4890 ///
4891 /// # Example: only changing the year can fail
4892 ///
4893 /// For example, while `2024-02-29T01:30:00[America/New_York]` is valid,
4894 /// `2023-02-29T01:30:00[America/New_York]` is not:
4895 ///
4896 /// ```
4897 /// use jiff::civil::date;
4898 ///
4899 /// let zdt = date(2024, 2, 29).at(1, 30, 0, 0).in_tz("America/New_York")?;
4900 /// assert!(zdt.with().year(2023).build().is_err());
4901 ///
4902 /// # Ok::<(), Box<dyn std::error::Error>>(())
4903 /// ```
4904 #[inline]
4905 pub fn year(self, year: i16) -> ZonedWith {
4906 ZonedWith { datetime_with: self.datetime_with.year(year), ..self }
4907 }
4908
4909 /// Set the year of a zoned datetime via its era and its non-negative
4910 /// numeric component.
4911 ///
4912 /// One can access this value via [`Zoned::era_year`].
4913 ///
4914 /// # Errors
4915 ///
4916 /// This returns an error when [`ZonedWith::build`] is called if the
4917 /// year is outside the range for the era specified. For [`Era::BCE`], the
4918 /// range is `1..=10000`. For [`Era::CE`], the range is `1..=9999`.
4919 ///
4920 /// # Example
4921 ///
4922 /// This shows that `CE` years are equivalent to the years used by this
4923 /// crate:
4924 ///
4925 /// ```
4926 /// use jiff::civil::{Era, date};
4927 ///
4928 /// let zdt1 = date(2005, 11, 5).at(8, 0, 0, 0).in_tz("America/New_York")?;
4929 /// assert_eq!(zdt1.year(), 2005);
4930 /// let zdt2 = zdt1.with().era_year(2007, Era::CE).build()?;
4931 /// assert_eq!(zdt2.year(), 2007);
4932 ///
4933 /// // CE years are always positive and can be at most 9999:
4934 /// assert!(zdt1.with().era_year(-5, Era::CE).build().is_err());
4935 /// assert!(zdt1.with().era_year(10_000, Era::CE).build().is_err());
4936 ///
4937 /// # Ok::<(), Box<dyn std::error::Error>>(())
4938 /// ```
4939 ///
4940 /// But `BCE` years always correspond to years less than or equal to `0`
4941 /// in this crate:
4942 ///
4943 /// ```
4944 /// use jiff::civil::{Era, date};
4945 ///
4946 /// let zdt1 = date(-27, 7, 1).at(8, 22, 30, 0).in_tz("America/New_York")?;
4947 /// assert_eq!(zdt1.year(), -27);
4948 /// assert_eq!(zdt1.era_year(), (28, Era::BCE));
4949 ///
4950 /// let zdt2 = zdt1.with().era_year(509, Era::BCE).build()?;
4951 /// assert_eq!(zdt2.year(), -508);
4952 /// assert_eq!(zdt2.era_year(), (509, Era::BCE));
4953 ///
4954 /// let zdt2 = zdt1.with().era_year(10_000, Era::BCE).build()?;
4955 /// assert_eq!(zdt2.year(), -9_999);
4956 /// assert_eq!(zdt2.era_year(), (10_000, Era::BCE));
4957 ///
4958 /// // BCE years are always positive and can be at most 10000:
4959 /// assert!(zdt1.with().era_year(-5, Era::BCE).build().is_err());
4960 /// assert!(zdt1.with().era_year(10_001, Era::BCE).build().is_err());
4961 ///
4962 /// # Ok::<(), Box<dyn std::error::Error>>(())
4963 /// ```
4964 ///
4965 /// # Example: overrides `ZonedWith::year`
4966 ///
4967 /// Setting this option will override any previous `ZonedWith::year`
4968 /// option:
4969 ///
4970 /// ```
4971 /// use jiff::civil::{Era, date};
4972 ///
4973 /// let zdt1 = date(2024, 7, 2).at(10, 27, 10, 123).in_tz("America/New_York")?;
4974 /// let zdt2 = zdt1.with().year(2000).era_year(1900, Era::CE).build()?;
4975 /// assert_eq!(
4976 /// zdt2,
4977 /// date(1900, 7, 2).at(10, 27, 10, 123).in_tz("America/New_York")?,
4978 /// );
4979 ///
4980 /// # Ok::<(), Box<dyn std::error::Error>>(())
4981 /// ```
4982 ///
4983 /// Similarly, `ZonedWith::year` will override any previous call to
4984 /// `ZonedWith::era_year`:
4985 ///
4986 /// ```
4987 /// use jiff::civil::{Era, date};
4988 ///
4989 /// let zdt1 = date(2024, 7, 2).at(19, 0, 1, 1).in_tz("America/New_York")?;
4990 /// let zdt2 = zdt1.with().era_year(1900, Era::CE).year(2000).build()?;
4991 /// assert_eq!(
4992 /// zdt2,
4993 /// date(2000, 7, 2).at(19, 0, 1, 1).in_tz("America/New_York")?,
4994 /// );
4995 ///
4996 /// # Ok::<(), Box<dyn std::error::Error>>(())
4997 /// ```
4998 #[inline]
4999 pub fn era_year(self, year: i16, era: Era) -> ZonedWith {
5000 ZonedWith {
5001 datetime_with: self.datetime_with.era_year(year, era),
5002 ..self
5003 }
5004 }
5005
5006 /// Set the month field on a [`Zoned`].
5007 ///
5008 /// One can access this value via [`Zoned::month`].
5009 ///
5010 /// This overrides any previous month settings.
5011 ///
5012 /// # Errors
5013 ///
5014 /// This returns an error when [`ZonedWith::build`] is called if the
5015 /// given month is outside the range `1..=12`. This can also return an
5016 /// error if the resulting date is otherwise invalid.
5017 ///
5018 /// # Example
5019 ///
5020 /// This shows how to create a new zoned datetime with a different month:
5021 ///
5022 /// ```
5023 /// use jiff::civil::date;
5024 ///
5025 /// let zdt1 = date(2005, 11, 5)
5026 /// .at(18, 3, 59, 123_456_789)
5027 /// .in_tz("America/New_York")?;
5028 /// assert_eq!(zdt1.month(), 11);
5029 ///
5030 /// let zdt2 = zdt1.with().month(6).build()?;
5031 /// assert_eq!(zdt2.month(), 6);
5032 ///
5033 /// # Ok::<(), Box<dyn std::error::Error>>(())
5034 /// ```
5035 ///
5036 /// # Example: only changing the month can fail
5037 ///
5038 /// For example, while `2024-10-31T00:00:00[America/New_York]` is valid,
5039 /// `2024-11-31T00:00:00[America/New_York]` is not:
5040 ///
5041 /// ```
5042 /// use jiff::civil::date;
5043 ///
5044 /// let zdt = date(2024, 10, 31).at(0, 0, 0, 0).in_tz("America/New_York")?;
5045 /// assert!(zdt.with().month(11).build().is_err());
5046 ///
5047 /// # Ok::<(), Box<dyn std::error::Error>>(())
5048 /// ```
5049 #[inline]
5050 pub fn month(self, month: i8) -> ZonedWith {
5051 ZonedWith { datetime_with: self.datetime_with.month(month), ..self }
5052 }
5053
5054 /// Set the day field on a [`Zoned`].
5055 ///
5056 /// One can access this value via [`Zoned::day`].
5057 ///
5058 /// This overrides any previous day settings.
5059 ///
5060 /// # Errors
5061 ///
5062 /// This returns an error when [`ZonedWith::build`] is called if the
5063 /// given given day is outside of allowable days for the corresponding year
5064 /// and month fields.
5065 ///
5066 /// # Example
5067 ///
5068 /// This shows some examples of setting the day, including a leap day:
5069 ///
5070 /// ```
5071 /// use jiff::civil::date;
5072 ///
5073 /// let zdt1 = date(2024, 2, 5).at(21, 59, 1, 999).in_tz("America/New_York")?;
5074 /// assert_eq!(zdt1.day(), 5);
5075 /// let zdt2 = zdt1.with().day(10).build()?;
5076 /// assert_eq!(zdt2.day(), 10);
5077 /// let zdt3 = zdt1.with().day(29).build()?;
5078 /// assert_eq!(zdt3.day(), 29);
5079 ///
5080 /// # Ok::<(), Box<dyn std::error::Error>>(())
5081 /// ```
5082 ///
5083 /// # Example: changing only the day can fail
5084 ///
5085 /// This shows some examples that will fail:
5086 ///
5087 /// ```
5088 /// use jiff::civil::date;
5089 ///
5090 /// let zdt1 = date(2023, 2, 5)
5091 /// .at(22, 58, 58, 9_999)
5092 /// .in_tz("America/New_York")?;
5093 /// // 2023 is not a leap year
5094 /// assert!(zdt1.with().day(29).build().is_err());
5095 ///
5096 /// // September has 30 days, not 31.
5097 /// let zdt1 = date(2023, 9, 5).in_tz("America/New_York")?;
5098 /// assert!(zdt1.with().day(31).build().is_err());
5099 ///
5100 /// # Ok::<(), Box<dyn std::error::Error>>(())
5101 /// ```
5102 #[inline]
5103 pub fn day(self, day: i8) -> ZonedWith {
5104 ZonedWith { datetime_with: self.datetime_with.day(day), ..self }
5105 }
5106
5107 /// Set the day field on a [`Zoned`] via the ordinal number of a day
5108 /// within a year.
5109 ///
5110 /// When used, any settings for month are ignored since the month is
5111 /// determined by the day of the year.
5112 ///
5113 /// The valid values for `day` are `1..=366`. Note though that `366` is
5114 /// only valid for leap years.
5115 ///
5116 /// This overrides any previous day settings.
5117 ///
5118 /// # Errors
5119 ///
5120 /// This returns an error when [`ZonedWith::build`] is called if the
5121 /// given day is outside the allowed range of `1..=366`, or when a value of
5122 /// `366` is given for a non-leap year.
5123 ///
5124 /// # Example
5125 ///
5126 /// This demonstrates that if a year is a leap year, then `60` corresponds
5127 /// to February 29:
5128 ///
5129 /// ```
5130 /// use jiff::civil::date;
5131 ///
5132 /// let zdt = date(2024, 1, 1)
5133 /// .at(23, 59, 59, 999_999_999)
5134 /// .in_tz("America/New_York")?;
5135 /// assert_eq!(
5136 /// zdt.with().day_of_year(60).build()?,
5137 /// date(2024, 2, 29)
5138 /// .at(23, 59, 59, 999_999_999)
5139 /// .in_tz("America/New_York")?,
5140 /// );
5141 ///
5142 /// # Ok::<(), Box<dyn std::error::Error>>(())
5143 /// ```
5144 ///
5145 /// But for non-leap years, day 60 is March 1:
5146 ///
5147 /// ```
5148 /// use jiff::civil::date;
5149 ///
5150 /// let zdt = date(2023, 1, 1)
5151 /// .at(23, 59, 59, 999_999_999)
5152 /// .in_tz("America/New_York")?;
5153 /// assert_eq!(
5154 /// zdt.with().day_of_year(60).build()?,
5155 /// date(2023, 3, 1)
5156 /// .at(23, 59, 59, 999_999_999)
5157 /// .in_tz("America/New_York")?,
5158 /// );
5159 ///
5160 /// # Ok::<(), Box<dyn std::error::Error>>(())
5161 /// ```
5162 ///
5163 /// And using `366` for a non-leap year will result in an error, since
5164 /// non-leap years only have 365 days:
5165 ///
5166 /// ```
5167 /// use jiff::civil::date;
5168 ///
5169 /// let zdt = date(2023, 1, 1).at(0, 0, 0, 0).in_tz("America/New_York")?;
5170 /// assert!(zdt.with().day_of_year(366).build().is_err());
5171 /// // The maximal year is not a leap year, so it returns an error too.
5172 /// let zdt = date(9999, 1, 1).at(0, 0, 0, 0).in_tz("America/New_York")?;
5173 /// assert!(zdt.with().day_of_year(366).build().is_err());
5174 ///
5175 /// # Ok::<(), Box<dyn std::error::Error>>(())
5176 /// ```
5177 #[inline]
5178 pub fn day_of_year(self, day: i16) -> ZonedWith {
5179 ZonedWith {
5180 datetime_with: self.datetime_with.day_of_year(day),
5181 ..self
5182 }
5183 }
5184
5185 /// Set the day field on a [`Zoned`] via the ordinal number of a day
5186 /// within a year, but ignoring leap years.
5187 ///
5188 /// When used, any settings for month are ignored since the month is
5189 /// determined by the day of the year.
5190 ///
5191 /// The valid values for `day` are `1..=365`. The value `365` always
5192 /// corresponds to the last day of the year, even for leap years. It is
5193 /// impossible for this routine to return a zoned datetime corresponding to
5194 /// February 29. (Unless there is a relevant time zone transition that
5195 /// provokes disambiguation that shifts the datetime into February 29.)
5196 ///
5197 /// This overrides any previous day settings.
5198 ///
5199 /// # Errors
5200 ///
5201 /// This returns an error when [`ZonedWith::build`] is called if the
5202 /// given day is outside the allowed range of `1..=365`.
5203 ///
5204 /// # Example
5205 ///
5206 /// This demonstrates that `60` corresponds to March 1, regardless of
5207 /// whether the year is a leap year or not:
5208 ///
5209 /// ```
5210 /// use jiff::civil::date;
5211 ///
5212 /// let zdt = date(2023, 1, 1)
5213 /// .at(23, 59, 59, 999_999_999)
5214 /// .in_tz("America/New_York")?;
5215 /// assert_eq!(
5216 /// zdt.with().day_of_year_no_leap(60).build()?,
5217 /// date(2023, 3, 1)
5218 /// .at(23, 59, 59, 999_999_999)
5219 /// .in_tz("America/New_York")?,
5220 /// );
5221 ///
5222 /// let zdt = date(2024, 1, 1)
5223 /// .at(23, 59, 59, 999_999_999)
5224 /// .in_tz("America/New_York")?;
5225 /// assert_eq!(
5226 /// zdt.with().day_of_year_no_leap(60).build()?,
5227 /// date(2024, 3, 1)
5228 /// .at(23, 59, 59, 999_999_999)
5229 /// .in_tz("America/New_York")?,
5230 /// );
5231 ///
5232 /// # Ok::<(), Box<dyn std::error::Error>>(())
5233 /// ```
5234 ///
5235 /// And using `365` for any year will always yield the last day of the
5236 /// year:
5237 ///
5238 /// ```
5239 /// use jiff::civil::date;
5240 ///
5241 /// let zdt = date(2023, 1, 1)
5242 /// .at(23, 59, 59, 999_999_999)
5243 /// .in_tz("America/New_York")?;
5244 /// assert_eq!(
5245 /// zdt.with().day_of_year_no_leap(365).build()?,
5246 /// zdt.last_of_year()?,
5247 /// );
5248 ///
5249 /// let zdt = date(2024, 1, 1)
5250 /// .at(23, 59, 59, 999_999_999)
5251 /// .in_tz("America/New_York")?;
5252 /// assert_eq!(
5253 /// zdt.with().day_of_year_no_leap(365).build()?,
5254 /// zdt.last_of_year()?,
5255 /// );
5256 ///
5257 /// // Careful at the boundaries. The last day of the year isn't
5258 /// // representable with all time zones. For example:
5259 /// let zdt = date(9999, 1, 1)
5260 /// .at(23, 59, 59, 999_999_999)
5261 /// .in_tz("America/New_York")?;
5262 /// assert!(zdt.with().day_of_year_no_leap(365).build().is_err());
5263 /// // But with other time zones, it works okay:
5264 /// let zdt = date(9999, 1, 1)
5265 /// .at(23, 59, 59, 999_999_999)
5266 /// .to_zoned(jiff::tz::TimeZone::fixed(jiff::tz::Offset::MAX))?;
5267 /// assert_eq!(
5268 /// zdt.with().day_of_year_no_leap(365).build()?,
5269 /// zdt.last_of_year()?,
5270 /// );
5271 ///
5272 /// # Ok::<(), Box<dyn std::error::Error>>(())
5273 /// ```
5274 ///
5275 /// A value of `366` is out of bounds, even for leap years:
5276 ///
5277 /// ```
5278 /// use jiff::civil::date;
5279 ///
5280 /// let zdt = date(2024, 1, 1).at(5, 30, 0, 0).in_tz("America/New_York")?;
5281 /// assert!(zdt.with().day_of_year_no_leap(366).build().is_err());
5282 ///
5283 /// # Ok::<(), Box<dyn std::error::Error>>(())
5284 /// ```
5285 #[inline]
5286 pub fn day_of_year_no_leap(self, day: i16) -> ZonedWith {
5287 ZonedWith {
5288 datetime_with: self.datetime_with.day_of_year_no_leap(day),
5289 ..self
5290 }
5291 }
5292
5293 /// Set the hour field on a [`Zoned`].
5294 ///
5295 /// One can access this value via [`Zoned::hour`].
5296 ///
5297 /// This overrides any previous hour settings.
5298 ///
5299 /// # Errors
5300 ///
5301 /// This returns an error when [`ZonedWith::build`] is called if the
5302 /// given hour is outside the range `0..=23`.
5303 ///
5304 /// # Example
5305 ///
5306 /// ```
5307 /// use jiff::civil::time;
5308 ///
5309 /// let zdt1 = time(15, 21, 59, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5310 /// assert_eq!(zdt1.hour(), 15);
5311 /// let zdt2 = zdt1.with().hour(3).build()?;
5312 /// assert_eq!(zdt2.hour(), 3);
5313 ///
5314 /// # Ok::<(), Box<dyn std::error::Error>>(())
5315 /// ```
5316 #[inline]
5317 pub fn hour(self, hour: i8) -> ZonedWith {
5318 ZonedWith { datetime_with: self.datetime_with.hour(hour), ..self }
5319 }
5320
5321 /// Set the minute field on a [`Zoned`].
5322 ///
5323 /// One can access this value via [`Zoned::minute`].
5324 ///
5325 /// This overrides any previous minute settings.
5326 ///
5327 /// # Errors
5328 ///
5329 /// This returns an error when [`ZonedWith::build`] is called if the
5330 /// given minute is outside the range `0..=59`.
5331 ///
5332 /// # Example
5333 ///
5334 /// ```
5335 /// use jiff::civil::time;
5336 ///
5337 /// let zdt1 = time(15, 21, 59, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5338 /// assert_eq!(zdt1.minute(), 21);
5339 /// let zdt2 = zdt1.with().minute(3).build()?;
5340 /// assert_eq!(zdt2.minute(), 3);
5341 ///
5342 /// # Ok::<(), Box<dyn std::error::Error>>(())
5343 /// ```
5344 #[inline]
5345 pub fn minute(self, minute: i8) -> ZonedWith {
5346 ZonedWith { datetime_with: self.datetime_with.minute(minute), ..self }
5347 }
5348
5349 /// Set the second field on a [`Zoned`].
5350 ///
5351 /// One can access this value via [`Zoned::second`].
5352 ///
5353 /// This overrides any previous second settings.
5354 ///
5355 /// # Errors
5356 ///
5357 /// This returns an error when [`ZonedWith::build`] is called if the
5358 /// given second is outside the range `0..=59`.
5359 ///
5360 /// # Example
5361 ///
5362 /// ```
5363 /// use jiff::civil::time;
5364 ///
5365 /// let zdt1 = time(15, 21, 59, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5366 /// assert_eq!(zdt1.second(), 59);
5367 /// let zdt2 = zdt1.with().second(3).build()?;
5368 /// assert_eq!(zdt2.second(), 3);
5369 ///
5370 /// # Ok::<(), Box<dyn std::error::Error>>(())
5371 /// ```
5372 #[inline]
5373 pub fn second(self, second: i8) -> ZonedWith {
5374 ZonedWith { datetime_with: self.datetime_with.second(second), ..self }
5375 }
5376
5377 /// Set the millisecond field on a [`Zoned`].
5378 ///
5379 /// One can access this value via [`Zoned::millisecond`].
5380 ///
5381 /// This overrides any previous millisecond settings.
5382 ///
5383 /// Note that this only sets the millisecond component. It does
5384 /// not change the microsecond or nanosecond components. To set
5385 /// the fractional second component to nanosecond precision, use
5386 /// [`ZonedWith::subsec_nanosecond`].
5387 ///
5388 /// # Errors
5389 ///
5390 /// This returns an error when [`ZonedWith::build`] is called if the
5391 /// given millisecond is outside the range `0..=999`, or if both this and
5392 /// [`ZonedWith::subsec_nanosecond`] are set.
5393 ///
5394 /// # Example
5395 ///
5396 /// This shows the relationship between [`Zoned::millisecond`] and
5397 /// [`Zoned::subsec_nanosecond`]:
5398 ///
5399 /// ```
5400 /// use jiff::civil::time;
5401 ///
5402 /// let zdt1 = time(15, 21, 35, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5403 /// let zdt2 = zdt1.with().millisecond(123).build()?;
5404 /// assert_eq!(zdt2.subsec_nanosecond(), 123_000_000);
5405 ///
5406 /// # Ok::<(), Box<dyn std::error::Error>>(())
5407 /// ```
5408 #[inline]
5409 pub fn millisecond(self, millisecond: i16) -> ZonedWith {
5410 ZonedWith {
5411 datetime_with: self.datetime_with.millisecond(millisecond),
5412 ..self
5413 }
5414 }
5415
5416 /// Set the microsecond field on a [`Zoned`].
5417 ///
5418 /// One can access this value via [`Zoned::microsecond`].
5419 ///
5420 /// This overrides any previous microsecond settings.
5421 ///
5422 /// Note that this only sets the microsecond component. It does
5423 /// not change the millisecond or nanosecond components. To set
5424 /// the fractional second component to nanosecond precision, use
5425 /// [`ZonedWith::subsec_nanosecond`].
5426 ///
5427 /// # Errors
5428 ///
5429 /// This returns an error when [`ZonedWith::build`] is called if the
5430 /// given microsecond is outside the range `0..=999`, or if both this and
5431 /// [`ZonedWith::subsec_nanosecond`] are set.
5432 ///
5433 /// # Example
5434 ///
5435 /// This shows the relationship between [`Zoned::microsecond`] and
5436 /// [`Zoned::subsec_nanosecond`]:
5437 ///
5438 /// ```
5439 /// use jiff::civil::time;
5440 ///
5441 /// let zdt1 = time(15, 21, 35, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5442 /// let zdt2 = zdt1.with().microsecond(123).build()?;
5443 /// assert_eq!(zdt2.subsec_nanosecond(), 123_000);
5444 ///
5445 /// # Ok::<(), Box<dyn std::error::Error>>(())
5446 /// ```
5447 #[inline]
5448 pub fn microsecond(self, microsecond: i16) -> ZonedWith {
5449 ZonedWith {
5450 datetime_with: self.datetime_with.microsecond(microsecond),
5451 ..self
5452 }
5453 }
5454
5455 /// Set the nanosecond field on a [`Zoned`].
5456 ///
5457 /// One can access this value via [`Zoned::nanosecond`].
5458 ///
5459 /// This overrides any previous nanosecond settings.
5460 ///
5461 /// Note that this only sets the nanosecond component. It does
5462 /// not change the millisecond or microsecond components. To set
5463 /// the fractional second component to nanosecond precision, use
5464 /// [`ZonedWith::subsec_nanosecond`].
5465 ///
5466 /// # Errors
5467 ///
5468 /// This returns an error when [`ZonedWith::build`] is called if the
5469 /// given nanosecond is outside the range `0..=999`, or if both this and
5470 /// [`ZonedWith::subsec_nanosecond`] are set.
5471 ///
5472 /// # Example
5473 ///
5474 /// This shows the relationship between [`Zoned::nanosecond`] and
5475 /// [`Zoned::subsec_nanosecond`]:
5476 ///
5477 /// ```
5478 /// use jiff::civil::time;
5479 ///
5480 /// let zdt1 = time(15, 21, 35, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5481 /// let zdt2 = zdt1.with().nanosecond(123).build()?;
5482 /// assert_eq!(zdt2.subsec_nanosecond(), 123);
5483 ///
5484 /// # Ok::<(), Box<dyn std::error::Error>>(())
5485 /// ```
5486 #[inline]
5487 pub fn nanosecond(self, nanosecond: i16) -> ZonedWith {
5488 ZonedWith {
5489 datetime_with: self.datetime_with.nanosecond(nanosecond),
5490 ..self
5491 }
5492 }
5493
5494 /// Set the subsecond nanosecond field on a [`Zoned`].
5495 ///
5496 /// If you want to access this value on `Zoned`, then use
5497 /// [`Zoned::subsec_nanosecond`].
5498 ///
5499 /// This overrides any previous subsecond nanosecond settings.
5500 ///
5501 /// Note that this sets the entire fractional second component to
5502 /// nanosecond precision, and overrides any individual millisecond,
5503 /// microsecond or nanosecond settings. To set individual components,
5504 /// use [`ZonedWith::millisecond`], [`ZonedWith::microsecond`] or
5505 /// [`ZonedWith::nanosecond`].
5506 ///
5507 /// # Errors
5508 ///
5509 /// This returns an error when [`ZonedWith::build`] is called if the
5510 /// given subsecond nanosecond is outside the range `0..=999,999,999`,
5511 /// or if both this and one of [`ZonedWith::millisecond`],
5512 /// [`ZonedWith::microsecond`] or [`ZonedWith::nanosecond`] are set.
5513 ///
5514 /// # Example
5515 ///
5516 /// This shows the relationship between constructing a `Zoned` value
5517 /// with subsecond nanoseconds and its individual subsecond fields:
5518 ///
5519 /// ```
5520 /// use jiff::civil::time;
5521 ///
5522 /// let zdt1 = time(15, 21, 35, 0).on(2010, 6, 1).in_tz("America/New_York")?;
5523 /// let zdt2 = zdt1.with().subsec_nanosecond(123_456_789).build()?;
5524 /// assert_eq!(zdt2.millisecond(), 123);
5525 /// assert_eq!(zdt2.microsecond(), 456);
5526 /// assert_eq!(zdt2.nanosecond(), 789);
5527 ///
5528 /// # Ok::<(), Box<dyn std::error::Error>>(())
5529 /// ```
5530 #[inline]
5531 pub fn subsec_nanosecond(self, subsec_nanosecond: i32) -> ZonedWith {
5532 ZonedWith {
5533 datetime_with: self
5534 .datetime_with
5535 .subsec_nanosecond(subsec_nanosecond),
5536 ..self
5537 }
5538 }
5539
5540 /// Set the offset to use in the new zoned datetime.
5541 ///
5542 /// This can be used in some cases to explicitly disambiguate a datetime
5543 /// that could correspond to multiple instants in time.
5544 ///
5545 /// How the offset is used to construct a new zoned datetime
5546 /// depends on the offset conflict resolution strategy
5547 /// set via [`ZonedWith::offset_conflict`]. The default is
5548 /// [`OffsetConflict::PreferOffset`], which will always try to use the
5549 /// offset to resolve a datetime to an instant, unless the offset is
5550 /// incorrect for this zoned datetime's time zone. In which case, only the
5551 /// time zone is used to select the correct offset (which may involve using
5552 /// the disambiguation strategy set via [`ZonedWith::disambiguation`]).
5553 ///
5554 /// # Example
5555 ///
5556 /// This example shows parsing the first time the 1 o'clock hour appeared
5557 /// on a clock in New York on 2024-11-03, and then changing only the
5558 /// offset to flip it to the second time 1 o'clock appeared on the clock:
5559 ///
5560 /// ```
5561 /// use jiff::{tz, Zoned};
5562 ///
5563 /// let zdt1: Zoned = "2024-11-03 01:30-04[America/New_York]".parse()?;
5564 /// let zdt2 = zdt1.with().offset(tz::offset(-5)).build()?;
5565 /// assert_eq!(
5566 /// zdt2.to_string(),
5567 /// // Everything stays the same, except for the offset.
5568 /// "2024-11-03T01:30:00-05:00[America/New_York]",
5569 /// );
5570 ///
5571 /// // If we use an invalid offset for the America/New_York time zone,
5572 /// // then it will be ignored and the disambiguation strategy set will
5573 /// // be used.
5574 /// let zdt3 = zdt1.with().offset(tz::offset(-12)).build()?;
5575 /// assert_eq!(
5576 /// zdt3.to_string(),
5577 /// // The default disambiguation is Compatible.
5578 /// "2024-11-03T01:30:00-04:00[America/New_York]",
5579 /// );
5580 /// // But we could change the disambiguation strategy to reject such
5581 /// // cases!
5582 /// let result = zdt1
5583 /// .with()
5584 /// .offset(tz::offset(-12))
5585 /// .disambiguation(tz::Disambiguation::Reject)
5586 /// .build();
5587 /// assert!(result.is_err());
5588 ///
5589 /// # Ok::<(), Box<dyn std::error::Error>>(())
5590 /// ```
5591 #[inline]
5592 pub fn offset(self, offset: Offset) -> ZonedWith {
5593 ZonedWith { offset: Some(offset), ..self }
5594 }
5595
5596 /// Set the conflict resolution strategy for when an offset is inconsistent
5597 /// with the time zone.
5598 ///
5599 /// See the documentation on [`OffsetConflict`] for more details about the
5600 /// different strategies one can choose.
5601 ///
5602 /// Unlike parsing (where the default is `OffsetConflict::Reject`), the
5603 /// default for `ZonedWith` is [`OffsetConflict::PreferOffset`], which
5604 /// avoids daylight saving time disambiguation causing unexpected 1-hour
5605 /// shifts after small changes to clock time.
5606 ///
5607 /// # Example
5608 ///
5609 /// ```
5610 /// use jiff::Zoned;
5611 ///
5612 /// // Set to the "second" time 1:30 is on the clocks in New York on
5613 /// // 2024-11-03. The offset in the datetime string makes this
5614 /// // unambiguous.
5615 /// let zdt1 = "2024-11-03T01:30-05[America/New_York]".parse::<Zoned>()?;
5616 /// // Now we change the minute field:
5617 /// let zdt2 = zdt1.with().minute(34).build()?;
5618 /// assert_eq!(
5619 /// zdt2.to_string(),
5620 /// // Without taking the offset of the `Zoned` value into account,
5621 /// // this would have defaulted to using the "compatible"
5622 /// // disambiguation strategy, which would have selected the earlier
5623 /// // offset of -04 instead of sticking with the later offset of -05.
5624 /// "2024-11-03T01:34:00-05:00[America/New_York]",
5625 /// );
5626 ///
5627 /// // But note that if we change the clock time such that the previous
5628 /// // offset is no longer valid (by moving back before DST ended), then
5629 /// // the default strategy will automatically adapt and change the offset.
5630 /// let zdt2 = zdt1.with().hour(0).build()?;
5631 /// assert_eq!(
5632 /// zdt2.to_string(),
5633 /// "2024-11-03T00:30:00-04:00[America/New_York]",
5634 /// );
5635 ///
5636 /// # Ok::<(), Box<dyn std::error::Error>>(())
5637 /// ```
5638 #[inline]
5639 pub fn offset_conflict(self, strategy: OffsetConflict) -> ZonedWith {
5640 ZonedWith { offset_conflict: strategy, ..self }
5641 }
5642
5643 /// Set the disambiguation strategy for when a zoned datetime falls into a
5644 /// time zone transition "fold" or "gap."
5645 ///
5646 /// The most common manifestation of such time zone transitions is daylight
5647 /// saving time. In most cases, the transition into daylight saving time
5648 /// moves the civil time ("the time you see on the clock") ahead one hour.
5649 /// This is called a "gap" because an hour on the clock is skipped. While
5650 /// the transition out of daylight saving time moves the civil time back
5651 /// one hour. This is called a "fold" because an hour on the clock is
5652 /// repeated.
5653 ///
5654 /// In the case of a gap, an ambiguous datetime manifests as a time that
5655 /// never appears on a clock. (For example, `02:30` on `2024-03-10` in New
5656 /// York.) In the case of a fold, an ambiguous datetime manifests as a
5657 /// time that repeats itself. (For example, `01:30` on `2024-11-03` in New
5658 /// York.) So when a fold occurs, you don't know whether it's the "first"
5659 /// occurrence of that time or the "second."
5660 ///
5661 /// Time zone transitions are not just limited to daylight saving time,
5662 /// although those are the most common. In other cases, a transition occurs
5663 /// because of a change in the offset of the time zone itself. (See the
5664 /// examples below.)
5665 ///
5666 /// # Example: time zone offset change
5667 ///
5668 /// In this example, we explore a time zone offset change in Hawaii that
5669 /// occurred on `1947-06-08`. Namely, Hawaii went from a `-10:30` offset
5670 /// to a `-10:00` offset at `02:00`. This results in a 30 minute gap in
5671 /// civil time.
5672 ///
5673 /// ```
5674 /// use jiff::{civil::date, tz, ToSpan, Zoned};
5675 ///
5676 /// // This datetime is unambiguous...
5677 /// let zdt1 = "1943-06-02T02:05[Pacific/Honolulu]".parse::<Zoned>()?;
5678 /// // but... 02:05 didn't exist on clocks on 1947-06-08.
5679 /// let zdt2 = zdt1
5680 /// .with()
5681 /// .disambiguation(tz::Disambiguation::Later)
5682 /// .year(1947)
5683 /// .day(8)
5684 /// .build()?;
5685 /// // Our parser is configured to select the later time, so we jump to
5686 /// // 02:35. But if we used `Disambiguation::Earlier`, then we'd get
5687 /// // 01:35.
5688 /// assert_eq!(zdt2.datetime(), date(1947, 6, 8).at(2, 35, 0, 0));
5689 /// assert_eq!(zdt2.offset(), tz::offset(-10));
5690 ///
5691 /// // If we subtract 10 minutes from 02:35, notice that we (correctly)
5692 /// // jump to 01:55 *and* our offset is corrected to -10:30.
5693 /// let zdt3 = zdt2.checked_sub(10.minutes())?;
5694 /// assert_eq!(zdt3.datetime(), date(1947, 6, 8).at(1, 55, 0, 0));
5695 /// assert_eq!(zdt3.offset(), tz::offset(-10).saturating_sub(30.minutes()));
5696 ///
5697 /// # Ok::<(), Box<dyn std::error::Error>>(())
5698 /// ```
5699 ///
5700 /// # Example: offset conflict resolution and disambiguation
5701 ///
5702 /// This example shows how the disambiguation configuration can
5703 /// interact with the default offset conflict resolution strategy of
5704 /// [`OffsetConflict::PreferOffset`]:
5705 ///
5706 /// ```
5707 /// use jiff::{civil::date, tz, Zoned};
5708 ///
5709 /// // This datetime is unambiguous.
5710 /// let zdt1 = "2024-03-11T02:05[America/New_York]".parse::<Zoned>()?;
5711 /// assert_eq!(zdt1.offset(), tz::offset(-4));
5712 /// // But the same time on March 10 is ambiguous because there is a gap!
5713 /// let zdt2 = zdt1
5714 /// .with()
5715 /// .disambiguation(tz::Disambiguation::Earlier)
5716 /// .day(10)
5717 /// .build()?;
5718 /// assert_eq!(zdt2.datetime(), date(2024, 3, 10).at(1, 5, 0, 0));
5719 /// assert_eq!(zdt2.offset(), tz::offset(-5));
5720 ///
5721 /// # Ok::<(), Box<dyn std::error::Error>>(())
5722 /// ```
5723 ///
5724 /// Namely, while we started with an offset of `-04`, it (along with all
5725 /// other offsets) are considered invalid during civil time gaps due to
5726 /// time zone transitions (such as the beginning of daylight saving time in
5727 /// most locations).
5728 ///
5729 /// The default disambiguation strategy is
5730 /// [`Disambiguation::Compatible`], which in the case of gaps, chooses the
5731 /// time after the gap:
5732 ///
5733 /// ```
5734 /// use jiff::{civil::date, tz, Zoned};
5735 ///
5736 /// // This datetime is unambiguous.
5737 /// let zdt1 = "2024-03-11T02:05[America/New_York]".parse::<Zoned>()?;
5738 /// assert_eq!(zdt1.offset(), tz::offset(-4));
5739 /// // But the same time on March 10 is ambiguous because there is a gap!
5740 /// let zdt2 = zdt1
5741 /// .with()
5742 /// .day(10)
5743 /// .build()?;
5744 /// assert_eq!(zdt2.datetime(), date(2024, 3, 10).at(3, 5, 0, 0));
5745 /// assert_eq!(zdt2.offset(), tz::offset(-4));
5746 ///
5747 /// # Ok::<(), Box<dyn std::error::Error>>(())
5748 /// ```
5749 ///
5750 /// Alternatively, one can choose to always respect the offset, and thus
5751 /// civil time for the provided time zone will be adjusted to match the
5752 /// instant prescribed by the offset. In this case, no disambiguation is
5753 /// performed:
5754 ///
5755 /// ```
5756 /// use jiff::{civil::date, tz, Zoned};
5757 ///
5758 /// // This datetime is unambiguous. But `2024-03-10T02:05` is!
5759 /// let zdt1 = "2024-03-11T02:05[America/New_York]".parse::<Zoned>()?;
5760 /// assert_eq!(zdt1.offset(), tz::offset(-4));
5761 /// // But the same time on March 10 is ambiguous because there is a gap!
5762 /// let zdt2 = zdt1
5763 /// .with()
5764 /// .offset_conflict(tz::OffsetConflict::AlwaysOffset)
5765 /// .day(10)
5766 /// .build()?;
5767 /// // Why do we get this result? Because `2024-03-10T02:05-04` is
5768 /// // `2024-03-10T06:05Z`. And in `America/New_York`, the civil time
5769 /// // for that timestamp is `2024-03-10T01:05-05`.
5770 /// assert_eq!(zdt2.datetime(), date(2024, 3, 10).at(1, 5, 0, 0));
5771 /// assert_eq!(zdt2.offset(), tz::offset(-5));
5772 ///
5773 /// # Ok::<(), Box<dyn std::error::Error>>(())
5774 /// ```
5775 #[inline]
5776 pub fn disambiguation(self, strategy: Disambiguation) -> ZonedWith {
5777 ZonedWith { disambiguation: strategy, ..self }
5778 }
5779}
5780
5781#[cfg(test)]
5782mod tests {
5783 use std::io::Cursor;
5784
5785 use alloc::string::ToString;
5786
5787 use crate::{
5788 civil::{date, datetime},
5789 span::span_eq,
5790 tz, ToSpan,
5791 };
5792
5793 use super::*;
5794
5795 #[test]
5796 fn until_with_largest_unit() {
5797 if crate::tz::db().is_definitively_empty() {
5798 return;
5799 }
5800
5801 let zdt1: Zoned = date(1995, 12, 7)
5802 .at(3, 24, 30, 3500)
5803 .in_tz("Asia/Kolkata")
5804 .unwrap();
5805 let zdt2: Zoned =
5806 date(2019, 1, 31).at(15, 30, 0, 0).in_tz("Asia/Kolkata").unwrap();
5807 let span = zdt1.until(&zdt2).unwrap();
5808 span_eq!(
5809 span,
5810 202956
5811 .hours()
5812 .minutes(5)
5813 .seconds(29)
5814 .milliseconds(999)
5815 .microseconds(996)
5816 .nanoseconds(500)
5817 );
5818 let span = zdt1.until((Unit::Year, &zdt2)).unwrap();
5819 span_eq!(
5820 span,
5821 23.years()
5822 .months(1)
5823 .days(24)
5824 .hours(12)
5825 .minutes(5)
5826 .seconds(29)
5827 .milliseconds(999)
5828 .microseconds(996)
5829 .nanoseconds(500)
5830 );
5831
5832 let span = zdt2.until((Unit::Year, &zdt1)).unwrap();
5833 span_eq!(
5834 span,
5835 -23.years()
5836 .months(1)
5837 .days(24)
5838 .hours(12)
5839 .minutes(5)
5840 .seconds(29)
5841 .milliseconds(999)
5842 .microseconds(996)
5843 .nanoseconds(500)
5844 );
5845 let span = zdt1.until((Unit::Nanosecond, &zdt2)).unwrap();
5846 span_eq!(span, 730641929999996500i64.nanoseconds());
5847
5848 let zdt1: Zoned =
5849 date(2020, 1, 1).at(0, 0, 0, 0).in_tz("America/New_York").unwrap();
5850 let zdt2: Zoned = date(2020, 4, 24)
5851 .at(21, 0, 0, 0)
5852 .in_tz("America/New_York")
5853 .unwrap();
5854 let span = zdt1.until(&zdt2).unwrap();
5855 span_eq!(span, 2756.hours());
5856 let span = zdt1.until((Unit::Year, &zdt2)).unwrap();
5857 span_eq!(span, 3.months().days(23).hours(21));
5858
5859 let zdt1: Zoned = date(2000, 10, 29)
5860 .at(0, 0, 0, 0)
5861 .in_tz("America/Vancouver")
5862 .unwrap();
5863 let zdt2: Zoned = date(2000, 10, 29)
5864 .at(23, 0, 0, 5)
5865 .in_tz("America/Vancouver")
5866 .unwrap();
5867 let span = zdt1.until((Unit::Day, &zdt2)).unwrap();
5868 span_eq!(span, 24.hours().nanoseconds(5));
5869 }
5870
5871 #[cfg(target_pointer_width = "64")]
5872 #[test]
5873 fn zoned_size() {
5874 #[cfg(debug_assertions)]
5875 {
5876 #[cfg(feature = "alloc")]
5877 {
5878 assert_eq!(40, core::mem::size_of::<Zoned>());
5879 }
5880 #[cfg(all(target_pointer_width = "64", not(feature = "alloc")))]
5881 {
5882 assert_eq!(40, core::mem::size_of::<Zoned>());
5883 }
5884 }
5885 #[cfg(not(debug_assertions))]
5886 {
5887 #[cfg(feature = "alloc")]
5888 {
5889 assert_eq!(40, core::mem::size_of::<Zoned>());
5890 }
5891 #[cfg(all(target_pointer_width = "64", not(feature = "alloc")))]
5892 {
5893 // This asserts the same value as the alloc value above, but
5894 // it wasn't always this way, which is why it's written out
5895 // separately. Moreover, in theory, I'd be open to regressing
5896 // this value if it led to an improvement in alloc-mode. But
5897 // more likely, it would be nice to decrease this size in
5898 // non-alloc modes.
5899 assert_eq!(40, core::mem::size_of::<Zoned>());
5900 }
5901 }
5902 }
5903
5904 /// A `serde` deserializer compatibility test.
5905 ///
5906 /// Serde YAML used to be unable to deserialize `jiff` types,
5907 /// as deserializing from bytes is not supported by the deserializer.
5908 ///
5909 /// - <https://github.com/BurntSushi/jiff/issues/138>
5910 /// - <https://github.com/BurntSushi/jiff/discussions/148>
5911 #[test]
5912 fn zoned_deserialize_yaml() {
5913 if crate::tz::db().is_definitively_empty() {
5914 return;
5915 }
5916
5917 let expected = datetime(2024, 10, 31, 16, 33, 53, 123456789)
5918 .in_tz("UTC")
5919 .unwrap();
5920
5921 let deserialized: Zoned =
5922 serde_yaml::from_str("2024-10-31T16:33:53.123456789+00:00[UTC]")
5923 .unwrap();
5924
5925 assert_eq!(deserialized, expected);
5926
5927 let deserialized: Zoned = serde_yaml::from_slice(
5928 "2024-10-31T16:33:53.123456789+00:00[UTC]".as_bytes(),
5929 )
5930 .unwrap();
5931
5932 assert_eq!(deserialized, expected);
5933
5934 let cursor = Cursor::new(b"2024-10-31T16:33:53.123456789+00:00[UTC]");
5935 let deserialized: Zoned = serde_yaml::from_reader(cursor).unwrap();
5936
5937 assert_eq!(deserialized, expected);
5938 }
5939
5940 /// This is a regression test for a case where changing a zoned datetime
5941 /// to have a time of midnight ends up producing a counter-intuitive
5942 /// result.
5943 ///
5944 /// See: <https://github.com/BurntSushi/jiff/issues/211>
5945 #[test]
5946 fn zoned_with_time_dst_after_gap() {
5947 if crate::tz::db().is_definitively_empty() {
5948 return;
5949 }
5950
5951 let zdt1: Zoned = "2024-03-31T12:00[Atlantic/Azores]".parse().unwrap();
5952 assert_eq!(
5953 zdt1.to_string(),
5954 "2024-03-31T12:00:00+00:00[Atlantic/Azores]"
5955 );
5956
5957 let zdt2 = zdt1.with().time(Time::midnight()).build().unwrap();
5958 assert_eq!(
5959 zdt2.to_string(),
5960 "2024-03-31T01:00:00+00:00[Atlantic/Azores]"
5961 );
5962 }
5963
5964 /// Similar to `zoned_with_time_dst_after_gap`, but tests what happens
5965 /// when moving from/to both sides of the gap.
5966 ///
5967 /// See: <https://github.com/BurntSushi/jiff/issues/211>
5968 #[test]
5969 fn zoned_with_time_dst_us_eastern() {
5970 if crate::tz::db().is_definitively_empty() {
5971 return;
5972 }
5973
5974 let zdt1: Zoned = "2024-03-10T01:30[US/Eastern]".parse().unwrap();
5975 assert_eq!(zdt1.to_string(), "2024-03-10T01:30:00-05:00[US/Eastern]");
5976 let zdt2 = zdt1.with().hour(2).build().unwrap();
5977 assert_eq!(zdt2.to_string(), "2024-03-10T03:30:00-04:00[US/Eastern]");
5978
5979 let zdt1: Zoned = "2024-03-10T03:30[US/Eastern]".parse().unwrap();
5980 assert_eq!(zdt1.to_string(), "2024-03-10T03:30:00-04:00[US/Eastern]");
5981 let zdt2 = zdt1.with().hour(2).build().unwrap();
5982 assert_eq!(zdt2.to_string(), "2024-03-10T03:30:00-04:00[US/Eastern]");
5983
5984 // I originally thought that this was difference from Temporal. Namely,
5985 // I thought that Temporal ignored the disambiguation setting (and the
5986 // bad offset). But it doesn't. I was holding it wrong.
5987 //
5988 // See: https://github.com/tc39/proposal-temporal/issues/3078
5989 let zdt1: Zoned = "2024-03-10T01:30[US/Eastern]".parse().unwrap();
5990 assert_eq!(zdt1.to_string(), "2024-03-10T01:30:00-05:00[US/Eastern]");
5991 let zdt2 = zdt1
5992 .with()
5993 .offset(tz::offset(10))
5994 .hour(2)
5995 .disambiguation(Disambiguation::Earlier)
5996 .build()
5997 .unwrap();
5998 assert_eq!(zdt2.to_string(), "2024-03-10T01:30:00-05:00[US/Eastern]");
5999
6000 // This should also respect the disambiguation setting even without
6001 // explicitly specifying an invalid offset. This is because `02:30-05`
6002 // is regarded as invalid since `02:30` isn't a valid civil time on
6003 // this date in this time zone.
6004 let zdt1: Zoned = "2024-03-10T01:30[US/Eastern]".parse().unwrap();
6005 assert_eq!(zdt1.to_string(), "2024-03-10T01:30:00-05:00[US/Eastern]");
6006 let zdt2 = zdt1
6007 .with()
6008 .hour(2)
6009 .disambiguation(Disambiguation::Earlier)
6010 .build()
6011 .unwrap();
6012 assert_eq!(zdt2.to_string(), "2024-03-10T01:30:00-05:00[US/Eastern]");
6013 }
6014
6015 #[test]
6016 fn zoned_precision_loss() {
6017 if crate::tz::db().is_definitively_empty() {
6018 return;
6019 }
6020
6021 let zdt1: Zoned = "2025-01-25T19:32:21.783444592+01:00[Europe/Paris]"
6022 .parse()
6023 .unwrap();
6024 let span = 1.second();
6025 let zdt2 = &zdt1 + span;
6026 assert_eq!(
6027 zdt2.to_string(),
6028 "2025-01-25T19:32:22.783444592+01:00[Europe/Paris]"
6029 );
6030 assert_eq!(zdt1, &zdt2 - span, "should be reversible");
6031 }
6032
6033 // See: https://github.com/BurntSushi/jiff/issues/290
6034 #[test]
6035 fn zoned_roundtrip_regression() {
6036 if crate::tz::db().is_definitively_empty() {
6037 return;
6038 }
6039
6040 let zdt: Zoned =
6041 "2063-03-31T10:00:00+11:00[Australia/Sydney]".parse().unwrap();
6042 assert_eq!(zdt.offset(), super::Offset::constant(11));
6043 let roundtrip = zdt.time_zone().to_zoned(zdt.datetime()).unwrap();
6044 assert_eq!(zdt, roundtrip);
6045 }
6046
6047 // See: https://github.com/BurntSushi/jiff/issues/305
6048 #[test]
6049 fn zoned_round_dst_day_length() {
6050 if crate::tz::db().is_definitively_empty() {
6051 return;
6052 }
6053
6054 let zdt1: Zoned =
6055 "2025-03-09T12:15[America/New_York]".parse().unwrap();
6056 let zdt2 = zdt1.round(Unit::Day).unwrap();
6057 // Since this day is only 23 hours long, it should round down instead
6058 // of up (as it would on a normal 24 hour day). Interestingly, the bug
6059 // was causing this to not only round up, but to a datetime that wasn't
6060 // the start of a day. Specifically, 2025-03-10T01:00:00-04:00.
6061 assert_eq!(
6062 zdt2.to_string(),
6063 "2025-03-09T00:00:00-05:00[America/New_York]"
6064 );
6065 }
6066
6067 #[test]
6068 fn zoned_round_errors() {
6069 if crate::tz::db().is_definitively_empty() {
6070 return;
6071 }
6072
6073 let zdt: Zoned = "2025-03-09T12:15[America/New_York]".parse().unwrap();
6074
6075 insta::assert_snapshot!(
6076 zdt.round(Unit::Year).unwrap_err(),
6077 @"failed rounding datetime: rounding to 'years' is not supported"
6078 );
6079 insta::assert_snapshot!(
6080 zdt.round(Unit::Month).unwrap_err(),
6081 @"failed rounding datetime: rounding to 'months' is not supported"
6082 );
6083 insta::assert_snapshot!(
6084 zdt.round(Unit::Week).unwrap_err(),
6085 @"failed rounding datetime: rounding to 'weeks' is not supported"
6086 );
6087
6088 let options = ZonedRound::new().smallest(Unit::Day).increment(2);
6089 insta::assert_snapshot!(
6090 zdt.round(options).unwrap_err(),
6091 @"failed rounding datetime: increment for rounding to 'days' must be equal to `1`"
6092 );
6093 }
6094
6095 // This tests that if we get a time zone offset with an explicit second
6096 // component, then it must *exactly* match the correct offset for that
6097 // civil time.
6098 //
6099 // See: https://github.com/tc39/proposal-temporal/issues/3099
6100 // See: https://github.com/tc39/proposal-temporal/pull/3107
6101 #[test]
6102 fn time_zone_offset_seconds_exact_match() {
6103 if crate::tz::db().is_definitively_empty() {
6104 return;
6105 }
6106
6107 let zdt: Zoned =
6108 "1970-06-01T00:00:00-00:45[Africa/Monrovia]".parse().unwrap();
6109 assert_eq!(
6110 zdt.to_string(),
6111 "1970-06-01T00:00:00-00:45[Africa/Monrovia]"
6112 );
6113
6114 let zdt: Zoned =
6115 "1970-06-01T00:00:00-00:44:30[Africa/Monrovia]".parse().unwrap();
6116 assert_eq!(
6117 zdt.to_string(),
6118 "1970-06-01T00:00:00-00:45[Africa/Monrovia]"
6119 );
6120
6121 insta::assert_snapshot!(
6122 "1970-06-01T00:00:00-00:44:40[Africa/Monrovia]".parse::<Zoned>().unwrap_err(),
6123 @"datetime could not resolve to a timestamp since `reject` conflict resolution was chosen, and because datetime has offset `-00:44:40`, but the time zone `Africa/Monrovia` for the given datetime unambiguously has offset `-00:44:30`",
6124 );
6125
6126 insta::assert_snapshot!(
6127 "1970-06-01T00:00:00-00:45:00[Africa/Monrovia]".parse::<Zoned>().unwrap_err(),
6128 @"datetime could not resolve to a timestamp since `reject` conflict resolution was chosen, and because datetime has offset `-00:45`, but the time zone `Africa/Monrovia` for the given datetime unambiguously has offset `-00:44:30`",
6129 );
6130 }
6131
6132 // These are some interesting tests because the time zones have transitions
6133 // that are very close to one another (within 14 days!). I picked these up
6134 // from a bug report to Temporal. Their reference implementation uses
6135 // different logic to examine time zone transitions than Jiff. In contrast,
6136 // Jiff uses the IANA time zone database directly. So it was unaffected.
6137 //
6138 // [1]: https://github.com/tc39/proposal-temporal/issues/3110
6139 #[test]
6140 fn weird_time_zone_transitions() {
6141 if crate::tz::db().is_definitively_empty() {
6142 return;
6143 }
6144
6145 let zdt: Zoned =
6146 "2000-10-08T01:00:00-01:00[America/Noronha]".parse().unwrap();
6147 let sod = zdt.start_of_day().unwrap();
6148 assert_eq!(
6149 sod.to_string(),
6150 "2000-10-08T01:00:00-01:00[America/Noronha]"
6151 );
6152
6153 let zdt: Zoned =
6154 "2000-10-08T03:00:00-03:00[America/Boa_Vista]".parse().unwrap();
6155 let sod = zdt.start_of_day().unwrap();
6156 assert_eq!(
6157 sod.to_string(),
6158 "2000-10-08T01:00:00-03:00[America/Boa_Vista]",
6159 );
6160 }
6161
6162 // An interesting test from the Temporal issue tracker, where one doesn't
6163 // get a rejection during a fold when the offset is included in the
6164 // datetime string.
6165 //
6166 // See: https://github.com/tc39/proposal-temporal/issues/2892#issuecomment-3863293014
6167 #[test]
6168 fn no_reject_in_fold_when_using_with() {
6169 if crate::tz::db().is_definitively_empty() {
6170 return;
6171 }
6172
6173 let zdt1: Zoned =
6174 "2016-09-30T02:01+02:00[Europe/Amsterdam]".parse().unwrap();
6175 let zdt2 = zdt1
6176 .with()
6177 .month(10)
6178 .disambiguation(Disambiguation::Reject)
6179 .offset_conflict(OffsetConflict::Reject)
6180 .build()
6181 .unwrap();
6182 assert_eq!(
6183 zdt2.to_string(),
6184 "2016-10-30T02:01:00+02:00[Europe/Amsterdam]"
6185 );
6186
6187 let zdt3: Zoned =
6188 "2016-10-30T02:01+02:00[Europe/Amsterdam]".parse().unwrap();
6189 assert_eq!(
6190 zdt3.to_string(),
6191 "2016-10-30T02:01:00+02:00[Europe/Amsterdam]"
6192 );
6193
6194 let zdt4: Zoned =
6195 "2016-10-30T02:01+01:00[Europe/Amsterdam]".parse().unwrap();
6196 assert_eq!(
6197 zdt4.to_string(),
6198 "2016-10-30T02:01:00+01:00[Europe/Amsterdam]"
6199 );
6200 }
6201}