time/utc_date_time.rs
1//! The [`UtcDateTime`] struct and associated `impl`s.
2
3#[cfg(feature = "formatting")]
4use alloc::string::String;
5use core::fmt;
6use core::mem::MaybeUninit;
7use core::ops::{Add, AddAssign, Sub, SubAssign};
8use core::time::Duration as StdDuration;
9#[cfg(feature = "formatting")]
10use std::io;
11
12use deranged::ri64;
13use powerfmt::smart_display::{FormatterOptions, Metadata, SmartDisplay};
14
15use crate::date::{MAX_YEAR, MIN_YEAR};
16#[cfg(feature = "formatting")]
17use crate::formatting::Formattable;
18use crate::internal_macros::{carry, cascade, const_try, const_try_opt, div_floor, ensure_ranged};
19use crate::num_fmt::str_from_raw_parts;
20#[cfg(feature = "parsing")]
21use crate::parsing::Parsable;
22use crate::unit::*;
23use crate::util::days_in_year;
24use crate::{
25 Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday, error,
26};
27
28/// The Julian day of the Unix epoch.
29const UNIX_EPOCH_JULIAN_DAY: i32 = UtcDateTime::UNIX_EPOCH.to_julian_day();
30
31/// A [`PrimitiveDateTime`] that is known to be UTC.
32///
33/// `UtcDateTime` is guaranteed to be ABI-compatible with [`PrimitiveDateTime`], meaning that
34/// transmuting from one to the other will not result in undefined behavior.
35#[repr(transparent)]
36#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct UtcDateTime {
38 inner: PrimitiveDateTime,
39}
40
41impl UtcDateTime {
42 /// Midnight, 1 January, 1970.
43 ///
44 /// ```rust
45 /// # use time::UtcDateTime;
46 /// # use time_macros::utc_datetime;
47 /// assert_eq!(UtcDateTime::UNIX_EPOCH, utc_datetime!(1970-01-01 0:00));
48 /// ```
49 pub const UNIX_EPOCH: Self = Self::new(Date::UNIX_EPOCH, Time::MIDNIGHT);
50
51 /// The smallest value that can be represented by `UtcDateTime`.
52 ///
53 /// Depending on `large-dates` feature flag, value of this constant may vary.
54 ///
55 /// 1. With `large-dates` disabled it is equal to `-9999-01-01 00:00:00.0`
56 /// 2. With `large-dates` enabled it is equal to `-999999-01-01 00:00:00.0`
57 ///
58 /// ```rust
59 /// # use time::UtcDateTime;
60 /// # use time_macros::utc_datetime;
61 #[cfg_attr(
62 feature = "large-dates",
63 doc = "// Assuming `large-dates` feature is enabled."
64 )]
65 #[cfg_attr(
66 feature = "large-dates",
67 doc = "assert_eq!(UtcDateTime::MIN, utc_datetime!(-999999-01-01 0:00));"
68 )]
69 #[cfg_attr(
70 not(feature = "large-dates"),
71 doc = "// Assuming `large-dates` feature is disabled."
72 )]
73 #[cfg_attr(
74 not(feature = "large-dates"),
75 doc = "assert_eq!(UtcDateTime::MIN, utc_datetime!(-9999-01-01 0:00));"
76 )]
77 /// ```
78 pub const MIN: Self = Self::new(Date::MIN, Time::MIDNIGHT);
79
80 /// The largest value that can be represented by `UtcDateTime`.
81 ///
82 /// Depending on `large-dates` feature flag, value of this constant may vary.
83 ///
84 /// 1. With `large-dates` disabled it is equal to `9999-12-31 23:59:59.999_999_999`
85 /// 2. With `large-dates` enabled it is equal to `999999-12-31 23:59:59.999_999_999`
86 ///
87 /// ```rust
88 /// # use time::UtcDateTime;
89 /// # use time_macros::utc_datetime;
90 #[cfg_attr(
91 feature = "large-dates",
92 doc = "// Assuming `large-dates` feature is enabled."
93 )]
94 #[cfg_attr(
95 feature = "large-dates",
96 doc = "assert_eq!(UtcDateTime::MAX, utc_datetime!(+999999-12-31 23:59:59.999_999_999));"
97 )]
98 #[cfg_attr(
99 not(feature = "large-dates"),
100 doc = "// Assuming `large-dates` feature is disabled."
101 )]
102 #[cfg_attr(
103 not(feature = "large-dates"),
104 doc = "assert_eq!(UtcDateTime::MAX, utc_datetime!(+9999-12-31 23:59:59.999_999_999));"
105 )]
106 /// ```
107 pub const MAX: Self = Self::new(Date::MAX, Time::MAX);
108
109 /// Create a new `UtcDateTime` with the current date and time.
110 ///
111 /// ```rust
112 /// # use time::UtcDateTime;
113 /// assert!(UtcDateTime::now().year() >= 2019);
114 /// ```
115 #[cfg(feature = "std")]
116 #[inline]
117 pub fn now() -> Self {
118 #[cfg(all(
119 target_family = "wasm",
120 not(any(target_os = "emscripten", target_os = "wasi")),
121 feature = "wasm-bindgen"
122 ))]
123 {
124 js_sys::Date::new_0().into()
125 }
126
127 #[cfg(not(all(
128 target_family = "wasm",
129 not(any(target_os = "emscripten", target_os = "wasi")),
130 feature = "wasm-bindgen"
131 )))]
132 std::time::SystemTime::now().into()
133 }
134
135 /// Create a new `UtcDateTime` from the provided [`Date`] and [`Time`].
136 ///
137 /// ```rust
138 /// # use time::UtcDateTime;
139 /// # use time_macros::{date, utc_datetime, time};
140 /// assert_eq!(
141 /// UtcDateTime::new(date!(2019-01-01), time!(0:00)),
142 /// utc_datetime!(2019-01-01 0:00),
143 /// );
144 /// ```
145 #[inline]
146 pub const fn new(date: Date, time: Time) -> Self {
147 Self {
148 inner: PrimitiveDateTime::new(date, time),
149 }
150 }
151
152 /// Create a new `UtcDateTime` from the [`PrimitiveDateTime`], assuming that the latter is UTC.
153 #[inline]
154 pub(crate) const fn from_primitive(date_time: PrimitiveDateTime) -> Self {
155 Self { inner: date_time }
156 }
157
158 /// Obtain the [`PrimitiveDateTime`] that this `UtcDateTime` represents. The no-longer-attached
159 /// [`UtcOffset`] is assumed to be UTC.
160 #[inline]
161 pub(crate) const fn as_primitive(self) -> PrimitiveDateTime {
162 self.inner
163 }
164
165 /// Create a `UtcDateTime` from the provided Unix timestamp.
166 ///
167 /// ```rust
168 /// # use time::UtcDateTime;
169 /// # use time_macros::utc_datetime;
170 /// assert_eq!(
171 /// UtcDateTime::from_unix_timestamp(0),
172 /// Ok(UtcDateTime::UNIX_EPOCH),
173 /// );
174 /// assert_eq!(
175 /// UtcDateTime::from_unix_timestamp(1_546_300_800),
176 /// Ok(utc_datetime!(2019-01-01 0:00)),
177 /// );
178 /// ```
179 ///
180 /// If you have a timestamp-nanosecond pair, you can use something along the lines of the
181 /// following:
182 ///
183 /// ```rust
184 /// # use time::{Duration, UtcDateTime, ext::NumericalDuration};
185 /// let (timestamp, nanos) = (1, 500_000_000);
186 /// assert_eq!(
187 /// UtcDateTime::from_unix_timestamp(timestamp)? + Duration::nanoseconds(nanos),
188 /// UtcDateTime::UNIX_EPOCH + 1.5.seconds()
189 /// );
190 /// # Ok::<_, time::Error>(())
191 /// ```
192 #[inline]
193 pub const fn from_unix_timestamp(timestamp: i64) -> Result<Self, error::ComponentRange> {
194 type Timestamp =
195 ri64<{ UtcDateTime::MIN.unix_timestamp() }, { UtcDateTime::MAX.unix_timestamp() }>;
196 ensure_ranged!(Timestamp: timestamp);
197
198 // Use the unchecked method here, as the input validity has already been verified.
199 // Safety: The Julian day number is in range.
200 let date = unsafe {
201 Date::from_julian_day_unchecked(
202 UNIX_EPOCH_JULIAN_DAY + div_floor!(timestamp, Second::per_t::<i64>(Day)) as i32,
203 )
204 };
205
206 let seconds_within_day = timestamp.rem_euclid(Second::per_t::<i64>(Day));
207 // Safety: All values are in range.
208 let time = unsafe {
209 Time::__from_hms_nanos_unchecked(
210 (seconds_within_day / Second::per_t::<i64>(Hour)) as u8,
211 ((seconds_within_day % Second::per_t::<i64>(Hour)) / Minute::per_t::<i64>(Hour))
212 as u8,
213 (seconds_within_day % Second::per_t::<i64>(Minute)) as u8,
214 0,
215 )
216 };
217
218 Ok(Self::new(date, time))
219 }
220
221 /// Construct an `UtcDateTime` from the provided Unix timestamp (in nanoseconds).
222 ///
223 /// ```rust
224 /// # use time::UtcDateTime;
225 /// # use time_macros::utc_datetime;
226 /// assert_eq!(
227 /// UtcDateTime::from_unix_timestamp_nanos(0),
228 /// Ok(UtcDateTime::UNIX_EPOCH),
229 /// );
230 /// assert_eq!(
231 /// UtcDateTime::from_unix_timestamp_nanos(1_546_300_800_000_000_000),
232 /// Ok(utc_datetime!(2019-01-01 0:00)),
233 /// );
234 /// ```
235 #[inline]
236 pub const fn from_unix_timestamp_nanos(timestamp: i128) -> Result<Self, error::ComponentRange> {
237 let datetime = const_try!(Self::from_unix_timestamp(div_floor!(
238 timestamp,
239 Nanosecond::per_t::<i128>(Second)
240 ) as i64));
241
242 Ok(Self::new(
243 datetime.date(),
244 // Safety: `nanosecond` is in range due to `rem_euclid`.
245 unsafe {
246 Time::__from_hms_nanos_unchecked(
247 datetime.hour(),
248 datetime.minute(),
249 datetime.second(),
250 timestamp.rem_euclid(Nanosecond::per_t(Second)) as u32,
251 )
252 },
253 ))
254 }
255
256 /// Convert the `UtcDateTime` from UTC to the provided [`UtcOffset`], returning an
257 /// [`OffsetDateTime`].
258 ///
259 /// ```rust
260 /// # use time_macros::{utc_datetime, offset};
261 /// assert_eq!(
262 /// utc_datetime!(2000-01-01 0:00)
263 /// .to_offset(offset!(-1))
264 /// .year(),
265 /// 1999,
266 /// );
267 ///
268 /// // Construct midnight on new year's, UTC.
269 /// let utc = utc_datetime!(2000-01-01 0:00);
270 /// let new_york = utc.to_offset(offset!(-5));
271 /// let los_angeles = utc.to_offset(offset!(-8));
272 /// assert_eq!(utc.hour(), 0);
273 /// assert_eq!(new_york.hour(), 19);
274 /// assert_eq!(los_angeles.hour(), 16);
275 /// ```
276 ///
277 /// # Panics
278 ///
279 /// This method panics if the local date-time in the new offset is outside the supported range.
280 #[inline]
281 #[track_caller]
282 pub const fn to_offset(self, offset: UtcOffset) -> OffsetDateTime {
283 self.checked_to_offset(offset)
284 .expect("local datetime out of valid range")
285 }
286
287 /// Convert the `UtcDateTime` from UTC to the provided [`UtcOffset`], returning an
288 /// [`OffsetDateTime`]. `None` is returned if the date-time in the resulting offset is
289 /// invalid.
290 ///
291 /// ```rust
292 /// # use time::UtcDateTime;
293 /// # use time_macros::{utc_datetime, offset};
294 /// assert_eq!(
295 /// utc_datetime!(2000-01-01 0:00)
296 /// .checked_to_offset(offset!(-1))
297 /// .unwrap()
298 /// .year(),
299 /// 1999,
300 /// );
301 /// assert_eq!(
302 /// UtcDateTime::MAX.checked_to_offset(offset!(+1)),
303 /// None,
304 /// );
305 /// ```
306 #[inline]
307 pub const fn checked_to_offset(self, offset: UtcOffset) -> Option<OffsetDateTime> {
308 // Fast path for when no conversion is necessary.
309 if offset.is_utc() {
310 return Some(self.inner.assume_utc());
311 }
312
313 let (year, ordinal, time) = self.to_offset_raw(offset);
314
315 if year > MAX_YEAR || year < MIN_YEAR {
316 return None;
317 }
318
319 Some(OffsetDateTime::new_in_offset(
320 // Safety: `ordinal` is not zero.
321 unsafe { Date::__from_ordinal_date_unchecked(year, ordinal) },
322 time,
323 offset,
324 ))
325 }
326
327 /// Equivalent to `.to_offset(offset)`, but returning the year, ordinal, and time. This avoids
328 /// constructing an invalid [`Date`] if the new value is out of range.
329 #[inline]
330 pub(crate) const fn to_offset_raw(self, offset: UtcOffset) -> (i32, u16, Time) {
331 let (second, carry) = carry!(@most_once
332 self.second().cast_signed() + offset.seconds_past_minute(),
333 0..Second::per_t(Minute)
334 );
335 let (minute, carry) = carry!(@most_once
336 self.minute().cast_signed() + offset.minutes_past_hour() + carry,
337 0..Minute::per_t(Hour)
338 );
339 let (hour, carry) = carry!(@most_twice
340 self.hour().cast_signed() + offset.whole_hours() + carry,
341 0..Hour::per_t(Day)
342 );
343 let (mut year, ordinal) = self.to_ordinal_date();
344 let mut ordinal = ordinal.cast_signed() + carry;
345 cascade!(ordinal => year);
346
347 debug_assert!(ordinal > 0);
348 debug_assert!(ordinal <= days_in_year(year).cast_signed());
349
350 (
351 year,
352 ordinal.cast_unsigned(),
353 // Safety: The cascades above ensure the values are in range.
354 unsafe {
355 Time::__from_hms_nanos_unchecked(
356 hour.cast_unsigned(),
357 minute.cast_unsigned(),
358 second.cast_unsigned(),
359 self.nanosecond(),
360 )
361 },
362 )
363 }
364
365 /// Get the [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time).
366 ///
367 /// ```rust
368 /// # use time_macros::utc_datetime;
369 /// assert_eq!(utc_datetime!(1970-01-01 0:00).unix_timestamp(), 0);
370 /// assert_eq!(utc_datetime!(1970-01-01 1:00).unix_timestamp(), 3_600);
371 /// ```
372 #[inline]
373 pub const fn unix_timestamp(self) -> i64 {
374 let days = (self.to_julian_day() as i64 - UNIX_EPOCH_JULIAN_DAY as i64)
375 * Second::per_t::<i64>(Day);
376 let hours = self.hour() as i64 * Second::per_t::<i64>(Hour);
377 let minutes = self.minute() as i64 * Second::per_t::<i64>(Minute);
378 let seconds = self.second() as i64;
379 days + hours + minutes + seconds
380 }
381
382 /// Get the Unix timestamp in nanoseconds.
383 ///
384 /// ```rust
385 /// use time_macros::utc_datetime;
386 /// assert_eq!(utc_datetime!(1970-01-01 0:00).unix_timestamp_nanos(), 0);
387 /// assert_eq!(
388 /// utc_datetime!(1970-01-01 1:00).unix_timestamp_nanos(),
389 /// 3_600_000_000_000,
390 /// );
391 /// ```
392 #[inline]
393 pub const fn unix_timestamp_nanos(self) -> i128 {
394 self.unix_timestamp() as i128 * Nanosecond::per_t::<i128>(Second)
395 + self.nanosecond() as i128
396 }
397
398 /// Get the [`Date`] component of the `UtcDateTime`.
399 ///
400 /// ```rust
401 /// # use time_macros::{date, utc_datetime};
402 /// assert_eq!(utc_datetime!(2019-01-01 0:00).date(), date!(2019-01-01));
403 /// ```
404 #[inline]
405 pub const fn date(self) -> Date {
406 self.inner.date()
407 }
408
409 /// Get the [`Time`] component of the `UtcDateTime`.
410 ///
411 /// ```rust
412 /// # use time_macros::{utc_datetime, time};
413 /// assert_eq!(utc_datetime!(2019-01-01 0:00).time(), time!(0:00));
414 /// ```
415 #[inline]
416 pub const fn time(self) -> Time {
417 self.inner.time()
418 }
419
420 /// Get the year of the date.
421 ///
422 /// ```rust
423 /// # use time_macros::utc_datetime;
424 /// assert_eq!(utc_datetime!(2019-01-01 0:00).year(), 2019);
425 /// assert_eq!(utc_datetime!(2019-12-31 0:00).year(), 2019);
426 /// assert_eq!(utc_datetime!(2020-01-01 0:00).year(), 2020);
427 /// ```
428 #[inline]
429 pub const fn year(self) -> i32 {
430 self.date().year()
431 }
432
433 /// Get the month of the date.
434 ///
435 /// ```rust
436 /// # use time::Month;
437 /// # use time_macros::utc_datetime;
438 /// assert_eq!(utc_datetime!(2019-01-01 0:00).month(), Month::January);
439 /// assert_eq!(utc_datetime!(2019-12-31 0:00).month(), Month::December);
440 /// ```
441 #[inline]
442 pub const fn month(self) -> Month {
443 self.date().month()
444 }
445
446 /// Get the day of the date.
447 ///
448 /// The returned value will always be in the range `1..=31`.
449 ///
450 /// ```rust
451 /// # use time_macros::utc_datetime;
452 /// assert_eq!(utc_datetime!(2019-01-01 0:00).day(), 1);
453 /// assert_eq!(utc_datetime!(2019-12-31 0:00).day(), 31);
454 /// ```
455 #[inline]
456 pub const fn day(self) -> u8 {
457 self.date().day()
458 }
459
460 /// Get the day of the year.
461 ///
462 /// The returned value will always be in the range `1..=366` (`1..=365` for common years).
463 ///
464 /// ```rust
465 /// # use time_macros::utc_datetime;
466 /// assert_eq!(utc_datetime!(2019-01-01 0:00).ordinal(), 1);
467 /// assert_eq!(utc_datetime!(2019-12-31 0:00).ordinal(), 365);
468 /// ```
469 #[inline]
470 pub const fn ordinal(self) -> u16 {
471 self.date().ordinal()
472 }
473
474 /// Get the ISO week number.
475 ///
476 /// The returned value will always be in the range `1..=53`.
477 ///
478 /// ```rust
479 /// # use time_macros::utc_datetime;
480 /// assert_eq!(utc_datetime!(2019-01-01 0:00).iso_week(), 1);
481 /// assert_eq!(utc_datetime!(2019-10-04 0:00).iso_week(), 40);
482 /// assert_eq!(utc_datetime!(2020-01-01 0:00).iso_week(), 1);
483 /// assert_eq!(utc_datetime!(2020-12-31 0:00).iso_week(), 53);
484 /// assert_eq!(utc_datetime!(2021-01-01 0:00).iso_week(), 53);
485 /// ```
486 #[inline]
487 pub const fn iso_week(self) -> u8 {
488 self.date().iso_week()
489 }
490
491 /// Get the week number where week 1 begins on the first Sunday.
492 ///
493 /// The returned value will always be in the range `0..=53`.
494 ///
495 /// ```rust
496 /// # use time_macros::utc_datetime;
497 /// assert_eq!(utc_datetime!(2019-01-01 0:00).sunday_based_week(), 0);
498 /// assert_eq!(utc_datetime!(2020-01-01 0:00).sunday_based_week(), 0);
499 /// assert_eq!(utc_datetime!(2020-12-31 0:00).sunday_based_week(), 52);
500 /// assert_eq!(utc_datetime!(2021-01-01 0:00).sunday_based_week(), 0);
501 /// ```
502 #[inline]
503 pub const fn sunday_based_week(self) -> u8 {
504 self.date().sunday_based_week()
505 }
506
507 /// Get the week number where week 1 begins on the first Monday.
508 ///
509 /// The returned value will always be in the range `0..=53`.
510 ///
511 /// ```rust
512 /// # use time_macros::utc_datetime;
513 /// assert_eq!(utc_datetime!(2019-01-01 0:00).monday_based_week(), 0);
514 /// assert_eq!(utc_datetime!(2020-01-01 0:00).monday_based_week(), 0);
515 /// assert_eq!(utc_datetime!(2020-12-31 0:00).monday_based_week(), 52);
516 /// assert_eq!(utc_datetime!(2021-01-01 0:00).monday_based_week(), 0);
517 /// ```
518 #[inline]
519 pub const fn monday_based_week(self) -> u8 {
520 self.date().monday_based_week()
521 }
522
523 /// Get the year, month, and day.
524 ///
525 /// ```rust
526 /// # use time::Month;
527 /// # use time_macros::utc_datetime;
528 /// assert_eq!(
529 /// utc_datetime!(2019-01-01 0:00).to_calendar_date(),
530 /// (2019, Month::January, 1)
531 /// );
532 /// ```
533 #[inline]
534 pub const fn to_calendar_date(self) -> (i32, Month, u8) {
535 self.date().to_calendar_date()
536 }
537
538 /// Get the year and ordinal day number.
539 ///
540 /// ```rust
541 /// # use time_macros::utc_datetime;
542 /// assert_eq!(utc_datetime!(2019-01-01 0:00).to_ordinal_date(), (2019, 1));
543 /// ```
544 #[inline]
545 pub const fn to_ordinal_date(self) -> (i32, u16) {
546 self.date().to_ordinal_date()
547 }
548
549 /// Get the ISO 8601 year, week number, and weekday.
550 ///
551 /// ```rust
552 /// # use time::Weekday::*;
553 /// # use time_macros::utc_datetime;
554 /// assert_eq!(
555 /// utc_datetime!(2019-01-01 0:00).to_iso_week_date(),
556 /// (2019, 1, Tuesday)
557 /// );
558 /// assert_eq!(
559 /// utc_datetime!(2019-10-04 0:00).to_iso_week_date(),
560 /// (2019, 40, Friday)
561 /// );
562 /// assert_eq!(
563 /// utc_datetime!(2020-01-01 0:00).to_iso_week_date(),
564 /// (2020, 1, Wednesday)
565 /// );
566 /// assert_eq!(
567 /// utc_datetime!(2020-12-31 0:00).to_iso_week_date(),
568 /// (2020, 53, Thursday)
569 /// );
570 /// assert_eq!(
571 /// utc_datetime!(2021-01-01 0:00).to_iso_week_date(),
572 /// (2020, 53, Friday)
573 /// );
574 /// ```
575 #[inline]
576 pub const fn to_iso_week_date(self) -> (i32, u8, Weekday) {
577 self.date().to_iso_week_date()
578 }
579
580 /// Get the weekday.
581 ///
582 /// ```rust
583 /// # use time::Weekday::*;
584 /// # use time_macros::utc_datetime;
585 /// assert_eq!(utc_datetime!(2019-01-01 0:00).weekday(), Tuesday);
586 /// assert_eq!(utc_datetime!(2019-02-01 0:00).weekday(), Friday);
587 /// assert_eq!(utc_datetime!(2019-03-01 0:00).weekday(), Friday);
588 /// assert_eq!(utc_datetime!(2019-04-01 0:00).weekday(), Monday);
589 /// assert_eq!(utc_datetime!(2019-05-01 0:00).weekday(), Wednesday);
590 /// assert_eq!(utc_datetime!(2019-06-01 0:00).weekday(), Saturday);
591 /// assert_eq!(utc_datetime!(2019-07-01 0:00).weekday(), Monday);
592 /// assert_eq!(utc_datetime!(2019-08-01 0:00).weekday(), Thursday);
593 /// assert_eq!(utc_datetime!(2019-09-01 0:00).weekday(), Sunday);
594 /// assert_eq!(utc_datetime!(2019-10-01 0:00).weekday(), Tuesday);
595 /// assert_eq!(utc_datetime!(2019-11-01 0:00).weekday(), Friday);
596 /// assert_eq!(utc_datetime!(2019-12-01 0:00).weekday(), Sunday);
597 /// ```
598 #[inline]
599 pub const fn weekday(self) -> Weekday {
600 self.date().weekday()
601 }
602
603 /// Get the Julian day for the date. The time is not taken into account for this calculation.
604 ///
605 /// ```rust
606 /// # use time_macros::utc_datetime;
607 /// assert_eq!(utc_datetime!(-4713-11-24 0:00).to_julian_day(), 0);
608 /// assert_eq!(utc_datetime!(2000-01-01 0:00).to_julian_day(), 2_451_545);
609 /// assert_eq!(utc_datetime!(2019-01-01 0:00).to_julian_day(), 2_458_485);
610 /// assert_eq!(utc_datetime!(2019-12-31 0:00).to_julian_day(), 2_458_849);
611 /// ```
612 #[inline]
613 pub const fn to_julian_day(self) -> i32 {
614 self.date().to_julian_day()
615 }
616
617 /// Get the clock hour, minute, and second.
618 ///
619 /// ```rust
620 /// # use time_macros::utc_datetime;
621 /// assert_eq!(utc_datetime!(2020-01-01 0:00:00).as_hms(), (0, 0, 0));
622 /// assert_eq!(utc_datetime!(2020-01-01 23:59:59).as_hms(), (23, 59, 59));
623 /// ```
624 #[inline]
625 pub const fn as_hms(self) -> (u8, u8, u8) {
626 self.time().as_hms()
627 }
628
629 /// Get the clock hour, minute, second, and millisecond.
630 ///
631 /// ```rust
632 /// # use time_macros::utc_datetime;
633 /// assert_eq!(utc_datetime!(2020-01-01 0:00:00).as_hms_milli(), (0, 0, 0, 0));
634 /// assert_eq!(
635 /// utc_datetime!(2020-01-01 23:59:59.999).as_hms_milli(),
636 /// (23, 59, 59, 999)
637 /// );
638 /// ```
639 #[inline]
640 pub const fn as_hms_milli(self) -> (u8, u8, u8, u16) {
641 self.time().as_hms_milli()
642 }
643
644 /// Get the clock hour, minute, second, and microsecond.
645 ///
646 /// ```rust
647 /// # use time_macros::utc_datetime;
648 /// assert_eq!(utc_datetime!(2020-01-01 0:00:00).as_hms_micro(), (0, 0, 0, 0));
649 /// assert_eq!(
650 /// utc_datetime!(2020-01-01 23:59:59.999_999).as_hms_micro(),
651 /// (23, 59, 59, 999_999)
652 /// );
653 /// ```
654 #[inline]
655 pub const fn as_hms_micro(self) -> (u8, u8, u8, u32) {
656 self.time().as_hms_micro()
657 }
658
659 /// Get the clock hour, minute, second, and nanosecond.
660 ///
661 /// ```rust
662 /// # use time_macros::utc_datetime;
663 /// assert_eq!(utc_datetime!(2020-01-01 0:00:00).as_hms_nano(), (0, 0, 0, 0));
664 /// assert_eq!(
665 /// utc_datetime!(2020-01-01 23:59:59.999_999_999).as_hms_nano(),
666 /// (23, 59, 59, 999_999_999)
667 /// );
668 /// ```
669 #[inline]
670 pub const fn as_hms_nano(self) -> (u8, u8, u8, u32) {
671 self.time().as_hms_nano()
672 }
673
674 /// Get the clock hour.
675 ///
676 /// The returned value will always be in the range `0..24`.
677 ///
678 /// ```rust
679 /// # use time_macros::utc_datetime;
680 /// assert_eq!(utc_datetime!(2019-01-01 0:00).hour(), 0);
681 /// assert_eq!(utc_datetime!(2019-01-01 23:59:59).hour(), 23);
682 /// ```
683 #[inline]
684 pub const fn hour(self) -> u8 {
685 self.time().hour()
686 }
687
688 /// Get the minute within the hour.
689 ///
690 /// The returned value will always be in the range `0..60`.
691 ///
692 /// ```rust
693 /// # use time_macros::utc_datetime;
694 /// assert_eq!(utc_datetime!(2019-01-01 0:00).minute(), 0);
695 /// assert_eq!(utc_datetime!(2019-01-01 23:59:59).minute(), 59);
696 /// ```
697 #[inline]
698 pub const fn minute(self) -> u8 {
699 self.time().minute()
700 }
701
702 /// Get the second within the minute.
703 ///
704 /// The returned value will always be in the range `0..60`.
705 ///
706 /// ```rust
707 /// # use time_macros::utc_datetime;
708 /// assert_eq!(utc_datetime!(2019-01-01 0:00).second(), 0);
709 /// assert_eq!(utc_datetime!(2019-01-01 23:59:59).second(), 59);
710 /// ```
711 #[inline]
712 pub const fn second(self) -> u8 {
713 self.time().second()
714 }
715
716 /// Get the milliseconds within the second.
717 ///
718 /// The returned value will always be in the range `0..1_000`.
719 ///
720 /// ```rust
721 /// # use time_macros::utc_datetime;
722 /// assert_eq!(utc_datetime!(2019-01-01 0:00).millisecond(), 0);
723 /// assert_eq!(utc_datetime!(2019-01-01 23:59:59.999).millisecond(), 999);
724 /// ```
725 #[inline]
726 pub const fn millisecond(self) -> u16 {
727 self.time().millisecond()
728 }
729
730 /// Get the microseconds within the second.
731 ///
732 /// The returned value will always be in the range `0..1_000_000`.
733 ///
734 /// ```rust
735 /// # use time_macros::utc_datetime;
736 /// assert_eq!(utc_datetime!(2019-01-01 0:00).microsecond(), 0);
737 /// assert_eq!(
738 /// utc_datetime!(2019-01-01 23:59:59.999_999).microsecond(),
739 /// 999_999
740 /// );
741 /// ```
742 #[inline]
743 pub const fn microsecond(self) -> u32 {
744 self.time().microsecond()
745 }
746
747 /// Get the nanoseconds within the second.
748 ///
749 /// The returned value will always be in the range `0..1_000_000_000`.
750 ///
751 /// ```rust
752 /// # use time_macros::utc_datetime;
753 /// assert_eq!(utc_datetime!(2019-01-01 0:00).nanosecond(), 0);
754 /// assert_eq!(
755 /// utc_datetime!(2019-01-01 23:59:59.999_999_999).nanosecond(),
756 /// 999_999_999,
757 /// );
758 /// ```
759 #[inline]
760 pub const fn nanosecond(self) -> u32 {
761 self.time().nanosecond()
762 }
763
764 /// Computes `self + duration`, returning `None` if an overflow occurred.
765 ///
766 /// ```rust
767 /// # use time::{UtcDateTime, ext::NumericalDuration};
768 /// # use time_macros::utc_datetime;
769 /// assert_eq!(UtcDateTime::MIN.checked_add((-2).days()), None);
770 /// assert_eq!(UtcDateTime::MAX.checked_add(1.days()), None);
771 /// assert_eq!(
772 /// utc_datetime!(2019-11-25 15:30).checked_add(27.hours()),
773 /// Some(utc_datetime!(2019-11-26 18:30))
774 /// );
775 /// ```
776 #[inline]
777 pub const fn checked_add(self, duration: Duration) -> Option<Self> {
778 Some(Self::from_primitive(const_try_opt!(
779 self.inner.checked_add(duration)
780 )))
781 }
782
783 /// Computes `self - duration`, returning `None` if an overflow occurred.
784 ///
785 /// ```rust
786 /// # use time::{UtcDateTime, ext::NumericalDuration};
787 /// # use time_macros::utc_datetime;
788 /// assert_eq!(UtcDateTime::MIN.checked_sub(2.days()), None);
789 /// assert_eq!(UtcDateTime::MAX.checked_sub((-1).days()), None);
790 /// assert_eq!(
791 /// utc_datetime!(2019-11-25 15:30).checked_sub(27.hours()),
792 /// Some(utc_datetime!(2019-11-24 12:30))
793 /// );
794 /// ```
795 #[inline]
796 pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
797 Some(Self::from_primitive(const_try_opt!(
798 self.inner.checked_sub(duration)
799 )))
800 }
801
802 /// Computes `self + duration`, saturating value on overflow.
803 ///
804 /// ```rust
805 /// # use time::{UtcDateTime, ext::NumericalDuration};
806 /// # use time_macros::utc_datetime;
807 /// assert_eq!(
808 /// UtcDateTime::MIN.saturating_add((-2).days()),
809 /// UtcDateTime::MIN
810 /// );
811 /// assert_eq!(
812 /// UtcDateTime::MAX.saturating_add(2.days()),
813 /// UtcDateTime::MAX
814 /// );
815 /// assert_eq!(
816 /// utc_datetime!(2019-11-25 15:30).saturating_add(27.hours()),
817 /// utc_datetime!(2019-11-26 18:30)
818 /// );
819 /// ```
820 #[inline]
821 pub const fn saturating_add(self, duration: Duration) -> Self {
822 Self::from_primitive(self.inner.saturating_add(duration))
823 }
824
825 /// Computes `self - duration`, saturating value on overflow.
826 ///
827 /// ```rust
828 /// # use time::{UtcDateTime, ext::NumericalDuration};
829 /// # use time_macros::utc_datetime;
830 /// assert_eq!(
831 /// UtcDateTime::MIN.saturating_sub(2.days()),
832 /// UtcDateTime::MIN
833 /// );
834 /// assert_eq!(
835 /// UtcDateTime::MAX.saturating_sub((-2).days()),
836 /// UtcDateTime::MAX
837 /// );
838 /// assert_eq!(
839 /// utc_datetime!(2019-11-25 15:30).saturating_sub(27.hours()),
840 /// utc_datetime!(2019-11-24 12:30)
841 /// );
842 /// ```
843 #[inline]
844 pub const fn saturating_sub(self, duration: Duration) -> Self {
845 Self::from_primitive(self.inner.saturating_sub(duration))
846 }
847}
848
849/// Methods that replace part of the `UtcDateTime`.
850impl UtcDateTime {
851 /// Replace the time, preserving the date.
852 ///
853 /// ```rust
854 /// # use time_macros::{utc_datetime, time};
855 /// assert_eq!(
856 /// utc_datetime!(2020-01-01 17:00).replace_time(time!(5:00)),
857 /// utc_datetime!(2020-01-01 5:00)
858 /// );
859 /// ```
860 #[must_use = "This method does not mutate the original `UtcDateTime`."]
861 #[inline]
862 pub const fn replace_time(self, time: Time) -> Self {
863 Self::from_primitive(self.inner.replace_time(time))
864 }
865
866 /// Replace the date, preserving the time.
867 ///
868 /// ```rust
869 /// # use time_macros::{utc_datetime, date};
870 /// assert_eq!(
871 /// utc_datetime!(2020-01-01 12:00).replace_date(date!(2020-01-30)),
872 /// utc_datetime!(2020-01-30 12:00)
873 /// );
874 /// ```
875 #[must_use = "This method does not mutate the original `UtcDateTime`."]
876 #[inline]
877 pub const fn replace_date(self, date: Date) -> Self {
878 Self::from_primitive(self.inner.replace_date(date))
879 }
880
881 /// Replace the year. The month and day will be unchanged.
882 ///
883 /// ```rust
884 /// # use time_macros::utc_datetime;
885 /// assert_eq!(
886 /// utc_datetime!(2022-02-18 12:00).replace_year(2019),
887 /// Ok(utc_datetime!(2019-02-18 12:00))
888 /// );
889 /// assert!(utc_datetime!(2022-02-18 12:00).replace_year(-1_000_000_000).is_err()); // -1_000_000_000 isn't a valid year
890 /// assert!(utc_datetime!(2022-02-18 12:00).replace_year(1_000_000_000).is_err()); // 1_000_000_000 isn't a valid year
891 /// ```
892 #[must_use = "This method does not mutate the original `UtcDateTime`."]
893 #[inline]
894 pub const fn replace_year(self, year: i32) -> Result<Self, error::ComponentRange> {
895 Ok(Self::from_primitive(const_try!(
896 self.inner.replace_year(year)
897 )))
898 }
899
900 /// Replace the month of the year.
901 ///
902 /// ```rust
903 /// # use time_macros::utc_datetime;
904 /// # use time::Month;
905 /// assert_eq!(
906 /// utc_datetime!(2022-02-18 12:00).replace_month(Month::January),
907 /// Ok(utc_datetime!(2022-01-18 12:00))
908 /// );
909 /// assert!(utc_datetime!(2022-01-30 12:00).replace_month(Month::February).is_err()); // 30 isn't a valid day in February
910 /// ```
911 #[must_use = "This method does not mutate the original `UtcDateTime`."]
912 #[inline]
913 pub const fn replace_month(self, month: Month) -> Result<Self, error::ComponentRange> {
914 Ok(Self::from_primitive(const_try!(
915 self.inner.replace_month(month)
916 )))
917 }
918
919 /// Replace the day of the month.
920 ///
921 /// ```rust
922 /// # use time_macros::utc_datetime;
923 /// assert_eq!(
924 /// utc_datetime!(2022-02-18 12:00).replace_day(1),
925 /// Ok(utc_datetime!(2022-02-01 12:00))
926 /// );
927 /// assert!(utc_datetime!(2022-02-18 12:00).replace_day(0).is_err()); // 00 isn't a valid day
928 /// assert!(utc_datetime!(2022-02-18 12:00).replace_day(30).is_err()); // 30 isn't a valid day in February
929 /// ```
930 #[must_use = "This method does not mutate the original `UtcDateTime`."]
931 #[inline]
932 pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
933 Ok(Self::from_primitive(const_try!(
934 self.inner.replace_day(day)
935 )))
936 }
937
938 /// Replace the day of the year.
939 ///
940 /// ```rust
941 /// # use time_macros::utc_datetime;
942 /// assert_eq!(utc_datetime!(2022-049 12:00).replace_ordinal(1), Ok(utc_datetime!(2022-001 12:00)));
943 /// assert!(utc_datetime!(2022-049 12:00).replace_ordinal(0).is_err()); // 0 isn't a valid ordinal
944 /// assert!(utc_datetime!(2022-049 12:00).replace_ordinal(366).is_err()); // 2022 isn't a leap year
945 /// ```
946 #[must_use = "This method does not mutate the original `UtcDateTime`."]
947 #[inline]
948 pub const fn replace_ordinal(self, ordinal: u16) -> Result<Self, error::ComponentRange> {
949 Ok(Self::from_primitive(const_try!(
950 self.inner.replace_ordinal(ordinal)
951 )))
952 }
953
954 /// Truncate to the start of the day, setting the time to midnight.
955 ///
956 /// ```rust
957 /// # use time_macros::utc_datetime;
958 /// assert_eq!(
959 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_day(),
960 /// utc_datetime!(2022-02-18 0:00)
961 /// );
962 /// ```
963 #[must_use = "This method does not mutate the original `UtcDateTime`."]
964 #[inline]
965 pub const fn truncate_to_day(self) -> Self {
966 Self::from_primitive(self.inner.truncate_to_day())
967 }
968
969 /// Replace the clock hour.
970 ///
971 /// ```rust
972 /// # use time_macros::utc_datetime;
973 /// assert_eq!(
974 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_hour(7),
975 /// Ok(utc_datetime!(2022-02-18 07:02:03.004_005_006))
976 /// );
977 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_hour(24).is_err()); // 24 isn't a valid hour
978 /// ```
979 #[must_use = "This method does not mutate the original `UtcDateTime`."]
980 #[inline]
981 pub const fn replace_hour(self, hour: u8) -> Result<Self, error::ComponentRange> {
982 Ok(Self::from_primitive(const_try!(
983 self.inner.replace_hour(hour)
984 )))
985 }
986
987 /// Truncate to the hour, setting the minute, second, and subsecond components to zero.
988 ///
989 /// ```rust
990 /// # use time_macros::utc_datetime;
991 /// assert_eq!(
992 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_hour(),
993 /// utc_datetime!(2022-02-18 15:00)
994 /// );
995 /// ```
996 #[must_use = "This method does not mutate the original `UtcDateTime`."]
997 #[inline]
998 pub const fn truncate_to_hour(self) -> Self {
999 Self::from_primitive(self.inner.truncate_to_hour())
1000 }
1001
1002 /// Replace the minutes within the hour.
1003 ///
1004 /// ```rust
1005 /// # use time_macros::utc_datetime;
1006 /// assert_eq!(
1007 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_minute(7),
1008 /// Ok(utc_datetime!(2022-02-18 01:07:03.004_005_006))
1009 /// );
1010 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_minute(60).is_err()); // 60 isn't a valid minute
1011 /// ```
1012 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1013 #[inline]
1014 pub const fn replace_minute(
1015 self,
1016 sunday_based_week: u8,
1017 ) -> Result<Self, error::ComponentRange> {
1018 Ok(Self::from_primitive(const_try!(
1019 self.inner.replace_minute(sunday_based_week)
1020 )))
1021 }
1022
1023 /// Truncate to the minute, setting the second and subsecond components to zero.
1024 ///
1025 /// ```rust
1026 /// # use time_macros::utc_datetime;
1027 /// assert_eq!(
1028 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_minute(),
1029 /// utc_datetime!(2022-02-18 15:30)
1030 /// );
1031 /// ```
1032 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1033 #[inline]
1034 pub const fn truncate_to_minute(self) -> Self {
1035 Self::from_primitive(self.inner.truncate_to_minute())
1036 }
1037
1038 /// Replace the seconds within the minute.
1039 ///
1040 /// ```rust
1041 /// # use time_macros::utc_datetime;
1042 /// assert_eq!(
1043 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_second(7),
1044 /// Ok(utc_datetime!(2022-02-18 01:02:07.004_005_006))
1045 /// );
1046 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_second(60).is_err()); // 60 isn't a valid second
1047 /// ```
1048 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1049 #[inline]
1050 pub const fn replace_second(
1051 self,
1052 monday_based_week: u8,
1053 ) -> Result<Self, error::ComponentRange> {
1054 Ok(Self::from_primitive(const_try!(
1055 self.inner.replace_second(monday_based_week)
1056 )))
1057 }
1058
1059 /// Truncate to the second, setting the subsecond components to zero.
1060 ///
1061 /// ```rust
1062 /// # use time_macros::utc_datetime;
1063 /// assert_eq!(
1064 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_second(),
1065 /// utc_datetime!(2022-02-18 15:30:45)
1066 /// );
1067 /// ```
1068 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1069 #[inline]
1070 pub const fn truncate_to_second(self) -> Self {
1071 Self::from_primitive(self.inner.truncate_to_second())
1072 }
1073
1074 /// Replace the milliseconds within the second.
1075 ///
1076 /// ```rust
1077 /// # use time_macros::utc_datetime;
1078 /// assert_eq!(
1079 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_millisecond(7),
1080 /// Ok(utc_datetime!(2022-02-18 01:02:03.007))
1081 /// );
1082 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_millisecond(1_000).is_err()); // 1_000 isn't a valid millisecond
1083 /// ```
1084 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1085 #[inline]
1086 pub const fn replace_millisecond(
1087 self,
1088 millisecond: u16,
1089 ) -> Result<Self, error::ComponentRange> {
1090 Ok(Self::from_primitive(const_try!(
1091 self.inner.replace_millisecond(millisecond)
1092 )))
1093 }
1094
1095 /// Truncate to the millisecond, setting the microsecond and nanosecond components to zero.
1096 ///
1097 /// ```rust
1098 /// # use time_macros::utc_datetime;
1099 /// assert_eq!(
1100 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_millisecond(),
1101 /// utc_datetime!(2022-02-18 15:30:45.123)
1102 /// );
1103 /// ```
1104 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1105 #[inline]
1106 pub const fn truncate_to_millisecond(self) -> Self {
1107 Self::from_primitive(self.inner.truncate_to_millisecond())
1108 }
1109
1110 /// Replace the microseconds within the second.
1111 ///
1112 /// ```rust
1113 /// # use time_macros::utc_datetime;
1114 /// assert_eq!(
1115 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_microsecond(7_008),
1116 /// Ok(utc_datetime!(2022-02-18 01:02:03.007_008))
1117 /// );
1118 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_microsecond(1_000_000).is_err()); // 1_000_000 isn't a valid microsecond
1119 /// ```
1120 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1121 #[inline]
1122 pub const fn replace_microsecond(
1123 self,
1124 microsecond: u32,
1125 ) -> Result<Self, error::ComponentRange> {
1126 Ok(Self::from_primitive(const_try!(
1127 self.inner.replace_microsecond(microsecond)
1128 )))
1129 }
1130
1131 /// Truncate to the microsecond, setting the nanosecond component to zero.
1132 ///
1133 /// ```rust
1134 /// # use time_macros::utc_datetime;
1135 /// assert_eq!(
1136 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_microsecond(),
1137 /// utc_datetime!(2022-02-18 15:30:45.123_456)
1138 /// );
1139 /// ```
1140 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1141 #[inline]
1142 pub const fn truncate_to_microsecond(self) -> Self {
1143 Self::from_primitive(self.inner.truncate_to_microsecond())
1144 }
1145
1146 /// Replace the nanoseconds within the second.
1147 ///
1148 /// ```rust
1149 /// # use time_macros::utc_datetime;
1150 /// assert_eq!(
1151 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_nanosecond(7_008_009),
1152 /// Ok(utc_datetime!(2022-02-18 01:02:03.007_008_009))
1153 /// );
1154 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_nanosecond(1_000_000_000).is_err()); // 1_000_000_000 isn't a valid nanosecond
1155 /// ```
1156 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1157 #[inline]
1158 pub const fn replace_nanosecond(self, nanosecond: u32) -> Result<Self, error::ComponentRange> {
1159 Ok(Self::from_primitive(const_try!(
1160 self.inner.replace_nanosecond(nanosecond)
1161 )))
1162 }
1163}
1164
1165#[cfg(feature = "formatting")]
1166impl UtcDateTime {
1167 /// Format the `UtcDateTime` using the provided [format
1168 /// description](crate::format_description).
1169 #[inline]
1170 pub fn format_into(
1171 self,
1172 output: &mut (impl io::Write + ?Sized),
1173 format: &(impl Formattable + ?Sized),
1174 ) -> Result<usize, error::Format> {
1175 format.format_into(output, &self, &mut Default::default())
1176 }
1177
1178 /// Format the `UtcDateTime` using the provided [format
1179 /// description](crate::format_description).
1180 ///
1181 /// ```rust
1182 /// # use time::format_description;
1183 /// # use time_macros::utc_datetime;
1184 /// let format = format_description::parse(
1185 /// "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
1186 /// sign:mandatory]:[offset_minute]:[offset_second]",
1187 /// )?;
1188 /// assert_eq!(
1189 /// utc_datetime!(2020-01-02 03:04:05).format(&format)?,
1190 /// "2020-01-02 03:04:05 +00:00:00"
1191 /// );
1192 /// # Ok::<_, time::Error>(())
1193 /// ```
1194 #[inline]
1195 pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
1196 format.format(&self, &mut Default::default())
1197 }
1198}
1199
1200#[cfg(feature = "parsing")]
1201impl UtcDateTime {
1202 /// Parse an `UtcDateTime` from the input using the provided [format
1203 /// description](crate::format_description). A [`UtcOffset`] is permitted, but not required to
1204 /// be present. If present, the value will be converted to UTC.
1205 ///
1206 /// ```rust
1207 /// # use time::UtcDateTime;
1208 /// # use time_macros::{utc_datetime, format_description};
1209 /// let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
1210 /// assert_eq!(
1211 /// UtcDateTime::parse("2020-01-02 03:04:05", &format)?,
1212 /// utc_datetime!(2020-01-02 03:04:05)
1213 /// );
1214 /// # Ok::<_, time::Error>(())
1215 /// ```
1216 #[inline]
1217 pub fn parse(
1218 input: &str,
1219 description: &(impl Parsable + ?Sized),
1220 ) -> Result<Self, error::Parse> {
1221 description.parse_utc_date_time(input.as_bytes())
1222 }
1223
1224 /// A helper method to check if the `UtcDateTime` is a valid representation of a leap second.
1225 /// Leap seconds, when parsed, are represented as the preceding nanosecond. However, leap
1226 /// seconds can only occur as the last second of a month UTC.
1227 #[cfg(feature = "parsing")]
1228 #[inline]
1229 pub(crate) const fn is_valid_leap_second_stand_in(self) -> bool {
1230 let dt = self.inner;
1231
1232 dt.hour() == 23
1233 && dt.minute() == 59
1234 && dt.second() == 59
1235 && dt.nanosecond() == 999_999_999
1236 && dt.day() == dt.month().length(dt.year())
1237 }
1238}
1239
1240// This no longer needs special handling, as the format is fixed and doesn't require anything
1241// advanced. Trait impls can't be deprecated and the info is still useful for other types
1242// implementing `SmartDisplay`, so leave it as-is for now.
1243impl SmartDisplay for UtcDateTime {
1244 type Metadata = ();
1245
1246 #[inline]
1247 fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self> {
1248 let width = self.as_primitive().metadata(f).unpadded_width() + 4;
1249 Metadata::new(width, self, ())
1250 }
1251
1252 #[inline]
1253 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1254 fmt::Display::fmt(self, f)
1255 }
1256}
1257
1258impl UtcDateTime {
1259 /// The maximum number of bytes that the `fmt_into_buffer` method will write, which is also used
1260 /// for the `Display` implementation.
1261 pub(crate) const DISPLAY_BUFFER_SIZE: usize = PrimitiveDateTime::DISPLAY_BUFFER_SIZE + 4;
1262
1263 /// Format the `PrimitiveDateTime` into the provided buffer, returning the number of bytes
1264 /// written.
1265 #[inline]
1266 pub(crate) fn fmt_into_buffer(
1267 self,
1268 buf: &mut [MaybeUninit<u8>; Self::DISPLAY_BUFFER_SIZE],
1269 ) -> usize {
1270 // Safety: The buffer is large enough that the first chunk is in bounds.
1271 let pdt_len = self
1272 .inner
1273 .fmt_into_buffer(unsafe { buf.first_chunk_mut().unwrap_unchecked() });
1274 // Safety: The buffer is large enough to hold the additional 4 bytes.
1275 unsafe {
1276 b" +00"
1277 .as_ptr()
1278 .copy_to_nonoverlapping(buf.as_mut_ptr().add(pdt_len).cast(), 4)
1279 };
1280 pdt_len + 4
1281 }
1282}
1283
1284impl fmt::Display for UtcDateTime {
1285 #[inline]
1286 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1287 let mut buf = [MaybeUninit::uninit(); Self::DISPLAY_BUFFER_SIZE];
1288 let len = self.fmt_into_buffer(&mut buf);
1289 // Safety: All bytes up to `len` have been initialized with ASCII characters.
1290 let s = unsafe { str_from_raw_parts(buf.as_ptr().cast(), len) };
1291 f.pad(s)
1292 }
1293}
1294
1295impl fmt::Debug for UtcDateTime {
1296 #[inline]
1297 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1298 fmt::Display::fmt(self, f)
1299 }
1300}
1301
1302impl Add<Duration> for UtcDateTime {
1303 type Output = Self;
1304
1305 /// # Panics
1306 ///
1307 /// This may panic if an overflow occurs.
1308 #[inline]
1309 #[track_caller]
1310 fn add(self, duration: Duration) -> Self::Output {
1311 self.inner.add(duration).as_utc()
1312 }
1313}
1314
1315impl Add<StdDuration> for UtcDateTime {
1316 type Output = Self;
1317
1318 /// # Panics
1319 ///
1320 /// This may panic if an overflow occurs.
1321 #[inline]
1322 #[track_caller]
1323 fn add(self, duration: StdDuration) -> Self::Output {
1324 self.inner.add(duration).as_utc()
1325 }
1326}
1327
1328impl AddAssign<Duration> for UtcDateTime {
1329 /// # Panics
1330 ///
1331 /// This may panic if an overflow occurs.
1332 #[inline]
1333 #[track_caller]
1334 fn add_assign(&mut self, rhs: Duration) {
1335 self.inner.add_assign(rhs);
1336 }
1337}
1338
1339impl AddAssign<StdDuration> for UtcDateTime {
1340 /// # Panics
1341 ///
1342 /// This may panic if an overflow occurs.
1343 #[inline]
1344 #[track_caller]
1345 fn add_assign(&mut self, rhs: StdDuration) {
1346 self.inner.add_assign(rhs);
1347 }
1348}
1349
1350impl Sub<Duration> for UtcDateTime {
1351 type Output = Self;
1352
1353 /// # Panics
1354 ///
1355 /// This may panic if an overflow occurs.
1356 #[inline]
1357 #[track_caller]
1358 fn sub(self, rhs: Duration) -> Self::Output {
1359 self.checked_sub(rhs)
1360 .expect("resulting value is out of range")
1361 }
1362}
1363
1364impl Sub<StdDuration> for UtcDateTime {
1365 type Output = Self;
1366
1367 /// # Panics
1368 ///
1369 /// This may panic if an overflow occurs.
1370 #[inline]
1371 #[track_caller]
1372 fn sub(self, duration: StdDuration) -> Self::Output {
1373 Self::from_primitive(self.inner.sub(duration))
1374 }
1375}
1376
1377impl SubAssign<Duration> for UtcDateTime {
1378 /// # Panics
1379 ///
1380 /// This may panic if an overflow occurs.
1381 #[inline]
1382 #[track_caller]
1383 fn sub_assign(&mut self, rhs: Duration) {
1384 self.inner.sub_assign(rhs);
1385 }
1386}
1387
1388impl SubAssign<StdDuration> for UtcDateTime {
1389 /// # Panics
1390 ///
1391 /// This may panic if an overflow occurs.
1392 #[inline]
1393 #[track_caller]
1394 fn sub_assign(&mut self, rhs: StdDuration) {
1395 self.inner.sub_assign(rhs);
1396 }
1397}
1398
1399impl Sub for UtcDateTime {
1400 type Output = Duration;
1401
1402 #[inline]
1403 fn sub(self, rhs: Self) -> Self::Output {
1404 self.inner.sub(rhs.inner)
1405 }
1406}