utc2k 0.19.1

A fast and lean UTC date/time library concerned only with happenings in this century (2000-2099).
Documentation
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
/*!
# UTC2K: Local Dates!
*/

use crate::{
	DateChar,
	DAY_IN_SECONDS,
	FmtUtc2k,
	HOUR_IN_SECONDS,
	macros,
	MINUTE_IN_SECONDS,
	Month,
	Utc2k,
	Weekday,
};
use std::{
	borrow::Cow,
	cmp::Ordering,
	fmt,
	hash,
	num::NonZeroI32,
	sync::OnceLock,
};
use super::Abacus;
use tz::timezone::TimeZone;



/// # Parsed Timezone Details.
static TZ: OnceLock<Option<TimeZone>> = OnceLock::new();



#[derive(Debug, Clone, Copy)]
/// # Formatted Local ~~UTC~~2K.
///
/// This is the formatted companion to [`Local2k`]. You can use it to obtain a
/// string version of the date, print it, etc.
///
/// While this acts essentially as a glorified `String`, it is sized exactly
/// and therefore requires less memory to represent. It also implements `Copy`.
///
/// It follows the simple Unix date format of `YYYY-MM-DD hh:mm:ss`.
///
/// Speaking of, you can obtain an `&str` using `AsRef<str>`,
/// `Borrow<str>`, or [`FmtLocal2k::as_str`].
///
/// If you only want the date or time half, call [`FmtLocal2k::date`] or
/// [`FmtLocal2k::time`] respectively.
///
/// See [`Local2k`] for limitations and gotchas.
pub struct FmtLocal2k {
	/// # Date/Time (w/ `offset`)
	inner: FmtUtc2k,

	/// # Local Offset (Seconds).
	offset: Option<NonZeroI32>,
}

impl AsRef<[u8]> for FmtLocal2k {
	#[inline]
	fn as_ref(&self) -> &[u8] { self.as_bytes() }
}

macros::as_ref_borrow_cast!(FmtLocal2k: as_str str);

impl fmt::Display for FmtLocal2k {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		<FmtUtc2k as fmt::Display>::fmt(&self.inner, f)
	}
}

impl Eq for FmtLocal2k {}

impl From<Local2k> for FmtLocal2k {
	#[inline]
	fn from(src: Local2k) -> Self { Self::from_local2k(src) }
}

impl From<FmtLocal2k> for String {
	#[inline]
	fn from(src: FmtLocal2k) -> Self { src.as_str().to_owned() }
}

impl hash::Hash for FmtLocal2k {
	#[inline]
	fn hash<H: hash::Hasher>(&self, state: &mut H) {
		<Local2k as hash::Hash>::hash(&Local2k::from_fmtlocal2k(*self), state);
	}
}

impl Ord for FmtLocal2k {
	#[inline]
	fn cmp(&self, other: &Self) -> Ordering {
		if self.offset == other.offset { self.inner.cmp(&other.inner) }
		else {
			Local2k::from_fmtlocal2k(*self).cmp(&Local2k::from_fmtlocal2k(*other))
		}
	}
}

impl PartialEq for FmtLocal2k {
	#[inline]
	fn eq(&self, other: &Self) -> bool {
		if self.offset == other.offset { self.inner == other.inner }
		else {
			Local2k::from_fmtlocal2k(*self) == Local2k::from_fmtlocal2k(*other)
		}
	}
}

impl PartialEq<str> for FmtLocal2k {
	#[inline]
	fn eq(&self, other: &str) -> bool { self.as_str() == other }
}
impl PartialEq<FmtLocal2k> for str {
	#[inline]
	fn eq(&self, other: &FmtLocal2k) -> bool { <FmtLocal2k as PartialEq<Self>>::eq(other, self) }
}

/// # Helper: Reciprocal `PartialEq`.
macro_rules! fmt_eq {
	($($ty:ty)+) => ($(
		impl PartialEq<$ty> for FmtLocal2k {
			#[inline]
			fn eq(&self, other: &$ty) -> bool { <Self as PartialEq<str>>::eq(self, other) }
		}
		impl PartialEq<FmtLocal2k> for $ty {
			#[inline]
			fn eq(&self, other: &FmtLocal2k) -> bool { <FmtLocal2k as PartialEq<str>>::eq(other, self) }
		}
	)+);
}
fmt_eq! { &str &String String &Cow<'_, str> Cow<'_, str> &Box<str> Box<str> }

impl PartialOrd for FmtLocal2k {
	#[inline]
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}

/// ## Instantiation.
impl FmtLocal2k {
	#[inline]
	#[must_use]
	/// # Now.
	///
	/// Create a new instance representing the current local time.
	///
	/// ```
	/// use utc2k::{FmtLocal2k, Local2k};
	///
	/// // Equivalent.
	/// assert_eq!(
	///     FmtLocal2k::now(),
	///     FmtLocal2k::from(Local2k::now()),
	/// );
	/// ```
	pub fn now() -> Self { Self::from_local2k(Local2k::now()) }
}

/// ## Getters.
impl FmtLocal2k {
	#[inline]
	#[must_use]
	/// # As Bytes.
	///
	/// Return a byte string slice in `YYYY-MM-DD hh:mm:ss` format.
	///
	/// A byte slice can also be obtained using [`FmtLocal2k::as_ref`].
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let fmt = Local2k::from(Utc2k::MAX).formatted();
	/// # let fmt = Local2k::fixed_from_utc2k(Utc2k::MAX, -28800).formatted();
	/// assert_eq!(
	///     fmt.as_bytes(),
	///     b"2099-12-31 15:59:59", // e.g. California.
	/// );
	/// ```
	pub const fn as_bytes(&self) -> &[u8] { self.inner.as_bytes() }

	#[inline]
	#[must_use]
	/// # As Str.
	///
	/// Return a string slice in `YYYY-MM-DD hh:mm:ss` format.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let fmt = Local2k::from(Utc2k::MAX).formatted();
	/// # let fmt = Local2k::fixed_from_utc2k(Utc2k::MAX, -28800).formatted();
	/// assert_eq!(
	///     fmt.as_str(),
	///     "2099-12-31 15:59:59", // e.g. California.
	/// );
	/// ```
	pub const fn as_str(&self) -> &str { self.inner.as_str() }

	#[inline]
	#[must_use]
	/// # Just the Date Bits.
	///
	/// This returns the date as a string slice in `YYYY-MM-DD` format.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2025, 6, 19, 18, 57, 12);
	/// let fmt = Local2k::from(utc).formatted();
	/// # let fmt = Local2k::fixed_from_utc2k(utc, -25200).formatted();
	/// assert_eq!(
	///     fmt.as_str(),
	///     "2025-06-19 11:57:12", // e.g. California.
	/// );
	/// assert_eq!(fmt.date(), "2025-06-19");
	/// ```
	pub const fn date(&self) -> &str { self.inner.date() }

	#[inline]
	#[must_use]
	/// # Just the Year Bit.
	///
	/// This returns the year as a string slice.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2025, 6, 19, 18, 57, 12);
	/// let fmt = Local2k::from(utc).formatted();
	/// # let fmt = Local2k::fixed_from_utc2k(utc, -25200).formatted();
	/// assert_eq!(
	///     fmt.as_str(),
	///     "2025-06-19 11:57:12", // e.g. California.
	/// );
	/// assert_eq!(fmt.year(), "2025");
	/// ```
	pub const fn year(&self) -> &str { self.inner.year() }

	#[inline]
	#[must_use]
	/// # Just the Time Bits.
	///
	/// This returns the time as a string slice in `hh:mm:ss` format.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2025, 6, 19, 18, 57, 12);
	/// let fmt = Local2k::from(utc).formatted();
	/// # let fmt = Local2k::fixed_from_utc2k(utc, -25200).formatted();
	/// assert_eq!(
	///     fmt.as_str(),
	///     "2025-06-19 11:57:12", // e.g. California.
	/// );
	/// assert_eq!(fmt.time(), "11:57:12");
	/// ```
	pub const fn time(&self) -> &str { self.inner.time() }
}

/// ## Conversion.
impl FmtLocal2k {
	#[must_use]
	/// # To RFC2822.
	///
	/// Return a string formatted according to [RFC2822](https://datatracker.ietf.org/doc/html/rfc2822).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // A proper UTC date in RFC2822.
	/// let utc = Utc2k::new(2021, 12, 13, 04, 56, 1);
	/// assert_eq!(
	///     utc.to_rfc2822(),
	///     "Mon, 13 Dec 2021 04:56:01 +0000",
	/// );
	///
	/// // The same date localized to, say, California.
	/// let local = Local2k::from(utc).formatted();
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800).formatted();
	/// assert_eq!(
	///     local.to_rfc2822(),
	///     "Sun, 12 Dec 2021 20:56:01 -0800",
	/// );
	/// ```
	///
	/// The RFC2822 date/time format is portable, whether local or UTC.
	///
	/// ```
	/// # use utc2k::{Local2k, Utc2k};
	/// # let utc = Utc2k::new(2003, 7, 1, 10, 52, 37);
	/// # let local = Local2k::from(utc).formatted();
	/// let utc_2822 = utc.to_rfc2822();
	/// let local_2822 = local.to_rfc2822();
	///
	/// // The RFC2822 representations will vary if there's an offset, but
	/// // if parsed back into a Utc2k, that'll get sorted and they'll match!
	/// assert_eq!(
	///     Utc2k::from_rfc2822(utc_2822.as_bytes()),
	///     Some(utc),
	/// );
	/// assert_eq!(
	///     Utc2k::from_rfc2822(local_2822.as_bytes()),
	///     Some(utc),
	/// );
	/// ```
	pub fn to_rfc2822(&self) -> String {
		let local = Local2k::from_fmtlocal2k(*self);

		let mut out = String::with_capacity(31);
		out.push_str(local.weekday().abbreviation());
		out.push_str(", ");
		out.push(self.inner.0[8].as_char());
		out.push(self.inner.0[9].as_char());
		out.push(' ');
		out.push_str(local.month().abbreviation());
		out.push(' ');
		out.push_str(self.year());
		out.push(' ');
		out.push_str(self.time());
		if let Some(offset) = offset_suffix(self.offset) {
			out.push(' ');
			out.push_str(DateChar::as_str(offset.as_slice()));
		}
		else { out.push_str(" +0000"); }

		out
	}

	#[must_use]
	/// # To RFC3339.
	///
	/// Return a string formatted according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // A proper UTC date in RFC3339.
	/// let utc = Utc2k::new(2021, 12, 13, 11, 56, 1);
	/// assert_eq!(utc.to_rfc3339(), "2021-12-13T11:56:01Z");
	///
	/// // The same date localized to, say, California.
	/// let local = Local2k::from(utc).formatted();
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800).formatted();
	/// assert_eq!(local.to_rfc3339(), "2021-12-13T03:56:01-0800");
	/// ```
	///
	/// The RFC3339 date/time format is portable, whether local or UTC.
	///
	/// ```
	/// # use utc2k::{Local2k, Utc2k};
	/// # let utc = Utc2k::new(2021, 12, 13, 11, 56, 1);
	/// # let local = Local2k::from(utc).formatted();
	/// let utc_3339 = utc.to_rfc3339();
	/// let local_3339 = local.to_rfc3339();
	///
	/// // The RFC3339 representations will vary if there's an offset, but
	/// // if parsed back into a Utc2k, that'll get sorted and they'll match!
	/// assert_eq!(
	///     Utc2k::from_ascii(utc_3339.as_bytes()),
	///     Some(utc),
	/// );
	/// assert_eq!(
	///     Utc2k::from_ascii(local_3339.as_bytes()),
	///     Some(utc),
	/// );
	/// ```
	pub fn to_rfc3339(&self) -> String {
		let mut out = String::with_capacity(if self.offset.is_some() { 24 } else { 20 });
		out.push_str(self.date());
		out.push('T');
		out.push_str(self.time());
		if let Some(offset) = offset_suffix(self.offset) {
			out.push_str(DateChar::as_str(offset.as_slice()));
		}
		else { out.push('Z'); }
		out
	}
}

/// ## Internal.
impl FmtLocal2k {
	#[must_use]
	/// # From [`Local2k`].
	const fn from_local2k(src: Local2k) -> Self {
		Self {
			inner: FmtUtc2k::from_utc2k(src.inner),
			offset: src.offset,
		}
	}
}



#[derive(Debug, Clone, Copy)]
/// # Local ~~UTC~~2K.
///
/// This struct brings barebones locale awareness to [`Utc2k`], allowing
/// date/time digits to be carved up according to the user's local time zone
/// instead of the usual UTC.
///
/// Time zone detection is automatic, but only supported on unix platforms.
/// If the lookup fails or the user is running something weird like Windows,
/// it'll stick with UTC.
///
/// UTC is also used in cases where the local offset would cause the date/time
/// to be clamped to the `2000..=2099` range. (This is only applicable to the
/// first and final hours of the century, so shouldn't come up very often!)
///
/// To keep things simple, `Local2k` is effectively read-only, requiring
/// [`Utc2k`] as a go-between for both [instantiation](Local2k::from_utc2k)
/// and [modification](Local2k::to_utc2k), except for a few convenience methods
/// like [`Local2k::now`], [`Local2k::tomorrow`], and [`Local2k::yesterday`].
///
/// Note that offsets, or the lack thereof, have no effect on date/time
/// equality, hashing, or ordering. `Local2k` objects can be freely compared
/// with one another and/or [`Utc2k`] date/times.
pub struct Local2k {
	/// # Date/Time (w/ `offset`)
	inner: Utc2k,

	/// # Local Offset (Seconds).
	offset: Option<NonZeroI32>,
}

impl fmt::Display for Local2k {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		<FmtLocal2k as fmt::Display>::fmt(&FmtLocal2k::from_local2k(*self), f)
	}
}

impl Eq for Local2k {}

impl From<&FmtLocal2k> for Local2k {
	#[inline]
	fn from(src: &FmtLocal2k) -> Self { Self::from_fmtlocal2k(*src) }
}

impl From<FmtLocal2k> for Local2k {
	#[inline]
	fn from(src: FmtLocal2k) -> Self { Self::from_fmtlocal2k(src) }
}

impl From<&Utc2k> for Local2k {
	#[inline]
	fn from(src: &Utc2k) -> Self { Self::from_utc2k(*src) }
}

impl From<Utc2k> for Local2k {
	#[inline]
	fn from(src: Utc2k) -> Self { Self::from_utc2k(src) }
}

impl From<Local2k> for String {
	#[inline]
	fn from(src: Local2k) -> Self { Self::from(FmtLocal2k::from_local2k(src)) }
}

impl From<&Local2k> for Utc2k {
	#[inline]
	fn from(src: &Local2k) -> Self { src.to_utc2k() }
}

impl From<Local2k> for Utc2k {
	#[inline]
	fn from(src: Local2k) -> Self { src.to_utc2k() }
}

impl hash::Hash for Local2k {
	#[inline]
	fn hash<H: hash::Hasher>(&self, state: &mut H) {
		<Utc2k as hash::Hash>::hash(&self.to_utc2k(), state);
	}
}

impl Ord for Local2k {
	#[inline]
	fn cmp(&self, other: &Self) -> Ordering {
		if self.offset == other.offset { self.inner.cmp(&other.inner) }
		else { self.unixtime().cmp(&other.unixtime()) }
	}
}

impl PartialEq for Local2k {
	#[inline]
	/// # Equality.
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2001, 1, 15, 0, 0, 0);
	/// let local = Local2k::from(utc);
	///
	/// // Offsets don't affect equality.
	/// assert_eq!(utc, local);
	/// assert_eq!(local, local);
	/// ```
	fn eq(&self, other: &Self) -> bool {
		if self.offset == other.offset { self.inner == other.inner }
		else { self.unixtime() == other.unixtime() }
	}
}

impl PartialEq<Utc2k> for Local2k {
	#[inline]
	/// # Cross-Offset Equality.
	///
	/// Local and UTC dates are compared as unix timestamps, so should always
	/// match up.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2025, 1, 1, 0, 0, 0);
	/// let local = Local2k::from(utc);
	/// assert_eq!(utc, local);
	///
	/// // String representations, however, will only be equal if there's
	/// // no offset.
	/// assert_eq!(
	///     utc.to_string() == local.to_string(),
	///     local.offset().is_none(),
	/// );
	/// ```
	fn eq(&self, other: &Utc2k) -> bool { self.unixtime() == other.unixtime() }
}
impl PartialEq<Local2k> for Utc2k {
	#[inline]
	fn eq(&self, other: &Local2k) -> bool { <Local2k as PartialEq<Self>>::eq(other, self) }
}

impl PartialOrd for Local2k {
	#[inline]
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}

/// # Instantiation.
impl Local2k {
	#[must_use]
	/// # From UTC.
	///
	/// Convert a UTC date/time into a local one.
	///
	/// Refer to the main [`Local2k`] for limitations and gotchas.
	pub fn from_utc2k(src: Utc2k) -> Self {
		// If we have an offset, we need to do some things.
		let unixtime = src.unixtime();

		// Is there an offset?
		if let Some(offset) = unixtime_offset(unixtime) {
			let localtime = unixtime.saturating_add_signed(offset.get());
			if (Utc2k::MIN_UNIXTIME..=Utc2k::MAX_UNIXTIME).contains(&localtime) {
				return Self {
					inner: Utc2k::from_unixtime(localtime),
					offset: Some(offset),
				};
			}
		}

		// Keep it UTC.
		Self { inner: src, offset: None }
	}

	#[doc(hidden)]
	#[must_use]
	/// # From UTC w/ Fixed Offset.
	///
	/// Same as [`Local2k::from_utc2k`], but localized with a fixed offset
	/// instead of the system one.
	///
	/// Offsets can be positive or negative, but must break down evenly into
	/// hours and/or minutes, and must be (absolutely) less than one day.
	///
	/// Note: this method is used internally for debugging/testing and is not
	/// intended for broader use.
	///
	/// ```
	/// use std::num::NonZeroI32;
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let one = Local2k::now();
	/// let two = Local2k::fixed_from_utc2k(
	///     one.to_utc2k(),
	///     one.offset().map_or(0, NonZeroI32::get),
	/// );
	///
	/// assert_eq!(one, two);
	/// ```
	pub fn fixed_from_utc2k(src: Utc2k, offset: i32) -> Self {
		// If we have an offset, we need to do some things.
		let unixtime = src.unixtime();

		// Is there an offset?
		if let Some(offset) = nonzero_offset(offset) {
			let localtime = unixtime.saturating_add_signed(offset.get());
			if (Utc2k::MIN_UNIXTIME..=Utc2k::MAX_UNIXTIME).contains(&localtime) {
				return Self {
					inner: Utc2k::from_unixtime(localtime),
					offset: Some(offset),
				};
			}
		}

		// Keep it UTC.
		Self { inner: src, offset: None }
	}

	#[inline]
	#[must_use]
	/// # Now.
	///
	/// Create a new instance representing the current local time.
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // Equivalent.
	/// assert_eq!(
	///     Local2k::now(),
	///     Local2k::from(Utc2k::now()),
	/// );
	/// ```
	pub fn now() -> Self { Self::from_utc2k(Utc2k::now()) }

	#[inline]
	#[must_use]
	/// # Tomorrow.
	///
	/// Create a new instance representing one day from now (present time).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // Equivalent.
	/// assert_eq!(
	///     Local2k::tomorrow(),
	///     Local2k::from(Utc2k::tomorrow()),
	/// );
	/// ```
	pub fn tomorrow() -> Self { Self::from_utc2k(Utc2k::tomorrow()) }

	#[inline]
	#[must_use]
	/// # Yesterday.
	///
	/// Create a new instance representing one day ago (present time).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // Equivalent.
	/// assert_eq!(
	///     Local2k::yesterday(),
	///     Local2k::from(Utc2k::yesterday()),
	/// );
	/// ```
	pub fn yesterday() -> Self { Self::from_utc2k(Utc2k::yesterday()) }
}

/// # Conversion.
impl Local2k {
	#[inline]
	#[must_use]
	/// # Formatted.
	///
	/// This returns a [`FmtLocal2k`] and is equivalent to calling
	/// `FmtLocal2k::from(self)`.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{FmtLocal2k, Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// assert_eq!(local.formatted(), FmtLocal2k::from(local));
	/// ```
	pub const fn formatted(self) -> FmtLocal2k { FmtLocal2k::from_local2k(self) }

	#[must_use]
	/// # To RFC2822.
	///
	/// Return a string formatted according to [RFC2822](https://datatracker.ietf.org/doc/html/rfc2822).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // A proper UTC date in RFC2822.
	/// let utc = Utc2k::new(2021, 12, 13, 04, 56, 1);
	/// assert_eq!(
	///     utc.to_rfc2822(),
	///     "Mon, 13 Dec 2021 04:56:01 +0000",
	/// );
	///
	/// // The same date localized to, say, California.
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800);
	/// assert_eq!(
	///     local.to_rfc2822(),
	///     "Sun, 12 Dec 2021 20:56:01 -0800",
	/// );
	/// ```
	///
	/// The RFC2822 date/time format is portable, whether local or UTC.
	///
	/// ```
	/// # use utc2k::{Local2k, Utc2k};
	/// # let utc = Utc2k::new(2003, 7, 1, 10, 52, 37);
	/// # let local = Local2k::from(utc);
	/// let utc_2822 = utc.to_rfc2822();
	/// let local_2822 = local.to_rfc2822();
	///
	/// // The RFC2822 representations will vary if there's an offset, but
	/// // if parsed back into a Utc2k, that'll get sorted and they'll match!
	/// assert_eq!(
	///     Utc2k::from_rfc2822(utc_2822.as_bytes()),
	///     Some(utc),
	/// );
	/// assert_eq!(
	///     Utc2k::from_rfc2822(local_2822.as_bytes()),
	///     Some(utc),
	/// );
	/// ```
	pub fn to_rfc2822(&self) -> String {
		let mut out = String::with_capacity(31);

		macro_rules! push {
			($($expr:expr),+) => ($( out.push(((($expr) % 10) | b'0') as char); )+);
		}

		out.push_str(self.weekday().abbreviation());
		out.push_str(", ");
		push!(self.inner.d / 10, self.inner.d);
		out.push(' ');
		out.push_str(self.month().abbreviation());
		out.push_str(self.inner.y.as_str()); // Includes spaces on either end.
		push!(self.inner.hh / 10, self.inner.hh);
		out.push(':');
		push!(self.inner.mm / 10, self.inner.mm);
		out.push(':');
		push!(self.inner.ss / 10, self.inner.ss);

		if let Some(offset) = offset_suffix(self.offset) {
			out.push(' ');
			out.push_str(DateChar::as_str(offset.as_slice()));
		}
		else { out.push_str(" +0000"); }

		out
	}

	#[inline]
	#[must_use]
	/// # To RFC3339.
	///
	/// Return a string formatted according to [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339).
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// // A proper UTC date in RFC3339.
	/// let utc = Utc2k::new(2021, 12, 13, 11, 56, 1);
	/// assert_eq!(utc.to_rfc3339(), "2021-12-13T11:56:01Z");
	///
	/// // The same date localized to, say, California.
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800);
	/// assert_eq!(local.to_rfc3339(), "2021-12-13T03:56:01-0800");
	/// ```
	///
	/// The RFC3339 date/time format is portable, whether local or UTC.
	///
	/// ```
	/// # use utc2k::{Local2k, Utc2k};
	/// # let utc = Utc2k::new(2021, 12, 13, 11, 56, 1);
	/// # let local = Local2k::from(utc);
	/// let utc_3339 = utc.to_rfc3339();
	/// let local_3339 = local.to_rfc3339();
	///
	/// // The RFC3339 representations will vary if there's an offset, but
	/// // if parsed back into a Utc2k, that'll get sorted and they'll match!
	/// assert_eq!(
	///     Utc2k::from_ascii(utc_3339.as_bytes()),
	///     Some(utc),
	/// );
	/// assert_eq!(
	///     Utc2k::from_ascii(local_3339.as_bytes()),
	///     Some(utc),
	/// );
	/// ```
	pub fn to_rfc3339(&self) -> String {
		FmtLocal2k::from_local2k(*self).to_rfc3339()
	}

	#[must_use]
	/// # Into UTC.
	///
	/// Convert a local date/time back into UTC one.
	///
	/// ```
	/// use utc2k::{Utc2k, Local2k};
	///
	/// let utc = Utc2k::now();
	/// let local = Local2k::from(utc);
	/// assert_eq!(
	///     local.to_utc2k(),
	///     utc,
	/// );
	/// ```
	pub const fn to_utc2k(&self) -> Utc2k {
		if let Some(offset) = self.offset {
			let (y, m, d, hh, mm, ss) = self.parts();
			Utc2k::from_abacus(Abacus::new_with_offset(y, m, d, hh, mm, ss, offset.get()))
		}
		else { self.inner }
	}

	#[inline]
	#[must_use]
	/// # Unixtime.
	///
	/// Return the (original) unix timestamp used to create this instance.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::from_unixtime(1_434_765_671_u32);
	/// let local = Local2k::from(utc);
	///
	/// assert_eq!(utc.unixtime(),   1_434_765_671);
	/// assert_eq!(local.unixtime(), 1_434_765_671, "local {:?}", local.offset());
	/// ```
	pub const fn unixtime(&self) -> u32 {
		let unixtime = self.inner.unixtime();
		if let Some(offset) = self.offset {
			unixtime.saturating_add_signed(0 - offset.get())
		}
		else { unixtime }
	}
}

/// # Get Parts.
impl Local2k {
	#[inline]
	#[must_use]
	/// # Is UTC?
	///
	/// Returns `true` if there is no offset applied to the "local" date/time.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::Local2k;
	///
	/// let date = Local2k::now();
	/// assert_eq!(
	///     date.is_utc(),
	///     date.offset().is_none(),
	/// );
	/// ```
	pub const fn is_utc(&self) -> bool { self.offset.is_none() }

	#[inline]
	#[must_use]
	/// # Offset.
	///
	/// Return the UTC offset in seconds, if any.
	///
	/// ## Examples
	///
	/// ```
	/// use std::num::NonZeroI32;
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2005, 1, 1, 12, 0, 0);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800);
	/// assert_eq!(
	///     local.offset(),
	///     NonZeroI32::new(-28_800), // e.g. California.
	/// );
	///
	/// // Don't forget about Daylight Saving! 🕱
	/// let utc = Utc2k::new(2005, 6, 1, 12, 0, 0);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(
	///     local.offset(),
	///     NonZeroI32::new(-25_200), // e.g. California.
	/// );
	///
	/// // Remember, 1999 and 2100 DO NOT EXIST. To prevent a loss of
	/// // precision, UTC will be used to represent the first or final hours
	/// // of the century to prevent precision loss.
	/// let local = Local2k::from(Utc2k::MIN);
	/// # let local = Local2k::fixed_from_utc2k(Utc2k::MIN, -28800);
	/// assert!(local.offset().is_none()); // Can't apply a -0800 offset
	///                                    // without leaving the century!
	///
	/// let local = Local2k::from(Utc2k::MAX);
	/// # let local = Local2k::fixed_from_utc2k(Utc2k::MAX, -28800);
	/// assert!(local.offset().is_some()); // The -0800 is no problem on the
	///                                    // other end, though.
	/// # // In Moscow, it'd be the other way around.
	/// # let local = Local2k::fixed_from_utc2k(Utc2k::MIN, 14400);
	/// # assert_eq!(local.offset(), NonZeroI32::new(14400));
	/// # let local = Local2k::fixed_from_utc2k(Utc2k::MAX, 14400);
	/// # assert!(local.offset().is_none());
	/// ```
	pub const fn offset(&self) -> Option<NonZeroI32> { self.offset }

	#[inline]
	#[must_use]
	/// # Parts.
	///
	/// Return the individual numerical components of the datetime, from years
	/// down to seconds.
	///
	/// Alternatively, if you only want the date bits, use [`Local2k::ymd`], or
	/// if you only want the time bits, use [`Local2k::hms`].
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 4, 16, 30, 1);
	/// assert_eq!(
	///     utc.parts(),
	///     (2010, 5, 4, 16, 30, 1),
	/// );
	///
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(
	///     local.parts(),
	///     (2010, 5, 4, 9, 30, 1), // e.g. California.
	/// //               ^ -0700
	/// );
	/// ```
	pub const fn parts(&self) -> (u16, u8, u8, u8, u8, u8) { self.inner.parts() }

	#[inline]
	#[must_use]
	/// # Date Parts.
	///
	/// Return the year, month, and day.
	///
	/// If you want the time too, call [`Utc2k::parts`] instead.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 5, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.ymd(), (2010, 5, 5)); // e.g. California.
	/// ```
	pub const fn ymd(&self) -> (u16, u8, u8) { self.inner.ymd() }

	#[inline]
	#[must_use]
	/// # Time Parts.
	///
	/// Return the hours, minutes, and seconds.
	///
	/// If you want the date too, call [`Utc2k::parts`] instead.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 5, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.hms(), (9, 30, 1)); // e.g. California.
	/// ```
	pub const fn hms(&self) -> (u8, u8, u8) { self.inner.hms() }

	#[inline]
	#[must_use]
	/// # Year.
	///
	/// This returns the year value.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.year(), 2010);
	/// ```
	pub const fn year(&self) -> u16 { self.inner.year() }

	#[inline]
	#[must_use]
	/// # Month (enum).
	///
	/// This returns the month value as a [`Month`].
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Month, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.month(), Month::May);
	/// ```
	pub const fn month(&self) -> Month { self.inner.month() }

	#[inline]
	#[must_use]
	/// # Day.
	///
	/// This returns the day value.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.day(), 15);
	/// ```
	pub const fn day(&self) -> u8 { self.inner.day() }

	#[inline]
	#[must_use]
	/// # Hour.
	///
	/// This returns the hour value.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.hour(), 9); // e.g. California.
	/// ```
	pub const fn hour(&self) -> u8 { self.inner.hour() }

	#[inline]
	#[must_use]
	/// # Minute.
	///
	/// This returns the minute value.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.minute(), 30);
	/// ```
	pub const fn minute(&self) -> u8 { self.inner.minute() }

	#[inline]
	#[must_use]
	/// # Second.
	///
	/// This returns the second value.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 5, 15, 16, 30, 1);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.second(), 1);
	/// ```
	pub const fn second(&self) -> u8 { self.inner.second() }
}

/// ## Other Getters.
impl Local2k {
	#[inline]
	#[must_use]
	/// # Is Leap Year?
	///
	/// This returns `true` if this date is/was in a leap year.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let date = Local2k::from(
	///     Utc2k::try_from("2020-05-10").unwrap()
	/// );
	/// assert!(date.leap_year());
	///
	/// let date = Local2k::from(
	///     Utc2k::try_from("2021-03-15").unwrap()
	/// );
	/// assert!(! date.leap_year());
	/// ```
	pub const fn leap_year(&self) -> bool { self.inner.leap_year() }

	#[inline]
	#[must_use]
	/// # Month Size (Days).
	///
	/// This method returns the "size" of the datetime's month, or its last
	/// day, whichever way you prefer to think of it.
	///
	/// The value will always be between `28..=31`, with leap Februaries
	/// returning `29`.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2021, 7, 8, 0, 0, 0);
	/// let local = Local2k::from(utc);
	/// assert_eq!(local.month_size(), 31);
	///
	/// let utc = Utc2k::new(2020, 2, 20, 0, 0, 0);
	/// let local = Local2k::from(utc);
	/// assert_eq!(local.month_size(), 29); // Leap!
	/// ```
	pub const fn month_size(&self) -> u8 { self.inner.month_size() }

	#[inline]
	#[must_use]
	/// # Ordinal.
	///
	/// Return the day-of-year value. This will be between `1..=365` (or `1..=366`
	/// for leap years).
	///
	/// ## Examples
	///
	/// ```no_run
	/// use utc2k::{Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2020, 5, 10, 12, 0, 0);
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.ordinal(), 131);
	/// ```
	pub const fn ordinal(&self) -> u16 { self.inner.ordinal() }

	#[inline]
	#[must_use]
	/// # Seconds From Midnight.
	///
	/// Return the number of seconds since (the current day's) midnight. In
	/// other words, this adds up all of the time bits.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{DAY_IN_SECONDS, Local2k, Utc2k};
	///
	/// let utc = Utc2k::new(2010, 11, 01, 0, 0, 0);
	/// assert_eq!(utc.seconds_from_midnight(), 0); // It _is_ midnight!
	///
	/// // In California, though, it's still Halloween!
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -28800);
	/// assert_eq!(
	///     local.parts(),
	///     (2010, 10, 31, 16, 0, 0),
	/// );
	///
	/// // The distance from _its_ midnight is very different!
	/// assert_eq!(local.seconds_from_midnight(), 57_600);
	/// ```
	pub const fn seconds_from_midnight(&self) -> u32 {
		self.inner.seconds_from_midnight()
	}

	#[inline]
	#[must_use]
	/// # Weekday.
	///
	/// Return the [`Weekday`] corresponding to the given date.
	///
	/// ## Examples
	///
	/// ```
	/// use utc2k::{Local2k, Utc2k, Weekday};
	///
	/// let utc = Utc2k::new(2021, 7, 8, 5, 22, 1);
	/// assert_eq!(utc.weekday(), Weekday::Thursday);
	///
	/// // Local date/times may differ. In California, for example, it'd
	/// // still be the night before.
	/// let local = Local2k::from(utc);
	/// # let local = Local2k::fixed_from_utc2k(utc, -25200);
	/// assert_eq!(local.weekday(), Weekday::Wednesday);
	/// ```
	pub const fn weekday(&self) -> Weekday { self.inner.weekday() }
}

/// ## Internal Helpers.
impl Local2k {
	#[must_use]
	/// # From `FmtLocal2k`.
	const fn from_fmtlocal2k(src: FmtLocal2k) -> Self {
		Self {
			inner: Utc2k::from_fmtutc2k(src.inner),
			offset: src.offset,
		}
	}
}



#[expect(clippy::cast_possible_truncation, reason = "False positive.")]
/// # Offset Suffix.
///
/// Convert an offset back to `±hhmm` format.
const fn offset_suffix(offset: Option<NonZeroI32>) -> Option<[DateChar; 5]> {
	if let Some(offset) = offset {
		let sign =
			if offset.get() < 0 { DateChar::Dash }
			else { DateChar::Plus };

		// Offsets that make it here are less than a day.
		let offset = offset.get().unsigned_abs();
		let hh = offset.wrapping_div(HOUR_IN_SECONDS) as u8;
		let mm = (offset % HOUR_IN_SECONDS).wrapping_div(MINUTE_IN_SECONDS) as u8;

		Some([
			sign,
			DateChar::from_digit(hh / 10),
			DateChar::from_digit(hh),
			DateChar::from_digit(mm / 10),
			DateChar::from_digit(mm),
		])
	}
	else { None }
}

#[expect(clippy::cast_possible_wrap, reason = "False positive.")]
/// # Sanitize Offset.
///
/// Strip multi-day bullshit, make sure it is a multiple of sixty, and return
/// if nonzero.
const fn nonzero_offset(offset: i32) -> Option<NonZeroI32> {
	let offset = offset % DAY_IN_SECONDS as i32;
	if offset.unsigned_abs().is_multiple_of(MINUTE_IN_SECONDS) {
		NonZeroI32::new(offset)
	}
	else { None }
}

#[inline]
#[must_use]
/// # Offset From Unixtime.
///
/// Return the local offset details for a given UTC date/time, ensuring it is
/// less than a day (absolutely) and limited to hour/minute precision.
///
/// The local time zone details are cached on the first call; subsequent runs
/// should be much faster.
fn unixtime_offset(unixtime: u32) -> Option<NonZeroI32> {
	TZ.get_or_init(|| TimeZone::local().ok())
		.as_ref()
		.and_then(|tz|
			tz.find_local_time_type(i64::from(unixtime))
				.ok()
				.and_then(|tz| nonzero_offset(tz.ut_offset()))
		)
}



#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn t_lossless() {
		// Make sure the first couple days of the century can be losslessly
		// converted to/from utc/local with both positive and negative offsets.
		for i in Utc2k::MIN_UNIXTIME..=Utc2k::MIN_UNIXTIME + DAY_IN_SECONDS * 2 {
			let utc = Utc2k::from_unixtime(i);
			let local = Local2k::fixed_from_utc2k(utc, -28860);
			assert_eq!(utc, local);
			assert_eq!(utc, local.to_utc2k());

			let local = Local2k::fixed_from_utc2k(utc, 28860);
			assert_eq!(utc, local);
			assert_eq!(utc, local.to_utc2k());
		}

		// Now the same for the end.
		for i in Utc2k::MAX_UNIXTIME - DAY_IN_SECONDS * 2..=Utc2k::MAX_UNIXTIME {
			let utc = Utc2k::from_unixtime(i);
			let local = Local2k::fixed_from_utc2k(utc, -28860);
			assert_eq!(utc, local);
			assert_eq!(utc, local.to_utc2k());

			let local = Local2k::fixed_from_utc2k(utc, 28860);
			assert_eq!(utc, local);
			assert_eq!(utc, local.to_utc2k());
		}

		// Lastly, let's check the first 15 days of March, since there'll be
		// a Daylight Saving boundary in there for some time zones.
		for i in Utc2k::new(2025, 3, 1, 0, 0, 0).unixtime()..=Utc2k::new(2025, 3, 15, 0, 0, 0).unixtime() {
			let utc = Utc2k::from_unixtime(i);
			let local = Local2k::from_utc2k(utc);
			assert_eq!(utc, local);
			assert_eq!(utc, local.to_utc2k());
		}
	}
}