@@ -23,7 +23,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
23
23
use crate :: format:: DelayedFormat ;
24
24
#[ cfg( feature = "unstable-locales" ) ]
25
25
use crate :: format:: Locale ;
26
- use crate :: format:: { parse, ParseError , ParseResult , Parsed , StrftimeItems } ;
26
+ use crate :: format:: { parse, parse_and_remainder , ParseError , ParseResult , Parsed , StrftimeItems } ;
27
27
use crate :: format:: { Fixed , Item } ;
28
28
use crate :: naive:: { Days , IsoWeek , NaiveDate , NaiveDateTime , NaiveTime } ;
29
29
#[ cfg( feature = "clock" ) ]
@@ -587,7 +587,8 @@ impl DateTime<FixedOffset> {
587
587
///
588
588
/// Note that this method *requires a timezone* in the string. See
589
589
/// [`NaiveDateTime::parse_from_str`]
590
- /// for a version that does not require a timezone in the to-be-parsed str.
590
+ /// for a version that does not require a timezone in `s`. The returned [`DateTime`] value will
591
+ /// have a [`FixedOffset`] reflecting the parsed timezone.
591
592
///
592
593
/// # Example
593
594
///
@@ -603,6 +604,40 @@ impl DateTime<FixedOffset> {
603
604
parse ( & mut parsed, s, StrftimeItems :: new ( fmt) ) ?;
604
605
parsed. to_datetime ( )
605
606
}
607
+
608
+ /// Parses a string from a user-specified format into a `DateTime<FixedOffset>` value, and a
609
+ /// slice with the remaining portion of the string.
610
+ ///
611
+ /// Note that this method *requires a timezone* in the input string. See
612
+ /// [`NaiveDateTime::parse_and_remainder`] for a version that does not
613
+ /// require a timezone in `s`. The returned [`DateTime`] value will have a [`FixedOffset`]
614
+ /// reflecting the parsed timezone.
615
+ ///
616
+ /// See the [`format::strftime` module](./format/strftime/index.html) for supported format
617
+ /// sequences.
618
+ ///
619
+ /// Similar to [`parse_from_str`](#method.parse_from_str).
620
+ ///
621
+ /// # Example
622
+ ///
623
+ /// ```rust
624
+ /// # use chrono::{DateTime, FixedOffset, TimeZone, NaiveDate};
625
+ /// let (datetime, remainder) = DateTime::parse_and_remainder(
626
+ /// "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap();
627
+ /// assert_eq!(
628
+ /// datetime,
629
+ /// FixedOffset::east_opt(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
630
+ /// );
631
+ /// assert_eq!(remainder, " trailing text");
632
+ /// ```
633
+ pub fn parse_and_remainder < ' a > (
634
+ s : & ' a str ,
635
+ fmt : & str ,
636
+ ) -> ParseResult < ( DateTime < FixedOffset > , & ' a str ) > {
637
+ let mut parsed = Parsed :: new ( ) ;
638
+ let remainder = parse_and_remainder ( & mut parsed, s, StrftimeItems :: new ( fmt) ) ?;
639
+ parsed. to_datetime ( ) . map ( |d| ( d, remainder) )
640
+ }
606
641
}
607
642
608
643
impl < Tz : TimeZone > DateTime < Tz >
0 commit comments