Skip to content

Commit f1ff7fd

Browse files
committed
Test and update the examples
1 parent 4b3aa94 commit f1ff7fd

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

blog/temporal-release/index.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,55 +237,72 @@ features.
237237

238238
```rust
239239
use temporal_rs::Temporal;
240-
let today = Temporal::now().to_plain_date_iso(None).unwrap()
241240

241+
// Get today's date
242+
let today = Temporal::now().to_plain_date_iso(None).unwrap()
242243
```
243244

244245
#### Date operation's available
245246

247+
Temporal provides a nice API for working with date and date/time via
248+
`PlainDate` and `PlainDateTime`.
249+
246250
```rust
247-
use temporal_rs::{Temporal, Calendar, partial::PartialDuration, options::DifferenceSettings};
248-
let today = Temporal::now().to_plain_date_iso(None).unwrap();
251+
use std::convert::TryFrom;
252+
use temporal_rs::{Calendar, Temporal, options::DifferenceSettings, partial::PartialDuration};
253+
254+
// Get today's date
255+
let today = Temporal::now().plain_date_iso(None).unwrap();
249256

250257
// We can add a Duration.
251258
let partial = PartialDuration::empty().with_days(1);
252-
let tomorrow = today.add(Duration::from_partial_duration(partial).unwrap()).unwrap();
259+
let tomorrow = today.add(&partial.try_into().unwrap(), None).unwrap();
253260

254261
// We can get the difference two dates
255-
let diff = today.since(&tomorrow, DifferenceSettings::default()).unwrap();
262+
let diff = today
263+
.since(&tomorrow, DifferenceSettings::default())
264+
.unwrap();
256265

257266
// We can change the calendar
258267
let tomorrow_japanese = tomorrow.with_calendar(Calendar::JAPANESE);
259268

260269
// We can retrieve the calendar's RFC9557 string
261-
let tomorrow_string = tomorrow_japanese.to_string();
270+
println!("{tomorrow_japanese}");
262271
```
263272

264-
#### Working with `ZonedDateTime`
273+
#### Working with dates and time zones
274+
275+
You can also easily work with dates and time zones with the
276+
`ZonedDateTime` type.
265277

266278
```rust
267-
use temporal_rs::{ZonedDateTime, TimeZone, Temporal};
268-
use temporal_rs::options::{Disambiguation, OffsetDisambiguation, DifferenceSettings};
279+
use temporal_rs::options::{DifferenceSettings, Disambiguation, OffsetDisambiguation, Unit};
280+
use temporal_rs::{Calendar, Temporal, TimeZone, ZonedDateTime};
269281

270282
// Parse a ZonedDateTime from utf8 bytes.
271283
let zdt = ZonedDateTime::from_utf8(
272284
b"2025-03-01T11:16:10Z[America/Chicago][u-ca=iso8601]",
273285
Disambiguation::Compatible,
274286
OffsetDisambiguation::Reject,
275-
).unwrap();
287+
)
288+
.unwrap();
276289

277290
// Change the time zone.
278291
let zurich_zone = TimeZone::try_from_str("Europe/Zurich").unwrap();
279-
let zdt_zurich = zdt.with_timezone(zurich_zone).unwrap();
292+
let _zdt_zurich = zdt.with_timezone(zurich_zone).unwrap();
280293

281294
// Or get the current ZonedDateTime
282295
let today = Temporal::now().zoned_date_time_iso(None).unwrap();
283296

284297
// Difference the two `ZonedDateTime`s
285-
let diff = today.since(&zdt, DifferenceSettings::default()).unwrap();
298+
let mut options = DifferenceSettings::default();
299+
options.largest_unit = Some(Unit::Year);
300+
let diff = today.since(&zdt, options).unwrap();
301+
println!("{diff}");
286302

287303
// Change the calendar
288304
let today_coptic = today.with_calendar(Calendar::COPTIC);
305+
println!("{today_coptic}");
289306
```
290307

291308
While we can extend these examples further, a more fun exercise for the

0 commit comments

Comments
 (0)