Skip to content

Commit

Permalink
Add doctests for *::parse_and_remainder
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 12, 2023
1 parent 30a2f1f commit cad52c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/datetime/mod.rs
Expand Up @@ -618,6 +618,19 @@ impl DateTime<FixedOffset> {
/// 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,
Expand Down
10 changes: 10 additions & 0 deletions src/naive/date.rs
Expand Up @@ -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))?;
Expand Down
13 changes: 13 additions & 0 deletions src/naive/datetime/mod.rs
Expand Up @@ -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))?;
Expand Down
10 changes: 10 additions & 0 deletions src/naive/time/mod.rs
Expand Up @@ -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))?;
Expand Down

0 comments on commit cad52c6

Please sign in to comment.