@@ -237,55 +237,72 @@ features.
237
237
238
238
``` rust
239
239
use temporal_rs :: Temporal ;
240
- let today = Temporal :: now (). to_plain_date_iso (None ). unwrap ()
241
240
241
+ // Get today's date
242
+ let today = Temporal :: now (). to_plain_date_iso (None ). unwrap ()
242
243
```
243
244
244
245
#### Date operation's available
245
246
247
+ Temporal provides a nice API for working with date and date/time via
248
+ ` PlainDate ` and ` PlainDateTime ` .
249
+
246
250
``` 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 ();
249
256
250
257
// We can add a Duration.
251
258
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 ();
253
260
254
261
// 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 ();
256
265
257
266
// We can change the calendar
258
267
let tomorrow_japanese = tomorrow . with_calendar (Calendar :: JAPANESE );
259
268
260
269
// We can retrieve the calendar's RFC9557 string
261
- let tomorrow_string = tomorrow_japanese . to_string ( );
270
+ println! ( " {tomorrow_japanese} " );
262
271
```
263
272
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.
265
277
266
278
``` 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 };
269
281
270
282
// Parse a ZonedDateTime from utf8 bytes.
271
283
let zdt = ZonedDateTime :: from_utf8 (
272
284
b " 2025-03-01T11:16:10Z[America/Chicago][u-ca=iso8601]" ,
273
285
Disambiguation :: Compatible ,
274
286
OffsetDisambiguation :: Reject ,
275
- ). unwrap ();
287
+ )
288
+ . unwrap ();
276
289
277
290
// Change the time zone.
278
291
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 ();
280
293
281
294
// Or get the current ZonedDateTime
282
295
let today = Temporal :: now (). zoned_date_time_iso (None ). unwrap ();
283
296
284
297
// 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}" );
286
302
287
303
// Change the calendar
288
304
let today_coptic = today . with_calendar (Calendar :: COPTIC );
305
+ println! (" {today_coptic}" );
289
306
```
290
307
291
308
While we can extend these examples further, a more fun exercise for the
0 commit comments