From 6eaa25934226ee5ff2038a34e6d32b5d69559c1d Mon Sep 17 00:00:00 2001
From: "Andres O. Vela" <andresovela@gmail.com>
Date: Thu, 30 May 2024 22:16:56 +0200
Subject: [PATCH] embassy-time: add timestamp features

---
 embassy-time/Cargo.toml | 10 +++++++++-
 embassy-time/src/lib.rs | 17 ++++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/embassy-time/Cargo.toml b/embassy-time/Cargo.toml
index ca7ad2d09..ce3f3d8c2 100644
--- a/embassy-time/Cargo.toml
+++ b/embassy-time/Cargo.toml
@@ -27,9 +27,17 @@ features = ["defmt", "std"]
 std = ["tick-hz-1_000_000", "critical-section/std"]
 wasm = ["dep:wasm-bindgen", "dep:js-sys", "dep:wasm-timer", "tick-hz-1_000_000"]
 
-## Display a timestamp of the number of seconds since startup next to defmt log messages
+## Display the time since startup next to defmt log messages.
+## At most 1 `defmt-timestamp-uptime-*` feature can be used.
+## `defmt-timestamp-uptime` is provided for backwards compatibility (provides the same format as `uptime-us`).
 ## To use this you must have a time driver provided.
 defmt-timestamp-uptime = ["defmt"]
+defmt-timestamp-uptime-s = ["defmt"]
+defmt-timestamp-uptime-ms = ["defmt"]
+defmt-timestamp-uptime-us = ["defmt"]
+defmt-timestamp-uptime-ts = ["defmt"]
+defmt-timestamp-uptime-tms = ["defmt"]
+defmt-timestamp-uptime-tus = ["defmt"]
 
 ## Create a `MockDriver` that can be manually advanced for testing purposes.
 mock-driver = ["tick-hz-1_000_000"]
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs
index 3c8575ee9..24ee51be7 100644
--- a/embassy-time/src/lib.rs
+++ b/embassy-time/src/lib.rs
@@ -46,5 +46,20 @@ pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000);
 pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000);
 pub(crate) const GCD_1G: u64 = gcd(TICK_HZ, 1_000_000_000);
 
-#[cfg(feature = "defmt-timestamp-uptime")]
+#[cfg(feature = "defmt-timestamp-uptime-s")]
+defmt::timestamp! {"{=u64}", Instant::now().as_secs() }
+
+#[cfg(feature = "defmt-timestamp-uptime-ms")]
+defmt::timestamp! {"{=u64:ms}", Instant::now().as_millis() }
+
+#[cfg(any(feature = "defmt-timestamp-uptime", feature = "defmt-timestamp-uptime-us"))]
 defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }
+
+#[cfg(feature = "defmt-timestamp-uptime-ts")]
+defmt::timestamp! {"{=u64:ts}", Instant::now().as_secs() }
+
+#[cfg(feature = "defmt-timestamp-uptime-tms")]
+defmt::timestamp! {"{=u64:tms}", Instant::now().as_millis() }
+
+#[cfg(feature = "defmt-timestamp-uptime-tus")]
+defmt::timestamp! {"{=u64:tus}", Instant::now().as_micros() }