diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index b690f5bec8..699ba85bc7 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -618,6 +618,19 @@ impl DateTime { /// sequences. /// /// Similar to [`parse_from_str`](#method.parse_from_str). + /// + /// # Example + /// + /// ```rust + /// # use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate}; + /// let (datetime, remainder) = DateTime::parse_and_remainder( + /// "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap(); + /// assert_eq!( + /// datetime, + /// FixedOffset::east_opt(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() + /// ); + /// assert_eq!(remainder, " trailing text"); + /// ``` pub fn parse_and_remainder<'a>( s: &'a str, fmt: &str, diff --git a/src/naive/date.rs b/src/naive/date.rs index 2db9442088..2849a31f88 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -555,6 +555,16 @@ impl NaiveDate { /// on the supported escape sequences. /// /// Similar to [`parse_from_str`](#method.parse_from_str). + /// + /// # Example + /// + /// ```rust + /// # use chrono::{NaiveDate}; + /// let (date, remainder) = NaiveDate::parse_and_remainder( + /// "2015-02-18 trailing text", "%Y-%m-%d").unwrap(); + /// assert_eq!(date, NaiveDate::from_ymd_opt(2015, 2, 18).unwrap()); + /// assert_eq!(remainder, " trailing text"); + /// ``` pub fn parse_and_remainder<'a>(s: &'a str, fmt: &str) -> ParseResult<(NaiveDate, &'a str)> { let mut parsed = Parsed::new(); let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?; diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 1114f90c02..fdc1782dab 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -334,6 +334,19 @@ impl NaiveDateTime { /// on the supported escape sequences. /// /// Similar to [`parse_from_str`](#method.parse_from_str). + /// + /// # Example + /// + /// ```rust + /// # use chrono::{NaiveDate, NaiveDateTime}; + /// let (datetime, remainder) = NaiveDateTime::parse_and_remainder( + /// "2015-02-18 23:16:09 trailing text", "%Y-%m-%d %H:%M:%S").unwrap(); + /// assert_eq!( + /// datetime, + /// NaiveDate::from_ymd_opt(2015, 2, 18).unwrap().and_hms_opt(23, 16, 9).unwrap() + /// ); + /// assert_eq!(remainder, " trailing text"); + /// ``` pub fn parse_and_remainder<'a>(s: &'a str, fmt: &str) -> ParseResult<(NaiveDateTime, &'a str)> { let mut parsed = Parsed::new(); let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?; diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index a2e86f7552..689138f8cd 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -491,6 +491,16 @@ impl NaiveTime { /// on the supported escape sequences. /// /// Similar to [`parse_from_str`](#method.parse_from_str). + /// + /// # Example + /// + /// ```rust + /// # use chrono::{NaiveTime}; + /// let (time, remainder) = NaiveTime::parse_and_remainder( + /// "3h4m33s trailing text", "%-Hh%-Mm%-Ss").unwrap(); + /// assert_eq!(time, NaiveTime::from_hms_opt(3, 4, 33).unwrap()); + /// assert_eq!(remainder, " trailing text"); + /// ``` pub fn parse_and_remainder<'a>(s: &'a str, fmt: &str) -> ParseResult<(NaiveTime, &'a str)> { let mut parsed = Parsed::new(); let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?;