Skip to content

Commit 496d76a

Browse files
committed
Constant width timestamps
Shows a fixed number of digits in the subsecond fraction in timestamps. This reverts to using `format_description!`, and will always show UTC time with an offset of `Z` and local time with an offset of `+/-HH:MM`. This should still comply with RFC 3339, which specifically calls out number of digits when talking about ordering timestamp strings. Closes #55
1 parent 6b3bb6d commit 496d76a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "simple_logger"
3-
version = "2.0.0"
3+
version = "2.1.0"
44
license = "MIT"
55
authors = ["Sam Clements <[email protected]>"]
66
description = "A logger that prints all messages with a readable output format"

src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ use colored::*;
3535
use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
3636
use std::collections::HashMap;
3737
#[cfg(feature = "timestamps")]
38-
use time::{format_description::well_known::Rfc3339, OffsetDateTime, UtcOffset};
38+
use time::{format_description::FormatItem, OffsetDateTime, UtcOffset};
39+
40+
#[cfg(feature = "timestamps")]
41+
const TIMESTAMP_FORMAT_OFFSET: &[FormatItem] = time::macros::format_description!(
42+
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory]:[offset_minute]"
43+
);
44+
45+
#[cfg(feature = "timestamps")]
46+
const TIMESTAMP_FORMAT_UTC: &[FormatItem] = time::macros::format_description!(
47+
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
48+
);
3949

4050
#[cfg(feature = "timestamps")]
4151
#[derive(PartialEq)]
@@ -420,9 +430,9 @@ impl Log for SimpleLogger {
420430
"the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
421431
"behaviour. See the time crate's documentation for more information. ",
422432
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
423-
)).format(&Rfc3339).unwrap()),
424-
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&Rfc3339).unwrap()),
425-
Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&Rfc3339).unwrap()),
433+
)).format(&TIMESTAMP_FORMAT_OFFSET).unwrap()),
434+
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT_UTC).unwrap()),
435+
Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&TIMESTAMP_FORMAT_UTC).unwrap()),
426436
}
427437

428438
#[cfg(not(feature = "timestamps"))]

0 commit comments

Comments
 (0)