Skip to content

Commit bc4cb9b

Browse files
committed
Update time to jiff
1 parent e7afa1c commit bc4cb9b

1 file changed

Lines changed: 125 additions & 5 deletions

File tree

content/rust/time.md

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
+++
22
title="Time"
33
date=2023-08-15
4-
updated = 2025-04-26
4+
updated = 2026-05-15
55
extra = { series = "Rust" }
66
taxonomies = { tags = ["Rust"] }
77
+++
88

99
# Standard Library
1010

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.
1314

1415
## Measure duration
1516

17+
**Note:**: duration does **NOT** keep counting if the computer goes to sleep (The time while the computer is suspended does not count).
18+
1619
```rust
1720
use std::time::{Duration, Instant};
1821
use std::thread;
@@ -52,10 +55,10 @@ match UNIX_EPOCH.elapsed() {
5255

5356
# Display current date/time
5457

58+
<details>
59+
<summary>See previous example that used chrono</summary>
5560
Source: <https://rust-lang-nursery.github.io/rust-cookbook/datetime/parse.html#display-formatted-date-and-time>
5661

57-
See [Formatting Syntax](#formatting-syntax) for more details on options regarding formatting.
58-
5962
```rust
6063
fn main() {
6164
let now: chrono::DateTime<chrono::Utc> = chrono::Utc::now();
@@ -66,8 +69,30 @@ fn main() {
6669
}
6770
```
6871

72+
---
73+
74+
</details>
75+
76+
See [Formatting Syntax](#formatting-syntax) for more details on options regarding formatting.
77+
78+
```rust
79+
fn main() {
80+
let now_zoned = jiff::Zoned::now();
81+
println!("Now in the detected timezone is: {now_zoned}");
82+
// Prints something like:
83+
// Now in the detected timezone is: 2026-05-15T13:30:31.200583642-04:00[America/New_York]
84+
85+
let now_civil = now_zoned.datetime();
86+
println!("civil::DateTime capture the date and time but not the timezone: {now_civil}");
87+
// Prints something like:
88+
// civil::DateTime capture the date and time but not the timezone: 2026-05-15T13:30:31.200583642
89+
}
90+
```
91+
6992
# Convert between timezones
7093

94+
<details>
95+
<summary>See previous example that used chrono</summary>
7196
Source: <https://stackoverflow.com/questions/28747694/how-do-i-convert-a-chrono-datetimeutc-instance-to-datetimelocal>
7297

7398
```rust
@@ -79,8 +104,30 @@ fn main() {
79104
}
80105
```
81106

107+
---
108+
109+
</details>
110+
111+
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+
fn main() -> Result<(), jiff::Error> {
115+
let local = jiff::Zoned::now();
116+
let utc = 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+
82126
# In WASM
83127

128+
<details>
129+
<summary>See previous example that used chrono</summary>
130+
84131
Source: <https://timclicks.dev/tip/convert-a-unix-timestamp-to-rust>
85132

86133
```rust
@@ -125,8 +172,28 @@ fn now_date_time_as_string_native_only() -> String {
125172
}
126173
```
127174

175+
---
176+
177+
</details>
178+
179+
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+
fn main() {
183+
println!("The date and time now is {}", now_date_time_as_string());
184+
}
185+
186+
fn now_date_time_as_string() -> String {
187+
let now = jiff::Zoned::now();
188+
now.strftime("%F %T").to_string()
189+
}
190+
```
191+
128192
# Formatting syntax
129193

194+
<details>
195+
<summary>See previous example that used chrono</summary>
196+
130197
See [docs.rs](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers) for full details
131198

132199
Excerpt from docs.rs
@@ -139,8 +206,27 @@ Excerpt from docs.rs
139206
| %a | Sun | Abbreviated weekday name. Always 3 letters. |
140207
| %A | Sunday | Full weekday name. Also accepts corresponding abbreviation in parsing. |
141208

209+
---
210+
211+
</details>
212+
213+
See [docs](https://docs.rs/jiff/latest/jiff/fmt/strtime/index.html#conversion-specifications)
214+
215+
Excerpt from docs.rs
216+
217+
| Spec. | Example | Description |
218+
| :----- | :-----------------------: | :---------------------------------------------------------------------------- |
219+
| %F | 2024-07-14 | Equivalent to %Y-%m-%d. |
220+
| %T | 23:30:59 | Equivalent to %H:%M:%S. |
221+
| %c | 2024 M07 14, Sun 17:31:59 | The date and clock time via [Custom][custom]. Supported when formatting only. |
222+
| %A, %a | Sunday, Sun | The full and abbreviated weekday, respectively. |
223+
142224
## Example usage
143225

226+
<details>
227+
228+
<summary>See previous example that used chrono</summary>
229+
144230
Source: <https://docs.rs/chrono/latest/chrono/offset/struct.Local.html#method.now>
145231

146232
```rust
@@ -184,3 +270,37 @@ now_fixed_offset: 2023-11-06 18:08:19.700585376 +00:00
184270
offset: +05:00
185271
now_with_offset: 2023-11-06 23:08:19.700591966 +05:00
186272
```
273+
274+
---
275+
276+
</details>
277+
278+
```rust
279+
fn main() -> Result<(), jiff::Error> {
280+
// Current local time
281+
let now = jiff::Zoned::now();
282+
println!("Formatted output: {}", now.strftime("%Y-%m-%d %H:%M:%S"));
283+
println!("now: {now}");
284+
285+
// Current local date
286+
let today = now.date();
287+
println!("today: {today}");
288+
289+
// Current time in some timezone (let's use the time in Dominica)
290+
// If you don't need to start with the local time you can start with a timestamp (See section above called "Convert between timezones")
291+
let dominica_time = now.in_tz("America/Dominica")?;
292+
println!("Dominica Time: {dominica_time}");
293+
Ok(())
294+
}
295+
```
296+
297+
Output:
298+
299+
```
300+
Formatted output: 2026-05-15 14:12:06
301+
now: 2026-05-15T14:12:06.779365104-04:00[America/New_York]
302+
today: 2026-05-15
303+
Dominica Time: 2026-05-15T14:12:06.779365104-04:00[America/Dominica]
304+
```
305+
306+
[custom]: https://docs.rs/jiff/latest/jiff/fmt/strtime/trait.Custom.html

0 commit comments

Comments
 (0)