Coverage Report

Created: 2025-07-23 06:05

/rust/registry/src/index.crates.io-6f17d22bba15001f/jiff-0.2.5/src/timestamp.rs
Line
Count
Source (jump to first uncovered line)
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
0
        // We now normalize our seconds and nanoseconds such that they have
527
0
        // the same sign (or where one is zero). So for example, when given
528
0
        // `-1s 1ns`, then we should turn that into `-999,999,999ns`.
529
0
        //
530
0
        // 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
0
            "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
0
            "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
0
            "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
    }
Unexecuted instantiation: <jiff::timestamp::Timestamp>::from_duration
Unexecuted instantiation: <jiff::timestamp::Timestamp>::from_duration
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
0
        // The common case is probably a span without fractional seconds, so
1514
0
        // we specialize for that since it requires a fair bit less math.
1515
0
        //
1516
0
        // Note that this only works when *both* the span and timestamp lack
1517
0
        // 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
0
            "saturating `Timestamp` arithmetic requires only time units",
1653
0
        )
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: impl RInto<UnixSeconds>,
2258
0
        nanosecond: impl RInto<FractionalNanosecond>,
2259
0
    ) -> Result<Timestamp, Error> {
2260
0
        let (second, nanosecond) = (second.rinto(), nanosecond.rinto());
2261
0
        if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2262
0
            return Err(Error::range(
2263
0
                "seconds and nanoseconds",
2264
0
                nanosecond,
2265
0
                0,
2266
0
                1_000_000_000,
2267
0
            ));
2268
0
        }
2269
0
        // We now normalize our seconds and nanoseconds such that they have
2270
0
        // the same sign (or where one is zero). So for example, when given
2271
0
        // `-1s 1ns`, then we should turn that into `-999,999,999ns`.
2272
0
        //
2273
0
        // But first, if we're already normalized, we're done!
2274
0
        if second.signum() == nanosecond.signum()
2275
0
            || second == C(0)
2276
0
            || nanosecond == C(0)
2277
        {
2278
0
            return Ok(Timestamp { second, nanosecond });
2279
0
        }
2280
0
        let second = second.without_bounds();
2281
0
        let nanosecond = nanosecond.without_bounds();
2282
0
        let [delta_second, delta_nanosecond] = t::NoUnits::vary_many(
2283
0
            [second, nanosecond],
2284
0
            |[second, nanosecond]| {
2285
0
                if second < C(0) && nanosecond > C(0) {
2286
0
                    [C(1), (-t::NANOS_PER_SECOND).rinto()]
2287
0
                } else if second > C(0) && nanosecond < C(0) {
2288
0
                    [C(-1), t::NANOS_PER_SECOND.rinto()]
2289
                } else {
2290
0
                    [C(0), C(0)]
2291
                }
2292
0
            },
2293
0
        );
2294
0
        Ok(Timestamp {
2295
0
            second: (second + delta_second).rinto(),
2296
0
            nanosecond: (nanosecond + delta_nanosecond).rinto(),
2297
0
        })
2298
0
    }
2299
2300
    #[inline]
2301
0
    fn from_second_ranged(second: UnixSeconds) -> Timestamp {
2302
0
        Timestamp { second, nanosecond: FractionalNanosecond::N::<0>() }
2303
0
    }
2304
2305
    #[inline]
2306
0
    fn from_millisecond_ranged(millisecond: UnixMilliseconds) -> Timestamp {
2307
0
        let second =
2308
0
            UnixSeconds::rfrom(millisecond.div_ceil(t::MILLIS_PER_SECOND));
2309
0
        let nanosecond = FractionalNanosecond::rfrom(
2310
0
            millisecond.rem_ceil(t::MILLIS_PER_SECOND) * t::NANOS_PER_MILLI,
2311
0
        );
2312
0
        Timestamp { second, nanosecond }
2313
0
    }
2314
2315
    #[inline]
2316
0
    fn from_microsecond_ranged(microsecond: UnixMicroseconds) -> Timestamp {
2317
0
        let second =
2318
0
            UnixSeconds::rfrom(microsecond.div_ceil(t::MICROS_PER_SECOND));
2319
0
        let nanosecond = FractionalNanosecond::rfrom(
2320
0
            microsecond.rem_ceil(t::MICROS_PER_SECOND) * t::NANOS_PER_MICRO,
2321
0
        );
2322
0
        Timestamp { second, nanosecond }
2323
0
    }
2324
2325
    #[inline]
2326
0
    pub(crate) fn from_nanosecond_ranged(
2327
0
        nanosecond: UnixNanoseconds,
2328
0
    ) -> Timestamp {
2329
0
        let second =
2330
0
            UnixSeconds::rfrom(nanosecond.div_ceil(t::NANOS_PER_SECOND));
2331
0
        let nanosecond = nanosecond.rem_ceil(t::NANOS_PER_SECOND).rinto();
2332
0
        Timestamp { second, nanosecond }
2333
0
    }
2334
2335
    #[inline]
2336
0
    pub(crate) fn from_itimestamp(
2337
0
        its: Composite<ITimestamp>,
2338
0
    ) -> Result<Timestamp, Error> {
2339
0
        let (second, nanosecond) =
2340
0
            rangeint::uncomposite!(its, c => (c.second, c.nanosecond));
2341
0
        Ok(Timestamp {
2342
0
            second: second.try_to_rint("unix-seconds")?,
2343
0
            nanosecond: nanosecond.to_rint(),
2344
        })
2345
0
    }
2346
2347
    #[inline]
2348
0
    pub(crate) fn to_itimestamp(&self) -> Composite<ITimestamp> {
2349
0
        rangeint::composite! {
2350
0
            (second = self.second, nanosecond = self.nanosecond) => {
2351
0
                ITimestamp { second, nanosecond }
2352
0
            }
2353
0
        }
2354
0
    }
2355
2356
    #[inline]
2357
0
    pub(crate) const fn from_itimestamp_const(its: ITimestamp) -> Timestamp {
2358
0
        Timestamp {
2359
0
            second: UnixSeconds::new_unchecked(its.second),
2360
0
            nanosecond: FractionalNanosecond::new_unchecked(its.nanosecond),
2361
0
        }
2362
0
    }
2363
2364
    #[inline]
2365
0
    pub(crate) const fn to_itimestamp_const(&self) -> ITimestamp {
2366
0
        ITimestamp {
2367
0
            second: self.second.get_unchecked(),
2368
0
            nanosecond: self.nanosecond.get_unchecked(),
2369
0
        }
2370
0
    }
2371
2372
    #[inline]
2373
0
    pub(crate) fn as_second_ranged(self) -> UnixSeconds {
2374
0
        self.second
2375
0
    }
2376
2377
    #[inline]
2378
0
    fn as_millisecond_ranged(self) -> UnixMilliseconds {
2379
0
        let second = NoUnits::rfrom(self.as_second_ranged());
2380
0
        let nanosecond = NoUnits::rfrom(self.subsec_nanosecond_ranged());
2381
0
        // The minimum value of a timestamp has the fractional nanosecond set
2382
0
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2383
0
        // a case where we return a ranged integer with a minimum value less
2384
0
        // than the actual minimum, we clamp the fractional part to 0 when the
2385
0
        // second value is the minimum.
2386
0
        let [second, nanosecond] =
2387
0
            NoUnits::vary_many([second, nanosecond], |[second, nanosecond]| {
2388
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2389
0
                    [second, C(0).rinto()]
2390
                } else {
2391
0
                    [second, nanosecond]
2392
                }
2393
0
            });
2394
0
        UnixMilliseconds::rfrom(
2395
0
            (second * t::MILLIS_PER_SECOND)
2396
0
                + (nanosecond.div_ceil(t::NANOS_PER_MILLI)),
2397
0
        )
2398
0
    }
2399
2400
    #[inline]
2401
0
    fn as_microsecond_ranged(self) -> UnixMicroseconds {
2402
0
        let second = NoUnits::rfrom(self.as_second_ranged());
2403
0
        let nanosecond = NoUnits::rfrom(self.subsec_nanosecond_ranged());
2404
0
        // The minimum value of a timestamp has the fractional nanosecond set
2405
0
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2406
0
        // a case where we return a ranged integer with a minimum value less
2407
0
        // than the actual minimum, we clamp the fractional part to 0 when the
2408
0
        // second value is the minimum.
2409
0
        let [second, nanosecond] =
2410
0
            NoUnits::vary_many([second, nanosecond], |[second, nanosecond]| {
2411
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2412
0
                    [second, C(0).rinto()]
2413
                } else {
2414
0
                    [second, nanosecond]
2415
                }
2416
0
            });
2417
0
        UnixMicroseconds::rfrom(
2418
0
            (second * t::MICROS_PER_SECOND)
2419
0
                + (nanosecond.div_ceil(t::NANOS_PER_MICRO)),
2420
0
        )
2421
0
    }
2422
2423
    #[inline]
2424
0
    pub(crate) fn as_nanosecond_ranged(self) -> UnixNanoseconds {
2425
0
        let second = NoUnits128::rfrom(self.as_second_ranged());
2426
0
        let nanosecond = NoUnits128::rfrom(self.subsec_nanosecond_ranged());
2427
0
        // The minimum value of a timestamp has the fractional nanosecond set
2428
0
        // to 0, but otherwise its minimum value is -999_999_999. So to avoid
2429
0
        // a case where we return a ranged integer with a minimum value less
2430
0
        // than the actual minimum, we clamp the fractional part to 0 when the
2431
0
        // second value is the minimum.
2432
0
        let [second, nanosecond] = NoUnits128::vary_many(
2433
0
            [second, nanosecond],
2434
0
            |[second, nanosecond]| {
2435
0
                if second == UnixSeconds::MIN_SELF && nanosecond < C(0) {
2436
0
                    [second, C(0).rinto()]
2437
                } else {
2438
0
                    [second, nanosecond]
2439
                }
2440
0
            },
2441
0
        );
2442
0
        UnixNanoseconds::rfrom(second * t::NANOS_PER_SECOND + nanosecond)
2443
0
    }
2444
2445
    #[inline]
2446
0
    fn subsec_millisecond_ranged(self) -> t::FractionalMillisecond {
2447
0
        let millis =
2448
0
            self.subsec_nanosecond_ranged().div_ceil(t::NANOS_PER_MILLI);
2449
0
        t::FractionalMillisecond::rfrom(millis)
2450
0
    }
2451
2452
    #[inline]
2453
0
    fn subsec_microsecond_ranged(self) -> t::FractionalMicrosecond {
2454
0
        let micros =
2455
0
            self.subsec_nanosecond_ranged().div_ceil(t::NANOS_PER_MICRO);
2456
0
        t::FractionalMicrosecond::rfrom(micros)
2457
0
    }
2458
2459
    #[inline]
2460
0
    pub(crate) fn subsec_nanosecond_ranged(self) -> FractionalNanosecond {
2461
0
        self.nanosecond
2462
0
    }
2463
}
2464
2465
impl Default for Timestamp {
2466
    #[inline]
2467
0
    fn default() -> Timestamp {
2468
0
        Timestamp::UNIX_EPOCH
2469
0
    }
2470
}
2471
2472
/// Converts a `Timestamp` datetime into a human readable datetime string.
2473
///
2474
/// (This `Debug` representation currently emits the same string as the
2475
/// `Display` representation, but this is not a guarantee.)
2476
///
2477
/// Options currently supported:
2478
///
2479
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2480
/// of the fractional second component.
2481
///
2482
/// # Example
2483
///
2484
/// ```
2485
/// use jiff::Timestamp;
2486
///
2487
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2488
/// assert_eq!(
2489
///     format!("{ts:.6?}"),
2490
///     "2005-08-07T23:19:49.123000Z",
2491
/// );
2492
/// // Precision values greater than 9 are clamped to 9.
2493
/// assert_eq!(
2494
///     format!("{ts:.300?}"),
2495
///     "2005-08-07T23:19:49.123000000Z",
2496
/// );
2497
/// // A precision of 0 implies the entire fractional
2498
/// // component is always truncated.
2499
/// assert_eq!(
2500
///     format!("{ts:.0?}"),
2501
///     "2005-08-07T23:19:49Z",
2502
/// );
2503
///
2504
/// # Ok::<(), Box<dyn std::error::Error>>(())
2505
/// ```
2506
impl core::fmt::Debug for Timestamp {
2507
    #[inline]
2508
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2509
0
        core::fmt::Display::fmt(self, f)
2510
0
    }
2511
}
2512
2513
/// Converts a `Timestamp` datetime into a RFC 3339 compliant string.
2514
///
2515
/// Since a `Timestamp` never has an offset associated with it and is always
2516
/// in UTC, the string emitted by this trait implementation uses `Z` for "Zulu"
2517
/// time. The significance of Zulu time is prescribed by RFC 9557 and means
2518
/// that "the time in UTC is known, but the offset to local time is unknown."
2519
/// If you need to emit an RFC 3339 compliant string with a specific offset,
2520
/// then use [`Timestamp::display_with_offset`].
2521
///
2522
/// # Forrmatting options supported
2523
///
2524
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2525
/// of the fractional second component.
2526
///
2527
/// # Example
2528
///
2529
/// ```
2530
/// use jiff::Timestamp;
2531
///
2532
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2533
/// assert_eq!(
2534
///     format!("{ts:.6}"),
2535
///     "2005-08-07T23:19:49.123000Z",
2536
/// );
2537
/// // Precision values greater than 9 are clamped to 9.
2538
/// assert_eq!(
2539
///     format!("{ts:.300}"),
2540
///     "2005-08-07T23:19:49.123000000Z",
2541
/// );
2542
/// // A precision of 0 implies the entire fractional
2543
/// // component is always truncated.
2544
/// assert_eq!(
2545
///     format!("{ts:.0}"),
2546
///     "2005-08-07T23:19:49Z",
2547
/// );
2548
///
2549
/// # Ok::<(), Box<dyn std::error::Error>>(())
2550
/// ```
2551
impl core::fmt::Display for Timestamp {
2552
    #[inline]
2553
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2554
        use crate::fmt::StdFmtWrite;
2555
2556
0
        let precision =
2557
0
            f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
2558
0
        temporal::DateTimePrinter::new()
2559
0
            .precision(precision)
2560
0
            .print_timestamp(self, StdFmtWrite(f))
2561
0
            .map_err(|_| core::fmt::Error)
2562
0
    }
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::fmt::Display>::fmt
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::fmt::Display>::fmt
2563
}
2564
2565
impl core::str::FromStr for Timestamp {
2566
    type Err = Error;
2567
2568
    #[inline]
2569
0
    fn from_str(string: &str) -> Result<Timestamp, Error> {
2570
0
        DEFAULT_DATETIME_PARSER.parse_timestamp(string)
2571
0
    }
2572
}
2573
2574
impl Eq for Timestamp {}
2575
2576
impl PartialEq for Timestamp {
2577
    #[inline]
2578
0
    fn eq(&self, rhs: &Timestamp) -> bool {
2579
0
        self.as_second_ranged().get() == rhs.as_second_ranged().get()
2580
0
            && self.subsec_nanosecond_ranged().get()
2581
0
                == rhs.subsec_nanosecond_ranged().get()
2582
0
    }
2583
}
2584
2585
impl Ord for Timestamp {
2586
    #[inline]
2587
0
    fn cmp(&self, rhs: &Timestamp) -> core::cmp::Ordering {
2588
0
        (self.as_second_ranged().get(), self.subsec_nanosecond_ranged().get())
2589
0
            .cmp(&(
2590
0
                rhs.as_second_ranged().get(),
2591
0
                rhs.subsec_nanosecond_ranged().get(),
2592
0
            ))
2593
0
    }
2594
}
2595
2596
impl PartialOrd for Timestamp {
2597
    #[inline]
2598
0
    fn partial_cmp(&self, rhs: &Timestamp) -> Option<core::cmp::Ordering> {
2599
0
        Some(self.cmp(rhs))
2600
0
    }
2601
}
2602
2603
impl core::hash::Hash for Timestamp {
2604
    #[inline]
2605
0
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
2606
0
        self.as_second_ranged().get().hash(state);
2607
0
        self.subsec_nanosecond_ranged().get().hash(state);
2608
0
    }
2609
}
2610
2611
/// Adds a span of time to a timestamp.
2612
///
2613
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2614
/// without panics, use [`Timestamp::checked_add`]. Note that the failure
2615
/// condition includes overflow and using a `Span` with non-zero units greater
2616
/// than hours.
2617
impl core::ops::Add<Span> for Timestamp {
2618
    type Output = Timestamp;
2619
2620
    #[inline]
2621
0
    fn add(self, rhs: Span) -> Timestamp {
2622
0
        self.checked_add_span(rhs).expect("adding span to timestamp failed")
2623
0
    }
2624
}
2625
2626
/// Adds a span of time to a timestamp in place.
2627
///
2628
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2629
/// without panics, use [`Timestamp::checked_add`]. Note that the failure
2630
/// condition includes overflow and using a `Span` with non-zero units greater
2631
/// than hours.
2632
impl core::ops::AddAssign<Span> for Timestamp {
2633
    #[inline]
2634
0
    fn add_assign(&mut self, rhs: Span) {
2635
0
        *self = *self + rhs
2636
0
    }
2637
}
2638
2639
/// Subtracts a span of time from a timestamp.
2640
///
2641
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2642
/// without panics, use [`Timestamp::checked_sub`]. Note that the failure
2643
/// condition includes overflow and using a `Span` with non-zero units greater
2644
/// than hours.
2645
impl core::ops::Sub<Span> for Timestamp {
2646
    type Output = Timestamp;
2647
2648
    #[inline]
2649
0
    fn sub(self, rhs: Span) -> Timestamp {
2650
0
        self.checked_add_span(rhs.negate())
2651
0
            .expect("subtracting span from timestamp failed")
2652
0
    }
2653
}
2654
2655
/// Subtracts a span of time from a timestamp in place.
2656
///
2657
/// This uses checked arithmetic and panics when it fails. To handle arithmetic
2658
/// without panics, use [`Timestamp::checked_sub`]. Note that the failure
2659
/// condition includes overflow and using a `Span` with non-zero units greater
2660
/// than hours.
2661
impl core::ops::SubAssign<Span> for Timestamp {
2662
    #[inline]
2663
0
    fn sub_assign(&mut self, rhs: Span) {
2664
0
        *self = *self - rhs
2665
0
    }
2666
}
2667
2668
/// Computes the span of time between two timestamps.
2669
///
2670
/// This will return a negative span when the timestamp being subtracted is
2671
/// greater.
2672
///
2673
/// Since this uses the default configuration for calculating a span between
2674
/// two timestamps (no rounding and largest units is seconds), this will never
2675
/// panic or fail in any way.
2676
///
2677
/// To configure the largest unit or enable rounding, use [`Timestamp::since`].
2678
impl core::ops::Sub for Timestamp {
2679
    type Output = Span;
2680
2681
    #[inline]
2682
0
    fn sub(self, rhs: Timestamp) -> Span {
2683
0
        self.since(rhs).expect("since never fails when given Timestamp")
2684
0
    }
2685
}
2686
2687
/// Adds a signed duration of time to a timestamp.
2688
///
2689
/// This uses checked arithmetic and panics on overflow. To handle overflow
2690
/// without panics, use [`Timestamp::checked_add`].
2691
impl core::ops::Add<SignedDuration> for Timestamp {
2692
    type Output = Timestamp;
2693
2694
    #[inline]
2695
0
    fn add(self, rhs: SignedDuration) -> Timestamp {
2696
0
        self.checked_add_duration(rhs)
2697
0
            .expect("adding signed duration to timestamp overflowed")
2698
0
    }
2699
}
2700
2701
/// Adds a signed duration of time to a timestamp in place.
2702
///
2703
/// This uses checked arithmetic and panics on overflow. To handle overflow
2704
/// without panics, use [`Timestamp::checked_add`].
2705
impl core::ops::AddAssign<SignedDuration> for Timestamp {
2706
    #[inline]
2707
0
    fn add_assign(&mut self, rhs: SignedDuration) {
2708
0
        *self = *self + rhs
2709
0
    }
2710
}
2711
2712
/// Subtracts a signed duration of time from a timestamp.
2713
///
2714
/// This uses checked arithmetic and panics on overflow. To handle overflow
2715
/// without panics, use [`Timestamp::checked_sub`].
2716
impl core::ops::Sub<SignedDuration> for Timestamp {
2717
    type Output = Timestamp;
2718
2719
    #[inline]
2720
0
    fn sub(self, rhs: SignedDuration) -> Timestamp {
2721
0
        let rhs = rhs
2722
0
            .checked_neg()
2723
0
            .expect("signed duration negation resulted in overflow");
2724
0
        self.checked_add_duration(rhs)
2725
0
            .expect("subtracting signed duration from timestamp overflowed")
2726
0
    }
2727
}
2728
2729
/// Subtracts a signed duration of time from a timestamp in place.
2730
///
2731
/// This uses checked arithmetic and panics on overflow. To handle overflow
2732
/// without panics, use [`Timestamp::checked_sub`].
2733
impl core::ops::SubAssign<SignedDuration> for Timestamp {
2734
    #[inline]
2735
0
    fn sub_assign(&mut self, rhs: SignedDuration) {
2736
0
        *self = *self - rhs
2737
0
    }
2738
}
2739
2740
/// Adds an unsigned duration of time to a timestamp.
2741
///
2742
/// This uses checked arithmetic and panics on overflow. To handle overflow
2743
/// without panics, use [`Timestamp::checked_add`].
2744
impl core::ops::Add<UnsignedDuration> for Timestamp {
2745
    type Output = Timestamp;
2746
2747
    #[inline]
2748
0
    fn add(self, rhs: UnsignedDuration) -> Timestamp {
2749
0
        self.checked_add(rhs)
2750
0
            .expect("adding unsigned duration to timestamp overflowed")
2751
0
    }
2752
}
2753
2754
/// Adds an unsigned duration of time to a timestamp in place.
2755
///
2756
/// This uses checked arithmetic and panics on overflow. To handle overflow
2757
/// without panics, use [`Timestamp::checked_add`].
2758
impl core::ops::AddAssign<UnsignedDuration> for Timestamp {
2759
    #[inline]
2760
0
    fn add_assign(&mut self, rhs: UnsignedDuration) {
2761
0
        *self = *self + rhs
2762
0
    }
2763
}
2764
2765
/// Subtracts an unsigned duration of time from a timestamp.
2766
///
2767
/// This uses checked arithmetic and panics on overflow. To handle overflow
2768
/// without panics, use [`Timestamp::checked_sub`].
2769
impl core::ops::Sub<UnsignedDuration> for Timestamp {
2770
    type Output = Timestamp;
2771
2772
    #[inline]
2773
0
    fn sub(self, rhs: UnsignedDuration) -> Timestamp {
2774
0
        self.checked_sub(rhs)
2775
0
            .expect("subtracting unsigned duration from timestamp overflowed")
2776
0
    }
2777
}
2778
2779
/// Subtracts an unsigned duration of time from a timestamp in place.
2780
///
2781
/// This uses checked arithmetic and panics on overflow. To handle overflow
2782
/// without panics, use [`Timestamp::checked_sub`].
2783
impl core::ops::SubAssign<UnsignedDuration> for Timestamp {
2784
    #[inline]
2785
0
    fn sub_assign(&mut self, rhs: UnsignedDuration) {
2786
0
        *self = *self - rhs
2787
0
    }
2788
}
2789
2790
impl From<Zoned> for Timestamp {
2791
    #[inline]
2792
0
    fn from(zdt: Zoned) -> Timestamp {
2793
0
        zdt.timestamp()
2794
0
    }
2795
}
2796
2797
impl<'a> From<&'a Zoned> for Timestamp {
2798
    #[inline]
2799
0
    fn from(zdt: &'a Zoned) -> Timestamp {
2800
0
        zdt.timestamp()
2801
0
    }
2802
}
2803
2804
#[cfg(feature = "std")]
2805
impl From<Timestamp> for std::time::SystemTime {
2806
    #[inline]
2807
0
    fn from(time: Timestamp) -> std::time::SystemTime {
2808
0
        let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
2809
0
        let sdur = time.as_duration();
2810
0
        let dur = sdur.unsigned_abs();
2811
0
        // These are guaranteed to succeed because we assume that SystemTime
2812
0
        // uses at least 64 bits for the time, and our durations are capped via
2813
0
        // the range on UnixSeconds.
2814
0
        if sdur.is_negative() {
2815
0
            unix_epoch.checked_sub(dur).expect("duration too big (negative)")
2816
        } else {
2817
0
            unix_epoch.checked_add(dur).expect("duration too big (positive)")
2818
        }
2819
0
    }
2820
}
2821
2822
#[cfg(feature = "std")]
2823
impl TryFrom<std::time::SystemTime> for Timestamp {
2824
    type Error = Error;
2825
2826
    #[inline]
2827
0
    fn try_from(
2828
0
        system_time: std::time::SystemTime,
2829
0
    ) -> Result<Timestamp, Error> {
2830
0
        let unix_epoch = std::time::SystemTime::UNIX_EPOCH;
2831
0
        let dur = SignedDuration::system_until(unix_epoch, system_time)?;
2832
0
        Timestamp::from_duration(dur)
2833
0
    }
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::convert::TryFrom<std::time::SystemTime>>::try_from
Unexecuted instantiation: <jiff::timestamp::Timestamp as core::convert::TryFrom<std::time::SystemTime>>::try_from
2834
}
2835
2836
#[cfg(feature = "serde")]
2837
impl serde::Serialize for Timestamp {
2838
    #[inline]
2839
    fn serialize<S: serde::Serializer>(
2840
        &self,
2841
        serializer: S,
2842
    ) -> Result<S::Ok, S::Error> {
2843
        serializer.collect_str(self)
2844
    }
2845
}
2846
2847
#[cfg(feature = "serde")]
2848
impl<'de> serde::Deserialize<'de> for Timestamp {
2849
    #[inline]
2850
    fn deserialize<D: serde::Deserializer<'de>>(
2851
        deserializer: D,
2852
    ) -> Result<Timestamp, D::Error> {
2853
        use serde::de;
2854
2855
        struct TimestampVisitor;
2856
2857
        impl<'de> de::Visitor<'de> for TimestampVisitor {
2858
            type Value = Timestamp;
2859
2860
            fn expecting(
2861
                &self,
2862
                f: &mut core::fmt::Formatter,
2863
            ) -> core::fmt::Result {
2864
                f.write_str("a timestamp string")
2865
            }
2866
2867
            #[inline]
2868
            fn visit_bytes<E: de::Error>(
2869
                self,
2870
                value: &[u8],
2871
            ) -> Result<Timestamp, E> {
2872
                DEFAULT_DATETIME_PARSER
2873
                    .parse_timestamp(value)
2874
                    .map_err(de::Error::custom)
2875
            }
2876
2877
            #[inline]
2878
            fn visit_str<E: de::Error>(
2879
                self,
2880
                value: &str,
2881
            ) -> Result<Timestamp, E> {
2882
                self.visit_bytes(value.as_bytes())
2883
            }
2884
        }
2885
2886
        deserializer.deserialize_str(TimestampVisitor)
2887
    }
2888
}
2889
2890
#[cfg(test)]
2891
impl quickcheck::Arbitrary for Timestamp {
2892
    fn arbitrary(g: &mut quickcheck::Gen) -> Timestamp {
2893
        use quickcheck::Arbitrary;
2894
2895
        let seconds: UnixSeconds = Arbitrary::arbitrary(g);
2896
        let mut nanoseconds: FractionalNanosecond = Arbitrary::arbitrary(g);
2897
        // nanoseconds must be zero for the minimum second value,
2898
        // so just clamp it to 0.
2899
        if seconds == UnixSeconds::MIN_SELF && nanoseconds < C(0) {
2900
            nanoseconds = C(0).rinto();
2901
        }
2902
        Timestamp::new_ranged(seconds, nanoseconds).unwrap_or_default()
2903
    }
2904
2905
    fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Self>> {
2906
        let second = self.as_second_ranged();
2907
        let nanosecond = self.subsec_nanosecond_ranged();
2908
        alloc::boxed::Box::new((second, nanosecond).shrink().filter_map(
2909
            |(second, nanosecond)| {
2910
                if second == UnixSeconds::MIN_SELF && nanosecond > C(0) {
2911
                    None
2912
                } else {
2913
                    Timestamp::new_ranged(second, nanosecond).ok()
2914
                }
2915
            },
2916
        ))
2917
    }
2918
}
2919
2920
/// A type for formatting a [`Timestamp`] with a specific offset.
2921
///
2922
/// This type is created by the [`Timestamp::display_with_offset`] method.
2923
///
2924
/// Like the [`std::fmt::Display`] trait implementation for `Timestamp`, this
2925
/// always emits an RFC 3339 compliant string. Unlike `Timestamp`'s `Display`
2926
/// trait implementation, which always uses `Z` or "Zulu" time, this always
2927
/// uses an offfset.
2928
///
2929
/// # Forrmatting options supported
2930
///
2931
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2932
/// of the fractional second component.
2933
///
2934
/// # Example
2935
///
2936
/// ```
2937
/// use jiff::{tz, Timestamp};
2938
///
2939
/// let offset = tz::offset(-5);
2940
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2941
/// assert_eq!(
2942
///     format!("{ts:.6}", ts = ts.display_with_offset(offset)),
2943
///     "2005-08-07T18:19:49.123000-05:00",
2944
/// );
2945
/// // Precision values greater than 9 are clamped to 9.
2946
/// assert_eq!(
2947
///     format!("{ts:.300}", ts = ts.display_with_offset(offset)),
2948
///     "2005-08-07T18:19:49.123000000-05:00",
2949
/// );
2950
/// // A precision of 0 implies the entire fractional
2951
/// // component is always truncated.
2952
/// assert_eq!(
2953
///     format!("{ts:.0}", ts = ts.display_with_offset(tz::Offset::UTC)),
2954
///     "2005-08-07T23:19:49+00:00",
2955
/// );
2956
///
2957
/// # Ok::<(), Box<dyn std::error::Error>>(())
2958
/// ```
2959
#[derive(Clone, Copy, Debug)]
2960
pub struct TimestampDisplayWithOffset {
2961
    timestamp: Timestamp,
2962
    offset: Offset,
2963
}
2964
2965
impl core::fmt::Display for TimestampDisplayWithOffset {
2966
    #[inline]
2967
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
2968
        use crate::fmt::StdFmtWrite;
2969
2970
0
        let precision =
2971
0
            f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
2972
0
        temporal::DateTimePrinter::new()
2973
0
            .precision(precision)
2974
0
            .print_timestamp_with_offset(
2975
0
                &self.timestamp,
2976
0
                self.offset,
2977
0
                StdFmtWrite(f),
2978
0
            )
2979
0
            .map_err(|_| core::fmt::Error)
2980
0
    }
2981
}
2982
2983
/// An iterator over periodic timestamps, created by [`Timestamp::series`].
2984
///
2985
/// It is exhausted when the next value would exceed a [`Span`] or
2986
/// [`Timestamp`] value.
2987
#[derive(Clone, Debug)]
2988
pub struct TimestampSeries {
2989
    ts: Timestamp,
2990
    duration: Option<SignedDuration>,
2991
}
2992
2993
impl TimestampSeries {
2994
    #[inline]
2995
0
    fn new(ts: Timestamp, period: Span) -> TimestampSeries {
2996
0
        let duration = SignedDuration::try_from(period).ok();
2997
0
        TimestampSeries { ts, duration }
2998
0
    }
2999
}
3000
3001
impl Iterator for TimestampSeries {
3002
    type Item = Timestamp;
3003
3004
    #[inline]
3005
0
    fn next(&mut self) -> Option<Timestamp> {
3006
0
        let duration = self.duration?;
3007
0
        let this = self.ts;
3008
0
        self.ts = self.ts.checked_add_duration(duration).ok()?;
3009
0
        Some(this)
3010
0
    }
3011
}
3012
3013
/// Options for [`Timestamp::checked_add`] and [`Timestamp::checked_sub`].
3014
///
3015
/// This type provides a way to ergonomically add one of a few different
3016
/// duration types to a [`Timestamp`].
3017
///
3018
/// The main way to construct values of this type is with its `From` trait
3019
/// implementations:
3020
///
3021
/// * `From<Span> for TimestampArithmetic` adds (or subtracts) the given span
3022
/// to the receiver timestamp.
3023
/// * `From<SignedDuration> for TimestampArithmetic` adds (or subtracts)
3024
/// the given signed duration to the receiver timestamp.
3025
/// * `From<std::time::Duration> for TimestampArithmetic` adds (or subtracts)
3026
/// the given unsigned duration to the receiver timestamp.
3027
///
3028
/// # Example
3029
///
3030
/// ```
3031
/// use std::time::Duration;
3032
///
3033
/// use jiff::{SignedDuration, Timestamp, ToSpan};
3034
///
3035
/// let ts: Timestamp = "2024-02-28T00:00:00Z".parse()?;
3036
/// assert_eq!(
3037
///     ts.checked_add(48.hours())?,
3038
///     "2024-03-01T00:00:00Z".parse()?,
3039
/// );
3040
/// assert_eq!(
3041
///     ts.checked_add(SignedDuration::from_hours(48))?,
3042
///     "2024-03-01T00:00:00Z".parse()?,
3043
/// );
3044
/// assert_eq!(
3045
///     ts.checked_add(Duration::from_secs(48 * 60 * 60))?,
3046
///     "2024-03-01T00:00:00Z".parse()?,
3047
/// );
3048
///
3049
/// # Ok::<(), Box<dyn std::error::Error>>(())
3050
/// ```
3051
#[derive(Clone, Copy, Debug)]
3052
pub struct TimestampArithmetic {
3053
    duration: Duration,
3054
}
3055
3056
impl TimestampArithmetic {
3057
    #[inline]
3058
0
    fn checked_add(self, ts: Timestamp) -> Result<Timestamp, Error> {
3059
0
        match self.duration.to_signed()? {
3060
0
            SDuration::Span(span) => ts.checked_add_span(span),
3061
0
            SDuration::Absolute(sdur) => ts.checked_add_duration(sdur),
3062
        }
3063
0
    }
3064
3065
    #[inline]
3066
0
    fn saturating_add(self, ts: Timestamp) -> Result<Timestamp, Error> {
3067
0
        let Ok(signed) = self.duration.to_signed() else {
3068
0
            return Ok(Timestamp::MAX);
3069
        };
3070
0
        let result = match signed {
3071
0
            SDuration::Span(span) => {
3072
0
                if let Some(err) = span.smallest_non_time_non_zero_unit_error()
3073
                {
3074
0
                    return Err(err);
3075
0
                }
3076
0
                ts.checked_add_span(span)
3077
            }
3078
0
            SDuration::Absolute(sdur) => ts.checked_add_duration(sdur),
3079
        };
3080
0
        Ok(result.unwrap_or_else(|_| {
3081
0
            if self.is_negative() {
3082
0
                Timestamp::MIN
3083
            } else {
3084
0
                Timestamp::MAX
3085
            }
3086
0
        }))
3087
0
    }
3088
3089
    #[inline]
3090
0
    fn checked_neg(self) -> Result<TimestampArithmetic, Error> {
3091
0
        let duration = self.duration.checked_neg()?;
3092
0
        Ok(TimestampArithmetic { duration })
3093
0
    }
3094
3095
    #[inline]
3096
0
    fn is_negative(&self) -> bool {
3097
0
        self.duration.is_negative()
3098
0
    }
3099
}
3100
3101
impl From<Span> for TimestampArithmetic {
3102
0
    fn from(span: Span) -> TimestampArithmetic {
3103
0
        let duration = Duration::from(span);
3104
0
        TimestampArithmetic { duration }
3105
0
    }
3106
}
3107
3108
impl From<SignedDuration> for TimestampArithmetic {
3109
0
    fn from(sdur: SignedDuration) -> TimestampArithmetic {
3110
0
        let duration = Duration::from(sdur);
3111
0
        TimestampArithmetic { duration }
3112
0
    }
3113
}
3114
3115
impl From<UnsignedDuration> for TimestampArithmetic {
3116
0
    fn from(udur: UnsignedDuration) -> TimestampArithmetic {
3117
0
        let duration = Duration::from(udur);
3118
0
        TimestampArithmetic { duration }
3119
0
    }
3120
}
3121
3122
impl<'a> From<&'a Span> for TimestampArithmetic {
3123
0
    fn from(span: &'a Span) -> TimestampArithmetic {
3124
0
        TimestampArithmetic::from(*span)
3125
0
    }
3126
}
3127
3128
impl<'a> From<&'a SignedDuration> for TimestampArithmetic {
3129
0
    fn from(sdur: &'a SignedDuration) -> TimestampArithmetic {
3130
0
        TimestampArithmetic::from(*sdur)
3131
0
    }
3132
}
3133
3134
impl<'a> From<&'a UnsignedDuration> for TimestampArithmetic {
3135
0
    fn from(udur: &'a UnsignedDuration) -> TimestampArithmetic {
3136
0
        TimestampArithmetic::from(*udur)
3137
0
    }
3138
}
3139
3140
/// Options for [`Timestamp::since`] and [`Timestamp::until`].
3141
///
3142
/// This type provides a way to configure the calculation of
3143
/// spans between two [`Timestamp`] values. In particular, both
3144
/// `Timestamp::since` and `Timestamp::until` accept anything that implements
3145
/// `Into<TimestampDifference>`. There are a few key trait implementations that
3146
/// make this convenient:
3147
///
3148
/// * `From<Timestamp> for TimestampDifference` will construct a
3149
/// configuration consisting of just the timestamp. So for example,
3150
/// `timestamp1.until(timestamp2)` will return the span from `timestamp1` to
3151
/// `timestamp2`.
3152
/// * `From<Zoned> for TimestampDifference` will construct a configuration
3153
/// consisting of the timestamp from the given zoned datetime. So for example,
3154
/// `timestamp.since(zoned)` returns the span from `zoned.to_timestamp()` to
3155
/// `timestamp`.
3156
/// * `From<(Unit, Timestamp)>` is a convenient way to specify the largest
3157
/// units that should be present on the span returned. By default, the largest
3158
/// units are seconds. Using this trait implementation is equivalent to
3159
/// `TimestampDifference::new(timestamp).largest(unit)`.
3160
/// * `From<(Unit, Zoned)>` is like the one above, but with the time from
3161
/// the given zoned datetime.
3162
///
3163
/// One can also provide a `TimestampDifference` value directly. Doing so
3164
/// is necessary to use the rounding features of calculating a span. For
3165
/// example, setting the smallest unit (defaults to [`Unit::Nanosecond`]), the
3166
/// rounding mode (defaults to [`RoundMode::Trunc`]) and the rounding increment
3167
/// (defaults to `1`). The defaults are selected such that no rounding occurs.
3168
///
3169
/// Rounding a span as part of calculating it is provided as a convenience.
3170
/// Callers may choose to round the span as a distinct step via
3171
/// [`Span::round`].
3172
///
3173
/// # Example
3174
///
3175
/// This example shows how to round a span between two timestamps to the
3176
/// nearest half-hour, with ties breaking away from zero.
3177
///
3178
/// ```
3179
/// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3180
///
3181
/// let ts1 = "2024-03-15 08:14:00.123456789Z".parse::<Timestamp>()?;
3182
/// let ts2 = "2024-03-22 15:00Z".parse::<Timestamp>()?;
3183
/// let span = ts1.until(
3184
///     TimestampDifference::new(ts2)
3185
///         .smallest(Unit::Minute)
3186
///         .largest(Unit::Hour)
3187
///         .mode(RoundMode::HalfExpand)
3188
///         .increment(30),
3189
/// )?;
3190
/// assert_eq!(format!("{span:#}"), "175h");
3191
///
3192
/// // One less minute, and because of the HalfExpand mode, the span would
3193
/// // get rounded down.
3194
/// let ts2 = "2024-03-22 14:59Z".parse::<Timestamp>()?;
3195
/// let span = ts1.until(
3196
///     TimestampDifference::new(ts2)
3197
///         .smallest(Unit::Minute)
3198
///         .largest(Unit::Hour)
3199
///         .mode(RoundMode::HalfExpand)
3200
///         .increment(30),
3201
/// )?;
3202
/// assert_eq!(span, 174.hours().minutes(30).fieldwise());
3203
///
3204
/// # Ok::<(), Box<dyn std::error::Error>>(())
3205
/// ```
3206
#[derive(Clone, Copy, Debug)]
3207
pub struct TimestampDifference {
3208
    timestamp: Timestamp,
3209
    round: SpanRound<'static>,
3210
}
3211
3212
impl TimestampDifference {
3213
    /// Create a new default configuration for computing the span between
3214
    /// the given timestamp and some other time (specified as the receiver in
3215
    /// [`Timestamp::since`] or [`Timestamp::until`]).
3216
    #[inline]
3217
0
    pub fn new(timestamp: Timestamp) -> TimestampDifference {
3218
0
        // We use truncation rounding by default since it seems that's
3219
0
        // what is generally expected when computing the difference between
3220
0
        // datetimes.
3221
0
        //
3222
0
        // See: https://github.com/tc39/proposal-temporal/issues/1122
3223
0
        let round = SpanRound::new().mode(RoundMode::Trunc);
3224
0
        TimestampDifference { timestamp, round }
3225
0
    }
3226
3227
    /// Set the smallest units allowed in the span returned.
3228
    ///
3229
    /// # Errors
3230
    ///
3231
    /// The smallest units must be no greater than the largest units. If this
3232
    /// is violated, then computing a span with this configuration will result
3233
    /// in an error.
3234
    ///
3235
    /// # Example
3236
    ///
3237
    /// This shows how to round a span between two timestamps to units no less
3238
    /// than seconds.
3239
    ///
3240
    /// ```
3241
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3242
    ///
3243
    /// let ts1 = "2024-03-15 08:14:02.5001Z".parse::<Timestamp>()?;
3244
    /// let ts2 = "2024-03-15T08:16:03.0001Z".parse::<Timestamp>()?;
3245
    /// let span = ts1.until(
3246
    ///     TimestampDifference::new(ts2)
3247
    ///         .smallest(Unit::Second)
3248
    ///         .mode(RoundMode::HalfExpand),
3249
    /// )?;
3250
    /// assert_eq!(span, 121.seconds().fieldwise());
3251
    ///
3252
    /// // Because of the rounding mode, a small less-than-1-second increase in
3253
    /// // the first timestamp can change the result of rounding.
3254
    /// let ts1 = "2024-03-15 08:14:02.5002Z".parse::<Timestamp>()?;
3255
    /// let span = ts1.until(
3256
    ///     TimestampDifference::new(ts2)
3257
    ///         .smallest(Unit::Second)
3258
    ///         .mode(RoundMode::HalfExpand),
3259
    /// )?;
3260
    /// assert_eq!(span, 120.seconds().fieldwise());
3261
    ///
3262
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3263
    /// ```
3264
    #[inline]
3265
0
    pub fn smallest(self, unit: Unit) -> TimestampDifference {
3266
0
        TimestampDifference { round: self.round.smallest(unit), ..self }
3267
0
    }
3268
3269
    /// Set the largest units allowed in the span returned.
3270
    ///
3271
    /// When a largest unit is not specified, computing a span between
3272
    /// timestamps behaves as if it were set to [`Unit::Second`]. Unless
3273
    /// [`TimestampDifference::smallest`] is bigger than `Unit::Second`, then
3274
    /// the largest unit is set to the smallest unit.
3275
    ///
3276
    /// # Errors
3277
    ///
3278
    /// The largest units, when set, must be at least as big as the smallest
3279
    /// units (which defaults to [`Unit::Nanosecond`]). If this is violated,
3280
    /// then computing a span with this configuration will result in an error.
3281
    ///
3282
    /// # Example
3283
    ///
3284
    /// This shows how to round a span between two timestamps to units no
3285
    /// bigger than seconds.
3286
    ///
3287
    /// ```
3288
    /// use jiff::{Timestamp, TimestampDifference, ToSpan, Unit};
3289
    ///
3290
    /// let ts1 = "2024-03-15 08:14Z".parse::<Timestamp>()?;
3291
    /// let ts2 = "2030-11-22 08:30Z".parse::<Timestamp>()?;
3292
    /// let span = ts1.until(
3293
    ///     TimestampDifference::new(ts2).largest(Unit::Second),
3294
    /// )?;
3295
    /// assert_eq!(format!("{span:#}"), "211076160s");
3296
    ///
3297
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3298
    /// ```
3299
    #[inline]
3300
0
    pub fn largest(self, unit: Unit) -> TimestampDifference {
3301
0
        TimestampDifference { round: self.round.largest(unit), ..self }
3302
0
    }
3303
3304
    /// Set the rounding mode.
3305
    ///
3306
    /// This defaults to [`RoundMode::Trunc`] since it's plausible that
3307
    /// rounding "up" in the context of computing the span between
3308
    /// two timestamps could be surprising in a number of cases. The
3309
    /// [`RoundMode::HalfExpand`] mode corresponds to typical rounding you
3310
    /// might have learned about in school. But a variety of other rounding
3311
    /// modes exist.
3312
    ///
3313
    /// # Example
3314
    ///
3315
    /// This shows how to always round "up" towards positive infinity.
3316
    ///
3317
    /// ```
3318
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3319
    ///
3320
    /// let ts1 = "2024-03-15 08:10Z".parse::<Timestamp>()?;
3321
    /// let ts2 = "2024-03-15 08:11Z".parse::<Timestamp>()?;
3322
    /// let span = ts1.until(
3323
    ///     TimestampDifference::new(ts2)
3324
    ///         .smallest(Unit::Hour)
3325
    ///         .mode(RoundMode::Ceil),
3326
    /// )?;
3327
    /// // Only one minute elapsed, but we asked to always round up!
3328
    /// assert_eq!(span, 1.hour().fieldwise());
3329
    ///
3330
    /// // Since `Ceil` always rounds toward positive infinity, the behavior
3331
    /// // flips for a negative span.
3332
    /// let span = ts1.since(
3333
    ///     TimestampDifference::new(ts2)
3334
    ///         .smallest(Unit::Hour)
3335
    ///         .mode(RoundMode::Ceil),
3336
    /// )?;
3337
    /// assert_eq!(span, 0.hour().fieldwise());
3338
    ///
3339
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3340
    /// ```
3341
    #[inline]
3342
0
    pub fn mode(self, mode: RoundMode) -> TimestampDifference {
3343
0
        TimestampDifference { round: self.round.mode(mode), ..self }
3344
0
    }
3345
3346
    /// Set the rounding increment for the smallest unit.
3347
    ///
3348
    /// The default value is `1`. Other values permit rounding the smallest
3349
    /// unit to the nearest integer increment specified. For example, if the
3350
    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
3351
    /// `30` would result in rounding in increments of a half hour. That is,
3352
    /// the only minute value that could result would be `0` or `30`.
3353
    ///
3354
    /// # Errors
3355
    ///
3356
    /// The rounding increment must divide evenly into the next highest unit
3357
    /// after the smallest unit configured (and must not be equivalent to it).
3358
    /// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
3359
    /// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
3360
    /// `100` and `500`. Namely, any integer that divides evenly into `1,000`
3361
    /// nanoseconds since there are `1,000` nanoseconds in the next highest
3362
    /// unit (microseconds).
3363
    ///
3364
    /// The error will occur when computing the span, and not when setting
3365
    /// the increment here.
3366
    ///
3367
    /// # Example
3368
    ///
3369
    /// This shows how to round the span between two timestamps to the nearest
3370
    /// 5 minute increment.
3371
    ///
3372
    /// ```
3373
    /// use jiff::{RoundMode, Timestamp, TimestampDifference, ToSpan, Unit};
3374
    ///
3375
    /// let ts1 = "2024-03-15 08:19Z".parse::<Timestamp>()?;
3376
    /// let ts2 = "2024-03-15 12:52Z".parse::<Timestamp>()?;
3377
    /// let span = ts1.until(
3378
    ///     TimestampDifference::new(ts2)
3379
    ///         .smallest(Unit::Minute)
3380
    ///         .increment(5)
3381
    ///         .mode(RoundMode::HalfExpand),
3382
    /// )?;
3383
    /// assert_eq!(span.to_string(), "PT275M");
3384
    ///
3385
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3386
    /// ```
3387
    #[inline]
3388
0
    pub fn increment(self, increment: i64) -> TimestampDifference {
3389
0
        TimestampDifference { round: self.round.increment(increment), ..self }
3390
0
    }
3391
3392
    /// Returns true if and only if this configuration could change the span
3393
    /// via rounding.
3394
    #[inline]
3395
0
    fn rounding_may_change_span(&self) -> bool {
3396
0
        self.round.rounding_may_change_span_ignore_largest()
3397
0
    }
3398
3399
    /// Returns the span of time from `ts1` to the timestamp in this
3400
    /// configuration. The biggest units allowed are determined by the
3401
    /// `smallest` and `largest` settings, but defaults to `Unit::Second`.
3402
    #[inline]
3403
0
    fn until_with_largest_unit(&self, t1: Timestamp) -> Result<Span, Error> {
3404
0
        let t2 = self.timestamp;
3405
0
        let largest = self
3406
0
            .round
3407
0
            .get_largest()
3408
0
            .unwrap_or_else(|| self.round.get_smallest().max(Unit::Second));
3409
0
        if largest >= Unit::Day {
3410
0
            return Err(err!(
3411
0
                "unit {largest} is not supported when computing the \
3412
0
                 difference between timestamps (must use units smaller \
3413
0
                 than 'day')",
3414
0
                largest = largest.singular(),
3415
0
            ));
3416
0
        }
3417
0
        let nano1 = t1.as_nanosecond_ranged().without_bounds();
3418
0
        let nano2 = t2.as_nanosecond_ranged().without_bounds();
3419
0
        let diff = nano2 - nano1;
3420
0
        // This can fail when `largest` is nanoseconds since not all intervals
3421
0
        // can be represented by a single i64 in units of nanoseconds.
3422
0
        Span::from_invariant_nanoseconds(largest, diff)
3423
0
    }
3424
}
3425
3426
impl From<Timestamp> for TimestampDifference {
3427
    #[inline]
3428
0
    fn from(ts: Timestamp) -> TimestampDifference {
3429
0
        TimestampDifference::new(ts)
3430
0
    }
3431
}
3432
3433
impl From<Zoned> for TimestampDifference {
3434
    #[inline]
3435
0
    fn from(zdt: Zoned) -> TimestampDifference {
3436
0
        TimestampDifference::new(Timestamp::from(zdt))
3437
0
    }
3438
}
3439
3440
impl<'a> From<&'a Zoned> for TimestampDifference {
3441
    #[inline]
3442
0
    fn from(zdt: &'a Zoned) -> TimestampDifference {
3443
0
        TimestampDifference::from(Timestamp::from(zdt))
3444
0
    }
3445
}
3446
3447
impl From<(Unit, Timestamp)> for TimestampDifference {
3448
    #[inline]
3449
0
    fn from((largest, ts): (Unit, Timestamp)) -> TimestampDifference {
3450
0
        TimestampDifference::from(ts).largest(largest)
3451
0
    }
3452
}
3453
3454
impl From<(Unit, Zoned)> for TimestampDifference {
3455
    #[inline]
3456
0
    fn from((largest, zdt): (Unit, Zoned)) -> TimestampDifference {
3457
0
        TimestampDifference::from((largest, Timestamp::from(zdt)))
3458
0
    }
3459
}
3460
3461
impl<'a> From<(Unit, &'a Zoned)> for TimestampDifference {
3462
    #[inline]
3463
0
    fn from((largest, zdt): (Unit, &'a Zoned)) -> TimestampDifference {
3464
0
        TimestampDifference::from((largest, Timestamp::from(zdt)))
3465
0
    }
3466
}
3467
3468
/// Options for [`Timestamp::round`].
3469
///
3470
/// This type provides a way to configure the rounding of a timestamp. In
3471
/// particular, `Timestamp::round` accepts anything that implements the
3472
/// `Into<TimestampRound>` trait. There are some trait implementations that
3473
/// therefore make calling `Timestamp::round` in some common cases more
3474
/// ergonomic:
3475
///
3476
/// * `From<Unit> for TimestampRound` will construct a rounding
3477
/// configuration that rounds to the unit given. Specifically,
3478
/// `TimestampRound::new().smallest(unit)`.
3479
/// * `From<(Unit, i64)> for TimestampRound` is like the one above, but also
3480
/// specifies the rounding increment for [`TimestampRound::increment`].
3481
///
3482
/// Note that in the default configuration, no rounding occurs.
3483
///
3484
/// # Example
3485
///
3486
/// This example shows how to round a timestamp to the nearest second:
3487
///
3488
/// ```
3489
/// use jiff::{Timestamp, Unit};
3490
///
3491
/// let ts: Timestamp = "2024-06-20 16:24:59.5Z".parse()?;
3492
/// assert_eq!(
3493
///     ts.round(Unit::Second)?.to_string(),
3494
///     // The second rounds up and causes minutes to increase.
3495
///     "2024-06-20T16:25:00Z",
3496
/// );
3497
///
3498
/// # Ok::<(), Box<dyn std::error::Error>>(())
3499
/// ```
3500
///
3501
/// The above makes use of the fact that `Unit` implements
3502
/// `Into<TimestampRound>`. If you want to change the rounding mode to, say,
3503
/// truncation, then you'll need to construct a `TimestampRound` explicitly
3504
/// since there are no convenience `Into` trait implementations for
3505
/// [`RoundMode`].
3506
///
3507
/// ```
3508
/// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3509
///
3510
/// let ts: Timestamp = "2024-06-20 16:24:59.5Z".parse()?;
3511
/// assert_eq!(
3512
///     ts.round(
3513
///         TimestampRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
3514
///     )?.to_string(),
3515
///     // The second just gets truncated as if it wasn't there.
3516
///     "2024-06-20T16:24:59Z",
3517
/// );
3518
///
3519
/// # Ok::<(), Box<dyn std::error::Error>>(())
3520
/// ```
3521
#[derive(Clone, Copy, Debug)]
3522
pub struct TimestampRound {
3523
    smallest: Unit,
3524
    mode: RoundMode,
3525
    increment: i64,
3526
}
3527
3528
impl TimestampRound {
3529
    /// Create a new default configuration for rounding a [`Timestamp`].
3530
    #[inline]
3531
0
    pub fn new() -> TimestampRound {
3532
0
        TimestampRound {
3533
0
            smallest: Unit::Nanosecond,
3534
0
            mode: RoundMode::HalfExpand,
3535
0
            increment: 1,
3536
0
        }
3537
0
    }
3538
3539
    /// Set the smallest units allowed in the timestamp returned after
3540
    /// rounding.
3541
    ///
3542
    /// Any units below the smallest configured unit will be used, along with
3543
    /// the rounding increment and rounding mode, to determine the value of the
3544
    /// smallest unit. For example, when rounding `2024-06-20T03:25:30Z` to the
3545
    /// nearest minute, the `30` second unit will result in rounding the minute
3546
    /// unit of `25` up to `26` and zeroing out everything below minutes.
3547
    ///
3548
    /// This defaults to [`Unit::Nanosecond`].
3549
    ///
3550
    /// # Errors
3551
    ///
3552
    /// The smallest units must be no greater than [`Unit::Hour`].
3553
    ///
3554
    /// # Example
3555
    ///
3556
    /// ```
3557
    /// use jiff::{Timestamp, TimestampRound, Unit};
3558
    ///
3559
    /// let ts: Timestamp = "2024-06-20T03:25:30Z".parse()?;
3560
    /// assert_eq!(
3561
    ///     ts.round(TimestampRound::new().smallest(Unit::Minute))?.to_string(),
3562
    ///     "2024-06-20T03:26:00Z",
3563
    /// );
3564
    /// // Or, utilize the `From<Unit> for TimestampRound` impl:
3565
    /// assert_eq!(
3566
    ///     ts.round(Unit::Minute)?.to_string(),
3567
    ///     "2024-06-20T03:26:00Z",
3568
    /// );
3569
    ///
3570
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3571
    /// ```
3572
    #[inline]
3573
0
    pub fn smallest(self, unit: Unit) -> TimestampRound {
3574
0
        TimestampRound { smallest: unit, ..self }
3575
0
    }
3576
3577
    /// Set the rounding mode.
3578
    ///
3579
    /// This defaults to [`RoundMode::HalfExpand`], which rounds away from
3580
    /// zero. It matches the kind of rounding you might have been taught in
3581
    /// school.
3582
    ///
3583
    /// # Example
3584
    ///
3585
    /// This shows how to always round timestamps up towards positive infinity.
3586
    ///
3587
    /// ```
3588
    /// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3589
    ///
3590
    /// let ts: Timestamp = "2024-06-20 03:25:01Z".parse()?;
3591
    /// assert_eq!(
3592
    ///     ts.round(
3593
    ///         TimestampRound::new()
3594
    ///             .smallest(Unit::Minute)
3595
    ///             .mode(RoundMode::Ceil),
3596
    ///     )?.to_string(),
3597
    ///     "2024-06-20T03:26:00Z",
3598
    /// );
3599
    ///
3600
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3601
    /// ```
3602
    #[inline]
3603
0
    pub fn mode(self, mode: RoundMode) -> TimestampRound {
3604
0
        TimestampRound { mode, ..self }
3605
0
    }
3606
3607
    /// Set the rounding increment for the smallest unit.
3608
    ///
3609
    /// The default value is `1`. Other values permit rounding the smallest
3610
    /// unit to the nearest integer increment specified. For example, if the
3611
    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
3612
    /// `30` would result in rounding in increments of a half hour. That is,
3613
    /// the only minute value that could result would be `0` or `30`.
3614
    ///
3615
    /// # Errors
3616
    ///
3617
    /// The rounding increment, when combined with the smallest unit (which
3618
    /// defaults to [`Unit::Nanosecond`]), must divide evenly into `86,400`
3619
    /// seconds (one 24-hour civil day). For example, increments of both
3620
    /// 45 seconds and 15 minutes are allowed, but 7 seconds and 25 minutes are
3621
    /// both not allowed.
3622
    ///
3623
    /// # Example
3624
    ///
3625
    /// This example shows how to round a timestamp to the nearest 10 minute
3626
    /// increment.
3627
    ///
3628
    /// ```
3629
    /// use jiff::{RoundMode, Timestamp, TimestampRound, Unit};
3630
    ///
3631
    /// let ts: Timestamp = "2024-06-20 03:24:59Z".parse()?;
3632
    /// assert_eq!(
3633
    ///     ts.round((Unit::Minute, 10))?.to_string(),
3634
    ///     "2024-06-20T03:20:00Z",
3635
    /// );
3636
    ///
3637
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3638
    /// ```
3639
    #[inline]
3640
0
    pub fn increment(self, increment: i64) -> TimestampRound {
3641
0
        TimestampRound { increment, ..self }
3642
0
    }
3643
3644
    /// Does the actual rounding.
3645
0
    pub(crate) fn round(
3646
0
        &self,
3647
0
        timestamp: Timestamp,
3648
0
    ) -> Result<Timestamp, Error> {
3649
0
        let increment =
3650
0
            increment::for_timestamp(self.smallest, self.increment)?;
3651
0
        let nanosecond = timestamp.as_nanosecond_ranged().without_bounds();
3652
0
        let rounded = self.mode.round_by_unit_in_nanoseconds(
3653
0
            nanosecond,
3654
0
            self.smallest,
3655
0
            increment,
3656
0
        );
3657
0
        let nanosecond = UnixNanoseconds::rfrom(rounded);
3658
0
        Ok(Timestamp::from_nanosecond_ranged(nanosecond))
3659
0
    }
3660
}
3661
3662
impl Default for TimestampRound {
3663
    #[inline]
3664
0
    fn default() -> TimestampRound {
3665
0
        TimestampRound::new()
3666
0
    }
3667
}
3668
3669
impl From<Unit> for TimestampRound {
3670
    #[inline]
3671
0
    fn from(unit: Unit) -> TimestampRound {
3672
0
        TimestampRound::default().smallest(unit)
3673
0
    }
3674
}
3675
3676
impl From<(Unit, i64)> for TimestampRound {
3677
    #[inline]
3678
0
    fn from((unit, increment): (Unit, i64)) -> TimestampRound {
3679
0
        TimestampRound::from(unit).increment(increment)
3680
0
    }
3681
}
3682
3683
#[cfg(test)]
3684
mod tests {
3685
    use alloc::string::ToString;
3686
3687
    use std::io::Cursor;
3688
3689
    use crate::{
3690
        civil::{self, datetime},
3691
        tz::Offset,
3692
        ToSpan,
3693
    };
3694
3695
    use super::*;
3696
3697
    fn mktime(seconds: i64, nanos: i32) -> Timestamp {
3698
        Timestamp::new(seconds, nanos).unwrap()
3699
    }
3700
3701
    fn mkdt(
3702
        year: i16,
3703
        month: i8,
3704
        day: i8,
3705
        hour: i8,
3706
        minute: i8,
3707
        second: i8,
3708
        nano: i32,
3709
    ) -> civil::DateTime {
3710
        let date = civil::Date::new(year, month, day).unwrap();
3711
        let time = civil::Time::new(hour, minute, second, nano).unwrap();
3712
        civil::DateTime::from_parts(date, time)
3713
    }
3714
3715
    #[test]
3716
    fn to_datetime_specific_examples() {
3717
        let tests = [
3718
            ((UnixSeconds::MIN_REPR, 0), (-9999, 1, 2, 1, 59, 59, 0)),
3719
            (
3720
                (UnixSeconds::MIN_REPR + 1, -999_999_999),
3721
                (-9999, 1, 2, 1, 59, 59, 1),
3722
            ),
3723
            ((-1, 1), (1969, 12, 31, 23, 59, 59, 1)),
3724
            ((UnixSeconds::MAX_REPR, 0), (9999, 12, 30, 22, 0, 0, 0)),
3725
            ((UnixSeconds::MAX_REPR - 1, 0), (9999, 12, 30, 21, 59, 59, 0)),
3726
            (
3727
                (UnixSeconds::MAX_REPR - 1, 999_999_999),
3728
                (9999, 12, 30, 21, 59, 59, 999_999_999),
3729
            ),
3730
            (
3731
                (UnixSeconds::MAX_REPR, 999_999_999),
3732
                (9999, 12, 30, 22, 0, 0, 999_999_999),
3733
            ),
3734
            ((-2, -1), (1969, 12, 31, 23, 59, 57, 999_999_999)),
3735
            ((-86398, -1), (1969, 12, 31, 0, 0, 1, 999_999_999)),
3736
            ((-86399, -1), (1969, 12, 31, 0, 0, 0, 999_999_999)),
3737
            ((-86400, -1), (1969, 12, 30, 23, 59, 59, 999_999_999)),
3738
        ];
3739
        for (t, dt) in tests {
3740
            let timestamp = mktime(t.0, t.1);
3741
            let datetime = mkdt(dt.0, dt.1, dt.2, dt.3, dt.4, dt.5, dt.6);
3742
            assert_eq!(
3743
                Offset::UTC.to_datetime(timestamp),
3744
                datetime,
3745
                "timestamp: {t:?}"
3746
            );
3747
            assert_eq!(
3748
                timestamp,
3749
                datetime.to_zoned(TimeZone::UTC).unwrap().timestamp(),
3750
                "datetime: {datetime:?}"
3751
            );
3752
        }
3753
    }
3754
3755
    #[test]
3756
    fn to_datetime_many_seconds_in_some_days() {
3757
        let days = [
3758
            i64::from(t::UnixEpochDay::MIN_REPR),
3759
            -1000,
3760
            -5,
3761
            23,
3762
            2000,
3763
            i64::from(t::UnixEpochDay::MAX_REPR),
3764
        ];
3765
        let seconds = [
3766
            -86_400, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4,
3767
            5, 6, 7, 8, 9, 10, 86_400,
3768
        ];
3769
        let nanos = [0, 1, 5, 999_999_999];
3770
        for day in days {
3771
            let midpoint = day * 86_400;
3772
            for second in seconds {
3773
                let second = midpoint + second;
3774
                if !UnixSeconds::contains(second) {
3775
                    continue;
3776
                }
3777
                for nano in nanos {
3778
                    if second == UnixSeconds::MIN_REPR && nano != 0 {
3779
                        continue;
3780
                    }
3781
                    let t = Timestamp::new(second, nano).unwrap();
3782
                    let Ok(got) =
3783
                        Offset::UTC.to_datetime(t).to_zoned(TimeZone::UTC)
3784
                    else {
3785
                        continue;
3786
                    };
3787
                    assert_eq!(t, got.timestamp());
3788
                }
3789
            }
3790
        }
3791
    }
3792
3793
    #[test]
3794
    fn invalid_time() {
3795
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, -1).is_err());
3796
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, -999_999_999).is_err());
3797
        // These are greater than the minimum and thus okay!
3798
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, 1).is_ok());
3799
        assert!(Timestamp::new(UnixSeconds::MIN_REPR, 999_999_999).is_ok());
3800
    }
3801
3802
    #[cfg(target_pointer_width = "64")]
3803
    #[test]
3804
    fn timestamp_size() {
3805
        #[cfg(debug_assertions)]
3806
        {
3807
            assert_eq!(40, core::mem::size_of::<Timestamp>());
3808
        }
3809
        #[cfg(not(debug_assertions))]
3810
        {
3811
            assert_eq!(16, core::mem::size_of::<Timestamp>());
3812
        }
3813
    }
3814
3815
    #[test]
3816
    fn nanosecond_roundtrip_boundaries() {
3817
        let inst = Timestamp::MIN;
3818
        let nanos = inst.as_nanosecond_ranged();
3819
        assert_eq!(C(0), nanos % t::NANOS_PER_SECOND);
3820
        let got = Timestamp::from_nanosecond_ranged(nanos);
3821
        assert_eq!(inst, got);
3822
3823
        let inst = Timestamp::MAX;
3824
        let nanos = inst.as_nanosecond_ranged();
3825
        assert_eq!(
3826
            FractionalNanosecond::MAX_SELF,
3827
            nanos % t::NANOS_PER_SECOND
3828
        );
3829
        let got = Timestamp::from_nanosecond_ranged(nanos);
3830
        assert_eq!(inst, got);
3831
    }
3832
3833
    #[test]
3834
    fn timestamp_saturating_add() {
3835
        insta::assert_snapshot!(
3836
            Timestamp::MIN.saturating_add(Span::new().days(1)).unwrap_err(),
3837
            @"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`)",
3838
        )
3839
    }
3840
3841
    #[test]
3842
    fn timestamp_saturating_sub() {
3843
        insta::assert_snapshot!(
3844
            Timestamp::MAX.saturating_sub(Span::new().days(1)).unwrap_err(),
3845
            @"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`)",
3846
        )
3847
    }
3848
3849
    quickcheck::quickcheck! {
3850
        fn prop_unix_seconds_roundtrip(t: Timestamp) -> quickcheck::TestResult {
3851
            let dt = t.to_zoned(TimeZone::UTC).datetime();
3852
            let Ok(got) = dt.to_zoned(TimeZone::UTC) else {
3853
                return quickcheck::TestResult::discard();
3854
            };
3855
            quickcheck::TestResult::from_bool(t == got.timestamp())
3856
        }
3857
3858
        fn prop_nanos_roundtrip_unix_ranged(t: Timestamp) -> bool {
3859
            let nanos = t.as_nanosecond_ranged();
3860
            let got = Timestamp::from_nanosecond_ranged(nanos);
3861
            t == got
3862
        }
3863
3864
        fn prop_nanos_roundtrip_unix(t: Timestamp) -> bool {
3865
            let nanos = t.as_nanosecond();
3866
            let got = Timestamp::from_nanosecond(nanos).unwrap();
3867
            t == got
3868
        }
3869
3870
        fn timestamp_constant_and_new_are_same1(t: Timestamp) -> bool {
3871
            let got = Timestamp::constant(t.as_second(), t.subsec_nanosecond());
3872
            t == got
3873
        }
3874
3875
        fn timestamp_constant_and_new_are_same2(
3876
            secs: i64,
3877
            nanos: i32
3878
        ) -> quickcheck::TestResult {
3879
            let Ok(ts) = Timestamp::new(secs, nanos) else {
3880
                return quickcheck::TestResult::discard();
3881
            };
3882
            let got = Timestamp::constant(secs, nanos);
3883
            quickcheck::TestResult::from_bool(ts == got)
3884
        }
3885
    }
3886
3887
    /// A `serde` deserializer compatibility test.
3888
    ///
3889
    /// Serde YAML used to be unable to deserialize `jiff` types,
3890
    /// as deserializing from bytes is not supported by the deserializer.
3891
    ///
3892
    /// - <https://github.com/BurntSushi/jiff/issues/138>
3893
    /// - <https://github.com/BurntSushi/jiff/discussions/148>
3894
    #[test]
3895
    fn timestamp_deserialize_yaml() {
3896
        let expected = datetime(2024, 10, 31, 16, 33, 53, 123456789)
3897
            .to_zoned(TimeZone::UTC)
3898
            .unwrap()
3899
            .timestamp();
3900
3901
        let deserialized: Timestamp =
3902
            serde_yaml::from_str("2024-10-31T16:33:53.123456789+00:00")
3903
                .unwrap();
3904
3905
        assert_eq!(deserialized, expected);
3906
3907
        let deserialized: Timestamp = serde_yaml::from_slice(
3908
            "2024-10-31T16:33:53.123456789+00:00".as_bytes(),
3909
        )
3910
        .unwrap();
3911
3912
        assert_eq!(deserialized, expected);
3913
3914
        let cursor = Cursor::new(b"2024-10-31T16:33:53.123456789+00:00");
3915
        let deserialized: Timestamp = serde_yaml::from_reader(cursor).unwrap();
3916
3917
        assert_eq!(deserialized, expected);
3918
    }
3919
3920
    #[test]
3921
    fn timestamp_precision_loss() {
3922
        let ts1: Timestamp =
3923
            "2025-01-25T19:32:21.783444592+01:00".parse().unwrap();
3924
        let span = 1.second();
3925
        let ts2 = ts1 + span;
3926
        assert_eq!(ts2.to_string(), "2025-01-25T18:32:22.783444592Z");
3927
        assert_eq!(ts1, ts2 - span, "should be reversible");
3928
    }
3929
}