Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StrftimeItems::new return Result #902

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 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 @@ -353,9 +354,8 @@ where
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
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 @@ -392,8 +392,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 @@ -669,4 +669,10 @@ mod tests {
date_sub -= TimeDelta::days(5);
assert_eq!(date_sub, date - TimeDelta::days(5));
}

#[test]
fn test_invalid_format() {
let result = Utc::now().format("%");
assert!(result.is_err(), "Invalid format string should be format error")
}
}
29 changes: 21 additions & 8 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,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 @@ -649,7 +649,7 @@ impl DateTime<FixedOffset> {
fmt: &str,
) -> ParseResult<(DateTime<FixedOffset>, &'a str)> {
let mut parsed = Parsed::new();
let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?;
let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt)?)?;
parsed.to_datetime().map(|d| (d, remainder))
}
}
Expand Down Expand Up @@ -825,15 +825,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]
#[must_use]
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 @@ -873,8 +872,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 @@ -1410,3 +1409,17 @@ 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());
}

#[test]
#[cfg(feature = "unstable-locales")]
fn test_invalid_format_localized() {
assert!(
Utc::now().format_localized("%", Locale::de_DE).is_err(),
"Invalid format string should be format error"
)
}
13 changes: 8 additions & 5 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,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 @@ -1347,25 +1350,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 @@ -1472,7 +1472,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