Time
Chester Wyke August 15, 2023 Updated: April 15, 2025 #rust
rust (Series)
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Conditional Compilation
Install
Crate Serde
References
Cargo
Toolchain (Nightly)
String Formatting
Time
Crate Tokio
Publish Crate
rustfmt
Single file script
Snippets
CI
Pattern Type State
WASM
Create New Crate
Documentation
JSON
Pattern Builder
Testing
Working with collections of bytes
Thoughts about rust
Are we yet
OnceLock
Crate Actix Web
Stack Overflow
Crate Clap
Crate Poll Promise
Crate Insta
Tips
Create new egui project
Crate CSV
Crate egui
Iterators
Crate Tracing Subscriber
Regex
vscode
Enum Conversion
Macros
lettre
Google APIs
Standard Library
Unless you’re only measuring duration you’re probably going to want to reach for chrono. See example below of how to measure duration. Note that duration does NOT keep counting if the computer goes to sleep (The time while the computer is suspended does not count).
Measure duration
use ;
use thread;
Get duration since UNIX_EPOCH
Sources:
- https://doc.rust-lang.org/std/time/constant.UNIX_EPOCH.html
- https://stackoverflow.com/questions/26593387/how-can-i-get-the-current-time-in-milliseconds
use ;
match now.duration_since
match UNIX_EPOCH.elapsed
Display current date/time
See Formatting Syntax for more details on options regarding formatting.
Convert between timezones
In WASM
Source: https://timclicks.dev/tip/convert-a-unix-timestamp-to-rust
#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
package.edition = "2021" # Desirable to stop warning but not needed
chrono =
web-time = "1.0.0"
---
// Other version use web_time instead of std::time so it can work in WASM
// If you don't need WASM use this version
Formatting syntax
See docs.rs for full details
Excerpt from docs.rs
Spec. | Example | Description |
---|---|---|
%F | 2001-07-08 | Year-month-day format (ISO 8601). Same as %Y-%m-%d. |
%T | 00:34:60 | Hour-minute-second format. Same as %H:%M:%S. |
%c | Sun Jul 8 00:34:60 2001 | Locale’s date and time (e.g., Thu Mar 3 23:05:25 2005). |
%a | Sun | Abbreviated weekday name. Always 3 letters. |
%A | Sunday | Full weekday name. Also accepts corresponding abbreviation in parsing. |
Example usage
Source: https://docs.rs/chrono/latest/chrono/offset/struct.Local.html#method.now
use DateTime;
use FixedOffset;
use Local;
Output:
Formatted output: 2023-11-06 18:08:19
now: 2023-11-06 18:08:19.700476942 +00:00
today: 2023-11-06
now_fixed_offset: 2023-11-06 18:08:19.700581396 +00:00
now_fixed_offset: 2023-11-06 18:08:19.700585376 +00:00
offset: +05:00
now_with_offset: 2023-11-06 23:08:19.700591966 +05:00