Skip to content

Commit

Permalink
Make creating a SrtftimeItems return a Result
Browse files Browse the repository at this point in the history
  • Loading branch information
jaggededgedjustice committed Dec 13, 2022
1 parent 74ca661 commit 25e8396
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 449 deletions.
9 changes: 5 additions & 4 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
use crate::offset::{TimeZone, Utc};
use crate::time_delta::TimeDelta;
use crate::DateTime;
use crate::ParseError;
use crate::{Datelike, Weekday};

/// ISO 8601 calendar date with time zone.
Expand Down Expand Up @@ -329,8 +330,8 @@ where
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
self.format_with_items(StrftimeItems::new(fmt))
pub fn format<'a>(&self, fmt: &'a str) -> Result<DelayedFormat<StrftimeItems<'a>>, ParseError> {
Ok(self.format_with_items(StrftimeItems::new(fmt)?))
}

/// Formats the date with the specified formatting items and locale.
Expand Down Expand Up @@ -365,8 +366,8 @@ where
&self,
fmt: &'a str,
locale: Locale,
) -> DelayedFormat<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale)
) -> Result<DelayedFormat<StrftimeItems<'a>>, ParseError> {
Ok(self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale)?, locale))
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl DateTime<FixedOffset> {
/// ```
pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
let mut parsed = Parsed::new();
parse(&mut parsed, s, StrftimeItems::new(fmt))?;
parse(&mut parsed, s, StrftimeItems::new(fmt)?)?;
parsed.to_datetime()
}
}
Expand Down Expand Up @@ -753,14 +753,14 @@ where
/// use chrono::prelude::*;
///
/// let date_time: DateTime<Utc> = Utc.with_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M").unwrap());
/// assert_eq!(formatted, "02/04/2017 12:50");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
self.format_with_items(StrftimeItems::new(fmt))
pub fn format<'a>(&self, fmt: &'a str) -> Result<DelayedFormat<StrftimeItems<'a>>, ParseError> {
Ok(self.format_with_items(StrftimeItems::new(fmt)?))
}

/// Formats the combined date and time with the specified formatting items and locale.
Expand Down Expand Up @@ -798,8 +798,8 @@ where
&self,
fmt: &'a str,
locale: Locale,
) -> DelayedFormat<StrftimeItems<'a>> {
self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale), locale)
) -> Result<DelayedFormat<StrftimeItems<'a>>, ParseError> {
Ok(self.format_localized_with_items(StrftimeItems::new_with_locale(fmt, locale)?, locale))
}
}

Expand Down Expand Up @@ -1331,3 +1331,8 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(
assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
}

#[test]
fn test_invalid_format() {
assert!(Utc::now().format("%").is_err());
}
13 changes: 8 additions & 5 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ fn test_to_string_round_trip_with_local() {
fn test_datetime_format_with_local() {
// if we are not around the year boundary, local and UTC date should have the same year
let dt = Local::now().with_month(5).unwrap();
assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&Utc).format("%Y").to_string());
assert_eq!(
dt.format("%Y").unwrap().to_string(),
dt.with_timezone(&Utc).format("%Y").unwrap().to_string()
);
}

#[test]
Expand Down Expand Up @@ -579,25 +582,25 @@ fn test_datetime_format_alignment() {
let datetime = Utc.with_ymd_and_hms(2007, 1, 2, 0, 0, 0).unwrap();

// Item::Literal
let percent = datetime.format("%%");
let percent = datetime.format("%%").unwrap();
assert_eq!(" %", format!("{:>3}", percent));
assert_eq!("% ", format!("{:<3}", percent));
assert_eq!(" % ", format!("{:^3}", percent));

// Item::Numeric
let year = datetime.format("%Y");
let year = datetime.format("%Y").unwrap();
assert_eq!(" 2007", format!("{:>6}", year));
assert_eq!("2007 ", format!("{:<6}", year));
assert_eq!(" 2007 ", format!("{:^6}", year));

// Item::Fixed
let tz = datetime.format("%Z");
let tz = datetime.format("%Z").unwrap();
assert_eq!(" UTC", format!("{:>5}", tz));
assert_eq!("UTC ", format!("{:<5}", tz));
assert_eq!(" UTC ", format!("{:^5}", tz));

// [Item::Numeric, Item::Space, Item::Literal, Item::Space, Item::Numeric]
let ymd = datetime.format("%Y %B %d");
let ymd = datetime.format("%Y %B %d").unwrap();
let ymd_formatted = "2007 January 02";
assert_eq!(format!(" {}", ymd_formatted), format!("{:>17}", ymd));
assert_eq!(format!("{} ", ymd_formatted), format!("{:<17}", ymd));
Expand Down
2 changes: 1 addition & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//!
//! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();
//!
//! let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S"));
//! let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S").unwrap());
//! assert_eq!(formatted, "2020-11-10 00:01:32");
//!
//! let parsed = Utc.datetime_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?;
Expand Down
2 changes: 1 addition & 1 deletion src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ fn parse_rfc850() {
let dt = Utc.with_ymd_and_hms(1994, 11, 6, 8, 49, 37).unwrap();

// Check that the format is what we expect
assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
assert_eq!(dt.format(RFC850_FMT).unwrap().to_string(), dt_str);

// Check that it parses correctly
assert_eq!(Ok(dt), Utc.datetime_from_str("Sunday, 06-Nov-94 08:49:37 GMT", RFC850_FMT));
Expand Down
Loading

0 comments on commit 25e8396

Please sign in to comment.