Skip to content

Commit 3acc4f9

Browse files
committed
display: clarify default precision in Display impls
Previously, the documentation for `Display` trait implementations on datetime types discussed precision, but omitted the default behavior. This PR documents the default behavior and makes the docs for `Timestamp`, `Zoned`, `civil::DateTime` and `civil::Time` a bit more consistent. Fixes #328
1 parent c9701c3 commit 3acc4f9

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Add a "lenient" mode for `strtime` formatting APIs that ignores most errors.
2222

2323
Bug fixes:
2424

25+
* [#328](https://github.com/BurntSushi/jiff/issues/328):
26+
Document default precision behavior of `Display` impls for datetime types.
2527
* [#340](https://github.com/BurntSushi/jiff/issues/340):
2628
Allow whitespace in more places in RFC 2822 parser (improves spec compliance).
2729
* [#346](https://github.com/BurntSushi/jiff/issues/346):

src/civil/datetime.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,13 +2527,32 @@ impl core::fmt::Debug for DateTime {
25272527

25282528
/// Converts a `DateTime` into an ISO 8601 compliant string.
25292529
///
2530-
/// Options currently supported:
2530+
/// # Forrmatting options supported
25312531
///
25322532
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2533-
/// of the fractional second component.
2533+
/// of the fractional second component. When not set, the minimum precision
2534+
/// required to losslessly render the value is used.
25342535
///
25352536
/// # Example
25362537
///
2538+
/// This shows the default rendering:
2539+
///
2540+
/// ```
2541+
/// use jiff::civil::date;
2542+
///
2543+
/// // No fractional seconds:
2544+
/// let dt = date(2024, 6, 15).at(7, 0, 0, 0);
2545+
/// assert_eq!(format!("{dt}"), "2024-06-15T07:00:00");
2546+
///
2547+
/// // With fractional seconds:
2548+
/// let dt = date(2024, 6, 15).at(7, 0, 0, 123_000_000);
2549+
/// assert_eq!(format!("{dt}"), "2024-06-15T07:00:00.123");
2550+
///
2551+
/// # Ok::<(), Box<dyn std::error::Error>>(())
2552+
/// ```
2553+
///
2554+
/// # Example: setting the precision
2555+
///
25372556
/// ```
25382557
/// use jiff::civil::date;
25392558
///

src/civil/time.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,16 +1884,33 @@ impl core::fmt::Debug for Time {
18841884

18851885
/// Converts a `Time` into an ISO 8601 compliant string.
18861886
///
1887-
/// Options currently supported:
1887+
/// # Forrmatting options supported
18881888
///
18891889
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
1890-
/// of the fractional second component.
1890+
/// of the fractional second component. When not set, the minimum precision
1891+
/// required to losslessly render the value is used.
18911892
///
18921893
/// # Example
18931894
///
18941895
/// ```
18951896
/// use jiff::civil::time;
18961897
///
1898+
/// // No fractional seconds:
1899+
/// let t = time(7, 0, 0, 0);
1900+
/// assert_eq!(format!("{t}"), "07:00:00");
1901+
///
1902+
/// // With fractional seconds:
1903+
/// let t = time(7, 0, 0, 123_000_000);
1904+
/// assert_eq!(format!("{t}"), "07:00:00.123");
1905+
///
1906+
/// # Ok::<(), Box<dyn std::error::Error>>(())
1907+
/// ```
1908+
///
1909+
/// # Example: setting the precision
1910+
///
1911+
/// ```
1912+
/// use jiff::civil::time;
1913+
///
18971914
/// let t = time(7, 0, 0, 123_000_000);
18981915
/// assert_eq!(format!("{t:.6}"), "07:00:00.123000");
18991916
/// // Precision values greater than 9 are clamped to 9.

src/timestamp.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2521,10 +2521,29 @@ impl core::fmt::Debug for Timestamp {
25212521
/// # Forrmatting options supported
25222522
///
25232523
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
2524-
/// of the fractional second component.
2524+
/// of the fractional second component. When not set, the minimum precision
2525+
/// required to losslessly render the value is used.
25252526
///
25262527
/// # Example
25272528
///
2529+
/// This shows the default rendering:
2530+
///
2531+
/// ```
2532+
/// use jiff::Timestamp;
2533+
///
2534+
/// // No fractional seconds.
2535+
/// let ts = Timestamp::from_second(1_123_456_789)?;
2536+
/// assert_eq!(format!("{ts}"), "2005-08-07T23:19:49Z");
2537+
///
2538+
/// // With fractional seconds.
2539+
/// let ts = Timestamp::new(1_123_456_789, 123_000_000)?;
2540+
/// assert_eq!(format!("{ts}"), "2005-08-07T23:19:49.123Z");
2541+
///
2542+
/// # Ok::<(), Box<dyn std::error::Error>>(())
2543+
/// ```
2544+
///
2545+
/// # Example: setting the precision
2546+
///
25282547
/// ```
25292548
/// use jiff::Timestamp;
25302549
///

src/zoned.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,13 +3209,32 @@ impl core::fmt::Debug for Zoned {
32093209

32103210
/// Converts a `Zoned` datetime into a RFC 9557 compliant string.
32113211
///
3212-
/// Options currently supported:
3212+
/// # Forrmatting options supported
32133213
///
32143214
/// * [`std::fmt::Formatter::precision`] can be set to control the precision
3215-
/// of the fractional second component.
3215+
/// of the fractional second component. When not set, the minimum precision
3216+
/// required to losslessly render the value is used.
32163217
///
32173218
/// # Example
32183219
///
3220+
/// This shows the default rendering:
3221+
///
3222+
/// ```
3223+
/// use jiff::civil::date;
3224+
///
3225+
/// // No fractional seconds:
3226+
/// let zdt = date(2024, 6, 15).at(7, 0, 0, 0).in_tz("US/Eastern")?;
3227+
/// assert_eq!(format!("{zdt}"), "2024-06-15T07:00:00-04:00[US/Eastern]");
3228+
///
3229+
/// // With fractional seconds:
3230+
/// let zdt = date(2024, 6, 15).at(7, 0, 0, 123_000_000).in_tz("US/Eastern")?;
3231+
/// assert_eq!(format!("{zdt}"), "2024-06-15T07:00:00.123-04:00[US/Eastern]");
3232+
///
3233+
/// # Ok::<(), Box<dyn std::error::Error>>(())
3234+
/// ```
3235+
///
3236+
/// # Example: setting the precision
3237+
///
32193238
/// ```
32203239
/// use jiff::civil::date;
32213240
///

0 commit comments

Comments
 (0)