strftime: Improve "%+" documentation (#769)

This commit is contained in:
Mohammad AlSaleh 2022-08-14 14:56:27 +03:00 committed by GitHub
parent 45142764ab
commit 8275b13941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -113,6 +113,13 @@ Notes:
digits for seconds and colons in the time zone offset. digits for seconds and colons in the time zone offset.
<br> <br>
<br> <br>
This format also supports having a `Z` or `UTC` in place of `%:z`. They
are equivalent to `+00:00`.
<br>
<br>
Note that all `T`, `Z`, and `UTC` are parsed case-insensitively.
<br>
<br>
The typical `strftime` implementations have different (and locale-dependent) The typical `strftime` implementations have different (and locale-dependent)
formats for this specifier. While Chrono's format for `%+` is far more formats for this specifier. While Chrono's format for `%+` is far more
stable, it is best to avoid this specifier if you want to control the exact stable, it is best to avoid this specifier if you want to control the exact
@ -530,7 +537,7 @@ fn test_strftime_items() {
#[cfg(test)] #[cfg(test)]
#[test] #[test]
fn test_strftime_docs() { fn test_strftime_docs() {
use crate::{FixedOffset, TimeZone, Timelike}; use crate::{DateTime, FixedOffset, TimeZone, Timelike, Utc};
let dt = FixedOffset::east(34200).ymd(2001, 7, 8).and_hms_nano(0, 34, 59, 1_026_490_708); let dt = FixedOffset::east(34200).ymd(2001, 7, 8).and_hms_nano(0, 34, 59, 1_026_490_708);
@ -593,6 +600,24 @@ fn test_strftime_docs() {
// date & time specifiers // date & time specifiers
assert_eq!(dt.format("%c").to_string(), "Sun Jul 8 00:34:60 2001"); assert_eq!(dt.format("%c").to_string(), "Sun Jul 8 00:34:60 2001");
assert_eq!(dt.format("%+").to_string(), "2001-07-08T00:34:60.026490708+09:30"); assert_eq!(dt.format("%+").to_string(), "2001-07-08T00:34:60.026490708+09:30");
assert_eq!(
dt.with_timezone(&Utc).format("%+").to_string(),
"2001-07-07T15:04:60.026490708+00:00"
);
assert_eq!(
dt.with_timezone(&Utc),
DateTime::parse_from_str("2001-07-07T15:04:60.026490708Z", "%+").unwrap()
);
assert_eq!(
dt.with_timezone(&Utc),
DateTime::parse_from_str("2001-07-07T15:04:60.026490708UTC", "%+").unwrap()
);
assert_eq!(
dt.with_timezone(&Utc),
DateTime::parse_from_str("2001-07-07t15:04:60.026490708utc", "%+").unwrap()
);
assert_eq!( assert_eq!(
dt.with_nanosecond(1_026_490_000).unwrap().format("%+").to_string(), dt.with_nanosecond(1_026_490_000).unwrap().format("%+").to_string(),
"2001-07-08T00:34:60.026490+09:30" "2001-07-08T00:34:60.026490+09:30"