You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/rust/time.md
+125-5Lines changed: 125 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,21 @@
1
1
+++
2
2
title="Time"
3
3
date=2023-08-15
4
-
updated = 2025-04-26
4
+
updated = 2026-05-15
5
5
extra = { series = "Rust" }
6
6
taxonomies = { tags = ["Rust"] }
7
7
+++
8
8
9
9
# Standard Library
10
10
11
-
Unless you're only measuring duration you're probably going to want to reach for chrono. See example below of how to measure duration.
12
-
Note that duration does **NOT** keep counting if the computer goes to sleep (The time while the computer is suspended does not count).
11
+
Unless you're only measuring duration you're probably going to want to reach for a create for more full date/time support. See example below of how to measure duration.
12
+
13
+
Chrono is now soft deprecated (See [message](https://github.com/chronotope/chrono/issues/1301#issuecomment-4321773137) from maintainer), I'm moving to [jiff](https://docs.rs/jiff/latest/) as it's very easy to use and fits well in all use cases I've wanted to use it.
13
14
14
15
## Measure duration
15
16
17
+
**Note:**: duration does **NOT** keep counting if the computer goes to sleep (The time while the computer is suspended does not count).
18
+
16
19
```rust
17
20
usestd::time::{Duration, Instant};
18
21
usestd::thread;
@@ -52,10 +55,10 @@ match UNIX_EPOCH.elapsed() {
52
55
53
56
# Display current date/time
54
57
58
+
<details>
59
+
<summary>See previous example that used chrono</summary>
See the documentation on [jiff::Zoned::now()](https://docs.rs/jiff/latest/jiff/struct.Zoned.html#method.now) if you don't need to start off in the current timezone as you can start with just a timestamp and avoid the lookup for local.
112
+
113
+
```rust
114
+
fnmain() ->Result<(), jiff::Error> {
115
+
letlocal=jiff::Zoned::now();
116
+
letutc=local.in_tz("UTC")?;
117
+
println!("Local time now is: {local}");
118
+
println!("UTC time now is: {utc}");
119
+
// Prints something like
120
+
// Local time now is: 2026-05-15T13:39:41.211169613-04:00[America/New_York]
121
+
// UTC time now is: 2026-05-15T17:39:41.211169613+00:00[UTC]
122
+
Ok(())
123
+
}
124
+
```
125
+
82
126
# In WASM
83
127
128
+
<details>
129
+
<summary>See previous example that used chrono</summary>
If you are targeting the browser then you only need to enable the `js` feature in jiff and the same code works. If you don't enable it you actually get a really good error message. I was pleasantly surprised. It wasn't a stack trace from deep in the bowels of the standard library.
180
+
181
+
```rust
182
+
fnmain() {
183
+
println!("The date and time now is {}", now_date_time_as_string());
184
+
}
185
+
186
+
fnnow_date_time_as_string() ->String {
187
+
letnow=jiff::Zoned::now();
188
+
now.strftime("%F %T").to_string()
189
+
}
190
+
```
191
+
128
192
# Formatting syntax
129
193
194
+
<details>
195
+
<summary>See previous example that used chrono</summary>
196
+
130
197
See [docs.rs](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers) for full details
0 commit comments