1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// ~/src/zoned.rs
//
// * Copyright (C) ParsiCore (parsidate) 2024-2025 <parsicore.dev@gmail.com>
// * Package : parsidate
// * License : Apache-2.0
// * Version : 1.7.1
// * URL : https://github.com/parsicore/parsidate
// * Sign: parsidate-20250607-fea13e856dcd-459c6e73c83e49e10162ee28b26ac7cd
//
//! Defines a timezone-aware Jalali (Parsi) date and time.
//!
//! This module is available when the **`timezone`** feature is enabled.
//! It provides the [`ZonedParsiDateTime`] struct, which is essential for representing
//! an exact moment in time in the Persian calendar, associated with a specific timezone.
//!
//! # Overview
//!
//! In programming, it's crucial to distinguish between:
//! 1. **Naive Time**: A "wall-clock" time without timezone information (e.g., "14:30").
//! This is ambiguous; "14:30" can mean different things in Tehran, London, or New York.
//! [`ParsiDateTime`](crate::ParsiDateTime) represents this.
//! 2. **Aware Time**: An exact, unambiguous instant in time (e.g., "December 31st, 1403 at
//! 14:30 in Asia/Tehran"). This corresponds to a single point on the global timeline.
//!
//! [`ZonedParsiDateTime`] represents this aware time. It is a robust wrapper around
//! `chrono::DateTime<Tz>` and is the recommended type for any application that
//! needs to handle dates and times across different regions, store timestamps, or perform
//! timezone-sensitive calculations. It correctly handles complex scenarios like
//! Daylight Saving Time (DST).
//!
//! # Usage
//!
//! To use `ZonedParsiDateTime`, you need a `TimeZone` provider. The most common choice
//! in the Rust ecosystem is the `chrono-tz` crate.
//!
//! First, add `parsidate` with the `timezone` feature and `chrono-tz` to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! parsidate = { version = "1.7.1", features = ["timezone"] }
//! chrono-tz = "0.8"
//! ```
//!
//! Then, you can create instances of `ZonedParsiDateTime`:
//!
//! ```
//! # #[cfg(feature = "timezone")] {
//! use parsidate::ZonedParsiDateTime;
//! use chrono_tz::Asia::Tehran;
//! use chrono_tz::America::New_York;
//!
//! // Create a specific moment in time in the Tehran timezone.
//! let pdt_tehran = ZonedParsiDateTime::new(1403, 9, 21, 12, 0, 0, Tehran).unwrap();
//!
//! // Convert this exact moment to the New York timezone.
//! let pdt_new_york = pdt_tehran.with_timezone(&New_York);
//!
//! println!("Tehran Time: {}", pdt_tehran); // Outputs: 1403/09/21 12:00:00 +0330
//! println!("New York Time: {}", pdt_new_york); // Outputs: 1403/09/21 03:30:00 -0500
//!
//! // Despite having different local times, they represent the same instant.
//! assert_eq!(pdt_tehran, pdt_new_york);
//! # }
//! ```
use crate::;
use ;
use Ordering;
use fmt;
use ;
/// Represents a timezone-aware date and time in the Persian (Jalali) calendar.
///
/// `ZonedParsiDateTime<Tz>` is a high-level wrapper around `chrono::DateTime<Tz>`,
/// providing a calendar-aware API for an unambiguous moment in time. It is the
/// timezone-aware counterpart to [`ParsiDateTime`].
///
/// This struct is the preferred way to handle time when correctness across different
/// geographical locations is required. It accounts for UTC offsets and Daylight
/// Saving Time (DST) rules automatically, ensuring that operations are accurate.
///
/// An instance of `ZonedParsiDateTime<Tz>` contains both the Jalali date and time
/// components and the specific `TimeZone` (`Tz`) it belongs to.
///
/// This struct is only available if the `timezone` feature is enabled. You will typically
/// use it with a `TimeZone` implementation from a crate like `chrono-tz`.
///
/// # Examples
///
/// Creating a new `ZonedParsiDateTime`:
///
/// ```
/// # #[cfg(feature = "timezone")] {
/// use parsidate::ZonedParsiDateTime;
/// use chrono_tz::Asia::Tehran;
///
/// // Create a ZonedParsiDateTime for November 5, 1403, at 2:30 PM in Tehran.
/// let tehran_time = ZonedParsiDateTime::new(1403, 8, 15, 14, 30, 0, Tehran).unwrap();
///
/// assert_eq!(tehran_time.year(), 1403);
/// assert_eq!(tehran_time.month(), 8);
/// assert_eq!(tehran_time.day(), 15);
/// assert_eq!(tehran_time.hour(), 14);
/// assert_eq!(tehran_time.timezone(), Tehran);
/// # }
/// ```
// --- Core Implementation ---
// --- Trait Implementations ---
/// Compares two `ZonedParsiDateTime` instances for equality.
///
/// Two instances are considered equal if they represent the exact same moment in
/// universal time, regardless of their timezone.
///
/// For example, `1403-01-01 12:00:00` in `Asia/Tehran` is **not equal** to
/// `1403-01-01 12:00:00` in `Europe/London`, but it **is equal** to
/// `1403-01-01 08:30:00` in `Europe/London`.
/// Implements the `Eq` trait, allowing `ZonedParsiDateTime` to be used in
/// collections that require total equality, like `HashMap`.
/// Provides partial ordering for `ZonedParsiDateTime`.
///
/// The comparison is performed on the absolute instant in time, not on the
/// local "wall-clock" time.
/// Provides total ordering for `ZonedParsiDateTime`.
///
/// Like `PartialEq`, the comparison is based on the absolute instant in time.
/// Adds a `chrono::Duration` to a `ZonedParsiDateTime` using the `+` operator.
///
/// This operation performs exact time arithmetic, correctly handling DST and
/// timezone transitions.
/// Subtracts a `chrono::Duration` from a `ZonedParsiDateTime` using the `-` operator.
///
/// This operation performs exact time arithmetic.
/// Calculates the `chrono::Duration` between two `ZonedParsiDateTime` instances.
///
/// This operation returns the exact duration of time that has elapsed between
/// two moments, regardless of their timezones.
/// Formats the `ZonedParsiDateTime` for display.
///
/// The default format is `YYYY/MM/DD HH:MM:SS OFFSET`, which combines the local Parsi
/// datetime with its UTC offset.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "timezone")] {
/// # use parsidate::ZonedParsiDateTime;
/// # use chrono_tz::Asia::Tehran;
/// let dt = ZonedParsiDateTime::new(1403, 8, 15, 14, 30, 0, Tehran).unwrap();
/// assert_eq!(dt.to_string(), "1403/08/15 14:30:00 +0330");
/// # }
/// ```
/// Formats the `ZonedParsiDateTime` for debugging.
///
/// The debug format provides a more detailed, developer-friendly representation
/// of the struct's contents, including the local Parsi datetime and the timezone name.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "timezone")] {
/// # use parsidate::ZonedParsiDateTime;
/// # use chrono_tz::Asia::Tehran;
/// let dt = ZonedParsiDateTime::new(1403, 8, 15, 9, 5, 0, Tehran).unwrap();
/// let debug_str = format!("{:?}", dt);
/// assert!(debug_str.contains("datetime: ParsiDateTime(1403/08/15 09:05:00)"));
/// assert!(debug_str.contains("timezone: Asia/Tehran"));
/// # }
/// ```