Coverage Report

Created: 2025-12-08 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.16/src/timestamp.rs
Line
Count
Source
1
use core::time::Duration as UnsignedDuration;
2
3
use crate::{
4
    duration::{Duration, SDuration},
5
    error::{err, Error, ErrorContext},
6
    fmt::{
7
        self,
8
        temporal::{self, DEFAULT_DATETIME_PARSER},
9
    },
10
    shared::util::itime::ITimestamp,
11
    tz::{Offset, TimeZone},
12
    util::{
13
        rangeint::{self, Composite, RFrom, RInto},
14
        round::increment,
15
        t::{
16
            self, FractionalNanosecond, NoUnits, NoUnits128, UnixMicroseconds,
17
            UnixMilliseconds, UnixNanoseconds, UnixSeconds, C,
18
        },
19
    },
20
    zoned::Zoned,
21
    RoundMode, SignedDuration, Span, SpanRound, Unit,
22
};
23
24
/// An instant in time represented as the number of nanoseconds since the Unix
25
/// epoch.
26
///
27
/// A timestamp is always in the Unix timescale with a UTC offset of zero.
28
///
29
/// To obtain civil or "local" datetime units like year, month, day or hour, a
30
/// timestamp needs to be combined with a [`TimeZone`] to create a [`Zoned`].
31
/// That can be done with [`Timestamp::in_tz`] or [`Timestamp::to_zoned`].
32
///
33
/// The integer count of nanoseconds since the Unix epoch is signed, where
34
/// the Unix epoch is `1970-01-01 00:00:00Z`. A positive timestamp indicates
35
/// a point in time after the Unix epoch. A negative timestamp indicates a
36
/// point in time before the Unix epoch.
37
///
38
/// # Parsing and printing
39
///
40
/// The `Timestamp` type provides convenient trait implementations of
41
/// [`std::str::FromStr`] and [`std::fmt::Display`]:
42
///
43
/// ```
44
/// use jiff::Timestamp;
45
///
46
/// let ts: Timestamp = "2024-06-19 15:22:45-04".parse()?;
47
/// assert_eq!(ts.to_string(), "2024-06-19T19:22:45Z");
48
///
49
/// # Ok::<(), Box<dyn std::error::Error>>(())
50
/// ```
51
///
52
/// A `Timestamp` can also be parsed from something that _contains_ a
53
/// timestamp, but with perhaps other data (such as a time zone):
54
///
55
/// ```
56
/// use jiff::Timestamp;
57
///
58
/// let ts: Timestamp = "2024-06-19T15:22:45-04[America/New_York]".parse()?;
59
/// assert_eq!(ts.to_string(), "2024-06-19T19:22:45Z");
60
///
61
/// # Ok::<(), Box<dyn std::error::Error>>(())
62
/// ```
63
///
64
/// For more information on the specific format supported, see the
65
/// [`fmt::temporal`](crate::fmt::temporal) module documentation.
66
///
67
/// # Default value
68
///
69
/// For convenience, this type implements the `Default` trait. Its default
70
/// value corresponds to `1970-01-01T00:00:00.000000000`. That is, it is the
71
/// Unix epoch. One can also access this value via the `Timestamp::UNIX_EPOCH`
72
/// constant.
73
///
74
/// # Leap seconds
75
///
76
/// Jiff does not support leap seconds. Jiff behaves as if they don't exist.
77
/// The only exception is that if one parses a timestamp with a second
78
/// component of `60`, then it is automatically constrained to `59`:
79
///
80
/// ```
81
/// use jiff::Timestamp;
82
///
83
/// let ts: Timestamp = "2016-12-31 23:59:60Z".parse()?;
84
/// assert_eq!(ts.to_string(), "2016-12-31T23:59:59Z");
85
///
86
/// # Ok::<(), Box<dyn std::error::Error>>(())
87
/// ```
88
///
89
/// # Comparisons
90
///
91
/// The `Timestamp` type provides both `Eq` and `Ord` trait implementations
92
/// to facilitate easy comparisons. When a timestamp `ts1` occurs before a
93
/// timestamp `ts2`, then `dt1 < dt2`. For example:
94
///
95
/// ```
96
/// use jiff::Timestamp;
97
///
98
/// let ts1 = Timestamp::from_second(123_456_789)?;
99
/// let ts2 = Timestamp::from_second(123_456_790)?;
100
/// assert!(ts1 < ts2);
101
///
102
/// # Ok::<(), Box<dyn std::error::Error>>(())
103
/// ```
104
///
105
/// # Arithmetic
106
///
107
/// This type provides routines for adding and subtracting spans of time, as
108
/// well as computing the span of time between two `Timestamp` values.
109
///
110
/// For adding or subtracting spans of time, one can use any of the following
111
/// routines:
112
///
113
/// * [`Timestamp::checked_add`] or [`Timestamp::checked_sub`] for checked
114
/// arithmetic.
115
/// * [`Timestamp::saturating_add`] or [`Timestamp::saturating_sub`] for
116
/// saturating arithmetic.
117
///
118
/// Additionally, checked arithmetic is available via the `Add` and `Sub`
119
/// trait implementations. When the result overflows, a panic occurs.
120
///
121
/// ```
122
/// use jiff::{Timestamp, ToSpan};
123
///
124
/// let ts1: Timestamp = "2024-02-25T15:45Z".parse()?;
125
/// let ts2 = ts1 - 24.hours();
126
/// assert_eq!(ts2.to_string(), "2024-02-24T15:45:00Z");
127
///
128
/// # Ok::<(), Box<dyn std::error::Error>>(())
129
/// ```
130
///
131
/// One can compute the span of time between two timestamps using either
132
/// [`Timestamp::until`] or [`Timestamp::since`]. It's also possible to
133
/// subtract two `Timestamp` values directly via a `Sub` trait implementation:
134
///
135
/// ```
136
/// use jiff::{Timestamp, ToSpan};
137
///
138
/// let ts1: Timestamp = "2024-05-03 23:30:00.123Z".parse()?;
139
/// let ts2: Timestamp = "2024-02-25 07Z".parse()?;
140
/// // The default is to return spans with units no bigger than seconds.
141
/// assert_eq!(ts1 - ts2, 5934600.seconds().milliseconds(123).fieldwise());
142
///
143
/// # Ok::<(), Box<dyn std::error::Error>>(())
144
/// ```
145
///
146
/// The `until` and `since` APIs are polymorphic and allow re-balancing and
147
/// rounding the span returned. For example, the default largest unit is
148
/// seconds (as exemplified above), but we can ask for bigger units (up to
149
/// hours):
150
///
151
/// ```
152
/// use jiff::{Timestamp, ToSpan, Unit};
153
///
154
/// let ts1: Timestamp = "2024-05-03 23:30:00.123Z".parse()?;
155
/// let ts2: Timestamp = "2024-02-25 07Z".parse()?;
156
/// assert_eq!(
157
///     // If you want to deal in units bigger than hours, then you'll have to
158
///     // convert your timestamp to a [`Zoned`] first.
159
///     ts1.since((Unit::Hour, ts2))?,
160
///     1648.hours().minutes(30).milliseconds(123).fieldwise(),
161
/// );
162
///
163
/// # Ok::<(), Box<dyn std::error::Error>>(())
164
/// ```
165
///
166
/// You can also round the span returned:
167
///
168
/// ```
169
/// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
170
///
171
/// let ts1: Timestamp = "2024-05-03 23:30:59.123Z".parse()?;
172
/// let ts2: Timestamp = "2024-05-02 07Z".parse()?;
173
/// assert_eq!(
174
///     ts1.since(
175
///         TimestampDifference::new(ts2)
176
///             .smallest(Unit::Minute)
177
///             .largest(Unit::Hour),
178
///     )?,
179
///     40.hours().minutes(30).fieldwise(),
180
/// );
181
/// // `TimestampDifference` uses truncation as a rounding mode by default,
182
/// // but you can set the rounding mode to break ties away from zero:
183
/// assert_eq!(
184
///     ts1.since(
185
///         TimestampDifference::new(ts2)
186
///             .smallest(Unit::Minute)
187
///             .largest(Unit::Hour)
188
///             .mode(RoundMode::HalfExpand),
189
///     )?,
190
///     // Rounds up to 31 minutes.
191
///     40.hours().minutes(31).fieldwise(),
192
/// );
193
///
194
/// # Ok::<(), Box<dyn std::error::Error>>(())
195
/// ```
196
///
197
/// # Rounding timestamps
198
///
199
/// A `Timestamp` can be rounded based on a [`TimestampRound`] configuration of
200
/// smallest units, rounding increment and rounding mode. Here's an example
201
/// showing how to round to the nearest third hour:
202
///
203
/// ```
204
/// use jiff::{Timestamp, TimestampRound, Unit};
205
///
206
/// let ts: Timestamp = "2024-06-19 16:27:29.999999999Z".parse()?;
207
/// assert_eq!(
208
///     ts.round(TimestampRound::new().smallest(Unit::Hour).increment(3))?,
209
///     "2024-06-19 15Z".parse::<Timestamp>()?,
210
/// );
211
/// // Or alternatively, make use of the `From<(Unit, i64)> for TimestampRound`
212
/// // trait implementation:
213
/// assert_eq!(
214
///     ts.round((Unit::Hour, 3))?.to_string(),
215
///     "2024-06-19T15:00:00Z",
216
/// );
217
///
218
/// # Ok::<(), Box<dyn std::error::Error>>(())
219
/// ```
220
///
221
/// See [`Timestamp::round`] for more details.
222
///
223
/// # An instant in time
224
///
225
/// Unlike a [`civil::DateTime`](crate::civil::DateTime), a `Timestamp`
226
/// _always_ corresponds, unambiguously, to a precise instant in time (to
227
/// nanosecond precision). This means that attaching a time zone to a timestamp
228
/// is always unambiguous because there's never any question as to which
229
/// instant it refers to. This is true even for gaps in civil time.
230
///
231
/// For example, in `America/New_York`, clocks were moved ahead one hour
232
/// at clock time `2024-03-10 02:00:00`. That is, the 2 o'clock hour never
233
/// appeared on clocks in the `America/New_York` region. Since parsing a
234
/// timestamp always requires an offset, the time it refers to is unambiguous.
235
/// We can see this by writing a clock time, `02:30`, that never existed but
236
/// with two different offsets:
237
///
238
/// ```
239
/// use jiff::Timestamp;
240
///
241
/// // All we're doing here is attaching an offset to a civil datetime.
242
/// // There is no time zone information here, and thus there is no
243
/// // accounting for ambiguity due to daylight saving time transitions.
244
/// let before_hour_jump: Timestamp = "2024-03-10 02:30-04".parse()?;
245
/// let after_hour_jump: Timestamp = "2024-03-10 02:30-05".parse()?;
246
/// // This shows the instant in time in UTC.
247
/// assert_eq!(before_hour_jump.to_string(), "2024-03-10T06:30:00Z");
248
/// assert_eq!(after_hour_jump.to_string(), "2024-03-10T07:30:00Z");
249
///
250
/// // Now let's attach each instant to an `America/New_York` time zone.
251
/// let zdt_before = before_hour_jump.in_tz("America/New_York")?;
252
/// let zdt_after = after_hour_jump.in_tz("America/New_York")?;
253
/// // And now we can see that even though the original instant refers to
254
/// // the 2 o'clock hour, since that hour never existed on the clocks in
255
/// // `America/New_York`, an instant with a time zone correctly adjusts.
256
/// assert_eq!(
257
///     zdt_before.to_string(),
258
///     "2024-03-10T01:30:00-05:00[America/New_York]",
259
/// );
260
/// assert_eq!(
261
///     zdt_after.to_string(),
262
///     "2024-03-10T03:30:00-04:00[America/New_York]",
263
/// );
264
///
265
/// # Ok::<(), Box<dyn std::error::Error>>(())
266
/// ```
267
///
268
/// In the example above, there is never a step that is incorrect or has an
269
/// alternative answer. Every step is unambiguous because we never involve
270
/// any [`civil`](crate::civil) datetimes.
271
///
272
/// But note that if the datetime string you're parsing from lacks an offset,
273
/// then it *could* be ambiguous even if a time zone is specified. In this
274
/// case, parsing will always fail:
275
///
276
/// ```
277
/// use jiff::Timestamp;
278
///
279
/// let result = "2024-06-30 08:30[America/New_York]".parse::<Timestamp>();
280
/// assert_eq!(
281
///     result.unwrap_err().to_string(),
282
///     "failed to find offset component in \
283
///      \"2024-06-30 08:30[America/New_York]\", \
284
///      which is required for parsing a timestamp",
285
/// );
286
/// ```
287
///
288
/// # Converting a civil datetime to a timestamp
289
///
290
/// Sometimes you want to convert the "time on the clock" to a precise instant
291
/// in time. One way to do this was demonstrated in the previous section, but
292
/// it only works if you know your current time zone offset:
293
///
294
/// ```
295
/// use jiff::Timestamp;
296
///
297
/// let ts: Timestamp = "2024-06-30 08:36-04".parse()?;
298
/// assert_eq!(ts.to_string(), "2024-06-30T12:36:00Z");
299
///
300
/// # Ok::<(), Box<dyn std::error::Error>>(())
301
/// ```
302
///
303
/// The above happened to be the precise instant in time I wrote the example.
304
/// Since I happened to know the offset, this worked okay. But what if I
305
/// didn't? We could instead construct a civil datetime and attach a time zone
306
/// to it. This will create a [`Zoned`] value, from which we can access the
307
/// timestamp:
308
///
309
/// ```
310
/// use jiff::civil::date;
311
///
312
/// let clock = date(2024, 6, 30).at(8, 36, 0, 0).in_tz("America/New_York")?;
313
/// assert_eq!(clock.timestamp().to_string(), "2024-06-30T12:36:00Z");
314
///
315
/// # Ok::<(), Box<dyn std::error::Error>>(())
316
/// ```
317
#[derive(Clone, Copy)]
318
pub struct Timestamp {
319
    second: UnixSeconds,
320
    nanosecond: FractionalNanosecond,
321
}
322
323
impl Timestamp {
324
    /// The minimum representable timestamp.
325
    ///
326
    /// The minimum is chosen such that it can be combined with
327
    /// any legal [`Offset`](crate::tz::Offset) and turned into a
328
    /// [`civil::DateTime`](crate::civil::DateTime).
329
    ///
330
    /// # Example
331
    ///
332
    /// ```
333
    /// use jiff::{civil::date, tz::Offset, Timestamp};
334
    ///
335
    /// let dt = Offset::MIN.to_datetime(Timestamp::MIN);
336
    /// assert_eq!(dt, date(-9999, 1, 1).at(0, 0, 0, 0));
337
    /// ```
338
    pub const MIN: Timestamp = Timestamp {
339
        second: UnixSeconds::MIN_SELF,
340
        nanosecond: FractionalNanosecond::N::<0>(),
341
    };
342
343
    /// The maximum representable timestamp.
344
    ///
345
    /// The maximum is chosen such that it can be combined with
346
    /// any legal [`Offset`](crate::tz::Offset) and turned into a
347
    /// [`civil::DateTime`](crate::civil::DateTime).
348
    ///
349
    /// # Example
350
    ///
351
    /// ```
352
    /// use jiff::{civil::date, tz::Offset, Timestamp};
353
    ///
354
    /// let dt = Offset::MAX.to_datetime(Timestamp::MAX);
355
    /// assert_eq!(dt, date(9999, 12, 31).at(23, 59, 59, 999_999_999));
356
    /// ```
357
    pub const MAX: Timestamp = Timestamp {
358
        second: UnixSeconds::MAX_SELF,
359
        nanosecond: FractionalNanosecond::MAX_SELF,
360
    };
361
362
    /// The Unix epoch represented as a timestamp.
363
    ///
364
    /// The Unix epoch corresponds to the instant at `1970-01-01T00:00:00Z`.
365
    /// As a timestamp, it corresponds to `0` nanoseconds.
366
    ///
367
    /// A timestamp is positive if and only if it is greater than the Unix
368
    /// epoch. A timestamp is negative if and only if it is less than the Unix
369
    /// epoch.
370
    pub const UNIX_EPOCH: Timestamp = Timestamp {
371
        second: UnixSeconds::N::<0>(),
372
        nanosecond: FractionalNanosecond::N::<0>(),
373
    };
374
375
    /// Returns the current system time as a timestamp.
376
    ///
377
    /// # Panics
378
    ///
379
    /// This panics if the system clock is set to a time value outside of the
380
    /// range `-009999-01-01T00:00:00Z..=9999-12-31T11:59:59.999999999Z`. The
381
    /// justification here is that it is reasonable to expect the system clock
382
    /// to be set to a somewhat sane, if imprecise, value.
383
    ///
384
    /// If you want to get the current Unix time fallibly, use
385
    /// [`Timestamp::try_from`] with a `std::time::SystemTime` as input.
386
    ///
387
    /// This may also panic when `SystemTime::now()` itself panics. The most
388
    /// common context in which this happens is on the `wasm32-unknown-unknown`
389
    /// target. If you're using that target in the context of the web (for
390
    /// example, via `wasm-pack`), and you're an application, then you should
391
    /// enable Jiff's `js` feature. This will automatically instruct Jiff in
392
    /// this very specific circumstance to execute JavaScript code to determine
393
    /// the current time from the web browser.
394
    ///
395
    /// # Example
396
    ///
397
    /// ```
398
    /// use jiff::Timestamp;
399
    ///
400
    /// assert!(Timestamp::now() > Timestamp::UNIX_EPOCH);
401
    /// ```
402
    #[cfg(feature = "std")]
403
0
    pub fn now() -> Timestamp {
404
0
        Timestamp::try_from(crate::now::system_time())
405
0
            .expect("system time is valid")
406
0
    }
407
408
    /// Creates a new instant in time represented as a timestamp.
409
    ///
410
    /// While a timestamp is logically a count of nanoseconds since the Unix
411
    /// epoch, this constructor provides a convenience way of constructing
412
    /// the timestamp from two components: seconds and fractional seconds
413
    /// expressed as nanoseconds.
414
    ///
415
    /// The signs of `second` and `nanosecond` need not be the same.
416
    ///
417
    /// # Errors
418
    ///
419
    /// This returns an error if the given components would correspond to
420
    /// an instant outside the supported range. Also, `nanosecond` is limited
421
    /// to the range `-999,999,999..=999,999,999`.
422
    ///
423
    /// # Example
424
    ///
425
    /// This example shows the instant in time 123,456,789 seconds after the
426
    /// Unix epoch:
427
    ///
428
    /// ```
429
    /// use jiff::Timestamp;
430
    ///
431
    /// assert_eq!(
432
    ///     Timestamp::new(123_456_789, 0)?.to_string(),
433
    ///     "1973-11-29T21:33:09Z",
434
    /// );
435
    ///
436
    /// # Ok::<(), Box<dyn std::error::Error>>(())
437
    /// ```
438
    ///
439
    /// # Example: normalized sign
440
    ///
441
    /// This example shows how `second` and `nanosecond` are resolved when
442
    /// their signs differ.
443
    ///
444
    /// ```
445
    /// use jiff::Timestamp;
446
    ///
447
    /// let ts = Timestamp::new(2, -999_999_999)?;
448
    /// assert_eq!(ts.as_second(), 1);
449
    /// assert_eq!(ts.subsec_nanosecond(), 1);
450
    ///
451
    /// let ts = Timestamp::new(-2, 999_999_999)?;
452
    /// assert_eq!(ts.as_second(), -1);
453
    /// assert_eq!(ts.subsec_nanosecond(), -1);
454
    ///
455
    /// # Ok::<(), Box<dyn std::error::Error>>(())
456
    /// ```
457
    ///
458
    /// # Example: limits
459
    ///
460
    /// The minimum timestamp has nanoseconds set to zero, while the maximum
461
    /// timestamp has nanoseconds set to `999,999,999`:
462
    ///
463
    /// ```
464
    /// use jiff::Timestamp;
465
    ///
466
    /// assert_eq!(Timestamp::MIN.subsec_nanosecond(), 0);
467
    /// assert_eq!(Timestamp::MAX.subsec_nanosecond(), 999_999_999);
468
    /// ```
469
    ///
470
    /// As a consequence, nanoseconds cannot be negative when a timestamp has
471
    /// minimal seconds:
472
    ///
473
    /// ```
474
    /// use jiff::Timestamp;
475
    ///
476
    /// assert!(Timestamp::new(Timestamp::MIN.as_second(), -1).is_err());
477
    /// // But they can be positive!
478
    /// let one_ns_more = Timestamp::new(Timestamp::MIN.as_second(), 1)?;
479
    /// assert_eq!(
480
    ///     one_ns_more.to_string(),
481
    ///     "-009999-01-02T01:59:59.000000001Z",
482
    /// );
483
    /// // Or, when combined with a minimal offset:
484
    /// assert_eq!(
485
    ///     jiff::tz::Offset::MIN.to_datetime(one_ns_more).to_string(),
486
    ///     "-009999-01-01T00:00:00.000000001",
487
    /// );
488
    ///
489
    /// # Ok::<(), Box<dyn std::error::Error>>(())
490
    /// ```
491
    #[inline]
492
0
    pub fn new(second: i64, nanosecond: i32) -> Result<Timestamp, Error> {
493
0
        Timestamp::new_ranged(
494
0
            UnixSeconds::try_new("second", second)?,
495
0
            FractionalNanosecond::try_new("nanosecond", nanosecond)?,
496
        )
497
0
    }
498
499
    /// Creates a new `Timestamp` value in a `const` context.
500
    ///
501
    /// # Panics
502
    ///
503
    /// This routine panics when [`Timestamp::new`] would return an error.
504
    /// That is, when the given components would correspond to
505
    /// an instant outside the supported range. Also, `nanosecond` is limited
506
    /// to the range `-999,999,999..=999,999,999`.
507
    ///
508
    /// # Example
509
    ///
510
    /// This example shows the instant in time 123,456,789 seconds after the
511
    /// Unix epoch:
512
    ///
513
    /// ```
514
    /// use jiff::Timestamp;
515
    ///
516
    /// assert_eq!(
517
    ///     Timestamp::constant(123_456_789, 0).to_string(),
518
    ///     "1973-11-29T21:33:09Z",
519
    /// );
520
    /// ```
521
    #[inline]
522
0
    pub const fn constant(mut second: i64, mut nanosecond: i32) -> Timestamp {
523
0
        if second == UnixSeconds::MIN_REPR && nanosecond < 0 {
524
0
            panic!("nanoseconds must be >=0 when seconds are minimal");
525
0
        }
526
        // We now normalize our seconds and nanoseconds such that they have
527
        // the same sign (or where one is zero). So for example, when given
528
        // `-1s 1ns`, then we should turn that into `-999,999,999ns`.
529
        //
530
        // But first, if we're already normalized, we're done!
531
0
        if second.signum() as i8 == nanosecond.signum() as i8
532
0
            || second == 0
533
0
            || nanosecond == 0
534
        {
535
0
            return Timestamp {
536
0
                second: UnixSeconds::new_unchecked(second),
537
0
                nanosecond: FractionalNanosecond::new_unchecked(nanosecond),
538
0
            };
539
0
        }
540
0
        if second < 0 && nanosecond > 0 {
541
0
            second += 1;
542
0
            nanosecond -= t::NANOS_PER_SECOND.value() as i32;
543
0
        } else if second > 0 && nanosecond < 0 {
544
0
            second -= 1;
545
0
            nanosecond += t::NANOS_PER_SECOND.value() as i32;
546
0
        }
547
0
        Timestamp {
548
0
            second: UnixSeconds::new_unchecked(second),
549
0
            nanosecond: FractionalNanosecond::new_unchecked(nanosecond),
550
0
        }
551
0
    }
552
553
    /// Creates a new instant in time from the number of seconds elapsed since
554
    /// the Unix epoch.
555
    ///
556
    /// When `second` is negative, it corresponds to an instant in time before
557
    /// the Unix epoch. A smaller number corresponds to an instant in time
558
    /// further into the past.
559
    ///
560
    /// # Errors
561
    ///
562
    /// This returns an error if the given second corresponds to a timestamp
563
    /// outside of the [`Timestamp::MIN`] and [`Timestamp::MAX`] boundaries.
564
    ///
565
    /// It is a semver guarantee that the only way for this to return an error
566
    /// is if the given value is out of range. That is, when it is less than
567
    /// `Timestamp::MIN` or greater than `Timestamp::MAX`.
568
    ///
569
    /// # Example
570
    ///
571
    /// This example shows the instants in time 1 second immediately after and
572
    /// before the Unix epoch:
573
    ///
574
    /// ```
575
    /// use jiff::Timestamp;
576
    ///
577
    /// assert_eq!(
578
    ///     Timestamp::from_second(1)?.to_string(),
579
    ///     "1970-01-01T00:00:01Z",
580
    /// );
581
    /// assert_eq!(
582
    ///     Timestamp::from_second(-1)?.to_string(),
583
    ///     "1969-12-31T23:59:59Z",
584
    /// );
585
    ///
586
    /// # Ok::<(), Box<dyn std::error::Error>>(())
587
    /// ```
588
    ///
589
    /// # Example: saturating construction
590
    ///
591
    /// If you need a way to build a `Timestamp` value that saturates to
592
    /// the minimum and maximum values supported by Jiff, then this is
593
    /// guaranteed to work:
594
    ///
595
    /// ```
596
    /// use jiff::Timestamp;
597
    ///
598
    /// fn from_second_saturating(seconds: i64) -> Timestamp {
599
    ///     Timestamp::from_second(seconds).unwrap_or_else(|_| {
600
    ///         if seconds < 0 {
601
    ///             Timestamp::MIN
602
    ///         } else {
603
    ///             Timestamp::MAX
604
    ///         }
605
    ///     })
606
    /// }
607
    ///
608
    /// assert_eq!(from_second_saturating(0), Timestamp::UNIX_EPOCH);
609
    /// assert_eq!(
610
    ///     from_second_saturating(-999999999999999999),
611
    ///     Timestamp::MIN
612
    /// );
613
    /// assert_eq!(
614
    ///     from_second_saturating(999999999999999999),
615
    ///     Timestamp::MAX
616
    /// );
617
    /// ```
618
    #[inline]
619
0
    pub fn from_second(second: i64) -> Result<Timestamp, Error> {
620
0
        Timestamp::new(second, 0)
621
0
    }
622
623
    /// Creates a new instant in time from the number of milliseconds elapsed
624
    /// since the Unix epoch.
625
    ///
626
    /// When `millisecond` is negative, it corresponds to an instant in time
627
    /// before the Unix epoch. A smaller number corresponds to an instant in
628
    /// time further into the past.
629
    ///
630
    /// # Errors
631
    ///
632
    /// This returns an error if the given millisecond corresponds to a
633
    /// timestamp outside of the [`Timestamp::MIN`] and [`Timestamp::MAX`]
634
    /// boundaries.
635
    ///
636
    /// It is a semver guarantee that the only way for this to return an error
637
    /// is if the given value is out of range. That is, when it is less than
638
    /// `Timestamp::MIN` or greater than `Timestamp::MAX`.
639
    ///
640
    /// # Example
641
    ///
642
    /// This example shows the instants in time 1 millisecond immediately after
643
    /// and before the Unix epoch:
644
    ///
645
    /// ```
646
    /// use jiff::Timestamp;
647
    ///
648
    /// assert_eq!(
649
    ///     Timestamp::from_millisecond(1)?.to_string(),
650
    ///     "1970-01-01T00:00:00.001Z",
651
    /// );
652
    /// assert_eq!(
653
    ///     Timestamp::from_millisecond(-1)?.to_string(),
654
    ///     "1969-12-31T23:59:59.999Z",
655
    /// );
656
    ///
657
    /// # Ok::<(), Box<dyn std::error::Error>>(())
658
    /// ```
659
    ///
660
    /// # Example: saturating construction
661
    ///
662
    /// If you need a way to build a `Timestamp` value that saturates to
663
    /// the minimum and maximum values supported by Jiff, then this is
664
    /// guaranteed to work:
665
    ///
666
    /// ```
667
    /// use jiff::Timestamp;
668
    ///
669
    /// fn from_millisecond_saturating(millis: i64) -> Timestamp {
670
    ///     Timestamp::from_millisecond(millis).unwrap_or_else(|_| {
671
    ///         if millis < 0 {
672
    ///             Timestamp::MIN
673
    ///         } else {
674
    ///             Timestamp::MAX
675
    ///         }
676
    ///     })
677
    /// }
678
    ///
679
    /// assert_eq!(from_millisecond_saturating(0), Timestamp::UNIX_EPOCH);
680
    /// assert_eq!(
681
    ///     from_millisecond_saturating(-999999999999999999),
682
    ///     Timestamp::MIN
683
    /// );
684
    /// assert_eq!(
685
    ///     from_millisecond_saturating(999999999999999999),
686
    ///     Timestamp::MAX
687
    /// );
688
    /// ```
689
    #[inline]
690
0
    pub fn from_millisecond(millisecond: i64) -> Result<Timestamp, Error> {
691
0
        let millisecond = UnixMilliseconds::try_new128(
692
            "millisecond timestamp",
693
0
            millisecond,
694
0
        )?;
695
0
        Ok(Timestamp::from_millisecond_ranged(millisecond))
696
0
    }
697
698
    /// Creates a new instant in time from the number of microseconds elapsed
699
    /// since the Unix epoch.
700
    ///
701
    /// When `microsecond` is negative, it corresponds to an instant in time
702
    /// before the Unix epoch. A smaller number corresponds to an instant in
703
    /// time further into the past.
704
    ///
705
    /// # Errors
706
    ///
707
    /// This returns an error if the given microsecond corresponds to a
708
    /// timestamp outside of the [`Timestamp::MIN`] and [`Timestamp::MAX`]
709
    /// boundaries.
710
    ///
711
    /// It is a semver guarantee that the only way for this to return an error
712
    /// is if the given value is out of range. That is, when it is less than
713
    /// `Timestamp::MIN` or greater than `Timestamp::MAX`.
714
    ///
715
    /// # Example
716
    ///
717
    /// This example shows the instants in time 1 microsecond immediately after
718
    /// and before the Unix epoch:
719
    ///
720
    /// ```
721
    /// use jiff::Timestamp;
722
    ///
723
    /// assert_eq!(
724
    ///     Timestamp::from_microsecond(1)?.to_string(),
725
    ///     "1970-01-01T00:00:00.000001Z",
726
    /// );
727
    /// assert_eq!(
728
    ///     Timestamp::from_microsecond(-1)?.to_string(),
729
    ///     "1969-12-31T23:59:59.999999Z",
730
    /// );
731
    ///
732
    /// # Ok::<(), Box<dyn std::error::Error>>(())
733
    /// ```
734
    ///
735
    /// # Example: saturating construction
736
    ///
737
    /// If you need a way to build a `Timestamp` value that saturates to
738
    /// the minimum and maximum values supported by Jiff, then this is
739
    /// guaranteed to work:
740
    ///
741
    /// ```
742
    /// use jiff::Timestamp;
743
    ///
744
    /// fn from_microsecond_saturating(micros: i64) -> Timestamp {
745
    ///     Timestamp::from_microsecond(micros).unwrap_or_else(|_| {
746
    ///         if micros < 0 {
747
    ///             Timestamp::MIN
748
    ///         } else {
749
    ///             Timestamp::MAX
750
    ///         }
751
    ///     })
752
    /// }
753
    ///
754
    /// assert_eq!(from_microsecond_saturating(0), Timestamp::UNIX_EPOCH);
755
    /// assert_eq!(
756
    ///     from_microsecond_saturating(-999999999999999999),
757
    ///     Timestamp::MIN
758
    /// );
759
    /// assert_eq!(
760
    ///     from_microsecond_saturating(999999999999999999),
761
    ///     Timestamp::MAX
762
    /// );
763
    /// ```
764
    #[inline]
765
0
    pub fn from_microsecond(microsecond: i64) -> Result<Timestamp, Error> {
766
0
        let microsecond = UnixMicroseconds::try_new128(
767
            "microsecond timestamp",
768
0
            microsecond,
769
0
        )?;
770
0
        Ok(Timestamp::from_microsecond_ranged(microsecond))
771
0
    }
772
773
    /// Creates a new instant in time from the number of nanoseconds elapsed
774
    /// since the Unix epoch.
775
    ///
776
    /// When `nanosecond` is negative, it corresponds to an instant in time
777
    /// before the Unix epoch. A smaller number corresponds to an instant in
778
    /// time further into the past.
779
    ///
780
    /// # Errors
781
    ///
782
    /// This returns an error if the given nanosecond corresponds to a
783
    /// timestamp outside of the [`Timestamp::MIN`] and [`Timestamp::MAX`]
784
    /// boundaries.
785
    ///
786
    /// It is a semver guarantee that the only way for this to return an error
787
    /// is if the given value is out of range. That is, when it is less than
788
    /// `Timestamp::MIN` or greater than `Timestamp::MAX`.
789
    ///
790
    /// # Example
791
    ///
792
    /// This example shows the instants in time 1 nanosecond immediately after
793
    /// and before the Unix epoch:
794
    ///
795
    /// ```
796
    /// use jiff::Timestamp;
797
    ///
798
    /// assert_eq!(
799
    ///     Timestamp::from_nanosecond(1)?.to_string(),
800
    ///     "1970-01-01T00:00:00.000000001Z",
801
    /// );
802
    /// assert_eq!(
803
    ///     Timestamp::from_nanosecond(-1)?.to_string(),
804
    ///     "1969-12-31T23:59:59.999999999Z",
805
    /// );
806
    ///
807
    /// # Ok::<(), Box<dyn std::error::Error>>(())
808
    /// ```
809
    ///
810
    /// # Example: saturating construction
811
    ///
812
    /// If you need a way to build a `Timestamp` value that saturates to
813
    /// the minimum and maximum values supported by Jiff, then this is
814
    /// guaranteed to work:
815
    ///
816
    /// ```
817
    /// use jiff::Timestamp;
818
    ///
819
    /// fn from_nanosecond_saturating(nanos: i128) -> Timestamp {
820
    ///     Timestamp::from_nanosecond(nanos).unwrap_or_else(|_| {
821
    ///         if nanos < 0 {
822
    ///             Timestamp::MIN
823
    ///         } else {
824
    ///             Timestamp::MAX
825
    ///         }
826
    ///     })
827
    /// }
828
    ///
829
    /// assert_eq!(from_nanosecond_saturating(0), Timestamp::UNIX_EPOCH);
830
    /// assert_eq!(
831
    ///     from_nanosecond_saturating(-9999999999999999999999999999999999),
832
    ///     Timestamp::MIN
833
    /// );
834
    /// assert_eq!(
835
    ///     from_nanosecond_saturating(9999999999999999999999999999999999),
836
    ///     Timestamp::MAX
837
    /// );
838
    /// ```
839
    #[inline]
840
0
    pub fn from_nanosecond(nanosecond: i128) -> Result<Timestamp, Error> {
841
0
        let nanosecond =
842
0
            UnixNanoseconds::try_new128("nanosecond timestamp", nanosecond)?;
843
0
        Ok(Timestamp::from_nanosecond_ranged(nanosecond))
844
0
    }
845
846
    /// Creates a new timestamp from a `Duration` with the given sign since the
847
    /// Unix epoch.
848
    ///
849
    /// Positive durations result in a timestamp after the Unix epoch. Negative
850
    /// durations result in a timestamp before the Unix epoch.
851
    ///
852
    /// # Errors
853
    ///
854
    /// This returns an error if the given duration corresponds to a timestamp
855
    /// outside of the [`Timestamp::MIN`] and [`Timestamp::MAX`] boundaries.
856
    ///
857
    /// It is a semver guarantee that the only way for this to return an error
858
    /// is if the given value is out of range. That is, when it is less than
859
    /// `Timestamp::MIN` or greater than `Timestamp::MAX`.
860
    ///
861
    /// # Example
862
    ///
863
    /// How one might construct a `Timestamp` from a `SystemTime`:
864
    ///
865
    /// ```
866
    /// use std::time::SystemTime;
867
    /// use jiff::{SignedDuration, Timestamp};
868
    ///
869
    /// let unix_epoch = SystemTime::UNIX_EPOCH;
870
    /// let now = SystemTime::now();
871
    /// let duration = SignedDuration::system_until(unix_epoch, now)?;
872
    /// let ts = Timestamp::from_duration(duration)?;
873
    /// assert!(ts > Timestamp::UNIX_EPOCH);
874
    ///
875
    /// # Ok::<(), Box<dyn std::error::Error>>(())
876
    /// ```
877
    ///
878
    /// Of course, one should just use [`Timestamp::try_from`] for this
879
    /// instead. Indeed, the above example is copied almost exactly from the
880
    /// `TryFrom` implementation.
881
    ///
882
    /// # Example: out of bounds
883
    ///
884
    /// This example shows how some of the boundary conditions are dealt with.
885
    ///
886
    /// ```
887
    /// use jiff::{SignedDuration, Timestamp};
888
    ///
889
    /// // OK, we get the minimum timestamp supported by Jiff:
890
    /// let duration = SignedDuration::new(-377705023201, 0);
891
    /// let ts = Timestamp::from_duration(duration)?;
892
    /// assert_eq!(ts, Timestamp::MIN);
893
    ///
894
    /// // We use the minimum number of seconds, but even subtracting
895
    /// // one more nanosecond after it will result in an error.
896
    /// let duration = SignedDuration::new(-377705023201, -1);
897
    /// assert_eq!(
898
    ///     Timestamp::from_duration(duration).unwrap_err().to_string(),
899
    ///     "parameter 'seconds and nanoseconds' with value -1 is not \
900
    ///      in the required range of 0..=1000000000",
901
    /// );
902
    ///
903
    /// # Ok::<(), Box<dyn std::error::Error>>(())
904
    /// ```
905
    ///
906
    /// # Example: saturating construction
907
    ///
908
    /// If you need a way to build a `Timestamp` value that saturates to
909
    /// the minimum and maximum values supported by Jiff, then this is
910
    /// guaranteed to work:
911
    ///
912
    /// ```
913
    /// use jiff::{SignedDuration, Timestamp};
914
    ///
915
    /// fn from_duration_saturating(dur: SignedDuration) -> Timestamp {
916
    ///     Timestamp::from_duration(dur).unwrap_or_else(|_| {
917
    ///         if dur.is_negative() {
918
    ///             Timestamp::MIN
919
    ///         } else {
920
    ///             Timestamp::MAX
921
    ///         }
922
    ///     })
923
    /// }
924
    ///
925
    /// assert_eq!(
926
    ///     from_duration_saturating(SignedDuration::ZERO),
927
    ///     Timestamp::UNIX_EPOCH,
928
    /// );
929
    /// assert_eq!(
930
    ///     from_duration_saturating(SignedDuration::from_secs(-999999999999)),
931
    ///     Timestamp::MIN
932
    /// );
933
    /// assert_eq!(
934
    ///     from_duration_saturating(SignedDuration::from_secs(999999999999)),
935
    ///     Timestamp::MAX
936
    /// );
937
    /// ```
938
    #[inline]
939
0
    pub fn from_duration(
940
0
        duration: SignedDuration,
941
0
    ) -> Result<Timestamp, Error> {
942
        // As an optimization, we don't need to go through `Timestamp::new`
943
        // (or `Timestamp::new_ranged`) here. That's because a `SignedDuration`
944
        // already guarantees that its seconds and nanoseconds are "coherent."
945
        // That is, we know we can't have a negative second with a positive
946
        // nanosecond (or vice versa).
947
0
        let second = UnixSeconds::try_new("second", duration.as_secs())?;
948
0
        let nanosecond = FractionalNanosecond::try_new(
949
            "nanosecond",
950
0
            duration.subsec_nanos(),
951
0
        )?;
952
        // ... but we do have to check that the *combination* of seconds and
953
        // nanoseconds aren't out of bounds, which is possible even when both
954
        // are, on their own, legal values.
955
0
        if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
956
0
            return Err(Error::range(
957
0
                "seconds and nanoseconds",
958
0
                nanosecond,
959
0
                0,
960
0
                1_000_000_000,
961
0
            ));
962
0
        }
963
0
        Ok(Timestamp { second, nanosecond })
964
0
    }
965
966
    /// Returns this timestamp as a number of seconds since the Unix epoch.
967
    ///
968
    /// This only returns the number of whole seconds. That is, if there are
969
    /// any fractional seconds in this timestamp, then they are truncated.
970
    ///
971
    /// # Example
972
    ///
973
    /// ```
974
    /// use jiff::Timestamp;
975
    ///
976
    /// let ts = Timestamp::new(5, 123_456_789)?;
977
    /// assert_eq!(ts.as_second(), 5);
978
    /// let ts = Timestamp::new(5, 999_999_999)?;
979
    /// assert_eq!(ts.as_second(), 5);
980
    ///
981
    /// let ts = Timestamp::new(-5, -123_456_789)?;
982
    /// assert_eq!(ts.as_second(), -5);
983
    /// let ts = Timestamp::new(-5, -999_999_999)?;
984
    /// assert_eq!(ts.as_second(), -5);
985
    ///
986
    /// # Ok::<(), Box<dyn std::error::Error>>(())
987
    /// ```
988
    #[inline]
989
0
    pub fn as_second(self) -> i64 {
990
0
        self.as_second_ranged().get()
991
0
    }
992
993
    /// Returns this timestamp as a number of milliseconds since the Unix
994
    /// epoch.
995
    ///
996
    /// This only returns the number of whole milliseconds. That is, if there
997
    /// are any fractional milliseconds in this timestamp, then they are
998
    /// truncated.
999
    ///
1000
    /// # Example
1001
    ///
1002
    /// ```
1003
    /// use jiff::Timestamp;
1004
    ///
1005
    /// let ts = Timestamp::new(5, 123_456_789)?;
1006
    /// assert_eq!(ts.as_millisecond(), 5_123);
1007
    /// let ts = Timestamp::new(5, 999_999_999)?;
1008
    /// assert_eq!(ts.as_millisecond(), 5_999);
1009
    ///
1010
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1011
    /// assert_eq!(ts.as_millisecond(), -5_123);
1012
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1013
    /// assert_eq!(ts.as_millisecond(), -5_999);
1014
    ///
1015
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1016
    /// ```
1017
    #[inline]
1018
0
    pub fn as_millisecond(self) -> i64 {
1019
0
        self.as_millisecond_ranged().get()
1020
0
    }
1021
1022
    /// Returns this timestamp as a number of microseconds since the Unix
1023
    /// epoch.
1024
    ///
1025
    /// This only returns the number of whole microseconds. That is, if there
1026
    /// are any fractional microseconds in this timestamp, then they are
1027
    /// truncated.
1028
    ///
1029
    /// # Example
1030
    ///
1031
    /// ```
1032
    /// use jiff::Timestamp;
1033
    ///
1034
    /// let ts = Timestamp::new(5, 123_456_789)?;
1035
    /// assert_eq!(ts.as_microsecond(), 5_123_456);
1036
    /// let ts = Timestamp::new(5, 999_999_999)?;
1037
    /// assert_eq!(ts.as_microsecond(), 5_999_999);
1038
    ///
1039
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1040
    /// assert_eq!(ts.as_microsecond(), -5_123_456);
1041
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1042
    /// assert_eq!(ts.as_microsecond(), -5_999_999);
1043
    ///
1044
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1045
    /// ```
1046
    #[inline]
1047
0
    pub fn as_microsecond(self) -> i64 {
1048
0
        self.as_microsecond_ranged().get()
1049
0
    }
1050
1051
    /// Returns this timestamp as a number of nanoseconds since the Unix
1052
    /// epoch.
1053
    ///
1054
    /// Since a `Timestamp` has a nanosecond precision, the nanoseconds
1055
    /// returned here represent this timestamp losslessly. That is, the
1056
    /// nanoseconds returned can be used with [`Timestamp::from_nanosecond`] to
1057
    /// create an identical timestamp with no loss of precision.
1058
    ///
1059
    /// # Example
1060
    ///
1061
    /// ```
1062
    /// use jiff::Timestamp;
1063
    ///
1064
    /// let ts = Timestamp::new(5, 123_456_789)?;
1065
    /// assert_eq!(ts.as_nanosecond(), 5_123_456_789);
1066
    /// let ts = Timestamp::new(5, 999_999_999)?;
1067
    /// assert_eq!(ts.as_nanosecond(), 5_999_999_999);
1068
    ///
1069
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1070
    /// assert_eq!(ts.as_nanosecond(), -5_123_456_789);
1071
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1072
    /// assert_eq!(ts.as_nanosecond(), -5_999_999_999);
1073
    ///
1074
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1075
    /// ```
1076
    #[inline]
1077
0
    pub fn as_nanosecond(self) -> i128 {
1078
0
        self.as_nanosecond_ranged().get()
1079
0
    }
1080
1081
    /// Returns the fractional second component of this timestamp in units
1082
    /// of milliseconds.
1083
    ///
1084
    /// It is guaranteed that this will never return a value that is greater
1085
    /// than 1 second (or less than -1 second).
1086
    ///
1087
    /// This only returns the number of whole milliseconds. That is, if there
1088
    /// are any fractional milliseconds in this timestamp, then they are
1089
    /// truncated.
1090
    ///
1091
    /// # Example
1092
    ///
1093
    /// ```
1094
    /// use jiff::Timestamp;
1095
    ///
1096
    /// let ts = Timestamp::new(5, 123_456_789)?;
1097
    /// assert_eq!(ts.subsec_millisecond(), 123);
1098
    /// let ts = Timestamp::new(5, 999_999_999)?;
1099
    /// assert_eq!(ts.subsec_millisecond(), 999);
1100
    ///
1101
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1102
    /// assert_eq!(ts.subsec_millisecond(), -123);
1103
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1104
    /// assert_eq!(ts.subsec_millisecond(), -999);
1105
    ///
1106
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1107
    /// ```
1108
    #[inline]
1109
0
    pub fn subsec_millisecond(self) -> i32 {
1110
0
        self.subsec_millisecond_ranged().get()
1111
0
    }
1112
1113
    /// Returns the fractional second component of this timestamp in units of
1114
    /// microseconds.
1115
    ///
1116
    /// It is guaranteed that this will never return a value that is greater
1117
    /// than 1 second (or less than -1 second).
1118
    ///
1119
    /// This only returns the number of whole microseconds. That is, if there
1120
    /// are any fractional microseconds in this timestamp, then they are
1121
    /// truncated.
1122
    ///
1123
    /// # Example
1124
    ///
1125
    /// ```
1126
    /// use jiff::Timestamp;
1127
    ///
1128
    /// let ts = Timestamp::new(5, 123_456_789)?;
1129
    /// assert_eq!(ts.subsec_microsecond(), 123_456);
1130
    /// let ts = Timestamp::new(5, 999_999_999)?;
1131
    /// assert_eq!(ts.subsec_microsecond(), 999_999);
1132
    ///
1133
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1134
    /// assert_eq!(ts.subsec_microsecond(), -123_456);
1135
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1136
    /// assert_eq!(ts.subsec_microsecond(), -999_999);
1137
    ///
1138
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1139
    /// ```
1140
    #[inline]
1141
0
    pub fn subsec_microsecond(self) -> i32 {
1142
0
        self.subsec_microsecond_ranged().get()
1143
0
    }
1144
1145
    /// Returns the fractional second component of this timestamp in units of
1146
    /// nanoseconds.
1147
    ///
1148
    /// It is guaranteed that this will never return a value that is greater
1149
    /// than 1 second (or less than -1 second).
1150
    ///
1151
    /// # Example
1152
    ///
1153
    /// ```
1154
    /// use jiff::Timestamp;
1155
    ///
1156
    /// let ts = Timestamp::new(5, 123_456_789)?;
1157
    /// assert_eq!(ts.subsec_nanosecond(), 123_456_789);
1158
    /// let ts = Timestamp::new(5, 999_999_999)?;
1159
    /// assert_eq!(ts.subsec_nanosecond(), 999_999_999);
1160
    ///
1161
    /// let ts = Timestamp::new(-5, -123_456_789)?;
1162
    /// assert_eq!(ts.subsec_nanosecond(), -123_456_789);
1163
    /// let ts = Timestamp::new(-5, -999_999_999)?;
1164
    /// assert_eq!(ts.subsec_nanosecond(), -999_999_999);
1165
    ///
1166
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1167
    /// ```
1168
    #[inline]
1169
0
    pub fn subsec_nanosecond(self) -> i32 {
1170
0
        self.subsec_nanosecond_ranged().get()
1171
0
    }
1172
1173
    /// Returns this timestamp as a [`SignedDuration`] since the Unix epoch.
1174
    ///
1175
    /// # Example
1176
    ///
1177
    /// ```
1178
    /// use jiff::{SignedDuration, Timestamp};
1179
    ///
1180
    /// assert_eq!(
1181
    ///     Timestamp::UNIX_EPOCH.as_duration(),
1182
    ///     SignedDuration::ZERO,
1183
    /// );
1184
    /// assert_eq!(
1185
    ///     Timestamp::new(5, 123_456_789)?.as_duration(),
1186
    ///     SignedDuration::new(5, 123_456_789),
1187
    /// );
1188
    /// assert_eq!(
1189
    ///     Timestamp::new(-5, -123_456_789)?.as_duration(),
1190
    ///     SignedDuration::new(-5, -123_456_789),
1191
    /// );
1192
    ///
1193
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1194
    /// ```
1195
    #[inline]
1196
0
    pub fn as_duration(self) -> SignedDuration {
1197
0
        SignedDuration::from_timestamp(self)
1198
0
    }
1199
1200
    /// Returns the sign of this timestamp.
1201
    ///
1202
    /// This can return one of three possible values:
1203
    ///
1204
    /// * `0` when this timestamp is precisely equivalent to
1205
    /// [`Timestamp::UNIX_EPOCH`].
1206
    /// * `1` when this timestamp occurs after the Unix epoch.
1207
    /// * `-1` when this timestamp occurs before the Unix epoch.
1208
    ///
1209
    /// The sign returned is guaranteed to match the sign of all "getter"
1210
    /// methods on `Timestamp`. For example, [`Timestamp::as_second`] and
1211
    /// [`Timestamp::subsec_nanosecond`]. This is true even if the signs
1212
    /// of the `second` and `nanosecond` components were mixed when given to
1213
    /// the [`Timestamp::new`] constructor.
1214
    ///
1215
    /// # Example
1216
    ///
1217
    /// ```
1218
    /// use jiff::Timestamp;
1219
    ///
1220
    /// let ts = Timestamp::new(5, -999_999_999)?;
1221
    /// assert_eq!(ts.signum(), 1);
1222
    /// // The mixed signs were normalized away!
1223
    /// assert_eq!(ts.as_second(), 4);
1224
    /// assert_eq!(ts.subsec_nanosecond(), 1);
1225
    ///
1226
    /// // The same applies for negative timestamps.
1227
    /// let ts = Timestamp::new(-5, 999_999_999)?;
1228
    /// assert_eq!(ts.signum(), -1);
1229
    /// assert_eq!(ts.as_second(), -4);
1230
    /// assert_eq!(ts.subsec_nanosecond(), -1);
1231
    ///
1232
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1233
    /// ```
1234
    #[inline]
1235
0
    pub fn signum(self) -> i8 {
1236
0
        if self.is_zero() {
1237
0
            0
1238
0
        } else if self.as_second() > 0 || self.subsec_nanosecond() > 0 {
1239
0
            1
1240
        } else {
1241
0
            -1
1242
        }
1243
0
    }
1244
1245
    /// Returns true if and only if this timestamp corresponds to the instant
1246
    /// in time known as the Unix epoch.
1247
    ///
1248
    /// # Example
1249
    ///
1250
    /// ```
1251
    /// use jiff::Timestamp;
1252
    ///
1253
    /// assert!(Timestamp::UNIX_EPOCH.is_zero());
1254
    /// ```
1255
    #[inline]
1256
0
    pub fn is_zero(self) -> bool {
1257
0
        self.as_second() == 0 && self.subsec_nanosecond() == 0
1258
0
    }
1259
1260
    /// Creates a [`Zoned`] value by attaching a time zone for the given name
1261
    /// to this instant in time.
1262
    ///
1263
    /// The name given is resolved to a [`TimeZone`] by using the default
1264
    /// [`TimeZoneDatabase`](crate::tz::TimeZoneDatabase) created by
1265
    /// [`tz::db`](crate::tz::db). Indeed, this is a convenience function
1266
    /// for [`Timestamp::to_zoned`] where the time zone database lookup
1267
    /// is done automatically.
1268
    ///
1269
    /// Assuming the time zone name could be resolved to a [`TimeZone`], this
1270
    /// routine is otherwise infallible and never results in any ambiguity
1271
    /// since both a [`Timestamp`] and a [`Zoned`] correspond to precise
1272
    /// instant in time. This is unlike
1273
    /// [`civil::DateTime::to_zoned`](crate::civil::DateTime::to_zoned),
1274
    /// where a civil datetime might correspond to more than one instant in
1275
    /// time (i.e., a fold, typically DST ending) or no instants in time (i.e.,
1276
    /// a gap, typically DST starting).
1277
    ///
1278
    /// # Errors
1279
    ///
1280
    /// This returns an error when the given time zone name could not be found
1281
    /// in the default time zone database.
1282
    ///
1283
    /// # Example
1284
    ///
1285
    /// This is a simple example of converting the instant that is `123,456,789`
1286
    /// seconds after the Unix epoch to an instant that is aware of its time
1287
    /// zone:
1288
    ///
1289
    /// ```
1290
    /// use jiff::Timestamp;
1291
    ///
1292
    /// let ts = Timestamp::new(123_456_789, 0).unwrap();
1293
    /// let zdt = ts.in_tz("America/New_York")?;
1294
    /// assert_eq!(zdt.to_string(), "1973-11-29T16:33:09-05:00[America/New_York]");
1295
    ///
1296
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1297
    /// ```
1298
    ///
1299
    /// This can be used to answer questions like, "What time was it at the
1300
    /// Unix epoch in Tasmania?"
1301
    ///
1302
    /// ```
1303
    /// use jiff::Timestamp;
1304
    ///
1305
    /// // Time zone database lookups are case insensitive!
1306
    /// let zdt = Timestamp::UNIX_EPOCH.in_tz("australia/tasmania")?;
1307
    /// assert_eq!(zdt.to_string(), "1970-01-01T11:00:00+11:00[Australia/Tasmania]");
1308
    ///
1309
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1310
    /// ```
1311
    ///
1312
    /// # Example: errors
1313
    ///
1314
    /// This routine can return an error when the time zone is unrecognized:
1315
    ///
1316
    /// ```
1317
    /// use jiff::Timestamp;
1318
    ///
1319
    /// assert!(Timestamp::UNIX_EPOCH.in_tz("does not exist").is_err());
1320
    /// ```
1321
    #[inline]
1322
0
    pub fn in_tz(self, time_zone_name: &str) -> Result<Zoned, Error> {
1323
0
        let tz = crate::tz::db().get(time_zone_name)?;
1324
0
        Ok(self.to_zoned(tz))
1325
0
    }
1326
1327
    /// Creates a [`Zoned`] value by attaching the given time zone to this
1328
    /// instant in time.
1329
    ///
1330
    /// This is infallible and never results in any ambiguity since both a
1331
    /// [`Timestamp`] and a [`Zoned`] correspond to precise instant in time.
1332
    /// This is unlike
1333
    /// [`civil::DateTime::to_zoned`](crate::civil::DateTime::to_zoned),
1334
    /// where a civil datetime might correspond to more than one instant in
1335
    /// time (i.e., a fold, typically DST ending) or no instants in time (i.e.,
1336
    /// a gap, typically DST starting).
1337
    ///
1338
    /// In the common case of a time zone being represented as a name string,
1339
    /// like `Australia/Tasmania`, consider using [`Timestamp::in_tz`]
1340
    /// instead.
1341
    ///
1342
    /// # Example
1343
    ///
1344
    /// This example shows how to create a zoned value with a fixed time zone
1345
    /// offset:
1346
    ///
1347
    /// ```
1348
    /// use jiff::{tz::{self, TimeZone}, Timestamp};
1349
    ///
1350
    /// let ts = Timestamp::new(123_456_789, 0).unwrap();
1351
    /// let tz = TimeZone::fixed(tz::offset(-4));
1352
    /// let zdt = ts.to_zoned(tz);
1353
    /// // A time zone annotation is still included in the printable version
1354
    /// // of the Zoned value, but it is fixed to a particular offset.
1355
    /// assert_eq!(zdt.to_string(), "1973-11-29T17:33:09-04:00[-04:00]");
1356
    /// ```
1357
    ///
1358
    /// # Example: POSIX time zone strings
1359
    ///
1360
    /// This example shows how to create a time zone from a POSIX time zone
1361
    /// string that describes the transition to and from daylight saving
1362
    /// time for `America/St_Johns`. In particular, this rule uses non-zero
1363
    /// minutes, which is atypical.
1364
    ///
1365
    /// ```
1366
    /// use jiff::{tz::TimeZone, Timestamp};
1367
    ///
1368
    /// let ts = Timestamp::new(123_456_789, 0)?;
1369
    /// let tz = TimeZone::posix("NST3:30NDT,M3.2.0,M11.1.0")?;
1370
    /// let zdt = ts.to_zoned(tz);
1371
    /// // There isn't any agreed upon mechanism for transmitting a POSIX time
1372
    /// // zone string within an RFC 9557 TZ annotation, so Jiff just emits the
1373
    /// // offset. In practice, POSIX TZ strings are rarely user facing anyway.
1374
    /// // (They are still in widespread use as an implementation detail of the
1375
    /// // IANA Time Zone Database however.)
1376
    /// assert_eq!(zdt.to_string(), "1973-11-29T18:03:09-03:30[-03:30]");
1377
    ///
1378
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1379
    /// ```
1380
    #[inline]
1381
0
    pub fn to_zoned(self, tz: TimeZone) -> Zoned {
1382
0
        Zoned::new(self, tz)
1383
0
    }
1384
1385
    /// Add the given span of time to this timestamp.
1386
    ///
1387
    /// This operation accepts three different duration types: [`Span`],
1388
    /// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
1389
    /// `From` trait implementations for the [`TimestampArithmetic`] type.
1390
    ///
1391
    /// # Properties
1392
    ///
1393
    /// Given a timestamp `ts1` and a span `s`, and assuming `ts2 = ts1 + s`
1394
    /// exists, it follows then that `ts1 = ts2 - s` for all values of `ts1`
1395
    /// and `s` that sum to a valid `ts2`.
1396
    ///
1397
    /// In short, subtracting the given span from the sum returned by this
1398
    /// function is guaranteed to result in precisely the original timestamp.
1399
    ///
1400
    /// # Errors
1401
    ///
1402
    /// If the sum would overflow the minimum or maximum timestamp values, then
1403
    /// an error is returned.
1404
    ///
1405
    /// This also returns an error if the given duration is a `Span` with any
1406
    /// non-zero units greater than hours. If you want to use bigger units,
1407
    /// convert this timestamp to a `Zoned` and use [`Zoned::checked_add`].
1408
    /// This error occurs because a `Timestamp` has no time zone attached to
1409
    /// it, and thus cannot unambiguously resolve the length of a single day.
1410
    ///
1411
    /// # Example
1412
    ///
1413
    /// This shows how to add `5` hours to the Unix epoch:
1414
    ///
1415
    /// ```
1416
    /// use jiff::{Timestamp, ToSpan};
1417
    ///
1418
    /// let ts = Timestamp::UNIX_EPOCH.checked_add(5.hours())?;
1419
    /// assert_eq!(ts.to_string(), "1970-01-01T05:00:00Z");
1420
    ///
1421
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1422
    /// ```
1423
    ///
1424
    /// # Example: negative spans are supported
1425
    ///
1426
    /// This shows how to add `-5` hours to the Unix epoch. This is the same
1427
    /// as subtracting `5` hours from the Unix epoch.
1428
    ///
1429
    /// ```
1430
    /// use jiff::{Timestamp, ToSpan};
1431
    ///
1432
    /// let ts = Timestamp::UNIX_EPOCH.checked_add(-5.hours())?;
1433
    /// assert_eq!(ts.to_string(), "1969-12-31T19:00:00Z");
1434
    ///
1435
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1436
    /// ```
1437
    ///
1438
    /// # Example: available via addition operator
1439
    ///
1440
    /// This routine can be used via the `+` operator. Note though that if it
1441
    /// fails, it will result in a panic.
1442
    ///
1443
    /// ```
1444
    /// use jiff::{Timestamp, ToSpan};
1445
    ///
1446
    /// let ts1 = Timestamp::new(2_999_999_999, 0)?;
1447
    /// assert_eq!(ts1.to_string(), "2065-01-24T05:19:59Z");
1448
    ///
1449
    /// let ts2 = ts1 + 1.hour().minutes(30).nanoseconds(123);
1450
    /// assert_eq!(ts2.to_string(), "2065-01-24T06:49:59.000000123Z");
1451
    ///
1452
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1453
    /// ```
1454
    ///
1455
    /// # Example: error on overflow
1456
    ///
1457
    /// ```
1458
    /// use jiff::{Timestamp, ToSpan};
1459
    ///
1460
    /// let ts = Timestamp::MAX;
1461
    /// assert_eq!(ts.to_string(), "9999-12-30T22:00:00.999999999Z");
1462
    /// assert!(ts.checked_add(1.nanosecond()).is_err());
1463
    ///
1464
    /// let ts = Timestamp::MIN;
1465
    /// assert_eq!(ts.to_string(), "-009999-01-02T01:59:59Z");
1466
    /// assert!(ts.checked_add(-1.nanosecond()).is_err());
1467
    /// ```
1468
    ///
1469
    /// # Example: adding absolute durations
1470
    ///
1471
    /// This shows how to add signed and unsigned absolute durations to a
1472
    /// `Timestamp`.
1473
    ///
1474
    /// ```
1475
    /// use std::time::Duration;
1476
    ///
1477
    /// use jiff::{SignedDuration, Timestamp};
1478
    ///
1479
    /// let ts1 = Timestamp::new(2_999_999_999, 0)?;
1480
    /// assert_eq!(ts1.to_string(), "2065-01-24T05:19:59Z");
1481
    ///
1482
    /// let dur = SignedDuration::new(60 * 60 + 30 * 60, 123);
1483
    /// assert_eq!(
1484
    ///     ts1.checked_add(dur)?.to_string(),
1485
    ///     "2065-01-24T06:49:59.000000123Z",
1486
    /// );
1487
    ///
1488
    /// let dur = Duration::new(60 * 60 + 30 * 60, 123);
1489
    /// assert_eq!(
1490
    ///     ts1.checked_add(dur)?.to_string(),
1491
    ///     "2065-01-24T06:49:59.000000123Z",
1492
    /// );
1493
    ///
1494
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1495
    /// ```
1496
    #[inline]
1497
0
    pub fn checked_add<A: Into<TimestampArithmetic>>(
1498
0
        self,
1499
0
        duration: A,
1500
0
    ) -> Result<Timestamp, Error> {
1501
0
        let duration: TimestampArithmetic = duration.into();
1502
0
        duration.checked_add(self)
1503
0
    }
Unexecuted instantiation: <jiff::timestamp::Timestamp>::checked_add::<jiff::signed_duration::SignedDuration>
Unexecuted instantiation: <jiff::timestamp::Timestamp>::checked_add::<jiff::span::Span>
1504
1505
    #[inline]
1506
0
    fn checked_add_span(self, span: Span) -> Result<Timestamp, Error> {
1507
0
        if let Some(err) = span.smallest_non_time_non_zero_unit_error() {
1508
0
            return Err(err);
1509
0
        }
1510
0
        if span.is_zero() {
1511
0
            return Ok(self);
1512
0
        }
1513
        // The common case is probably a span without fractional seconds, so
1514
        // we specialize for that since it requires a fair bit less math.
1515
        //
1516
        // Note that this only works when *both* the span and timestamp lack
1517
        // fractional seconds.
1518
0
        if self.subsec_nanosecond_ranged() == C(0) {
1519
0
            if let Some(span_seconds) = span.to_invariant_seconds() {
1520
0
                let time_seconds = self.as_second_ranged();
1521
0
                let sum = time_seconds
1522
0
                    .try_checked_add("span", span_seconds)
1523
0
                    .with_context(|| {
1524
0
                    err!("adding {span} to {self} overflowed")
1525
0
                })?;
1526
0
                return Ok(Timestamp::from_second_ranged(sum));
1527
0
            }
1528
0
        }
1529
0
        let time_nanos = self.as_nanosecond_ranged();
1530
0
        let span_nanos = span.to_invariant_nanoseconds();
1531
0
        let sum = time_nanos
1532
0
            .try_checked_add("span", span_nanos)
1533
0
            .with_context(|| err!("adding {span} to {self} overflowed"))?;
1534
0
        Ok(Timestamp::from_nanosecond_ranged(sum))
1535
0
    }
1536
1537
    #[inline]
1538
0
    fn checked_add_duration(
1539
0
        self,
1540
0
        duration: SignedDuration,
1541
0
    ) -> Result<Timestamp, Error> {
1542
0
        let start = self.as_duration();
1543
0
        let end = start.checked_add(duration).ok_or_else(|| {
1544
0
            err!("overflow when adding {duration:?} to {self}")
1545
0
        })?;
1546
0
        Timestamp::from_duration(end)
1547
0
    }
1548
1549
    /// This routine is identical to [`Timestamp::checked_add`] with the
1550
    /// duration negated.
1551
    ///
1552
    /// # Errors
1553
    ///
1554
    /// This has the same error conditions as [`Timestamp::checked_add`].
1555
    ///
1556
    /// # Example
1557
    ///
1558
    /// This routine can be used via the `-` operator. Note though that if it
1559
    /// fails, it will result in a panic.
1560
    ///
1561
    /// ```
1562
    /// use jiff::{SignedDuration, Timestamp, ToSpan};
1563
    ///
1564
    /// let ts1 = Timestamp::new(2_999_999_999, 0)?;
1565
    /// assert_eq!(ts1.to_string(), "2065-01-24T05:19:59Z");
1566
    ///
1567
    /// let ts2 = ts1 - 1.hour().minutes(30).nanoseconds(123);
1568
    /// assert_eq!(ts2.to_string(), "2065-01-24T03:49:58.999999877Z");
1569
    ///
1570
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1571
    /// ```
1572
    ///
1573
    /// # Example: use with [`SignedDuration`] and [`std::time::Duration`]
1574
    ///
1575
    /// ```
1576
    /// use std::time::Duration;
1577
    ///
1578
    /// use jiff::{SignedDuration, Timestamp};
1579
    ///
1580
    /// let ts1 = Timestamp::new(2_999_999_999, 0)?;
1581
    /// assert_eq!(ts1.to_string(), "2065-01-24T05:19:59Z");
1582
    ///
1583
    /// let dur = SignedDuration::new(60 * 60 + 30 * 60, 123);
1584
    /// assert_eq!(
1585
    ///     ts1.checked_sub(dur)?.to_string(),
1586
    ///     "2065-01-24T03:49:58.999999877Z",
1587
    /// );
1588
    ///
1589
    /// let dur = Duration::new(60 * 60 + 30 * 60, 123);
1590
    /// assert_eq!(
1591
    ///     ts1.checked_sub(dur)?.to_string(),
1592
    ///     "2065-01-24T03:49:58.999999877Z",
1593
    /// );
1594
    ///
1595
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1596
    /// ```
1597
    #[inline]
1598
0
    pub fn checked_sub<A: Into<TimestampArithmetic>>(
1599
0
        self,
1600
0
        duration: A,
1601
0
    ) -> Result<Timestamp, Error> {
1602
0
        let duration: TimestampArithmetic = duration.into();
1603
0
        duration.checked_neg().and_then(|ta| ta.checked_add(self))
1604
0
    }
1605
1606
    /// This routine is identical to [`Timestamp::checked_add`], except the
1607
    /// result saturates on overflow. That is, instead of overflow, either
1608
    /// [`Timestamp::MIN`] or [`Timestamp::MAX`] is returned.
1609
    ///
1610
    /// # Errors
1611
    ///
1612
    /// This returns an error if the given `Span` contains any non-zero units
1613
    /// greater than hours.
1614
    ///
1615
    /// # Example
1616
    ///
1617
    /// This example shows that arithmetic saturates on overflow.
1618
    ///
1619
    /// ```
1620
    /// use jiff::{SignedDuration, Timestamp, ToSpan};
1621
    ///
1622
    /// assert_eq!(
1623
    ///     Timestamp::MAX,
1624
    ///     Timestamp::MAX.saturating_add(1.nanosecond())?,
1625
    /// );
1626
    /// assert_eq!(
1627
    ///     Timestamp::MIN,
1628
    ///     Timestamp::MIN.saturating_add(-1.nanosecond())?,
1629
    /// );
1630
    /// assert_eq!(
1631
    ///     Timestamp::MAX,
1632
    ///     Timestamp::UNIX_EPOCH.saturating_add(SignedDuration::MAX)?,
1633
    /// );
1634
    /// assert_eq!(
1635
    ///     Timestamp::MIN,
1636
    ///     Timestamp::UNIX_EPOCH.saturating_add(SignedDuration::MIN)?,
1637
    /// );
1638
    /// assert_eq!(
1639
    ///     Timestamp::MAX,
1640
    ///     Timestamp::UNIX_EPOCH.saturating_add(std::time::Duration::MAX)?,
1641
    /// );
1642
    ///
1643
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1644
    /// ```
1645
    #[inline]
1646
0
    pub fn saturating_add<A: Into<TimestampArithmetic>>(
1647
0
        self,
1648
0
        duration: A,
1649
0
    ) -> Result<Timestamp, Error> {
1650
0
        let duration: TimestampArithmetic = duration.into();
1651
0
        duration.saturating_add(self).context(
1652
            "saturating `Timestamp` arithmetic requires only time units",
1653
        )
1654
0
    }
1655
1656
    /// This routine is identical to [`Timestamp::saturating_add`] with the
1657
    /// span parameter negated.
1658
    ///
1659
    /// # Errors
1660
    ///
1661
    /// This returns an error if the given `Span` contains any non-zero units
1662
    /// greater than hours.
1663
    ///
1664
    /// # Example
1665
    ///
1666
    /// This example shows that arithmetic saturates on overflow.
1667
    ///
1668
    /// ```
1669
    /// use jiff::{SignedDuration, Timestamp, ToSpan};
1670
    ///
1671
    /// assert_eq!(
1672
    ///     Timestamp::MIN,
1673
    ///     Timestamp::MIN.saturating_sub(1.nanosecond())?,
1674
    /// );
1675
    /// assert_eq!(
1676
    ///     Timestamp::MAX,
1677
    ///     Timestamp::MAX.saturating_sub(-1.nanosecond())?,
1678
    /// );
1679
    /// assert_eq!(
1680
    ///     Timestamp::MIN,
1681
    ///     Timestamp::UNIX_EPOCH.saturating_sub(SignedDuration::MAX)?,
1682
    /// );
1683
    /// assert_eq!(
1684
    ///     Timestamp::MAX,
1685
    ///     Timestamp::UNIX_EPOCH.saturating_sub(SignedDuration::MIN)?,
1686
    /// );
1687
    /// assert_eq!(
1688
    ///     Timestamp::MIN,
1689
    ///     Timestamp::UNIX_EPOCH.saturating_sub(std::time::Duration::MAX)?,
1690
    /// );
1691
    ///
1692
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1693
    /// ```
1694
    #[inline]
1695
0
    pub fn saturating_sub<A: Into<TimestampArithmetic>>(
1696
0
        self,
1697
0
        duration: A,
1698
0
    ) -> Result<Timestamp, Error> {
1699
0
        let duration: TimestampArithmetic = duration.into();
1700
0
        let Ok(duration) = duration.checked_neg() else {
1701
0
            return Ok(Timestamp::MIN);
1702
        };
1703
0
        self.saturating_add(duration)
1704
0
    }
1705
1706
    /// Returns a span representing the elapsed time from this timestamp until
1707
    /// the given `other` timestamp.
1708
    ///
1709
    /// When `other` occurs before this timestamp, then the span returned will
1710
    /// be negative.
1711
    ///
1712
    /// Depending on the input provided, the span returned is rounded. It may
1713
    /// also be balanced up to bigger units than the default. By default,
1714
    /// the span returned is balanced such that the biggest possible unit is
1715
    /// seconds.
1716
    ///
1717
    /// This operation is configured by providing a [`TimestampDifference`]
1718
    /// value. Since this routine accepts anything that implements
1719
    /// `Into<TimestampDifference>`, once can pass a `Timestamp` directly.
1720
    /// One can also pass a `(Unit, Timestamp)`, where `Unit` is treated as
1721
    /// [`TimestampDifference::largest`].
1722
    ///
1723
    /// # Properties
1724
    ///
1725
    /// It is guaranteed that if the returned span is subtracted from `other`,
1726
    /// and if no rounding is requested, then the original timestamp will be
1727
    /// returned.
1728
    ///
1729
    /// This routine is equivalent to `self.since(other).map(|span| -span)`
1730
    /// if no rounding options are set. If rounding options are set, then
1731
    /// it's equivalent to
1732
    /// `self.since(other_without_rounding_options).map(|span| -span)`,
1733
    /// followed by a call to [`Span::round`] with the appropriate rounding
1734
    /// options set. This is because the negation of a span can result in
1735
    /// different rounding results depending on the rounding mode.
1736
    ///
1737
    /// # Errors
1738
    ///
1739
    /// An error can occur in some cases when the requested configuration
1740
    /// would result in a span that is beyond allowable limits. For example,
1741
    /// the nanosecond component of a span cannot represent the span of
1742
    /// time between the minimum and maximum timestamps supported by Jiff.
1743
    /// Therefore, if one requests a span with its largest unit set to
1744
    /// [`Unit::Nanosecond`], then it's possible for this routine to fail.
1745
    ///
1746
    /// An error can also occur if `TimestampDifference` is misconfigured. For
1747
    /// example, if the smallest unit provided is bigger than the largest unit,
1748
    /// or if the largest unit provided is bigger than hours. (To use bigger
1749
    /// units with an instant in time, use [`Zoned::until`] instead.)
1750
    ///
1751
    /// It is guaranteed that if one provides a timestamp with the default
1752
    /// [`TimestampDifference`] configuration, then this routine will never
1753
    /// fail.
1754
    ///
1755
    /// # Example
1756
    ///
1757
    /// ```
1758
    /// use jiff::{Timestamp, ToSpan};
1759
    ///
1760
    /// let earlier: Timestamp = "2006-08-24T22:30:00Z".parse()?;
1761
    /// let later: Timestamp = "2019-01-31 21:00:00Z".parse()?;
1762
    /// assert_eq!(earlier.until(later)?, 392509800.seconds().fieldwise());
1763
    ///
1764
    /// // Flipping the timestamps is fine, but you'll get a negative span.
1765
    /// assert_eq!(later.until(earlier)?, -392509800.seconds().fieldwise());
1766
    ///
1767
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1768
    /// ```
1769
    ///
1770
    /// # Example: using bigger units
1771
    ///
1772
    /// This example shows how to expand the span returned to bigger units.
1773
    /// This makes use of a `From<(Unit, Timestamp)> for TimestampDifference`
1774
    /// trait implementation.
1775
    ///
1776
    /// ```
1777
    /// use jiff::{Timestamp, ToSpan, Unit};
1778
    ///
1779
    /// let ts1: Timestamp = "1995-12-07T03:24:30.000003500Z".parse()?;
1780
    /// let ts2: Timestamp = "2019-01-31 15:30:00Z".parse()?;
1781
    ///
1782
    /// // The default limits durations to using "seconds" as the biggest unit.
1783
    /// let span = ts1.until(ts2)?;
1784
    /// assert_eq!(span.to_string(), "PT730641929.9999965S");
1785
    ///
1786
    /// // But we can ask for units all the way up to hours.
1787
    /// let span = ts1.until((Unit::Hour, ts2))?;
1788
    /// assert_eq!(span.to_string(), "PT202956H5M29.9999965S");
1789
    ///
1790
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1791
    /// ```
1792
    ///
1793
    /// # Example: rounding the result
1794
    ///
1795
    /// This shows how one might find the difference between two timestamps and
1796
    /// have the result rounded such that sub-seconds are removed.
1797
    ///
1798
    /// In this case, we need to hand-construct a [`TimestampDifference`]
1799
    /// in order to gain full configurability.
1800
    ///
1801
    /// ```
1802
    /// use jiff::{Timestamp, TimestampDifference, ToSpan, Unit};
1803
    ///
1804
    /// let ts1: Timestamp = "1995-12-07 03:24:30.000003500Z".parse()?;
1805
    /// let ts2: Timestamp = "2019-01-31 15:30:00Z".parse()?;
1806
    ///
1807
    /// let span = ts1.until(
1808
    ///     TimestampDifference::from(ts2).smallest(Unit::Second),
1809
    /// )?;
1810
    /// assert_eq!(span.to_string(), "PT730641929S");
1811
    ///
1812
    /// // We can combine smallest and largest units too!
1813
    /// let span = ts1.until(
1814
    ///     TimestampDifference::from(ts2)
1815
    ///         .smallest(Unit::Second)
1816
    ///         .largest(Unit::Hour),
1817
    /// )?;
1818
    /// assert_eq!(span.to_string(), "PT202956H5M29S");
1819
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1820
    /// ```
1821
    #[inline]
1822
0
    pub fn until<A: Into<TimestampDifference>>(
1823
0
        self,
1824
0
        other: A,
1825
0
    ) -> Result<Span, Error> {
1826
0
        let args: TimestampDifference = other.into();
1827
0
        let span = args.until_with_largest_unit(self)?;
1828
0
        if args.rounding_may_change_span() {
1829
0
            span.round(args.round)
1830
        } else {
1831
0
            Ok(span)
1832
        }
1833
0
    }
1834
1835
    /// This routine is identical to [`Timestamp::until`], but the order of the
1836
    /// parameters is flipped.
1837
    ///
1838
    /// # Errors
1839
    ///
1840
    /// This has the same error conditions as [`Timestamp::until`].
1841
    ///
1842
    /// # Example
1843
    ///
1844
    /// This routine can be used via the `-` operator. Since the default
1845
    /// configuration is used and because a `Span` can represent the difference
1846
    /// between any two possible timestamps, it will never panic.
1847
    ///
1848
    /// ```
1849
    /// use jiff::{Timestamp, ToSpan};
1850
    ///
1851
    /// let earlier: Timestamp = "2006-08-24T22:30:00Z".parse()?;
1852
    /// let later: Timestamp = "2019-01-31 21:00:00Z".parse()?;
1853
    /// assert_eq!(later - earlier, 392509800.seconds().fieldwise());
1854
    ///
1855
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1856
    /// ```
1857
    #[inline]
1858
0
    pub fn since<A: Into<TimestampDifference>>(
1859
0
        self,
1860
0
        other: A,
1861
0
    ) -> Result<Span, Error> {
1862
0
        let args: TimestampDifference = other.into();
1863
0
        let span = -args.until_with_largest_unit(self)?;
1864
0
        if args.rounding_may_change_span() {
1865
0
            span.round(args.round)
1866
        } else {
1867
0
            Ok(span)
1868
        }
1869
0
    }
1870
1871
    /// Returns an absolute duration representing the elapsed time from this
1872
    /// timestamp until the given `other` timestamp.
1873
    ///
1874
    /// When `other` occurs before this timestamp, then the duration returned
1875
    /// will be negative.
1876
    ///
1877
    /// Unlike [`Timestamp::until`], this always returns a duration
1878
    /// corresponding to a 96-bit integer of nanoseconds between two
1879
    /// timestamps.
1880
    ///
1881
    /// # Fallibility
1882
    ///
1883
    /// This routine never panics or returns an error. Since there are no
1884
    /// configuration options that can be incorrectly provided, no error is
1885
    /// possible when calling this routine. In contrast, [`Timestamp::until`]
1886
    /// can return an error in some cases due to misconfiguration. But like
1887
    /// this routine, [`Timestamp::until`] never panics or returns an error in
1888
    /// its default configuration.
1889
    ///
1890
    /// # When should I use this versus [`Timestamp::until`]?
1891
    ///
1892
    /// See the type documentation for [`SignedDuration`] for the section on
1893
    /// when one should use [`Span`] and when one should use `SignedDuration`.
1894
    /// In short, use `Span` (and therefore `Timestamp::until`) unless you have
1895
    /// a specific reason to do otherwise.
1896
    ///
1897
    /// # Example
1898
    ///
1899
    /// ```
1900
    /// use jiff::{Timestamp, SignedDuration};
1901
    ///
1902
    /// let earlier: Timestamp = "2006-08-24T22:30:00Z".parse()?;
1903
    /// let later: Timestamp = "2019-01-31 21:00:00Z".parse()?;
1904
    /// assert_eq!(
1905
    ///     earlier.duration_until(later),
1906
    ///     SignedDuration::from_secs(392509800),
1907
    /// );
1908
    ///
1909
    /// // Flipping the timestamps is fine, but you'll get a negative span.
1910
    /// assert_eq!(
1911
    ///     later.duration_until(earlier),
1912
    ///     SignedDuration::from_secs(-392509800),
1913
    /// );
1914
    ///
1915
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1916
    /// ```
1917
    ///
1918
    /// # Example: difference with [`Timestamp::until`]
1919
    ///
1920
    /// The primary difference between this routine and
1921
    /// `Timestamp::until`, other than the return type, is that this
1922
    /// routine is likely to be faster. Namely, it does simple 96-bit
1923
    /// integer math, where as `Timestamp::until` has to do a bit more
1924
    /// work to deal with the different types of units on a `Span`.
1925
    ///
1926
    /// Additionally, since the difference between two timestamps is always
1927
    /// expressed in units of hours or smaller, and units of hours or smaller
1928
    /// are always uniform, there is no "expressive" difference between this
1929
    /// routine and `Timestamp::until`. Because of this, one can always
1930
    /// convert between `Span` and `SignedDuration` as returned by methods
1931
    /// on `Timestamp` without a relative datetime:
1932
    ///
1933
    /// ```
1934
    /// use jiff::{SignedDuration, Span, Timestamp};
1935
    ///
1936
    /// let ts1: Timestamp = "2024-02-28T00:00:00Z".parse()?;
1937
    /// let ts2: Timestamp = "2024-03-01T00:00:00Z".parse()?;
1938
    /// let dur = ts1.duration_until(ts2);
1939
    /// // Guaranteed to never fail because the duration
1940
    /// // between two civil times never exceeds the limits
1941
    /// // of a `Span`.
1942
    /// let span = Span::try_from(dur).unwrap();
1943
    /// assert_eq!(format!("{span:#}"), "172800s");
1944
    /// // Guaranteed to succeed and always return the original
1945
    /// // duration because the units are always hours or smaller,
1946
    /// // and thus uniform. This means a relative datetime is
1947
    /// // never required to do this conversion.
1948
    /// let dur = SignedDuration::try_from(span).unwrap();
1949
    /// assert_eq!(dur, SignedDuration::from_secs(172_800));
1950
    ///
1951
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1952
    /// ```
1953
    ///
1954
    /// This conversion guarantee also applies to [`Timestamp::until`] since it
1955
    /// always returns a balanced span. That is, it never returns spans like
1956
    /// `1 second 1000 milliseconds`. (Those cannot be losslessly converted to
1957
    /// a `SignedDuration` since a `SignedDuration` is only represented as a
1958
    /// single 96-bit integer of nanoseconds.)
1959
    #[inline]
1960
0
    pub fn duration_until(self, other: Timestamp) -> SignedDuration {
1961
0
        SignedDuration::timestamp_until(self, other)
1962
0
    }
1963
1964
    /// This routine is identical to [`Timestamp::duration_until`], but the
1965
    /// order of the parameters is flipped.
1966
    ///
1967
    /// # Example
1968
    ///
1969
    /// ```
1970
    /// use jiff::{SignedDuration, Timestamp};
1971
    ///
1972
    /// let earlier: Timestamp = "2006-08-24T22:30:00Z".parse()?;
1973
    /// let later: Timestamp = "2019-01-31 21:00:00Z".parse()?;
1974
    /// assert_eq!(
1975
    ///     later.duration_since(earlier),
1976
    ///     SignedDuration::from_secs(392509800),
1977
    /// );
1978
    ///
1979
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1980
    /// ```
1981
    #[inline]
1982
0
    pub fn duration_since(self, other: Timestamp) -> SignedDuration {
1983
0
        SignedDuration::timestamp_until(other, self)
1984
0
    }
1985
1986
    /// Rounds this timestamp according to the [`TimestampRound`] configuration
1987
    /// given.
1988
    ///
1989
    /// The principal option is [`TimestampRound::smallest`], which allows
1990
    /// one to configure the smallest units in the returned timestamp.
1991
    /// Rounding is what determines whether the specified smallest unit
1992
    /// should keep its current value or whether it should be incremented.
1993
    /// Moreover, the amount it should be incremented can be configured via
1994
    /// [`TimestampRound::increment`]. Finally, the rounding strategy itself
1995
    /// can be configured via [`TimestampRound::mode`].
1996
    ///
1997
    /// Note that this routine is generic and accepts anything that
1998
    /// implements `Into<TimestampRound>`. Some notable implementations are:
1999
    ///
2000
    /// * `From<Unit> for TimestampRound`, which will automatically create a
2001
    /// `TimestampRound::new().smallest(unit)` from the unit provided.
2002
    /// * `From<(Unit, i64)> for TimestampRound`, which will automatically
2003
    /// create a `TimestampRound::new().smallest(unit).increment(number)` from
2004
    /// the unit and increment provided.
2005
    ///
2006
    /// # Errors
2007
    ///
2008
    /// This returns an error if the smallest unit configured on the given
2009
    /// [`TimestampRound`] is bigger than hours.
2010
    ///
2011
    /// The rounding increment, when combined with the smallest unit (which
2012
    /// defaults to [`Unit::Nanosecond`]), must divide evenly into `86,400`
2013
    /// seconds (one 24-hour civil day). For example, increments of both
2014
    /// 45 seconds and 15 minutes are allowed, but 7 seconds and 25 minutes are
2015
    /// both not allowed.
2016
    ///
2017
    /// # Example
2018
    ///
2019
    /// This is a basic example that demonstrates rounding a timestamp to the
2020
    /// nearest hour. This also demonstrates calling this method with the
2021
    /// smallest unit directly, instead of constructing a `TimestampRound`
2022
    /// manually.
2023
    ///
2024
    /// ```
2025
    /// use jiff::{Timestamp, Unit};
2026
    ///
2027
    /// let ts: Timestamp = "2024-06-19 15:30:00Z".parse()?;
2028
    /// assert_eq!(
2029
    ///     ts.round(Unit::Hour)?.to_string(),
2030
    ///     "2024-06-19T16:00:00Z",
2031
    /// );
2032
    /// let ts: Timestamp = "2024-06-19 15:29:59Z".parse()?;
2033
    /// assert_eq!(
2034
    ///     ts.round(Unit::Hour)?.to_string(),
2035
    ///     "2024-06-19T15:00:00Z",
2036
    /// );
2037
    ///
2038
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2039
    /// ```
2040
    ///
2041
    /// # Example: changing the rounding mode
2042
    ///
2043
    /// The default rounding mode is [`RoundMode::HalfExpand`], which
2044
    /// breaks ties by rounding away from zero. But other modes like
2045
    /// [`RoundMode::Trunc`] can be used too:
2046
    ///
2047
    /// ```
2048
    /// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
2049
    ///
2050
    /// // The default will round up to the next hour for any time past the
2051
    /// // 30 minute mark, but using truncation rounding will always round
2052
    /// // down.
2053
    /// let ts: Timestamp = "2024-06-19 15:30:00Z".parse()?;
2054
    /// assert_eq!(
2055
    ///     ts.round(
2056
    ///         TimestampRound::new()
2057
    ///             .smallest(Unit::Hour)
2058
    ///             .mode(RoundMode::Trunc),
2059
    ///     )?.to_string(),
2060
    ///     "2024-06-19T15:00:00Z",
2061
    /// );
2062
    ///
2063
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2064
    /// ```
2065
    ///
2066
    /// # Example: rounding to the nearest 5 minute increment
2067
    ///
2068
    /// ```
2069
    /// use jiff::{Timestamp, Unit};
2070
    ///
2071
    /// // rounds down
2072
    /// let ts: Timestamp = "2024-06-19T15:27:29.999999999Z".parse()?;
2073
    /// assert_eq!(
2074
    ///     ts.round((Unit::Minute, 5))?.to_string(),
2075
    ///     "2024-06-19T15:25:00Z",
2076
    /// );
2077
    /// // rounds up
2078
    /// let ts: Timestamp = "2024-06-19T15:27:30Z".parse()?;
2079
    /// assert_eq!(
2080
    ///     ts.round((Unit::Minute, 5))?.to_string(),
2081
    ///     "2024-06-19T15:30:00Z",
2082
    /// );
2083
    ///
2084
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2085
    /// ```
2086
    #[inline]
2087
0
    pub fn round<R: Into<TimestampRound>>(
2088
0
        self,
2089
0
        options: R,
2090
0
    ) -> Result<Timestamp, Error> {
2091
0
        let options: TimestampRound = options.into();
2092
0
        options.round(self)
2093
0
    }
2094
2095
    /// Return an iterator of periodic timestamps determined by the given span.
2096
    ///
2097
    /// The given span may be negative, in which case, the iterator will move
2098
    /// backwards through time. The iterator won't stop until either the span
2099
    /// itself overflows, or it would otherwise exceed the minimum or maximum
2100
    /// `Timestamp` value.
2101
    ///
2102
    /// # Example: when to check a glucose monitor
2103
    ///
2104
    /// When my cat had diabetes, my veterinarian installed a glucose monitor
2105
    /// and instructed me to scan it about every 5 hours. This example lists
2106
    /// all of the times I need to scan it for the 2 days following its
2107
    /// installation:
2108
    ///
2109
    /// ```
2110
    /// use jiff::{Timestamp, ToSpan};
2111
    ///
2112
    /// let start: Timestamp = "2023-07-15 16:30:00-04".parse()?;
2113
    /// let end = start.checked_add(48.hours())?;
2114
    /// let mut scan_times = vec![];
2115
    /// for ts in start.series(5.hours()).take_while(|&ts| ts <= end) {
2116
    ///     scan_times.push(ts);
2117
    /// }
2118
    /// assert_eq!(scan_times, vec![
2119
    ///     "2023-07-15 16:30:00-04:00".parse::<Timestamp>()?,
2120
    ///     "2023-07-15 21:30:00-04:00".parse::<Timestamp>()?,
2121
    ///     "2023-07-16 02:30:00-04:00".parse::<Timestamp>()?,
2122
    ///     "2023-07-16 07:30:00-04:00".parse::<Timestamp>()?,
2123
    ///     "2023-07-16 12:30:00-04:00".parse::<Timestamp>()?,
2124
    ///     "2023-07-16 17:30:00-04:00".parse::<Timestamp>()?,
2125
    ///     "2023-07-16 22:30:00-04:00".parse::<Timestamp>()?,
2126
    ///     "2023-07-17 03:30:00-04:00".parse::<Timestamp>()?,
2127
    ///     "2023-07-17 08:30:00-04:00".parse::<Timestamp>()?,
2128
    ///     "2023-07-17 13:30:00-04:00".parse::<Timestamp>()?,
2129
    /// ]);
2130
    ///
2131
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2132
    /// ```
2133
    #[inline]
2134
0
    pub fn series(self, period: Span) -> TimestampSeries {
2135
0
        TimestampSeries::new(self, period)
2136
0
    }
2137
}
2138
2139
/// Parsing and formatting APIs.
2140
impl Timestamp {
2141
    /// Parses a timestamp (expressed as broken down time) in `input` matching
2142
    /// the given `format`.
2143
    ///
2144
    /// The format string uses a "printf"-style API where conversion
2145
    /// specifiers can be used as place holders to match components of
2146
    /// a datetime. For details on the specifiers supported, see the
2147
    /// [`fmt::strtime`] module documentation.
2148
    ///
2149
    /// # Errors
2150
    ///
2151
    /// This returns an error when parsing failed. This might happen because
2152
    /// the format string itself was invalid, or because the input didn't match
2153
    /// the format string.
2154
    ///
2155
    /// This also returns an error if there wasn't sufficient information to
2156
    /// construct a timestamp. For example, if an offset wasn't parsed. (The
2157
    /// offset is needed to turn the civil time parsed into a precise instant
2158
    /// in time.)
2159
    ///
2160
    /// # Example
2161
    ///
2162
    /// This example shows how to parse a datetime string into a timestamp:
2163
    ///
2164
    /// ```
2165
    /// use jiff::Timestamp;
2166
    ///
2167
    /// let ts = Timestamp::strptime("%F %H:%M %:z", "2024-07-14 21:14 -04:00")?;
2168
    /// assert_eq!(ts.to_string(), "2024-07-15T01:14:00Z");
2169
    ///
2170
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2171
    /// ```
2172
    #[inline]
2173
0
    pub fn strptime(
2174
0
        format: impl AsRef<[u8]>,
2175
0
        input: impl AsRef<[u8]>,
2176
0
    ) -> Result<Timestamp, Error> {
2177
0
        fmt::strtime::parse(format, input).and_then(|tm| tm.to_timestamp())
2178
0
    }
2179
2180
    /// Formats this timestamp according to the given `format`.
2181
    ///
2182
    /// The format string uses a "printf"-style API where conversion
2183
    /// specifiers can be used as place holders to format components of
2184
    /// a datetime. For details on the specifiers supported, see the
2185
    /// [`fmt::strtime`] module documentation.
2186
    ///
2187
    /// # Errors and panics
2188
    ///
2189
    /// While this routine itself does not error or panic, using the value
2190
    /// returned may result in a panic if formatting fails. See the
2191
    /// documentation on [`fmt::strtime::Display`] for more information.
2192
    ///
2193
    /// To format in a way that surfaces errors without panicking, use either
2194
    /// [`fmt::strtime::format`] or [`fmt::strtime::BrokenDownTime::format`].
2195
    ///
2196
    /// # Example
2197
    ///
2198
    /// This shows how to format a timestamp into a human readable datetime
2199
    /// in UTC:
2200
    ///
2201
    /// ```
2202
    /// use jiff::{civil::date, Timestamp};
2203
    ///
2204
    /// let ts = Timestamp::from_second(86_400)?;
2205
    /// let string = ts.strftime("%a %b %e %I:%M:%S %p UTC %Y").to_string();
2206
    /// assert_eq!(string, "Fri Jan  2 12:00:00 AM UTC 1970");
2207
    ///
2208
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2209
    /// ```
2210
    #[inline]
2211
0
    pub fn strftime<'f, F: 'f + ?Sized + AsRef<[u8]>>(
2212
0
        &self,
2213
0
        format: &'f F,
2214
0
    ) -> fmt::strtime::Display<'f> {
2215
0
        fmt::strtime::Display { fmt: format.as_ref(), tm: (*self).into() }
2216
0
    }
2217
2218
    /// Format a `Timestamp` datetime into a string with the given offset.
2219
    ///
2220
    /// This will format to an RFC 3339 compatible string with an offset.
2221
    ///
2222
    /// This will never use either `Z` (for Zulu time) or `-00:00` as an
2223
    /// offset. This is because Zulu time (and `-00:00`) mean "the time in UTC
2224
    /// is known, but the offset to local time is unknown." Since this routine
2225
    /// accepts an explicit offset, the offset is known. For example,
2226
    /// `Offset::UTC` will be formatted as `+00:00`.
2227
    ///
2228
    /// To format an RFC 3339 string in Zulu time, use the default
2229
    /// [`std::fmt::Display`] trait implementation on `Timestamp`.
2230
    ///
2231
    /// # Example
2232
    ///
2233
    /// ```
2234
    /// use jiff::{tz, Timestamp};
2235
    ///
2236
    /// let ts = Timestamp::from_second(1)?;
2237
    /// assert_eq!(
2238
    ///     ts.display_with_offset(tz::offset(-5)).to_string(),
2239
    ///     "1969-12-31T19:00:01-05:00",
2240
    /// );
2241
    ///
2242
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2243
    /// ```
2244
    #[inline]
2245
0
    pub fn display_with_offset(
2246
0
        &self,
2247
0
        offset: Offset,
2248
0
    ) -> TimestampDisplayWithOffset {
2249
0
        TimestampDisplayWithOffset { timestamp: *self, offset }
2250
0
    }
2251
}
2252
2253
/// Internal APIs using Jiff ranged integers.
2254
impl Timestamp {
2255
    #[inline]
2256
0
    pub(crate) fn new_ranged(
2257
0
        second: UnixSeconds,
2258
0
        nanosecond: FractionalNanosecond,
2259
0
    ) -> Result<Timestamp, Error> {
2260
0
        if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2261
0
            return Err(Error::range(
2262
0
                "seconds and nanoseconds",
2263
0
                nanosecond,
2264
0
                0,
2265
0
                1_000_000_000,
2266
0
            ));
2267
0
        }
2268
        // We now normalize our seconds and nanoseconds such that they have
2269
        // the same sign (or where one is zero). So for example, when given
2270
        // `-1s 1ns`, then we should turn that into `-999,999,999ns`.
2271
        //
2272
        // But first, if we're already normalized, we're done!
2273
0
        if second.signum() == nanosecond.signum()
2274
0
            || second == C(0)
2275
0
            || nanosecond == C(0)
2276
        {
2277
0
            return Ok(Timestamp { second, nanosecond });
2278
0
        }
2279
0
        let second = second.without_bounds();
2280
0
        let nanosecond = nanosecond.without_bounds();
2281
0
        let [delta_second, delta_nanosecond] = t::NoUnits::vary_many(
2282
0
            [second, nanosecond],
2283
0
            |[second, nanosecond]| {
2284
0
                if second < C(0) && nanosecond > C(0) {
2285
0
                    [C(1), (-t::NANOS_PER_SECOND).rinto()]
2286
0
                } else if second > C(0) && nanosecond < C(0) {
2287
0
                    [C(-1), t::NANOS_PER_SECOND.rinto()]
2288
                } else {
2289
0
                    [C(0), C(0)]
2290
                }
2291
0
            },
2292
        );
2293
0
        Ok(Timestamp {
2294
0
            second: (second + delta_second).rinto(),
2295
0
            nanosecond: (nanosecond + delta_nanosecond).rinto(),
2296
0
        })
2297
0
    }
2298
2299
    #[inline]
2300
0
    fn from_second_ranged(second: UnixSeconds) -> Timestamp {
2301
0
        Timestamp { second, nanosecond: FractionalNanosecond::N::<0>() }
2302
0
    }
2303
2304
    #[inline]
2305
0
    fn from_millisecond_ranged(millisecond: UnixMilliseconds) -> Timestamp {
2306
0
        let second =
2307
0
            UnixSeconds::rfrom(millisecond.div_ceil(t::MILLIS_PER_SECOND));
2308
0
        let nanosecond = FractionalNanosecond::rfrom(
2309
0
            millisecond.rem_ceil(t::MILLIS_PER_SECOND) * t::NANOS_PER_MILLI,
2310
        );
2311
0
        Timestamp { second, nanosecond }
2312
0
    }
2313
2314
    #[inline]
2315
0
    fn from_microsecond_ranged(microsecond: UnixMicroseconds) -> Timestamp {
2316
0
        let second =
2317
0
            UnixSeconds::rfrom(microsecond.div_ceil(t::MICROS_PER_SECOND));
2318
0
        let nanosecond = FractionalNanosecond::rfrom(
2319
0
            microsecond.rem_ceil(t::MICROS_PER_SECOND) * t::NANOS_PER_MICRO,
2320
        );
2321
0
        Timestamp { second, nanosecond }
2322
0
    }
2323
2324
    #[inline]
2325
0
    pub(crate) fn from_nanosecond_ranged(
2326
0
        nanosecond: UnixNanoseconds,
2327
0
    ) -> Timestamp {
2328
0
        let second =
2329
0
            UnixSeconds::rfrom(nanosecond.div_ceil(t::NANOS_PER_SECOND));
2330
0
        let nanosecond = nanosecond.rem_ceil(t::NANOS_PER_SECOND).rinto();
2331
0
        Timestamp { second, nanosecond }
2332
0
    }
2333
2334
    #[inline]
2335
0
    pub(crate) fn from_itimestamp(
2336
0
        its: Composite<ITimestamp>,
2337
0
    ) -> Result<Timestamp, Error> {
2338
0
        let (second, nanosecond) =
2339
0
            rangeint::uncomposite!(its, c => (c.second, c.nanosecond));
2340
        Ok(Timestamp {
2341
0
            second: second.try_to_rint("unix-seconds")?,
2342
0
            nanosecond: nanosecond.to_rint(),
2343
        })
2344
0
    }
2345
2346
    #[inline]
2347
0
    pub(crate) fn to_itimestamp(&self) -> Composite<ITimestamp> {
2348
0
        rangeint::composite! {
2349
            (second = self.second, nanosecond = self.nanosecond) => {
2350
0
                ITimestamp { second, nanosecond }
2351
            }
2352
        }
2353
0
    }
2354
2355
    #[inline]
2356
0
    pub(crate) const fn from_itimestamp_const(its: ITimestamp) -> Timestamp {
2357
0
        Timestamp {
2358
0
            second: UnixSeconds::new_unchecked(its.second),
2359
0
            nanosecond: FractionalNanosecond::new_unchecked(its.nanosecond),
2360
0
        }
2361
0
    }
2362
2363
    #[inline]
2364
0
    pub(crate) const fn to_itimestamp_const(&self) -> ITimestamp {
2365
0
        ITimestamp {
2366
0
            second: self.second.get_unchecked(),
2367
0
            nanosecond: self.nanosecond.get_unchecked(),
2368
0
        }
2369
0
    }
2370
2371
    #[inline]
2372
0
    pub(crate) fn as_second_ranged(self) -> UnixSeconds {
2373
0
        self.second
2374
0
    }
2375
2376
    #[inline]
2377
0
    fn as_millisecond_ranged(self) -> UnixMilliseconds {
2378
0
        let second = NoUnits::rfrom(self.as_second_ranged());
2379
0
        let nanosecond = NoUnits::rfrom(self.subsec_nanosecond_ranged());
2380
        // The minimum value of a timestamp has the fractional nanosecond set
2381
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2382
        // a case where we return a ranged integer with a minimum value less
2383
        // than the actual minimum, we clamp the fractional part to 0 when the
2384
        // second value is the minimum.
2385
0
        let [second, nanosecond] =
2386
0
            NoUnits::vary_many([second, nanosecond], |[second, nanosecond]| {
2387
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2388
0
                    [second, C(0).rinto()]
2389
                } else {
2390
0
                    [second, nanosecond]
2391
                }
2392
0
            });
2393
0
        UnixMilliseconds::rfrom(
2394
0
            (second * t::MILLIS_PER_SECOND)
2395
0
                + (nanosecond.div_ceil(t::NANOS_PER_MILLI)),
2396
        )
2397
0
    }
2398
2399
    #[inline]
2400
0
    fn as_microsecond_ranged(self) -> UnixMicroseconds {
2401
0
        let second = NoUnits::rfrom(self.as_second_ranged());
2402
0
        let nanosecond = NoUnits::rfrom(self.subsec_nanosecond_ranged());
2403
        // The minimum value of a timestamp has the fractional nanosecond set
2404
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2405
        // a case where we return a ranged integer with a minimum value less
2406
        // than the actual minimum, we clamp the fractional part to 0 when the
2407
        // second value is the minimum.
2408
0
        let [second, nanosecond] =
2409
0
            NoUnits::vary_many([second, nanosecond], |[second, nanosecond]| {
2410
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2411
0
                    [second, C(0).rinto()]
2412
                } else {
2413
0
                    [second, nanosecond]
2414
                }
2415
0
            });
2416
0
        UnixMicroseconds::rfrom(
2417
0
            (second * t::MICROS_PER_SECOND)
2418
0
                + (nanosecond.div_ceil(t::NANOS_PER_MICRO)),
2419
        )
2420
0
    }
2421
2422
    #[inline]
2423
0
    pub(crate) fn as_nanosecond_ranged(self) -> UnixNanoseconds {
2424
0
        let second = NoUnits128::rfrom(self.as_second_ranged());
2425
0
        let nanosecond = NoUnits128::rfrom(self.subsec_nanosecond_ranged());
2426
        // The minimum value of a timestamp has the fractional nanosecond set
2427
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2428
        // a case where we return a ranged integer with a minimum value less
2429
        // than the actual minimum, we clamp the fractional part to 0 when the
2430
        // second value is the minimum.
2431
0
        let [second, nanosecond] = NoUnits128::vary_many(
2432
0
            [second, nanosecond],
2433
0
            |[second, nanosecond]| {
2434
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2435
0
                    [second, C(0).rinto()]
2436
                } else {
2437
0
                    [second, nanosecond]
2438
                }
2439
0
            },
2440
        );
2441
0
        UnixNanoseconds::rfrom(second * t::NANOS_PER_SECOND + nanosecond)
2442
0
    }
2443
2444
    #[inline]
2445
0
    fn subsec_millisecond_ranged(self) -> t::FractionalMillisecond {
2446
0
        let millis =
2447
0
            self.subsec_nanosecond_ranged().div_ceil(t::NANOS_PER_MILLI);
2448
0
        t::FractionalMillisecond::rfrom(millis)
2449
0
    }
2450
2451
    #[inline]
2452
0
    fn subsec_microsecond_ranged(self) -> t::FractionalMicrosecond {
2453
0
        let micros =
2454
0
            self.subsec_nanosecond_ranged().div_ceil(t::NANOS_PER_MICRO);
2455
0
        t::FractionalMicrosecond::rfrom(micros)
2456
0
    }
2457
2458
    #[inline]
2459
0
    pub(crate) fn subsec_nanosecond_ranged(self) -> FractionalNanosecond {
2460
0
        self.nanosecond
2461
0
    }
2462
}
2463
2464
impl Default for Timestamp {
2465
    #[inline]
2466
0
    fn default() -> Timestamp {
2467
0
        Timestamp::UNIX_EPOCH
2468
0
    }
2469
}
2470
2471
/// Converts a `Timestamp` datetime into a human readable datetime string.
2472
///
2473
/// (This `Debug` representation currently emits the same string as the
2474
/// `Display` representation, but this is not a guarantee.)
2475
///
2476
/// Options currently supported:
2477
///
2478
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2479
/// of the fractional second component.
2480
///
2481
/// # Example
2482
///
2483
/// ```
2484
/// use jiff::Timestamp;
2485
///
2486
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2487
/// assert_eq!(
2488
///     format!("{ts:.6?}"),
2489
///     "2005-08-07T23:19:49.123000Z",
2490
/// );
2491
/// // Precision values greater than 9 are clamped to 9.
2492
/// assert_eq!(
2493
///     format!("{ts:.300?}"),
2494
///     "2005-08-07T23:19:49.123000000Z",
2495
/// );
2496
/// // A precision of 0 implies the entire fractional
2497
/// // component is always truncated.
2498
/// assert_eq!(
2499
///     format!("{ts:.0?}"),
2500
///     "2005-08-07T23:19:49Z",
2501
/// );
2502
///
2503
/// # Ok::<(), Box<dyn std::error::Error>>(())
2504
/// ```
2505
impl core::fmt::Debug for Timestamp {
2506
    #[inline]
2507
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2508
0
        core::fmt::Display::fmt(self, f)
2509
0
    }
2510
}
2511
2512
/// Converts a `Timestamp` datetime into a RFC 3339 compliant string.
2513
///
2514
/// Since a `Timestamp` never has an offset associated with it and is always
2515
/// in UTC, the string emitted by this trait implementation uses `Z` for "Zulu"
2516
/// time. The significance of Zulu time is prescribed by RFC 9557 and means
2517
/// that "the time in UTC is known, but the offset to local time is unknown."
2518
/// If you need to emit an RFC 3339 compliant string with a specific offset,
2519
/// then use [`Timestamp::display_with_offset`].
2520
///
2521
/// # Formatting options supported
2522
///
2523
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2524
/// of the fractional second component. When not set, the minimum precision
2525
/// required to losslessly render the value is used.
2526
///
2527
/// # Example
2528
///
2529
/// This shows the default rendering:
2530
///
2531
/// ```
2532
/// use jiff::Timestamp;
2533
///
2534
/// // No fractional seconds.
2535
/// let ts = Timestamp::from_second(1_123_456_789)?;
2536
/// assert_eq!(format!("{ts}"), "2005-08-07T23:19:49Z");
2537
///
2538
/// // With fractional seconds.
2539
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2540
/// assert_eq!(format!("{ts}"), "2005-08-07T23:19:49.123Z");
2541
///
2542
/// # Ok::<(), Box<dyn std::error::Error>>(())
2543
/// ```
2544
///
2545
/// # Example: setting the precision
2546
///
2547
/// ```
2548
/// use jiff::Timestamp;
2549
///
2550
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2551
/// assert_eq!(
2552
///     format!("{ts:.6}"),
2553
///     "2005-08-07T23:19:49.123000Z",
2554
/// );
2555
/// // Precision values greater than 9 are clamped to 9.
2556
/// assert_eq!(
2557
///     format!("{ts:.300}"),
2558
///     "2005-08-07T23:19:49.123000000Z",
2559
/// );
2560
/// // A precision of 0 implies the entire fractional
2561
/// // component is always truncated.
2562
/// assert_eq!(
2563
///     format!("{ts:.0}"),
2564
///     "2005-08-07T23:19:49Z",
2565
/// );
2566
///
2567
/// # Ok::<(), Box<dyn std::error::Error>>(())
2568
/// ```
2569
impl core::fmt::Display for Timestamp {
2570
    #[inline]
2571
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2572
        use crate::fmt::StdFmtWrite;
2573
2574
0
        let precision =
2575
0
            f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::fmt::Display>::fmt::{closure#0}
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::fmt::Display>::fmt::{closure#0}
2576
0
        temporal::DateTimePrinter::new()
2577
0
            .precision(precision)
2578
0
            .print_timestamp(self, StdFmtWrite(f))
2579
0
            .map_err(|_| core::fmt::Error)
2580
0
    }
2581
}
2582
2583
impl core::str::FromStr for Timestamp {
2584
    type Err = Error;
2585
2586
    #[inline]
2587
0
    fn from_str(string: &str) -> Result<Timestamp, Error> {
2588
0
        DEFAULT_DATETIME_PARSER.parse_timestamp(string)
2589
0
    }
2590
}
2591
2592
impl Eq for Timestamp {}
2593
2594
impl PartialEq for Timestamp {
2595
    #[inline]
2596
0
    fn eq(&self, rhs: &Timestamp) -> bool {
2597
0
        self.as_second_ranged().get() == rhs.as_second_ranged().get()
2598
0
            && self.subsec_nanosecond_ranged().get()
2599
0
                == rhs.subsec_nanosecond_ranged().get()
2600
0
    }
2601
}
2602
2603
impl Ord for Timestamp {
2604
    #[inline]
2605
0
    fn cmp(&self, rhs: &Timestamp) -> core::cmp::Ordering {
2606
0
        (self.as_second_ranged().get(), self.subsec_nanosecond_ranged().get())
2607
0
            .cmp(&(
2608
0
                rhs.as_second_ranged().get(),
2609
0
                rhs.subsec_nanosecond_ranged().get(),
2610
0
            ))
2611
0
    }
2612
}
2613
2614
impl PartialOrd for Timestamp {
2615
    #[inline]
2616
0
    fn partial_cmp(&self, rhs: &Timestamp) -> Option<core::cmp::Ordering> {
2617
0
        Some(self.cmp(rhs))
2618
0
    }
2619
}
2620
2621
impl core::hash::Hash for Timestamp {
2622
    #[inline]
2623
0
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
2624
0
        self.as_second_ranged().get().hash(state);
2625
0
        self.subsec_nanosecond_ranged().get().hash(state);
2626
0
    }
2627
}
2628
2629
/// Adds a span of time to a timestamp.
2630
///
2631
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2632
/// without panics, use [`Timestamp::checked_add`]. Note that the failure
2633
/// condition includes overflow and using a `Span` with non-zero units greater
2634
/// than hours.
2635
impl core::ops::Add<Span> for Timestamp {
2636
    type Output = Timestamp;
2637
2638
    #[inline]
2639
0
    fn add(self, rhs: Span) -> Timestamp {
2640
0
        self.checked_add_span(rhs).expect("adding span to timestamp failed")
2641
0
    }
2642
}
2643
2644
/// Adds a span of time to a timestamp in place.
2645
///
2646
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2647
/// without panics, use [`Timestamp::checked_add`]. Note that the failure
2648
/// condition includes overflow and using a `Span` with non-zero units greater
2649
/// than hours.
2650
impl core::ops::AddAssign<Span> for Timestamp {
2651
    #[inline]
2652
0
    fn add_assign(&mut self, rhs: Span) {
2653
0
        *self = *self + rhs
2654
0
    }
2655
}
2656
2657
/// Subtracts a span of time from a timestamp.
2658
///
2659
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2660
/// without panics, use [`Timestamp::checked_sub`]. Note that the failure
2661
/// condition includes overflow and using a `Span` with non-zero units greater
2662
/// than hours.
2663
impl core::ops::Sub<Span> for Timestamp {
2664
    type Output = Timestamp;
2665
2666
    #[inline]
2667
0
    fn sub(self, rhs: Span) -> Timestamp {
2668
0
        self.checked_add_span(rhs.negate())
2669
0
            .expect("subtracting span from timestamp failed")
2670
0
    }
2671
}
2672
2673
/// Subtracts a span of time from a timestamp in place.
2674
///
2675
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2676
/// without panics, use [`Timestamp::checked_sub`]. Note that the failure
2677
/// condition includes overflow and using a `Span` with non-zero units greater
2678
/// than hours.
2679
impl core::ops::SubAssign<Span> for Timestamp {
2680
    #[inline]
2681
0
    fn sub_assign(&mut self, rhs: Span) {
2682
0
        *self = *self - rhs
2683
0
    }
2684
}
2685
2686
/// Computes the span of time between two timestamps.
2687
///
2688
/// This will return a negative span when the timestamp being subtracted is
2689
/// greater.
2690
///
2691
/// Since this uses the default configuration for calculating a span between
2692
/// two timestamps (no rounding and largest units is seconds), this will never
2693
/// panic or fail in any way.
2694
///
2695
/// To configure the largest unit or enable rounding, use [`Timestamp::since`].
2696
impl core::ops::Sub for Timestamp {
2697
    type Output = Span;
2698
2699
    #[inline]
2700
0
    fn sub(self, rhs: Timestamp) -> Span {
2701
0
        self.since(rhs).expect("since never fails when given Timestamp")
2702
0
    }
2703
}
2704
2705
/// Adds a signed duration of time to a timestamp.
2706
///
2707
/// This uses checked arithmetic and panics on overflow. To handle overflow
2708
/// without panics, use [`Timestamp::checked_add`].
2709
impl core::ops::Add<SignedDuration> for Timestamp {
2710
    type Output = Timestamp;
2711
2712
    #[inline]
2713
0
    fn add(self, rhs: SignedDuration) -> Timestamp {
2714
0
        self.checked_add_duration(rhs)
2715
0
            .expect("adding signed duration to timestamp overflowed")
2716
0
    }
2717
}
2718
2719
/// Adds a signed duration of time to a timestamp in place.
2720
///
2721
/// This uses checked arithmetic and panics on overflow. To handle overflow
2722
/// without panics, use [`Timestamp::checked_add`].
2723
impl core::ops::AddAssign<SignedDuration> for Timestamp {
2724
    #[inline]
2725
0
    fn add_assign(&mut self, rhs: SignedDuration) {
2726
0
        *self = *self + rhs
2727
0
    }
2728
}
2729
2730
/// Subtracts a signed duration of time from a timestamp.
2731
///
2732
/// This uses checked arithmetic and panics on overflow. To handle overflow
2733
/// without panics, use [`Timestamp::checked_sub`].
2734
impl core::ops::Sub<SignedDuration> for Timestamp {
2735
    type Output = Timestamp;
2736
2737
    #[inline]
2738
0
    fn sub(self, rhs: SignedDuration) -> Timestamp {
2739
0
        let rhs = rhs
2740
0
            .checked_neg()
2741
0
            .expect("signed duration negation resulted in overflow");
2742
0
        self.checked_add_duration(rhs)
2743
0
            .expect("subtracting signed duration from timestamp overflowed")
2744
0
    }
2745
}
2746
2747
/// Subtracts a signed duration of time from a timestamp in place.
2748
///
2749
/// This uses checked arithmetic and panics on overflow. To handle overflow
2750
/// without panics, use [`Timestamp::checked_sub`].
2751
impl core::ops::SubAssign<SignedDuration> for Timestamp {
2752
    #[inline]
2753
0
    fn sub_assign(&mut self, rhs: SignedDuration) {
2754
0
        *self = *self - rhs
2755
0
    }
2756
}
2757
2758
/// Adds an unsigned duration of time to a timestamp.
2759
///
2760
/// This uses checked arithmetic and panics on overflow. To handle overflow
2761
/// without panics, use [`Timestamp::checked_add`].
2762
impl core::ops::Add<UnsignedDuration> for Timestamp {
2763
    type Output = Timestamp;
2764
2765
    #[inline]
2766
0
    fn add(self, rhs: UnsignedDuration) -> Timestamp {
2767
0
        self.checked_add(rhs)
2768
0
            .expect("adding unsigned duration to timestamp overflowed")
2769
0
    }
2770
}
2771
2772
/// Adds an unsigned duration of time to a timestamp in place.
2773
///
2774
/// This uses checked arithmetic and panics on overflow. To handle overflow
2775
/// without panics, use [`Timestamp::checked_add`].
2776
impl core::ops::AddAssign<UnsignedDuration> for Timestamp {
2777
    #[inline]
2778
0
    fn add_assign(&mut self, rhs: UnsignedDuration) {
2779
0
        *self = *self + rhs
2780
0
    }
2781
}
2782
2783
/// Subtracts an unsigned duration of time from a timestamp.
2784
///
2785
/// This uses checked arithmetic and panics on overflow. To handle overflow
2786
/// without panics, use [`Timestamp::checked_sub`].
2787
impl core::ops::Sub<UnsignedDuration> for Timestamp {
2788
    type Output = Timestamp;
2789
2790
    #[inline]
2791
0
    fn sub(self, rhs: UnsignedDuration) -> Timestamp {
2792
0
        self.checked_sub(rhs)
2793
0
            .expect("subtracting unsigned duration from timestamp overflowed")
2794
0
    }
2795
}
2796
2797
/// Subtracts an unsigned duration of time from a timestamp in place.
2798
///
2799
/// This uses checked arithmetic and panics on overflow. To handle overflow
2800
/// without panics, use [`Timestamp::checked_sub`].
2801
impl core::ops::SubAssign<UnsignedDuration> for Timestamp {
2802
    #[inline]
2803
0
    fn sub_assign(&mut self, rhs: UnsignedDuration) {
2804
0
        *self = *self - rhs
2805
0
    }
2806
}
2807
2808
impl From<Zoned> for Timestamp {
2809
    #[inline]
2810
0
    fn from(zdt: Zoned) -> Timestamp {
2811
0
        zdt.timestamp()
2812
0
    }
2813
}
2814
2815
impl<'a> From<&'a Zoned> for Timestamp {
2816
    #[inline]
2817
0
    fn from(zdt: &'a Zoned) -> Timestamp {
2818
0
        zdt.timestamp()
2819
0
    }
2820
}
2821
2822
#[cfg(feature = "std")]
2823
impl From<Timestamp> for std::time::SystemTime {
2824
    #[inline]
2825
0
    fn from(time: Timestamp) -> std::time::SystemTime {
2826
0
        let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
2827
0
        let sdur = time.as_duration();
2828
0
        let dur = sdur.unsigned_abs();
2829
        // These are guaranteed to succeed because we assume that SystemTime
2830
        // uses at least 64 bits for the time, and our durations are capped via
2831
        // the range on UnixSeconds.
2832
0
        if sdur.is_negative() {
2833
0
            unix_epoch.checked_sub(dur).expect("duration too big (negative)")
2834
        } else {
2835
0
            unix_epoch.checked_add(dur).expect("duration too big (positive)")
2836
        }
2837
0
    }
2838
}
2839
2840
#[cfg(feature = "std")]
2841
impl TryFrom<std::time::SystemTime> for Timestamp {
2842
    type Error = Error;
2843
2844
    #[inline]
2845
0
    fn try_from(
2846
0
        system_time: std::time::SystemTime,
2847
0
    ) -> Result<Timestamp, Error> {
2848
0
        let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
2849
0
        let dur = SignedDuration::system_until(unix_epoch, system_time)?;
2850
0
        Timestamp::from_duration(dur)
2851
0
    }
2852
}
2853
2854
#[cfg(feature = "serde")]
2855
impl serde_core::Serialize for Timestamp {
2856
    #[inline]
2857
    fn serialize<S: serde_core::Serializer>(
2858
        &self,
2859
        serializer: S,
2860
    ) -> Result<S::Ok, S::Error> {
2861
        serializer.collect_str(self)
2862
    }
2863
}
2864
2865
#[cfg(feature = "serde")]
2866
impl<'de> serde_core::Deserialize<'de> for Timestamp {
2867
    #[inline]
2868
    fn deserialize<D: serde_core::Deserializer<'de>>(
2869
        deserializer: D,
2870
    ) -> Result<Timestamp, D::Error> {
2871
        use serde_core::de;
2872
2873
        struct TimestampVisitor;
2874
2875
        impl<'de> de::Visitor<'de> for TimestampVisitor {
2876
            type Value = Timestamp;
2877
2878
            fn expecting(
2879
                &self,
2880
                f: &mut core::fmt::Formatter,
2881
            ) -> core::fmt::Result {
2882
                f.write_str("a timestamp string")
2883
            }
2884
2885
            #[inline]
2886
            fn visit_bytes<E: de::Error>(
2887
                self,
2888
                value: &[u8],
2889
            ) -> Result<Timestamp, E> {
2890
                DEFAULT_DATETIME_PARSER
2891
                    .parse_timestamp(value)
2892
                    .map_err(de::Error::custom)
2893
            }
2894
2895
            #[inline]
2896
            fn visit_str<E: de::Error>(
2897
                self,
2898
                value: &str,
2899
            ) -> Result<Timestamp, E> {
2900
                self.visit_bytes(value.as_bytes())
2901
            }
2902
        }
2903
2904
        deserializer.deserialize_str(TimestampVisitor)
2905
    }
2906
}
2907
2908
#[cfg(test)]
2909
impl quickcheck::Arbitrary for Timestamp {
2910
    fn arbitrary(g: &mut quickcheck::Gen) -> Timestamp {
2911
        use quickcheck::Arbitrary;
2912
2913
        let seconds: UnixSeconds = Arbitrary::arbitrary(g);
2914
        let mut nanoseconds: FractionalNanosecond = Arbitrary::arbitrary(g);
2915
        // nanoseconds must be zero for the minimum second value,
2916
        // so just clamp it to 0.
2917
        if seconds == UnixSeconds::MIN_SELF && nanoseconds < C(0) {
2918
            nanoseconds = C(0).rinto();
2919
        }
2920
        Timestamp::new_ranged(seconds, nanoseconds).unwrap_or_default()
2921
    }
2922
2923
    fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Self>> {
2924
        let second = self.as_second_ranged();
2925
        let nanosecond = self.subsec_nanosecond_ranged();
2926
        alloc::boxed::Box::new((second, nanosecond).shrink().filter_map(
2927
            |(second, nanosecond)| {
2928
                if second == UnixSeconds::MIN_SELF && nanosecond > C(0) {
2929
                    None
2930
                } else {
2931
                    Timestamp::new_ranged(second, nanosecond).ok()
2932
                }
2933
            },
2934
        ))
2935
    }
2936
}
2937
2938
/// A type for formatting a [`Timestamp`] with a specific offset.
2939
///
2940
/// This type is created by the [`Timestamp::display_with_offset`] method.
2941
///
2942
/// Like the [`std::fmt::Display`] trait implementation for `Timestamp`, this
2943
/// always emits an RFC 3339 compliant string. Unlike `Timestamp`'s `Display`
2944
/// trait implementation, which always uses `Z` or "Zulu" time, this always
2945
/// uses an offset.
2946
///
2947
/// # Formatting options supported
2948
///
2949
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2950
/// of the fractional second component.
2951
///
2952
/// # Example
2953
///
2954
/// ```
2955
/// use jiff::{tz, Timestamp};
2956
///
2957
/// let offset = tz::offset(-5);
2958
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2959
/// assert_eq!(
2960
///     format!("{ts:.6}", ts = ts.display_with_offset(offset)),
2961
///     "2005-08-07T18:19:49.123000-05:00",
2962
/// );
2963
/// // Precision values greater than 9 are clamped to 9.
2964
/// assert_eq!(
2965
///     format!("{ts:.300}", ts = ts.display_with_offset(offset)),
2966
///     "2005-08-07T18:19:49.123000000-05:00",
2967
/// );
2968
/// // A precision of 0 implies the entire fractional
2969
/// // component is always truncated.
2970
/// assert_eq!(
2971
///     format!("{ts:.0}", ts = ts.display_with_offset(tz::Offset::UTC)),
2972
///     "2005-08-07T23:19:49+00:00",
2973
/// );
2974
///
2975
/// # Ok::<(), Box<dyn std::error::Error>>(())
2976
/// ```
2977
#[derive(Clone, Copy, Debug)]
2978
pub struct TimestampDisplayWithOffset {
2979
    timestamp: Timestamp,
2980
    offset: Offset,
2981
}
2982
2983
impl core::fmt::Display for TimestampDisplayWithOffset {
2984
    #[inline]
2985
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2986
        use crate::fmt::StdFmtWrite;
2987
2988
0
        let precision =
2989
0
            f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
2990
0
        temporal::DateTimePrinter::new()
2991
0
            .precision(precision)
2992
0
            .print_timestamp_with_offset(
2993
0
                &self.timestamp,
2994
0
                self.offset,
2995
0
                StdFmtWrite(f),
2996
            )
2997
0
            .map_err(|_| core::fmt::Error)
2998
0
    }
2999
}
3000
3001
/// An iterator over periodic timestamps, created by [`Timestamp::series`].
3002
///
3003
/// It is exhausted when the next value would exceed the limits of a [`Span`]
3004
/// or [`Timestamp`] value.
3005
///
3006
/// This iterator is created by [`Timestamp::series`].
3007
#[derive(Clone, Debug)]
3008
pub struct TimestampSeries {
3009
    ts: Timestamp,
3010
    duration: Option<SignedDuration>,
3011
}
3012
3013
impl TimestampSeries {
3014
    #[inline]
3015
0
    fn new(ts: Timestamp, period: Span) -> TimestampSeries {
3016
0
        let duration = SignedDuration::try_from(period).ok();
3017
0
        TimestampSeries { ts, duration }
3018
0
    }
3019
}
3020
3021
impl Iterator for TimestampSeries {
3022
    type Item = Timestamp;
3023
3024
    #[inline]
3025
0
    fn next(&mut self) -> Option<Timestamp> {
3026
0
        let duration = self.duration?;
3027
0
        let this = self.ts;
3028
0
        self.ts = self.ts.checked_add_duration(duration).ok()?;
3029
0
        Some(this)
3030
0
    }
3031
}
3032
3033
impl core::iter::FusedIterator for TimestampSeries {}
3034
3035
/// Options for [`Timestamp::checked_add`] and [`Timestamp::checked_sub`].
3036
///
3037
/// This type provides a way to ergonomically add one of a few different
3038
/// duration types to a [`Timestamp`].
3039
///
3040
/// The main way to construct values of this type is with its `From` trait
3041
/// implementations:
3042
///
3043
/// * `From<Span> for TimestampArithmetic` adds (or subtracts) the given span
3044
/// to the receiver timestamp.
3045
/// * `From<SignedDuration> for TimestampArithmetic` adds (or subtracts)
3046
/// the given signed duration to the receiver timestamp.
3047
/// * `From<std::time::Duration> for TimestampArithmetic` adds (or subtracts)
3048
/// the given unsigned duration to the receiver timestamp.
3049
///
3050
/// # Example
3051
///
3052
/// ```
3053
/// use std::time::Duration;
3054
///
3055
/// use jiff::{SignedDuration, Timestamp, ToSpan};
3056
///
3057
/// let ts: Timestamp = "2024-02-28T00:00:00Z".parse()?;
3058
/// assert_eq!(
3059
///     ts.checked_add(48.hours())?,
3060
///     "2024-03-01T00:00:00Z".parse()?,
3061
/// );
3062
/// assert_eq!(
3063
///     ts.checked_add(SignedDuration::from_hours(48))?,
3064
///     "2024-03-01T00:00:00Z".parse()?,
3065
/// );
3066
/// assert_eq!(
3067
///     ts.checked_add(Duration::from_secs(48 * 60 * 60))?,
3068
///     "2024-03-01T00:00:00Z".parse()?,
3069
/// );
3070
///
3071
/// # Ok::<(), Box<dyn std::error::Error>>(())
3072
/// ```
3073
#[derive(Clone, Copy, Debug)]
3074
pub struct TimestampArithmetic {
3075
    duration: Duration,
3076
}
3077
3078
impl TimestampArithmetic {
3079
    #[inline]
3080
0
    fn checked_add(self, ts: Timestamp) -> Result<Timestamp, Error> {
3081
0
        match self.duration.to_signed()? {
3082
0
            SDuration::Span(span) => ts.checked_add_span(span),
3083
0
            SDuration::Absolute(sdur) => ts.checked_add_duration(sdur),
3084
        }
3085
0
    }
3086
3087
    #[inline]
3088
0
    fn saturating_add(self, ts: Timestamp) -> Result<Timestamp, Error> {
3089
0
        let Ok(signed) = self.duration.to_signed() else {
3090
0
            return Ok(Timestamp::MAX);
3091
        };
3092
0
        let result = match signed {
3093
0
            SDuration::Span(span) => {
3094
0
                if let Some(err) = span.smallest_non_time_non_zero_unit_error()
3095
                {
3096
0
                    return Err(err);
3097
0
                }
3098
0
                ts.checked_add_span(span)
3099
            }
3100
0
            SDuration::Absolute(sdur) => ts.checked_add_duration(sdur),
3101
        };
3102
0
        Ok(result.unwrap_or_else(|_| {
3103
0
            if self.is_negative() {
3104
0
                Timestamp::MIN
3105
            } else {
3106
0
                Timestamp::MAX
3107
            }
3108
0
        }))
3109
0
    }
3110
3111
    #[inline]
3112
0
    fn checked_neg(self) -> Result<TimestampArithmetic, Error> {
3113
0
        let duration = self.duration.checked_neg()?;
3114
0
        Ok(TimestampArithmetic { duration })
3115
0
    }
3116
3117
    #[inline]
3118
0
    fn is_negative(&self) -> bool {
3119
0
        self.duration.is_negative()
3120
0
    }
3121
}
3122
3123
impl From<Span> for TimestampArithmetic {
3124
0
    fn from(span: Span) -> TimestampArithmetic {
3125
0
        let duration = Duration::from(span);
3126
0
        TimestampArithmetic { duration }
3127
0
    }
3128
}
3129
3130
impl From<SignedDuration> for TimestampArithmetic {
3131
0
    fn from(sdur: SignedDuration) -> TimestampArithmetic {
3132
0
        let duration = Duration::from(sdur);
3133
0
        TimestampArithmetic { duration }
3134
0
    }
3135
}
3136
3137
impl From<UnsignedDuration> for TimestampArithmetic {
3138
0
    fn from(udur: UnsignedDuration) -> TimestampArithmetic {
3139
0
        let duration = Duration::from(udur);
3140
0
        TimestampArithmetic { duration }
3141
0
    }
3142
}
3143
3144
impl<'a> From<&'a Span> for TimestampArithmetic {
3145
0
    fn from(span: &'a Span) -> TimestampArithmetic {
3146
0
        TimestampArithmetic::from(*span)
3147
0
    }
3148
}
3149
3150
impl<'a> From<&'a SignedDuration> for TimestampArithmetic {
3151
0
    fn from(sdur: &'a SignedDuration) -> TimestampArithmetic {
3152
0
        TimestampArithmetic::from(*sdur)
3153
0
    }
3154
}
3155
3156
impl<'a> From<&'a UnsignedDuration> for TimestampArithmetic {
3157
0
    fn from(udur: &'a UnsignedDuration) -> TimestampArithmetic {
3158
0
        TimestampArithmetic::from(*udur)
3159
0
    }
3160
}
3161
3162
/// Options for [`Timestamp::since`] and [`Timestamp::until`].
3163
///
3164
/// This type provides a way to configure the calculation of
3165
/// spans between two [`Timestamp`] values. In particular, both
3166
/// `Timestamp::since` and `Timestamp::until` accept anything that implements
3167
/// `Into<TimestampDifference>`. There are a few key trait implementations that
3168
/// make this convenient:
3169
///
3170
/// * `From<Timestamp> for TimestampDifference` will construct a
3171
/// configuration consisting of just the timestamp. So for example,
3172
/// `timestamp1.until(timestamp2)` will return the span from `timestamp1` to
3173
/// `timestamp2`.
3174
/// * `From<Zoned> for TimestampDifference` will construct a configuration
3175
/// consisting of the timestamp from the given zoned datetime. So for example,
3176
/// `timestamp.since(zoned)` returns the span from `zoned.to_timestamp()` to
3177
/// `timestamp`.
3178
/// * `From<(Unit, Timestamp)>` is a convenient way to specify the largest
3179
/// units that should be present on the span returned. By default, the largest
3180
/// units are seconds. Using this trait implementation is equivalent to
3181
/// `TimestampDifference::new(timestamp).largest(unit)`.
3182
/// * `From<(Unit, Zoned)>` is like the one above, but with the time from
3183
/// the given zoned datetime.
3184
///
3185
/// One can also provide a `TimestampDifference` value directly. Doing so
3186
/// is necessary to use the rounding features of calculating a span. For
3187
/// example, setting the smallest unit (defaults to [`Unit::Nanosecond`]), the
3188
/// rounding mode (defaults to [`RoundMode::Trunc`]) and the rounding increment
3189
/// (defaults to `1`). The defaults are selected such that no rounding occurs.
3190
///
3191
/// Rounding a span as part of calculating it is provided as a convenience.
3192
/// Callers may choose to round the span as a distinct step via
3193
/// [`Span::round`].
3194
///
3195
/// # Example
3196
///
3197
/// This example shows how to round a span between two timestamps to the
3198
/// nearest half-hour, with ties breaking away from zero.
3199
///
3200
/// ```
3201
/// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3202
///
3203
/// let ts1 = "2024-03-15 08:14:00.123456789Z".parse::<Timestamp>()?;
3204
/// let ts2 = "2024-03-22 15:00Z".parse::<Timestamp>()?;
3205
/// let span = ts1.until(
3206
///     TimestampDifference::new(ts2)
3207
///         .smallest(Unit::Minute)
3208
///         .largest(Unit::Hour)
3209
///         .mode(RoundMode::HalfExpand)
3210
///         .increment(30),
3211
/// )?;
3212
/// assert_eq!(format!("{span:#}"), "175h");
3213
///
3214
/// // One less minute, and because of the HalfExpand mode, the span would
3215
/// // get rounded down.
3216
/// let ts2 = "2024-03-22 14:59Z".parse::<Timestamp>()?;
3217
/// let span = ts1.until(
3218
///     TimestampDifference::new(ts2)
3219
///         .smallest(Unit::Minute)
3220
///         .largest(Unit::Hour)
3221
///         .mode(RoundMode::HalfExpand)
3222
///         .increment(30),
3223
/// )?;
3224
/// assert_eq!(span, 174.hours().minutes(30).fieldwise());
3225
///
3226
/// # Ok::<(), Box<dyn std::error::Error>>(())
3227
/// ```
3228
#[derive(Clone, Copy, Debug)]
3229
pub struct TimestampDifference {
3230
    timestamp: Timestamp,
3231
    round: SpanRound<'static>,
3232
}
3233
3234
impl TimestampDifference {
3235
    /// Create a new default configuration for computing the span between
3236
    /// the given timestamp and some other time (specified as the receiver in
3237
    /// [`Timestamp::since`] or [`Timestamp::until`]).
3238
    #[inline]
3239
0
    pub fn new(timestamp: Timestamp) -> TimestampDifference {
3240
        // We use truncation rounding by default since it seems that's
3241
        // what is generally expected when computing the difference between
3242
        // datetimes.
3243
        //
3244
        // See: https://github.com/tc39/proposal-temporal/issues/1122
3245
0
        let round = SpanRound::new().mode(RoundMode::Trunc);
3246
0
        TimestampDifference { timestamp, round }
3247
0
    }
3248
3249
    /// Set the smallest units allowed in the span returned.
3250
    ///
3251
    /// # Errors
3252
    ///
3253
    /// The smallest units must be no greater than the largest units. If this
3254
    /// is violated, then computing a span with this configuration will result
3255
    /// in an error.
3256
    ///
3257
    /// # Example
3258
    ///
3259
    /// This shows how to round a span between two timestamps to units no less
3260
    /// than seconds.
3261
    ///
3262
    /// ```
3263
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3264
    ///
3265
    /// let ts1 = "2024-03-15 08:14:02.5001Z".parse::<Timestamp>()?;
3266
    /// let ts2 = "2024-03-15T08:16:03.0001Z".parse::<Timestamp>()?;
3267
    /// let span = ts1.until(
3268
    ///     TimestampDifference::new(ts2)
3269
    ///         .smallest(Unit::Second)
3270
    ///         .mode(RoundMode::HalfExpand),
3271
    /// )?;
3272
    /// assert_eq!(span, 121.seconds().fieldwise());
3273
    ///
3274
    /// // Because of the rounding mode, a small less-than-1-second increase in
3275
    /// // the first timestamp can change the result of rounding.
3276
    /// let ts1 = "2024-03-15 08:14:02.5002Z".parse::<Timestamp>()?;
3277
    /// let span = ts1.until(
3278
    ///     TimestampDifference::new(ts2)
3279
    ///         .smallest(Unit::Second)
3280
    ///         .mode(RoundMode::HalfExpand),
3281
    /// )?;
3282
    /// assert_eq!(span, 120.seconds().fieldwise());
3283
    ///
3284
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3285
    /// ```
3286
    #[inline]
3287
0
    pub fn smallest(self, unit: Unit) -> TimestampDifference {
3288
0
        TimestampDifference { round: self.round.smallest(unit), ..self }
3289
0
    }
3290
3291
    /// Set the largest units allowed in the span returned.
3292
    ///
3293
    /// When a largest unit is not specified, computing a span between
3294
    /// timestamps behaves as if it were set to [`Unit::Second`]. Unless
3295
    /// [`TimestampDifference::smallest`] is bigger than `Unit::Second`, then
3296
    /// the largest unit is set to the smallest unit.
3297
    ///
3298
    /// # Errors
3299
    ///
3300
    /// The largest units, when set, must be at least as big as the smallest
3301
    /// units (which defaults to [`Unit::Nanosecond`]). If this is violated,
3302
    /// then computing a span with this configuration will result in an error.
3303
    ///
3304
    /// # Example
3305
    ///
3306
    /// This shows how to round a span between two timestamps to units no
3307
    /// bigger than seconds.
3308
    ///
3309
    /// ```
3310
    /// use jiff::{Timestamp, TimestampDifference, ToSpan, Unit};
3311
    ///
3312
    /// let ts1 = "2024-03-15 08:14Z".parse::<Timestamp>()?;
3313
    /// let ts2 = "2030-11-22 08:30Z".parse::<Timestamp>()?;
3314
    /// let span = ts1.until(
3315
    ///     TimestampDifference::new(ts2).largest(Unit::Second),
3316
    /// )?;
3317
    /// assert_eq!(format!("{span:#}"), "211076160s");
3318
    ///
3319
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3320
    /// ```
3321
    #[inline]
3322
0
    pub fn largest(self, unit: Unit) -> TimestampDifference {
3323
0
        TimestampDifference { round: self.round.largest(unit), ..self }
3324
0
    }
3325
3326
    /// Set the rounding mode.
3327
    ///
3328
    /// This defaults to [`RoundMode::Trunc`] since it's plausible that
3329
    /// rounding "up" in the context of computing the span between
3330
    /// two timestamps could be surprising in a number of cases. The
3331
    /// [`RoundMode::HalfExpand`] mode corresponds to typical rounding you
3332
    /// might have learned about in school. But a variety of other rounding
3333
    /// modes exist.
3334
    ///
3335
    /// # Example
3336
    ///
3337
    /// This shows how to always round "up" towards positive infinity.
3338
    ///
3339
    /// ```
3340
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3341
    ///
3342
    /// let ts1 = "2024-03-15 08:10Z".parse::<Timestamp>()?;
3343
    /// let ts2 = "2024-03-15 08:11Z".parse::<Timestamp>()?;
3344
    /// let span = ts1.until(
3345
    ///     TimestampDifference::new(ts2)
3346
    ///         .smallest(Unit::Hour)
3347
    ///         .mode(RoundMode::Ceil),
3348
    /// )?;
3349
    /// // Only one minute elapsed, but we asked to always round up!
3350
    /// assert_eq!(span, 1.hour().fieldwise());
3351
    ///
3352
    /// // Since `Ceil` always rounds toward positive infinity, the behavior
3353
    /// // flips for a negative span.
3354
    /// let span = ts1.since(
3355
    ///     TimestampDifference::new(ts2)
3356
    ///         .smallest(Unit::Hour)
3357
    ///         .mode(RoundMode::Ceil),
3358
    /// )?;
3359
    /// assert_eq!(span, 0.hour().fieldwise());
3360
    ///
3361
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3362
    /// ```
3363
    #[inline]
3364
0
    pub fn mode(self, mode: RoundMode) -> TimestampDifference {
3365
0
        TimestampDifference { round: self.round.mode(mode), ..self }
3366
0
    }
3367
3368
    /// Set the rounding increment for the smallest unit.
3369
    ///
3370
    /// The default value is `1`. Other values permit rounding the smallest
3371
    /// unit to the nearest integer increment specified. For example, if the
3372
    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
3373
    /// `30` would result in rounding in increments of a half hour. That is,
3374
    /// the only minute value that could result would be `0` or `30`.
3375
    ///
3376
    /// # Errors
3377
    ///
3378
    /// The rounding increment must divide evenly into the next highest unit
3379
    /// after the smallest unit configured (and must not be equivalent to it).
3380
    /// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
3381
    /// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
3382
    /// `100` and `500`. Namely, any integer that divides evenly into `1,000`
3383
    /// nanoseconds since there are `1,000` nanoseconds in the next highest
3384
    /// unit (microseconds).
3385
    ///
3386
    /// The error will occur when computing the span, and not when setting
3387
    /// the increment here.
3388
    ///
3389
    /// # Example
3390
    ///
3391
    /// This shows how to round the span between two timestamps to the nearest
3392
    /// 5 minute increment.
3393
    ///
3394
    /// ```
3395
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3396
    ///
3397
    /// let ts1 = "2024-03-15 08:19Z".parse::<Timestamp>()?;
3398
    /// let ts2 = "2024-03-15 12:52Z".parse::<Timestamp>()?;
3399
    /// let span = ts1.until(
3400
    ///     TimestampDifference::new(ts2)
3401
    ///         .smallest(Unit::Minute)
3402
    ///         .increment(5)
3403
    ///         .mode(RoundMode::HalfExpand),
3404
    /// )?;
3405
    /// assert_eq!(span.to_string(), "PT275M");
3406
    ///
3407
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3408
    /// ```
3409
    #[inline]
3410
0
    pub fn increment(self, increment: i64) -> TimestampDifference {
3411
0
        TimestampDifference { round: self.round.increment(increment), ..self }
3412
0
    }
3413
3414
    /// Returns true if and only if this configuration could change the span
3415
    /// via rounding.
3416
    #[inline]
3417
0
    fn rounding_may_change_span(&self) -> bool {
3418
0
        self.round.rounding_may_change_span_ignore_largest()
3419
0
    }
3420
3421
    /// Returns the span of time from `ts1` to the timestamp in this
3422
    /// configuration. The biggest units allowed are determined by the
3423
    /// `smallest` and `largest` settings, but defaults to `Unit::Second`.
3424
    #[inline]
3425
0
    fn until_with_largest_unit(&self, t1: Timestamp) -> Result<Span, Error> {
3426
0
        let t2 = self.timestamp;
3427
0
        let largest = self
3428
0
            .round
3429
0
            .get_largest()
3430
0
            .unwrap_or_else(|| self.round.get_smallest().max(Unit::Second));
3431
0
        if largest >= Unit::Day {
3432
0
            return Err(err!(
3433
0
                "unit {largest} is not supported when computing the \
3434
0
                 difference between timestamps (must use units smaller \
3435
0
                 than 'day')",
3436
0
                largest = largest.singular(),
3437
0
            ));
3438
0
        }
3439
0
        let nano1 = t1.as_nanosecond_ranged().without_bounds();
3440
0
        let nano2 = t2.as_nanosecond_ranged().without_bounds();
3441
0
        let diff = nano2 - nano1;
3442
        // This can fail when `largest` is nanoseconds since not all intervals
3443
        // can be represented by a single i64 in units of nanoseconds.
3444
0
        Span::from_invariant_nanoseconds(largest, diff)
3445
0
    }
3446
}
3447
3448
impl From<Timestamp> for TimestampDifference {
3449
    #[inline]
3450
0
    fn from(ts: Timestamp) -> TimestampDifference {
3451
0
        TimestampDifference::new(ts)
3452
0
    }
3453
}
3454
3455
impl From<Zoned> for TimestampDifference {
3456
    #[inline]
3457
0
    fn from(zdt: Zoned) -> TimestampDifference {
3458
0
        TimestampDifference::new(Timestamp::from(zdt))
3459
0
    }
3460
}
3461
3462
impl<'a> From<&'a Zoned> for TimestampDifference {
3463
    #[inline]
3464
0
    fn from(zdt: &'a Zoned) -> TimestampDifference {
3465
0
        TimestampDifference::from(Timestamp::from(zdt))
3466
0
    }
3467
}
3468
3469
impl From<(Unit, Timestamp)> for TimestampDifference {
3470
    #[inline]
3471
0
    fn from((largest, ts): (Unit, Timestamp)) -> TimestampDifference {
3472
0
        TimestampDifference::from(ts).largest(largest)
3473
0
    }
3474
}
3475
3476
impl From<(Unit, Zoned)> for TimestampDifference {
3477
    #[inline]
3478
0
    fn from((largest, zdt): (Unit, Zoned)) -> TimestampDifference {
3479
0
        TimestampDifference::from((largest, Timestamp::from(zdt)))
3480
0
    }
3481
}
3482
3483
impl<'a> From<(Unit, &'a Zoned)> for TimestampDifference {
3484
    #[inline]
3485
0
    fn from((largest, zdt): (Unit, &'a Zoned)) -> TimestampDifference {
3486
0
        TimestampDifference::from((largest, Timestamp::from(zdt)))
3487
0
    }
3488
}
3489
3490
/// Options for [`Timestamp::round`].
3491
///
3492
/// This type provides a way to configure the rounding of a timestamp. In
3493
/// particular, `Timestamp::round` accepts anything that implements the
3494
/// `Into<TimestampRound>` trait. There are some trait implementations that
3495
/// therefore make calling `Timestamp::round` in some common cases more
3496
/// ergonomic:
3497
///
3498
/// * `From<Unit> for TimestampRound` will construct a rounding
3499
/// configuration that rounds to the unit given. Specifically,
3500
/// `TimestampRound::new().smallest(unit)`.
3501
/// * `From<(Unit, i64)> for TimestampRound` is like the one above, but also
3502
/// specifies the rounding increment for [`TimestampRound::increment`].
3503
///
3504
/// Note that in the default configuration, no rounding occurs.
3505
///
3506
/// # Example
3507
///
3508
/// This example shows how to round a timestamp to the nearest second:
3509
///
3510
/// ```
3511
/// use jiff::{Timestamp, Unit};
3512
///
3513
/// let ts: Timestamp = "2024-06-20 16:24:59.5Z".parse()?;
3514
/// assert_eq!(
3515
///     ts.round(Unit::Second)?.to_string(),
3516
///     // The second rounds up and causes minutes to increase.
3517
///     "2024-06-20T16:25:00Z",
3518
/// );
3519
///
3520
/// # Ok::<(), Box<dyn std::error::Error>>(())
3521
/// ```
3522
///
3523
/// The above makes use of the fact that `Unit` implements
3524
/// `Into<TimestampRound>`. If you want to change the rounding mode to, say,
3525
/// truncation, then you'll need to construct a `TimestampRound` explicitly
3526
/// since there are no convenience `Into` trait implementations for
3527
/// [`RoundMode`].
3528
///
3529
/// ```
3530
/// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3531
///
3532
/// let ts: Timestamp = "2024-06-20 16:24:59.5Z".parse()?;
3533
/// assert_eq!(
3534
///     ts.round(
3535
///         TimestampRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
3536
///     )?.to_string(),
3537
///     // The second just gets truncated as if it wasn't there.
3538
///     "2024-06-20T16:24:59Z",
3539
/// );
3540
///
3541
/// # Ok::<(), Box<dyn std::error::Error>>(())
3542
/// ```
3543
#[derive(Clone, Copy, Debug)]
3544
pub struct TimestampRound {
3545
    smallest: Unit,
3546
    mode: RoundMode,
3547
    increment: i64,
3548
}
3549
3550
impl TimestampRound {
3551
    /// Create a new default configuration for rounding a [`Timestamp`].
3552
    #[inline]
3553
0
    pub fn new() -> TimestampRound {
3554
0
        TimestampRound {
3555
0
            smallest: Unit::Nanosecond,
3556
0
            mode: RoundMode::HalfExpand,
3557
0
            increment: 1,
3558
0
        }
3559
0
    }
3560
3561
    /// Set the smallest units allowed in the timestamp returned after
3562
    /// rounding.
3563
    ///
3564
    /// Any units below the smallest configured unit will be used, along with
3565
    /// the rounding increment and rounding mode, to determine the value of the
3566
    /// smallest unit. For example, when rounding `2024-06-20T03:25:30Z` to the
3567
    /// nearest minute, the `30` second unit will result in rounding the minute
3568
    /// unit of `25` up to `26` and zeroing out everything below minutes.
3569
    ///
3570
    /// This defaults to [`Unit::Nanosecond`].
3571
    ///
3572
    /// # Errors
3573
    ///
3574
    /// The smallest units must be no greater than [`Unit::Hour`].
3575
    ///
3576
    /// # Example
3577
    ///
3578
    /// ```
3579
    /// use jiff::{Timestamp, TimestampRound, Unit};
3580
    ///
3581
    /// let ts: Timestamp = "2024-06-20T03:25:30Z".parse()?;
3582
    /// assert_eq!(
3583
    ///     ts.round(TimestampRound::new().smallest(Unit::Minute))?.to_string(),
3584
    ///     "2024-06-20T03:26:00Z",
3585
    /// );
3586
    /// // Or, utilize the `From<Unit> for TimestampRound` impl:
3587
    /// assert_eq!(
3588
    ///     ts.round(Unit::Minute)?.to_string(),
3589
    ///     "2024-06-20T03:26:00Z",
3590
    /// );
3591
    ///
3592
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3593
    /// ```
3594
    #[inline]
3595
0
    pub fn smallest(self, unit: Unit) -> TimestampRound {
3596
0
        TimestampRound { smallest: unit, ..self }
3597
0
    }
3598
3599
    /// Set the rounding mode.
3600
    ///
3601
    /// This defaults to [`RoundMode::HalfExpand`], which rounds away from
3602
    /// zero. It matches the kind of rounding you might have been taught in
3603
    /// school.
3604
    ///
3605
    /// # Example
3606
    ///
3607
    /// This shows how to always round timestamps up towards positive infinity.
3608
    ///
3609
    /// ```
3610
    /// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3611
    ///
3612
    /// let ts: Timestamp = "2024-06-20 03:25:01Z".parse()?;
3613
    /// assert_eq!(
3614
    ///     ts.round(
3615
    ///         TimestampRound::new()
3616
    ///             .smallest(Unit::Minute)
3617
    ///             .mode(RoundMode::Ceil),
3618
    ///     )?.to_string(),
3619
    ///     "2024-06-20T03:26:00Z",
3620
    /// );
3621
    ///
3622
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3623
    /// ```
3624
    #[inline]
3625
0
    pub fn mode(self, mode: RoundMode) -> TimestampRound {
3626
0
        TimestampRound { mode, ..self }
3627
0
    }
3628
3629
    /// Set the rounding increment for the smallest unit.
3630
    ///
3631
    /// The default value is `1`. Other values permit rounding the smallest
3632
    /// unit to the nearest integer increment specified. For example, if the
3633
    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
3634
    /// `30` would result in rounding in increments of a half hour. That is,
3635
    /// the only minute value that could result would be `0` or `30`.
3636
    ///
3637
    /// # Errors
3638
    ///
3639
    /// The rounding increment, when combined with the smallest unit (which
3640
    /// defaults to [`Unit::Nanosecond`]), must divide evenly into `86,400`
3641
    /// seconds (one 24-hour civil day). For example, increments of both
3642
    /// 45 seconds and 15 minutes are allowed, but 7 seconds and 25 minutes are
3643
    /// both not allowed.
3644
    ///
3645
    /// # Example
3646
    ///
3647
    /// This example shows how to round a timestamp to the nearest 10 minute
3648
    /// increment.
3649
    ///
3650
    /// ```
3651
    /// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3652
    ///
3653
    /// let ts: Timestamp = "2024-06-20 03:24:59Z".parse()?;
3654
    /// assert_eq!(
3655
    ///     ts.round((Unit::Minute, 10))?.to_string(),
3656
    ///     "2024-06-20T03:20:00Z",
3657
    /// );
3658
    ///
3659
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3660
    /// ```
3661
    #[inline]
3662
0
    pub fn increment(self, increment: i64) -> TimestampRound {
3663
0
        TimestampRound { increment, ..self }
3664
0
    }
3665
3666
    /// Does the actual rounding.
3667
0
    pub(crate) fn round(
3668
0
        &self,
3669
0
        timestamp: Timestamp,
3670
0
    ) -> Result<Timestamp, Error> {
3671
0
        let increment =
3672
0
            increment::for_timestamp(self.smallest, self.increment)?;
3673
0
        let nanosecond = timestamp.as_nanosecond_ranged().without_bounds();
3674
0
        let rounded = self.mode.round_by_unit_in_nanoseconds(
3675
0
            nanosecond,
3676
0
            self.smallest,
3677
0
            increment,
3678
        );
3679
0
        let nanosecond = UnixNanoseconds::rfrom(rounded);
3680
0
        Ok(Timestamp::from_nanosecond_ranged(nanosecond))
3681
0
    }
3682
}
3683
3684
impl Default for TimestampRound {
3685
    #[inline]
3686
0
    fn default() -> TimestampRound {
3687
0
        TimestampRound::new()
3688
0
    }
3689
}
3690
3691
impl From<Unit> for TimestampRound {
3692
    #[inline]
3693
0
    fn from(unit: Unit) -> TimestampRound {
3694
0
        TimestampRound::default().smallest(unit)
3695
0
    }
3696
}
3697
3698
impl From<(Unit, i64)> for TimestampRound {
3699
    #[inline]
3700
0
    fn from((unit, increment): (Unit, i64)) -> TimestampRound {
3701
0
        TimestampRound::from(unit).increment(increment)
3702
0
    }
3703
}
3704
3705
#[cfg(test)]
3706
mod tests {
3707
    use alloc::string::ToString;
3708
3709
    use std::io::Cursor;
3710
3711
    use crate::{
3712
        civil::{self, datetime},
3713
        tz::Offset,
3714
        ToSpan,
3715
    };
3716
3717
    use super::*;
3718
3719
    fn mktime(seconds: i64, nanos: i32) -> Timestamp {
3720
        Timestamp::new(seconds, nanos).unwrap()
3721
    }
3722
3723
    fn mkdt(
3724
        year: i16,
3725
        month: i8,
3726
        day: i8,
3727
        hour: i8,
3728
        minute: i8,
3729
        second: i8,
3730
        nano: i32,
3731
    ) -> civil::DateTime {
3732
        let date = civil::Date::new(year, month, day).unwrap();
3733
        let time = civil::Time::new(hour, minute, second, nano).unwrap();
3734
        civil::DateTime::from_parts(date, time)
3735
    }
3736
3737
    #[test]
3738
    fn to_datetime_specific_examples() {
3739
        let tests = [
3740
            ((UnixSeconds::MIN_REPR, 0), (-9999, 1, 2, 1, 59, 59, 0)),
3741
            (
3742
                (UnixSeconds::MIN_REPR + 1, -999_999_999),
3743
                (-9999, 1, 2, 1, 59, 59, 1),
3744
            ),
3745
            ((-1, 1), (1969, 12, 31, 23, 59, 59, 1)),
3746
            ((UnixSeconds::MAX_REPR, 0), (9999, 12, 30, 22, 0, 0, 0)),
3747
            ((UnixSeconds::MAX_REPR - 1, 0), (9999, 12, 30, 21, 59, 59, 0)),
3748
            (
3749
                (UnixSeconds::MAX_REPR - 1, 999_999_999),
3750
                (9999, 12, 30, 21, 59, 59, 999_999_999),
3751
            ),
3752
            (
3753
                (UnixSeconds::MAX_REPR, 999_999_999),
3754
                (9999, 12, 30, 22, 0, 0, 999_999_999),
3755
            ),
3756
            ((-2, -1), (1969, 12, 31, 23, 59, 57, 999_999_999)),
3757
            ((-86398, -1), (1969, 12, 31, 0, 0, 1, 999_999_999)),
3758
            ((-86399, -1), (1969, 12, 31, 0, 0, 0, 999_999_999)),
3759
            ((-86400, -1), (1969, 12, 30, 23, 59, 59, 999_999_999)),
3760
        ];
3761
        for (t, dt) in tests {
3762
            let timestamp = mktime(t.0, t.1);
3763
            let datetime = mkdt(dt.0, dt.1, dt.2, dt.3, dt.4, dt.5, dt.6);
3764
            assert_eq!(
3765
                Offset::UTC.to_datetime(timestamp),
3766
                datetime,
3767
                "timestamp: {t:?}"
3768
            );
3769
            assert_eq!(
3770
                timestamp,
3771
                datetime.to_zoned(TimeZone::UTC).unwrap().timestamp(),
3772
                "datetime: {datetime:?}"
3773
            );
3774
        }
3775
    }
3776
3777
    #[test]
3778
    fn to_datetime_many_seconds_in_some_days() {
3779
        let days = [
3780
            i64::from(t::UnixEpochDay::MIN_REPR),
3781
            -1000,
3782
            -5,
3783
            23,
3784
            2000,
3785
            i64::from(t::UnixEpochDay::MAX_REPR),
3786
        ];
3787
        let seconds = [
3788
            -86_400, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4,
3789
            5, 6, 7, 8, 9, 10, 86_400,
3790
        ];
3791
        let nanos = [0, 1, 5, 999_999_999];
3792
        for day in days {
3793
            let midpoint = day * 86_400;
3794
            for second in seconds {
3795
                let second = midpoint + second;
3796
                if !UnixSeconds::contains(second) {
3797
                    continue;
3798
                }
3799
                for nano in nanos {
3800
                    if second == UnixSeconds::MIN_REPR && nano != 0 {
3801
                        continue;
3802
                    }
3803
                    let t = Timestamp::new(second, nano).unwrap();
3804
                    let Ok(got) =
3805
                        Offset::UTC.to_datetime(t).to_zoned(TimeZone::UTC)
3806
                    else {
3807
                        continue;
3808
                    };
3809
                    assert_eq!(t, got.timestamp());
3810
                }
3811
            }
3812
        }
3813
    }
3814
3815
    #[test]
3816
    fn invalid_time() {
3817
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, -1).is_err());
3818
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, -999_999_999).is_err());
3819
        // These are greater than the minimum and thus okay!
3820
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, 1).is_ok());
3821
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, 999_999_999).is_ok());
3822
    }
3823
3824
    #[cfg(target_pointer_width = "64")]
3825
    #[test]
3826
    fn timestamp_size() {
3827
        #[cfg(debug_assertions)]
3828
        {
3829
            assert_eq!(40, core::mem::size_of::<Timestamp>());
3830
        }
3831
        #[cfg(not(debug_assertions))]
3832
        {
3833
            assert_eq!(16, core::mem::size_of::<Timestamp>());
3834
        }
3835
    }
3836
3837
    #[test]
3838
    fn nanosecond_roundtrip_boundaries() {
3839
        let inst = Timestamp::MIN;
3840
        let nanos = inst.as_nanosecond_ranged();
3841
        assert_eq!(C(0), nanos % t::NANOS_PER_SECOND);
3842
        let got = Timestamp::from_nanosecond_ranged(nanos);
3843
        assert_eq!(inst, got);
3844
3845
        let inst = Timestamp::MAX;
3846
        let nanos = inst.as_nanosecond_ranged();
3847
        assert_eq!(
3848
            FractionalNanosecond::MAX_SELF,
3849
            nanos % t::NANOS_PER_SECOND
3850
        );
3851
        let got = Timestamp::from_nanosecond_ranged(nanos);
3852
        assert_eq!(inst, got);
3853
    }
3854
3855
    #[test]
3856
    fn timestamp_saturating_add() {
3857
        insta::assert_snapshot!(
3858
            Timestamp::MIN.saturating_add(Span::new().days(1)).unwrap_err(),
3859
            @"saturating `Timestamp` arithmetic requires only time units: operation can only be performed with units of hours or smaller, but found non-zero day units (operations on `Timestamp`, `tz::Offset` and `civil::Time` don't support calendar units in a `Span`)",
3860
        )
3861
    }
3862
3863
    #[test]
3864
    fn timestamp_saturating_sub() {
3865
        insta::assert_snapshot!(
3866
            Timestamp::MAX.saturating_sub(Span::new().days(1)).unwrap_err(),
3867
            @"saturating `Timestamp` arithmetic requires only time units: operation can only be performed with units of hours or smaller, but found non-zero day units (operations on `Timestamp`, `tz::Offset` and `civil::Time` don't support calendar units in a `Span`)",
3868
        )
3869
    }
3870
3871
    quickcheck::quickcheck! {
3872
        fn prop_unix_seconds_roundtrip(t: Timestamp) -> quickcheck::TestResult {
3873
            let dt = t.to_zoned(TimeZone::UTC).datetime();
3874
            let Ok(got) = dt.to_zoned(TimeZone::UTC) else {
3875
                return quickcheck::TestResult::discard();
3876
            };
3877
            quickcheck::TestResult::from_bool(t == got.timestamp())
3878
        }
3879
3880
        fn prop_nanos_roundtrip_unix_ranged(t: Timestamp) -> bool {
3881
            let nanos = t.as_nanosecond_ranged();
3882
            let got = Timestamp::from_nanosecond_ranged(nanos);
3883
            t == got
3884
        }
3885
3886
        fn prop_nanos_roundtrip_unix(t: Timestamp) -> bool {
3887
            let nanos = t.as_nanosecond();
3888
            let got = Timestamp::from_nanosecond(nanos).unwrap();
3889
            t == got
3890
        }
3891
3892
        fn timestamp_constant_and_new_are_same1(t: Timestamp) -> bool {
3893
            let got = Timestamp::constant(t.as_second(), t.subsec_nanosecond());
3894
            t == got
3895
        }
3896
3897
        fn timestamp_constant_and_new_are_same2(
3898
            secs: i64,
3899
            nanos: i32
3900
        ) -> quickcheck::TestResult {
3901
            let Ok(ts) = Timestamp::new(secs, nanos) else {
3902
                return quickcheck::TestResult::discard();
3903
            };
3904
            let got = Timestamp::constant(secs, nanos);
3905
            quickcheck::TestResult::from_bool(ts == got)
3906
        }
3907
    }
3908
3909
    /// A `serde` deserializer compatibility test.
3910
    ///
3911
    /// Serde YAML used to be unable to deserialize `jiff` types,
3912
    /// as deserializing from bytes is not supported by the deserializer.
3913
    ///
3914
    /// - <https://github.com/BurntSushi/jiff/issues/138>
3915
    /// - <https://github.com/BurntSushi/jiff/discussions/148>
3916
    #[test]
3917
    fn timestamp_deserialize_yaml() {
3918
        let expected = datetime(2024, 10, 31, 16, 33, 53, 123456789)
3919
            .to_zoned(TimeZone::UTC)
3920
            .unwrap()
3921
            .timestamp();
3922
3923
        let deserialized: Timestamp =
3924
            serde_yaml::from_str("2024-10-31T16:33:53.123456789+00:00")
3925
                .unwrap();
3926
3927
        assert_eq!(deserialized, expected);
3928
3929
        let deserialized: Timestamp = serde_yaml::from_slice(
3930
            "2024-10-31T16:33:53.123456789+00:00".as_bytes(),
3931
        )
3932
        .unwrap();
3933
3934
        assert_eq!(deserialized, expected);
3935
3936
        let cursor = Cursor::new(b"2024-10-31T16:33:53.123456789+00:00");
3937
        let deserialized: Timestamp = serde_yaml::from_reader(cursor).unwrap();
3938
3939
        assert_eq!(deserialized, expected);
3940
    }
3941
3942
    #[test]
3943
    fn timestamp_precision_loss() {
3944
        let ts1: Timestamp =
3945
            "2025-01-25T19:32:21.783444592+01:00".parse().unwrap();
3946
        let span = 1.second();
3947
        let ts2 = ts1 + span;
3948
        assert_eq!(ts2.to_string(), "2025-01-25T18:32:22.783444592Z");
3949
        assert_eq!(ts1, ts2 - span, "should be reversible");
3950
    }
3951
}