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(self, minute: u8) -> Result<Self, error::ComponentRange> {
1015 Ok(Self::from_primitive(const_try!(
1016 self.inner.replace_minute(minute)
1017 )))
1018 }
1019
1020 /// Truncate to the minute, setting the second and subsecond components to zero.
1021 ///
1022 /// ```rust
1023 /// # use time_macros::utc_datetime;
1024 /// assert_eq!(
1025 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_minute(),
1026 /// utc_datetime!(2022-02-18 15:30)
1027 /// );
1028 /// ```
1029 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1030 #[inline]
1031 pub const fn truncate_to_minute(self) -> Self {
1032 Self::from_primitive(self.inner.truncate_to_minute())
1033 }
1034
1035 /// Replace the seconds within the minute.
1036 ///
1037 /// ```rust
1038 /// # use time_macros::utc_datetime;
1039 /// assert_eq!(
1040 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_second(7),
1041 /// Ok(utc_datetime!(2022-02-18 01:02:07.004_005_006))
1042 /// );
1043 /// assert!(utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_second(60).is_err()); // 60 isn't a valid second
1044 /// ```
1045 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1046 #[inline]
1047 pub const fn replace_second(self, second: u8) -> Result<Self, error::ComponentRange> {
1048 Ok(Self::from_primitive(const_try!(
1049 self.inner.replace_second(second)
1050 )))
1051 }
1052
1053 /// Truncate to the second, setting the subsecond components to zero.
1054 ///
1055 /// ```rust
1056 /// # use time_macros::utc_datetime;
1057 /// assert_eq!(
1058 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_second(),
1059 /// utc_datetime!(2022-02-18 15:30:45)
1060 /// );
1061 /// ```
1062 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1063 #[inline]
1064 pub const fn truncate_to_second(self) -> Self {
1065 Self::from_primitive(self.inner.truncate_to_second())
1066 }
1067
1068 /// Replace the milliseconds within the second.
1069 ///
1070 /// ```rust
1071 /// # use time_macros::utc_datetime;
1072 /// assert_eq!(
1073 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_millisecond(7),
1074 /// Ok(utc_datetime!(2022-02-18 01:02:03.007))
1075 /// );
1076 /// 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
1077 /// ```
1078 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1079 #[inline]
1080 pub const fn replace_millisecond(
1081 self,
1082 millisecond: u16,
1083 ) -> Result<Self, error::ComponentRange> {
1084 Ok(Self::from_primitive(const_try!(
1085 self.inner.replace_millisecond(millisecond)
1086 )))
1087 }
1088
1089 /// Truncate to the millisecond, setting the microsecond and nanosecond components to zero.
1090 ///
1091 /// ```rust
1092 /// # use time_macros::utc_datetime;
1093 /// assert_eq!(
1094 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_millisecond(),
1095 /// utc_datetime!(2022-02-18 15:30:45.123)
1096 /// );
1097 /// ```
1098 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1099 #[inline]
1100 pub const fn truncate_to_millisecond(self) -> Self {
1101 Self::from_primitive(self.inner.truncate_to_millisecond())
1102 }
1103
1104 /// Replace the microseconds within the second.
1105 ///
1106 /// ```rust
1107 /// # use time_macros::utc_datetime;
1108 /// assert_eq!(
1109 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_microsecond(7_008),
1110 /// Ok(utc_datetime!(2022-02-18 01:02:03.007_008))
1111 /// );
1112 /// 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
1113 /// ```
1114 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1115 #[inline]
1116 pub const fn replace_microsecond(
1117 self,
1118 microsecond: u32,
1119 ) -> Result<Self, error::ComponentRange> {
1120 Ok(Self::from_primitive(const_try!(
1121 self.inner.replace_microsecond(microsecond)
1122 )))
1123 }
1124
1125 /// Truncate to the microsecond, setting the nanosecond component to zero.
1126 ///
1127 /// ```rust
1128 /// # use time_macros::utc_datetime;
1129 /// assert_eq!(
1130 /// utc_datetime!(2022-02-18 15:30:45.123_456_789).truncate_to_microsecond(),
1131 /// utc_datetime!(2022-02-18 15:30:45.123_456)
1132 /// );
1133 /// ```
1134 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1135 #[inline]
1136 pub const fn truncate_to_microsecond(self) -> Self {
1137 Self::from_primitive(self.inner.truncate_to_microsecond())
1138 }
1139
1140 /// Replace the nanoseconds within the second.
1141 ///
1142 /// ```rust
1143 /// # use time_macros::utc_datetime;
1144 /// assert_eq!(
1145 /// utc_datetime!(2022-02-18 01:02:03.004_005_006).replace_nanosecond(7_008_009),
1146 /// Ok(utc_datetime!(2022-02-18 01:02:03.007_008_009))
1147 /// );
1148 /// 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
1149 /// ```
1150 #[must_use = "This method does not mutate the original `UtcDateTime`."]
1151 #[inline]
1152 pub const fn replace_nanosecond(self, nanosecond: u32) -> Result<Self, error::ComponentRange> {
1153 Ok(Self::from_primitive(const_try!(
1154 self.inner.replace_nanosecond(nanosecond)
1155 )))
1156 }
1157}
1158
1159#[cfg(feature = "formatting")]
1160impl UtcDateTime {
1161 /// Format the `UtcDateTime` using the provided [format
1162 /// description](crate::format_description).
1163 #[inline]
1164 pub fn format_into(
1165 self,
1166 output: &mut (impl io::Write + ?Sized),
1167 format: &(impl Formattable + ?Sized),
1168 ) -> Result<usize, error::Format> {
1169 format.format_into(output, &self, &mut Default::default())
1170 }
1171
1172 /// Format the `UtcDateTime` using the provided [format
1173 /// description](crate::format_description).
1174 ///
1175 /// ```rust
1176 /// # use time::format_description;
1177 /// # use time_macros::utc_datetime;
1178 /// let format = format_description::parse_borrowed::<3>(
1179 /// "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
1180 /// sign:mandatory]:[offset_minute]:[offset_second]",
1181 /// )?;
1182 /// assert_eq!(
1183 /// utc_datetime!(2020-01-02 03:04:05).format(&format)?,
1184 /// "2020-01-02 03:04:05 +00:00:00"
1185 /// );
1186 /// # Ok::<_, time::Error>(())
1187 /// ```
1188 #[inline]
1189 pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
1190 format.format(&self, &mut Default::default())
1191 }
1192}
1193
1194#[cfg(feature = "parsing")]
1195impl UtcDateTime {
1196 /// Parse an `UtcDateTime` from the input using the provided [format
1197 /// description](crate::format_description). A [`UtcOffset`] is permitted, but not required to
1198 /// be present. If present, the value will be converted to UTC.
1199 ///
1200 /// ```rust
1201 /// # use time::UtcDateTime;
1202 /// # use time_macros::{utc_datetime, format_description};
1203 /// let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
1204 /// assert_eq!(
1205 /// UtcDateTime::parse("2020-01-02 03:04:05", &format)?,
1206 /// utc_datetime!(2020-01-02 03:04:05)
1207 /// );
1208 /// # Ok::<_, time::Error>(())
1209 /// ```
1210 #[inline]
1211 pub fn parse(
1212 input: &str,
1213 description: &(impl Parsable + ?Sized),
1214 ) -> Result<Self, error::Parse> {
1215 description.parse_utc_date_time(input.as_bytes())
1216 }
1217
1218 /// A helper method to check if the `UtcDateTime` is a valid representation of a leap second.
1219 /// Leap seconds, when parsed, are represented as the preceding nanosecond. However, leap
1220 /// seconds can only occur as the last second of a month UTC.
1221 #[cfg(feature = "parsing")]
1222 #[inline]
1223 pub(crate) const fn is_valid_leap_second_stand_in(self) -> bool {
1224 let dt = self.inner;
1225
1226 dt.hour() == 23
1227 && dt.minute() == 59
1228 && dt.second() == 59
1229 && dt.nanosecond() == 999_999_999
1230 && dt.day() == dt.month().length(dt.year())
1231 }
1232}
1233
1234// This no longer needs special handling, as the format is fixed and doesn't require anything
1235// advanced. Trait impls can't be deprecated and the info is still useful for other types
1236// implementing `SmartDisplay`, so leave it as-is for now.
1237impl SmartDisplay for UtcDateTime {
1238 type Metadata = ();
1239
1240 #[inline]
1241 fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self> {
1242 let width = self.as_primitive().metadata(f).unpadded_width() + 4;
1243 Metadata::new(width, self, ())
1244 }
1245
1246 #[inline]
1247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1248 fmt::Display::fmt(self, f)
1249 }
1250}
1251
1252impl UtcDateTime {
1253 /// The maximum number of bytes that the `fmt_into_buffer` method will write, which is also used
1254 /// for the `Display` implementation.
1255 pub(crate) const DISPLAY_BUFFER_SIZE: usize = PrimitiveDateTime::DISPLAY_BUFFER_SIZE + 4;
1256
1257 /// Format the `PrimitiveDateTime` into the provided buffer, returning the number of bytes
1258 /// written.
1259 #[inline]
1260 pub(crate) fn fmt_into_buffer(
1261 self,
1262 buf: &mut [MaybeUninit<u8>; Self::DISPLAY_BUFFER_SIZE],
1263 ) -> usize {
1264 // Safety: The buffer is large enough that the first chunk is in bounds.
1265 let pdt_len = self
1266 .inner
1267 .fmt_into_buffer(unsafe { buf.first_chunk_mut().unwrap_unchecked() });
1268 // Safety: The buffer is large enough to hold the additional 4 bytes.
1269 unsafe {
1270 b" +00"
1271 .as_ptr()
1272 .copy_to_nonoverlapping(buf.as_mut_ptr().add(pdt_len).cast(), 4)
1273 };
1274 pdt_len + 4
1275 }
1276}
1277
1278impl fmt::Display for UtcDateTime {
1279 #[inline]
1280 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1281 let mut buf = [MaybeUninit::uninit(); Self::DISPLAY_BUFFER_SIZE];
1282 let len = self.fmt_into_buffer(&mut buf);
1283 // Safety: All bytes up to `len` have been initialized with ASCII characters.
1284 let s = unsafe { str_from_raw_parts(buf.as_ptr().cast(), len) };
1285 f.pad(s)
1286 }
1287}
1288
1289impl fmt::Debug for UtcDateTime {
1290 #[inline]
1291 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1292 fmt::Display::fmt(self, f)
1293 }
1294}
1295
1296impl Add<Duration> for UtcDateTime {
1297 type Output = Self;
1298
1299 /// # Panics
1300 ///
1301 /// This may panic if an overflow occurs.
1302 #[inline]
1303 #[track_caller]
1304 fn add(self, duration: Duration) -> Self::Output {
1305 self.inner.add(duration).as_utc()
1306 }
1307}
1308
1309impl Add<StdDuration> for UtcDateTime {
1310 type Output = Self;
1311
1312 /// # Panics
1313 ///
1314 /// This may panic if an overflow occurs.
1315 #[inline]
1316 #[track_caller]
1317 fn add(self, duration: StdDuration) -> Self::Output {
1318 self.inner.add(duration).as_utc()
1319 }
1320}
1321
1322impl AddAssign<Duration> for UtcDateTime {
1323 /// # Panics
1324 ///
1325 /// This may panic if an overflow occurs.
1326 #[inline]
1327 #[track_caller]
1328 fn add_assign(&mut self, rhs: Duration) {
1329 self.inner.add_assign(rhs);
1330 }
1331}
1332
1333impl AddAssign<StdDuration> for UtcDateTime {
1334 /// # Panics
1335 ///
1336 /// This may panic if an overflow occurs.
1337 #[inline]
1338 #[track_caller]
1339 fn add_assign(&mut self, rhs: StdDuration) {
1340 self.inner.add_assign(rhs);
1341 }
1342}
1343
1344impl Sub<Duration> for UtcDateTime {
1345 type Output = Self;
1346
1347 /// # Panics
1348 ///
1349 /// This may panic if an overflow occurs.
1350 #[inline]
1351 #[track_caller]
1352 fn sub(self, rhs: Duration) -> Self::Output {
1353 self.checked_sub(rhs)
1354 .expect("resulting value is out of range")
1355 }
1356}
1357
1358impl Sub<StdDuration> for UtcDateTime {
1359 type Output = Self;
1360
1361 /// # Panics
1362 ///
1363 /// This may panic if an overflow occurs.
1364 #[inline]
1365 #[track_caller]
1366 fn sub(self, duration: StdDuration) -> Self::Output {
1367 Self::from_primitive(self.inner.sub(duration))
1368 }
1369}
1370
1371impl SubAssign<Duration> for UtcDateTime {
1372 /// # Panics
1373 ///
1374 /// This may panic if an overflow occurs.
1375 #[inline]
1376 #[track_caller]
1377 fn sub_assign(&mut self, rhs: Duration) {
1378 self.inner.sub_assign(rhs);
1379 }
1380}
1381
1382impl SubAssign<StdDuration> for UtcDateTime {
1383 /// # Panics
1384 ///
1385 /// This may panic if an overflow occurs.
1386 #[inline]
1387 #[track_caller]
1388 fn sub_assign(&mut self, rhs: StdDuration) {
1389 self.inner.sub_assign(rhs);
1390 }
1391}
1392
1393impl Sub for UtcDateTime {
1394 type Output = Duration;
1395
1396 #[inline]
1397 fn sub(self, rhs: Self) -> Self::Output {
1398 self.inner.sub(rhs.inner)
1399 }
1400}