@@ -830,40 +830,11 @@ impl NaiveTime {
830830 }
831831
832832 /// Formats the time with the specified formatting items.
833- /// Otherwise it is the same as the ordinary [`format`](#method.format) method.
834- ///
835- /// The `Iterator` of items should be `Clone`able,
836- /// since the resulting `DelayedFormat` value may be formatted multiple times.
837- ///
838- /// # Example
839- ///
840- /// ```
841- /// use chrono::NaiveTime;
842- /// use chrono::format::strftime::StrftimeItems;
843- ///
844- /// let items = StrftimeItems::new("%H:%M:%S").parse()?;
845- /// let fmt = NaiveTime::formatter(&items)?;
846- /// let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
847- /// assert_eq!(t.format_with(&fmt).to_string(), "23:56:04");
848- /// assert_eq!(t.format_to_string("%H:%M:%S")?, "23:56:04");
849- /// # Ok::<(), chrono::ParseError>(())
850- /// ```
851- ///
852- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
853- ///
854- /// ```
855- /// # use chrono::NaiveTime;
856- /// # use chrono::format::strftime::StrftimeItems;
857- /// # let items = StrftimeItems::new("%H:%M:%S").parse()?;
858- /// # let fmt = NaiveTime::formatter(&items)?;
859- /// # let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
860- /// assert_eq!(format!("{}", t.format_with(&fmt)), "23:56:04");
861- /// # Ok::<(), chrono::ParseError>(())
862- /// ```
863833 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
864834 #[ cfg_attr( docsrs, doc( cfg( any( feature = "alloc" , feature = "std" ) ) ) ) ]
865835 #[ inline]
866836 #[ must_use]
837+ #[ deprecated( since = "0.4.27" , note = "Use NaiveTime::format_with() instead" ) ]
867838 pub fn format_with_items < ' a , I , B > ( & self , items : I ) -> DelayedFormat < I >
868839 where
869840 I : Iterator < Item = B > + Clone ,
@@ -873,44 +844,27 @@ impl NaiveTime {
873844 }
874845
875846 /// Formats the time with the specified format string.
876- /// See the [`format::strftime` module](../format/strftime/index.html)
877- /// on the supported escape sequences.
878847 ///
879- /// This returns a `DelayedFormat`,
880- /// which gets converted to a string only when actual formatting happens.
881- /// You may use the `to_string` method to get a `String`,
882- /// or just feed it into `print!` and other formatting macros.
883- /// (In this way it avoids the redundant memory allocation.)
848+ /// # Deprecated
884849 ///
885- /// A wrong format string does *not* issue an error immediately.
886- /// Rather, converting or formatting the `DelayedFormat` fails.
887- /// You are recommended to immediately use `DelayedFormat` for this reason.
850+ /// Use [`format_to_string`](#method.format_to_string) or [`format_with`](#method.format_with)
851+ /// instead.
888852 ///
889- /// # Example
853+ /// # Errors/panics
890854 ///
891- /// ```
892- /// use chrono::NaiveTime;
893- ///
894- /// let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
895- /// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
896- /// assert_eq!(t.format("%H:%M:%S%.6f").to_string(), "23:56:04.012345");
897- /// assert_eq!(t.format("%-I:%M %p").to_string(), "11:56 PM");
898- /// ```
855+ /// The `Display` implementation of the returned `DelayedFormat` can return an error if the
856+ /// format string is invalid. This goes against the [contract for `Display`][1], and causes a
857+ /// panic when used in combination with [`to_string`], [`println!`] and [`format!`].
899858 ///
900- /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
901- ///
902- /// ```
903- /// # use chrono::NaiveTime;
904- /// # let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
905- /// assert_eq!(format!("{}", t.format("%H:%M:%S")), "23:56:04");
906- /// assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345");
907- /// assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM");
908- /// ```
859+ /// [1]: https://doc.rust-lang.org/stable/std/fmt/index.html#formatting-traits
860+ /// [`to_string`]: ToString::to_string
909861 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
910862 #[ cfg_attr( docsrs, doc( cfg( any( feature = "alloc" , feature = "std" ) ) ) ) ]
911863 #[ inline]
912864 #[ must_use]
865+ #[ deprecated( since = "0.4.27" , note = "Use DateTime::format_to_string() instead" ) ]
913866 pub fn format < ' a > ( & self , fmt : & ' a str ) -> DelayedFormat < StrftimeItems < ' a > > {
867+ #[ allow( deprecated) ]
914868 self . format_with_items ( StrftimeItems :: new ( fmt) )
915869 }
916870
0 commit comments