1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
#[cfg(feature = "std")] use crate::PrimitiveDateTime; use crate::{ error, format::{parse, parse::AmPm, ParsedItems}, DeferredFormat, Duration, ParseResult, }; #[cfg(not(feature = "std"))] use alloc::{ borrow::ToOwned, string::{String, ToString}, }; use const_fn::const_fn; use core::{ cmp::Ordering, fmt::{self, Display}, num::NonZeroU8, ops::{Add, AddAssign, Sub, SubAssign}, time::Duration as StdDuration, }; use standback::convert::TryFrom; #[allow(unused_imports)] use standback::prelude::*; /// The number of nanoseconds in one day. pub(crate) const NANOS_PER_DAY: u64 = 24 * 60 * 60 * 1_000_000_000; /// The clock time within a given date. Nanosecond precision. /// /// All minutes are assumed to have exactly 60 seconds; no attempt is made to /// handle leap seconds (either positive or negative). /// /// When comparing two `Time`s, they are assumed to be in the same calendar /// date. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( feature = "serde", serde(from = "crate::serde::Time", into = "crate::serde::Time") )] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Time { #[allow(clippy::missing_docs_in_private_items)] pub(crate) hour: u8, #[allow(clippy::missing_docs_in_private_items)] pub(crate) minute: u8, #[allow(clippy::missing_docs_in_private_items)] pub(crate) second: u8, #[allow(clippy::missing_docs_in_private_items)] pub(crate) nanosecond: u32, } impl Time { /// Create a `Time` that is exactly midnight. /// /// ```rust /// # use time::{Time, time}; /// assert_eq!(Time::midnight(), time!(0:00)); /// ``` pub const fn midnight() -> Self { Time { hour: 0, minute: 0, second: 0, nanosecond: 0, } } /// Create a `Time` from the hour, minute, and second. /// /// ```rust /// # #![allow(deprecated)] /// # use time::Time; /// let time = Time::from_hms(1, 2, 3); /// assert_eq!(time.hour(), 1); /// assert_eq!(time.minute(), 2); /// assert_eq!(time.second(), 3); /// assert_eq!(time.nanosecond(), 0); /// ``` /// /// Panics if any component is not valid. /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms(24, 0, 0); // 24 isn't a valid hour. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms(0, 60, 0); // 60 isn't a valid minute. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms(0, 0, 60); // 60 isn't a valid second. /// ``` #[cfg(feature = "panicking-api")] #[cfg_attr(__time_02_docs, doc(cfg(feature = "panicking-api")))] #[deprecated( since = "0.2.3", note = "For times knowable at compile-time, use the `time!` macro. For situations where a \ value isn't known, use `Time::try_from_hms`." )] pub fn from_hms(hour: u8, minute: u8, second: u8) -> Self { assert_value_in_range!(hour in 0 => 23); assert_value_in_range!(minute in 0 => 59); assert_value_in_range!(second in 0 => 59); Self { hour, minute, second, nanosecond: 0, } } /// Attempt to create a `Time` from the hour, minute, and second. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms(1, 2, 3).is_ok()); /// ``` /// /// Returns `None` if any component is not valid. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms(24, 0, 0).is_err()); // 24 isn't a valid hour. /// assert!(Time::try_from_hms(0, 60, 0).is_err()); // 60 isn't a valid minute. /// assert!(Time::try_from_hms(0, 0, 60).is_err()); // 60 isn't a valid second. /// ``` /// /// This function is `const fn` when using rustc >= 1.46. #[const_fn("1.46")] pub const fn try_from_hms( hour: u8, minute: u8, second: u8, ) -> Result<Self, error::ComponentRange> { ensure_value_in_range!(hour in 0 => 23); ensure_value_in_range!(minute in 0 => 59); ensure_value_in_range!(second in 0 => 59); Ok(Self { hour, minute, second, nanosecond: 0, }) } /// Create a `Time` from the hour, minute, second, and millisecond. /// /// ```rust /// # #![allow(deprecated)] /// # use time::Time; /// let time = Time::from_hms_milli(1, 2, 3, 4); /// assert_eq!(time.hour(), 1); /// assert_eq!(time.minute(), 2); /// assert_eq!(time.second(), 3); /// assert_eq!(time.millisecond(), 4); /// assert_eq!(time.nanosecond(), 4_000_000); /// ``` /// /// Panics if any component is not valid. /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_milli(24, 0, 0, 0); // 24 isn't a valid hour. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_milli(0, 60, 0, 0); // 60 isn't a valid minute. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_milli(0, 0, 60, 0); // 60 isn't a valid second. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_milli(0, 0, 0, 1_000); // 1_000 isn't a valid millisecond. /// ``` #[cfg(feature = "panicking-api")] #[cfg_attr(__time_02_docs, doc(cfg(feature = "panicking-api")))] #[deprecated( since = "0.2.3", note = "For times knowable at compile-time, use the `time!` macro. For situations where a \ value isn't known, use `Time::try_from_hms_milli`." )] pub fn from_hms_milli(hour: u8, minute: u8, second: u8, millisecond: u16) -> Self { assert_value_in_range!(hour in 0 => 23); assert_value_in_range!(minute in 0 => 59); assert_value_in_range!(second in 0 => 59); assert_value_in_range!(millisecond in 0 => 999); Self { hour, minute, second, nanosecond: millisecond as u32 * 1_000_000, } } /// Attempt to create a `Time` from the hour, minute, second, and millisecond. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_milli(1, 2, 3, 4).is_ok()); /// ``` /// /// Returns `None` if any component is not valid. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_milli(24, 0, 0, 0).is_err()); // 24 isn't a valid hour. /// assert!(Time::try_from_hms_milli(0, 60, 0, 0).is_err()); // 60 isn't a valid minute. /// assert!(Time::try_from_hms_milli(0, 0, 60, 0).is_err()); // 60 isn't a valid second. /// assert!(Time::try_from_hms_milli(0, 0, 0, 1_000).is_err()); // 1_000 isn't a valid millisecond. /// ``` /// /// This function is `const fn` when using rustc >= 1.46. #[const_fn("1.46")] pub const fn try_from_hms_milli( hour: u8, minute: u8, second: u8, millisecond: u16, ) -> Result<Self, error::ComponentRange> { ensure_value_in_range!(hour in 0 => 23); ensure_value_in_range!(minute in 0 => 59); ensure_value_in_range!(second in 0 => 59); ensure_value_in_range!(millisecond in 0 => 999); Ok(Self { hour, minute, second, nanosecond: millisecond as u32 * 1_000_000, }) } /// Create a `Time` from the hour, minute, second, and microsecond. /// /// ```rust /// # #![allow(deprecated)] /// # use time::Time; /// let time = Time::from_hms_micro(1, 2, 3, 4); /// assert_eq!(time.hour(), 1); /// assert_eq!(time.minute(), 2); /// assert_eq!(time.second(), 3); /// assert_eq!(time.microsecond(), 4); /// assert_eq!(time.nanosecond(), 4_000); /// ``` /// /// Panics if any component is not valid. /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_micro(24, 0, 0, 0); // 24 isn't a valid hour. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_micro(0, 60, 0, 0); // 60 isn't a valid minute. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_micro(0, 0, 60, 0); // 60 isn't a valid second. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_micro(0, 0, 0, 1_000_000); // 1_000_000 isn't a valid microsecond. /// ``` #[cfg(feature = "panicking-api")] #[cfg_attr(__time_02_docs, doc(cfg(feature = "panicking-api")))] #[deprecated( since = "0.2.3", note = "For times knowable at compile-time, use the `time!` macro. For situations where a \ value isn't known, use `Time::try_from_hms_micro`." )] pub fn from_hms_micro(hour: u8, minute: u8, second: u8, microsecond: u32) -> Self { assert_value_in_range!(hour in 0 => 23); assert_value_in_range!(minute in 0 => 59); assert_value_in_range!(second in 0 => 59); assert_value_in_range!(microsecond in 0 => 999_999); Self { hour, minute, second, nanosecond: microsecond * 1_000, } } /// Attempt to create a `Time` from the hour, minute, second, and microsecond. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_micro(1, 2, 3, 4).is_ok()); /// ``` /// /// Returns `None` if any component is not valid. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_micro(24, 0, 0, 0).is_err()); // 24 isn't a valid hour. /// assert!(Time::try_from_hms_micro(0, 60, 0, 0).is_err()); // 60 isn't a valid minute. /// assert!(Time::try_from_hms_micro(0, 0, 60, 0).is_err()); // 60 isn't a valid second. /// assert!(Time::try_from_hms_micro(0, 0, 0, 1_000_000).is_err()); // 1_000_000 isn't a valid microsecond. /// ``` /// /// This function is `const fn` when using rustc >= 1.46. #[const_fn("1.46")] pub const fn try_from_hms_micro( hour: u8, minute: u8, second: u8, microsecond: u32, ) -> Result<Self, error::ComponentRange> { ensure_value_in_range!(hour in 0 => 23); ensure_value_in_range!(minute in 0 => 59); ensure_value_in_range!(second in 0 => 59); ensure_value_in_range!(microsecond in 0 => 999_999); Ok(Self { hour, minute, second, nanosecond: microsecond * 1_000, }) } /// Create a `Time` from the hour, minute, second, and nanosecond. /// /// ```rust /// # #![allow(deprecated)] /// # use time::Time; /// let time = Time::from_hms_nano(1, 2, 3, 4); /// assert_eq!(time.hour(), 1); /// assert_eq!(time.minute(), 2); /// assert_eq!(time.second(), 3); /// assert_eq!(time.nanosecond(), 4); /// ``` /// /// Panics if any component is not valid. /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_nano(24, 0, 0, 0); // 24 isn't a valid hour. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_nano(0, 60, 0, 0); // 60 isn't a valid minute. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_nano(0, 0, 60, 0); // 60 isn't a valid second. /// ``` /// /// ```rust,should_panic /// # #![allow(deprecated)] /// # use time::Time; /// Time::from_hms_nano(0, 0, 0, 1_000_000_000); // 1_000_000_000 isn't a valid nanosecond. /// ``` #[cfg(feature = "panicking-api")] #[cfg_attr(__time_02_docs, doc(cfg(feature = "panicking-api")))] #[deprecated( since = "0.2.3", note = "For times knowable at compile-time, use the `time!` macro. For situations where a \ value isn't known, use `Time::try_from_hms_nano`." )] pub fn from_hms_nano(hour: u8, minute: u8, second: u8, nanosecond: u32) -> Self { assert_value_in_range!(hour in 0 => 23); assert_value_in_range!(minute in 0 => 59); assert_value_in_range!(second in 0 => 59); assert_value_in_range!(nanosecond in 0 => 999_999_999); Self { hour, minute, second, nanosecond, } } /// Attempt to create a `Time` from the hour, minute, second, and nanosecond. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_nano(1, 2, 3, 4).is_ok()); /// ``` /// /// Returns `None` if any component is not valid. /// /// ```rust /// # use time::Time; /// assert!(Time::try_from_hms_nano(24, 0, 0, 0).is_err()); // 24 isn't a valid hour. /// assert!(Time::try_from_hms_nano(0, 60, 0, 0).is_err()); // 60 isn't a valid minute. /// assert!(Time::try_from_hms_nano(0, 0, 60, 0).is_err()); // 60 isn't a valid second. /// assert!(Time::try_from_hms_nano(0, 0, 0, 1_000_000_000).is_err()); // 1_000_000_000 isn't a valid nanosecond. /// ``` /// /// This function is `const fn` when using rustc >= 1.46. #[const_fn("1.46")] pub const fn try_from_hms_nano( hour: u8, minute: u8, second: u8, nanosecond: u32, ) -> Result<Self, error::ComponentRange> { ensure_value_in_range!(hour in 0 => 23); ensure_value_in_range!(minute in 0 => 59); ensure_value_in_range!(second in 0 => 59); ensure_value_in_range!(nanosecond in 0 => 999_999_999); Ok(Self { hour, minute, second, nanosecond, }) } /// Create a `Time` representing the current time (UTC). /// /// ```rust /// # #![allow(deprecated)] /// # use time::Time; /// println!("{:?}", Time::now()); /// ``` #[cfg(feature = "std")] #[cfg_attr(__time_02_docs, doc(cfg(feature = "std")))] #[deprecated( since = "0.2.7", note = "This method returns a value that assumes an offset of UTC." )] #[allow(deprecated)] pub fn now() -> Self { PrimitiveDateTime::now().time() } /// Get the clock hour. /// /// The returned value will always be in the range `0..24`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00:00).hour(), 0); /// assert_eq!(time!(23:59:59).hour(), 23); /// ``` pub const fn hour(self) -> u8 { self.hour } /// Get the minute within the hour. /// /// The returned value will always be in the range `0..60`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00:00).minute(), 0); /// assert_eq!(time!(23:59:59).minute(), 59); /// ``` pub const fn minute(self) -> u8 { self.minute } /// Get the second within the minute. /// /// The returned value will always be in the range `0..60`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00:00).second(), 0); /// assert_eq!(time!(23:59:59).second(), 59); /// ``` pub const fn second(self) -> u8 { self.second } /// Get the milliseconds within the second. /// /// The returned value will always be in the range `0..1_000`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00).millisecond(), 0); /// assert_eq!(time!(23:59:59.999).millisecond(), 999); /// ``` pub const fn millisecond(self) -> u16 { (self.nanosecond() / 1_000_000) as u16 } /// Get the microseconds within the second. /// /// The returned value will always be in the range `0..1_000_000`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00).microsecond(), 0); /// assert_eq!(time!(23:59:59.999_999).microsecond(), 999_999); /// ``` pub const fn microsecond(self) -> u32 { self.nanosecond() / 1_000 } /// Get the nanoseconds within the second. /// /// The returned value will always be in the range `0..1_000_000_000`. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00).nanosecond(), 0); /// assert_eq!(time!(23:59:59.999_999_999).nanosecond(), 999_999_999); /// ``` pub const fn nanosecond(self) -> u32 { self.nanosecond } /// Get the number of nanoseconds since midnight. pub(crate) const fn nanoseconds_since_midnight(self) -> u64 { self.hour() as u64 * 60 * 60 * 1_000_000_000 + self.minute() as u64 * 60 * 1_000_000_000 + self.second() as u64 * 1_000_000_000 + self.nanosecond() as u64 } /// Create a `Time` from the number of nanoseconds since midnight. pub(crate) const fn from_nanoseconds_since_midnight(nanosecond: u64) -> Self { Self { hour: (nanosecond / 1_000_000_000 / 60 / 60 % 24) as u8, minute: (nanosecond / 1_000_000_000 / 60 % 60) as u8, second: (nanosecond / 1_000_000_000 % 60) as u8, nanosecond: (nanosecond % 1_000_000_000) as u32, } } } /// Methods that allow formatting the `Time`. impl Time { /// Format the `Time` using the provided string. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00).format("%r"), "12:00:00 am"); /// ``` pub fn format(self, format: impl AsRef<str>) -> String { self.lazy_format(format).to_string() } /// Format the `Time` using the provided string. /// /// ```rust /// # use time::time; /// assert_eq!(time!(0:00).lazy_format("%r").to_string(), "12:00:00 am"); /// ``` pub fn lazy_format(self, format: impl AsRef<str>) -> impl Display { DeferredFormat::new(format.as_ref()) .with_time(self) .to_owned() } /// Attempt to parse a `Time` using the provided string. /// /// ```rust /// # use time::{Time, time}; /// assert_eq!( /// Time::parse("0:00:00", "%T"), /// Ok(time!(0:00)) /// ); /// assert_eq!( /// Time::parse("23:59:59", "%T"), /// Ok(time!(23:59:59)) /// ); /// assert_eq!( /// Time::parse("12:00:00 am", "%r"), /// Ok(time!(0:00)) /// ); /// assert_eq!( /// Time::parse("12:00:00 pm", "%r"), /// Ok(time!(12:00)) /// ); /// assert_eq!( /// Time::parse("11:59:59 pm", "%r"), /// Ok(time!(23:59:59)) /// ); /// ``` pub fn parse(s: impl AsRef<str>, format: impl AsRef<str>) -> ParseResult<Self> { Self::try_from_parsed_items(parse(s.as_ref(), &format.into())?) } /// Given the items already parsed, attempt to create a `Time`. pub(crate) fn try_from_parsed_items(items: ParsedItems) -> ParseResult<Self> { macro_rules! items { ($($item:ident),* $(,)?) => { ParsedItems { $($item: Some($item)),*, .. } }; } /// Convert a 12-hour time to a 24-hour time. #[allow(clippy::missing_const_for_fn)] // internal fn in non-const outer fn fn hour_12_to_24(hour: NonZeroU8, am_pm: AmPm) -> u8 { use AmPm::{AM, PM}; match (hour.get(), am_pm) { (12, AM) => 0, (12, PM) => 12, (h, AM) => h, (h, PM) => h + 12, } } match items { items!(hour_24, minute, second, nanosecond) => { Self::try_from_hms_nano(hour_24, minute, second, nanosecond).map_err(Into::into) } items!(hour_12, minute, second, nanosecond, am_pm) => { Self::try_from_hms_nano(hour_12_to_24(hour_12, am_pm), minute, second, nanosecond) .map_err(Into::into) } items!(hour_24, minute, second) => { Self::try_from_hms(hour_24, minute, second).map_err(Into::into) } items!(hour_12, minute, second, am_pm) => { Self::try_from_hms(hour_12_to_24(hour_12, am_pm), minute, second) .map_err(Into::into) } items!(hour_24, minute) => Self::try_from_hms(hour_24, minute, 0).map_err(Into::into), items!(hour_12, minute, am_pm) => { Self::try_from_hms(hour_12_to_24(hour_12, am_pm), minute, 0).map_err(Into::into) } items!(hour_24) => Self::try_from_hms(hour_24, 0, 0).map_err(Into::into), items!(hour_12, am_pm) => { Self::try_from_hms(hour_12_to_24(hour_12, am_pm), 0, 0).map_err(Into::into) } _ => Err(error::Parse::InsufficientInformation), } } } impl Display for Time { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use crate::format::{time, Padding}; time::fmt_H(f, *self, Padding::None)?; f.write_str(":")?; time::fmt_M(f, *self, Padding::Zero)?; if self.second != 0 || self.nanosecond != 0 { f.write_str(":")?; time::fmt_S(f, *self, Padding::Zero)?; } if self.nanosecond != 0 { f.write_str(".")?; if self.nanosecond % 1_000_000 == 0 { write!(f, "{:03}", self.nanosecond / 1_000_000)?; } else if self.nanosecond % 1_000 == 0 { write!(f, "{:06}", self.nanosecond / 1_000)?; } else { write!(f, "{:09}", self.nanosecond)?; } } Ok(()) } } impl Add<Duration> for Time { type Output = Self; /// Add the sub-day time of the `Duration` to the `Time`. Wraps on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// assert_eq!(time!(12:00) + 2.hours(), time!(14:00)); /// assert_eq!(time!(0:00:01) + (-2).seconds(), time!(23:59:59)); /// ``` fn add(self, duration: Duration) -> Self::Output { Self::from_nanoseconds_since_midnight( self.nanoseconds_since_midnight() + duration .whole_nanoseconds() .rem_euclid(NANOS_PER_DAY as i128) as u64, ) } } impl Add<StdDuration> for Time { type Output = Self; /// Add the sub-day time of the `std::time::Duration` to the `Time`. Wraps /// on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// assert_eq!(time!(12:00) + 2.std_hours(), time!(14:00)); /// assert_eq!(time!(23:59:59) + 2.std_seconds(), time!(0:00:01)); /// ``` fn add(self, duration: StdDuration) -> Self::Output { self + Duration::try_from(duration) .expect("overflow converting `core::time::Duration` to `time::Duration`") } } impl AddAssign<Duration> for Time { /// Add the sub-day time of the `Duration` to the existing `Time`. Wraps on /// overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// let mut time = time!(12:00); /// time += 2.hours(); /// assert_eq!(time, time!(14:00)); /// /// let mut time = time!(0:00:01); /// time += (-2).seconds(); /// assert_eq!(time, time!(23:59:59)); /// ``` fn add_assign(&mut self, duration: Duration) { *self = *self + duration; } } impl AddAssign<StdDuration> for Time { /// Add the sub-day time of the `std::time::Duration` to the existing /// `Time`. Wraps on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// let mut time = time!(12:00); /// time += 2.std_hours(); /// assert_eq!(time, time!(14:00)); /// /// let mut time = time!(23:59:59); /// time += 2.std_seconds(); /// assert_eq!(time, time!(0:00:01)); /// ``` fn add_assign(&mut self, duration: StdDuration) { *self = *self + duration; } } impl Sub<Duration> for Time { type Output = Self; /// Subtract the sub-day time of the `Duration` from the `Time`. Wraps on /// overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// assert_eq!( /// time!(14:00) - 2.hours(), /// time!(12:00) /// ); /// assert_eq!( /// time!(23:59:59) - (-2).seconds(), /// time!(0:00:01) /// ); /// ``` fn sub(self, duration: Duration) -> Self::Output { self + -duration } } impl Sub<StdDuration> for Time { type Output = Self; /// Subtract the sub-day time of the `std::time::Duration` from the `Time`. /// Wraps on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// assert_eq!(time!(14:00) - 2.std_hours(), time!(12:00)); /// assert_eq!(time!(0:00:01) - 2.std_seconds(), time!(23:59:59)); /// ``` fn sub(self, duration: StdDuration) -> Self::Output { self - Duration::try_from(duration) .expect("overflow converting `core::time::Duration` to `time::Duration`") } } impl SubAssign<Duration> for Time { /// Subtract the sub-day time of the `Duration` from the existing `Time`. /// Wraps on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// let mut time = time!(14:00); /// time -= 2.hours(); /// assert_eq!(time, time!(12:00)); /// /// let mut time = time!(23:59:59); /// time -= (-2).seconds(); /// assert_eq!(time, time!(0:00:01)); /// ``` fn sub_assign(&mut self, duration: Duration) { *self = *self - duration; } } impl SubAssign<StdDuration> for Time { /// Subtract the sub-day time of the `std::time::Duration` from the existing /// `Time`. Wraps on overflow. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// let mut time = time!(14:00); /// time -= 2.std_hours(); /// assert_eq!(time, time!(12:00)); /// /// let mut time = time!(0:00:01); /// time -= 2.std_seconds(); /// assert_eq!(time, time!(23:59:59)); /// ``` fn sub_assign(&mut self, duration: StdDuration) { *self = *self - duration; } } impl Sub<Time> for Time { type Output = Duration; /// Subtract two `Time`s, returning the `Duration` between. This assumes /// both `Time`s are in the same calendar day. /// /// ```rust /// # use time::prelude::*; /// # use time_macros::time; /// assert_eq!(time!(0:00) - time!(0:00), 0.seconds()); /// assert_eq!(time!(1:00) - time!(0:00), 1.hours()); /// assert_eq!(time!(0:00) - time!(1:00), (-1).hours()); /// assert_eq!(time!(0:00) - time!(23:00), (-23).hours()); /// ``` fn sub(self, rhs: Self) -> Self::Output { Duration::nanoseconds( self.nanoseconds_since_midnight() as i64 - rhs.nanoseconds_since_midnight() as i64, ) } } impl PartialOrd for Time { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.cmp(other).into() } } impl Ord for Time { fn cmp(&self, other: &Self) -> Ordering { self.hour .cmp(&other.hour) .then_with(|| self.minute.cmp(&other.minute)) .then_with(|| self.second.cmp(&other.second)) .then_with(|| self.nanosecond.cmp(&other.nanosecond)) } } #[cfg(test)] mod test { use super::*; #[test] fn nanoseconds_since_midnight() -> crate::Result<()> { let time = Time::midnight(); assert_eq!(time.nanoseconds_since_midnight(), 0); assert_eq!(Time::from_nanoseconds_since_midnight(0), time); let time = Time::try_from_hms_nano(23, 59, 59, 999_999_999)?; assert_eq!(time.nanoseconds_since_midnight(), NANOS_PER_DAY - 1); assert_eq!( Time::from_nanoseconds_since_midnight(NANOS_PER_DAY - 1), time ); Ok(()) } }