File: DateTime.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (1396 lines) | stat: -rw-r--r-- 36,651 bytes parent folder | download
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
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
#include "DateTime.h"
#include "ArrayView.h"
#include "String.h"
#include "../CommonWindows.h"
#include "../Asserts.h"

#include <cstdio>
#include <cstring>
#include <sys/types.h>

#if !defined(DEATH_TARGET_WINDOWS)
#	include <sys/time.h>
#endif

#if !defined(DEATH_USE_GMTOFF_IN_TM) && defined(DEATH_TARGET_APPLE)
#	define DEATH_USE_GMTOFF_IN_TM
#endif

#if defined(DEATH_USE_GMTOFF_IN_TM)
#	include <atomic>
#endif

#if defined(DEATH_TARGET_SWITCH) || defined(DEATH_TARGET_VITA) || (defined(__MINGW32_TOOLCHAIN__) && defined(__STRICT_ANSI__))
// These two are excluded from headers for some reason, define them here
extern "C" void tzset(void);
extern long _timezone;
#endif

#define MONTHS_IN_YEAR 12
#define SEC_PER_MIN 60
#define MIN_PER_HOUR 60
#define SECONDS_PER_DAY 86400
#define MILLISECONDS_PER_DAY 86400000
#define DST_OFFSET 3600

#define EPOCH_JDN 2440587l
#define JDN_OFFSET 32046l
#define DAYS_PER_5_MONTHS 153l
#define DAYS_PER_4_YEARS 1461l
#define DAYS_PER_400_YEARS 146097l

namespace Death { namespace Containers {
//###==##====#=====--==~--~=~- --- -- -  -  -   -

	namespace Implementation
	{
		// The first line is for normal years, the second one is for the leap ones
		static const std::int32_t DaysInMonth[2][MONTHS_IN_YEAR] = {
			{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
			{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
		};

		static bool IsLeapYear(std::int32_t year) noexcept
		{
			if (year == -1) {
				year = DateTime::UtcNow().GetYear();
			}

			return (year % 4 == 0 && ((year % 100) != 0 || (year % 400) == 0));
		}

		static std::int32_t GetNumberOfDaysInMonth(std::int32_t month, std::int32_t year) noexcept
		{
			return DaysInMonth[IsLeapYear(year)][month];
		}

		static std::int32_t GetTruncatedJDN(std::int32_t day, std::int32_t mon, std::int32_t year) noexcept
		{
			// Make the year positive to avoid problems with negative numbers division
			year += 4800;

			// Months are counted from March here
			std::int32_t month;
			if (mon >= 2 /*March*/) {
				month = mon - 2;
			} else {
				month = mon + 10;
				year--;
			}

			return ((year / 100) * DAYS_PER_400_YEARS) / 4 + ((year % 100) * DAYS_PER_4_YEARS) / 4
						+ (month * DAYS_PER_5_MONTHS + 2) / 5 + day - JDN_OFFSET;
		}

		static struct tm* GetLocalTm(const time_t* ticks, struct tm* temp) noexcept
		{
#if defined(DEATH_TARGET_WINDOWS)
			return (localtime_s(temp, ticks) == 0 ? temp : nullptr);
#else
			// TODO: This function is not thread-safe on Unix - use localtime_r instead
			const tm* const result = localtime(ticks);
			if (result == nullptr) {
				return nullptr;
			}

			std::memcpy(temp, result, sizeof(struct tm));
			return temp;
#endif
		}

		static bool IsDST(DateTime date, std::int32_t& bias) noexcept
		{
			time_t timet = date.GetTicks();
			if (timet == (time_t)-1) {
				return false;
			}

			struct tm temp;
			tm* tm = GetLocalTm(&timet, &temp);
			if (tm == nullptr || !tm->tm_isdst) {
				return false;
			}

//#if defined(DEATH_TARGET_WINDOWS) && defined(_UCRT)
//			long dstbias = 0;
//			_get_dstbias(&dstbias);
//			bias = dstbias;
//#else
			bias = DST_OFFSET;
//#endif
			return true;
		}

		static std::int32_t GetTimeZone() noexcept
		{
			// Struct tm doesn't always have the tm_gmtoff field, define this if it does
#if defined(DEATH_USE_GMTOFF_IN_TM)
			static std::atomic<std::int32_t> _gmtoffset{INT32_MAX}; // Invalid timezone
			if (_gmtoffset == INT32_MAX) {
				time_t t = time(nullptr);
				struct tm tm;
				GetLocalTm(&t, &tm);

				std::int32_t gmtoffset = static_cast<std::int32_t>(-tm.tm_gmtoff);

				// This function is supposed to return the same value whether DST is enabled or not, so we need to use
				// an additional offset if DST is on as tm_gmtoff already does include it
				if (tm.tm_isdst) {
					gmtoffset += DST_OFFSET;
				}

				_gmtoffset = gmtoffset;
			}
			return _gmtoffset;
#elif defined(__WINE__)
			struct timeb tb;
			ftime(&tb);
			return tb.timezone * 60;
#elif defined(DEATH_TARGET_MSVC)
#	if !defined(DEATH_TARGET_WINDOWS_RT)
			// In any case we must initialize the time zone before using it, try to do it only once
			static bool _tzSet = (_tzset(), true);
			(void)_tzSet;
#	endif

			long t;
			_get_timezone(&t);
			return t;
#else
			// In any case we must initialize the time zone before using it, try to do it only once
			static bool _tzSet = (tzset(), true);
			(void)_tzSet;

#	if defined(DEATH_TARGET_MINGW) || defined(DEATH_TARGET_SWITCH) || defined(DEATH_TARGET_VITA)
			return _timezone;
#	else // Unknown platform - assume it has timezone variable
			return timezone;
#	endif
#endif
		}

		static const tm* TryGetTm(time_t t, DateTime::TimeZone tz, struct tm* temp) noexcept
		{
			if (tz.IsLocal()) {
				return Implementation::GetLocalTm(&t, temp);
			}

			t += (time_t)tz.GetOffset();
#if !defined(__VMS__) // Time is unsigned so avoid warning
			if (t < 0) {
				return nullptr;
			}
#endif

#if defined(DEATH_TARGET_WINDOWS)
			return (gmtime_s(temp, &t) == 0 ? temp : nullptr);
#else
			// TODO: This function is not thread-safe on Unix - use gmtime_r instead
			const tm* const result = gmtime(&t);
			if (result == nullptr) {
				return nullptr;
			}

			std::memcpy(temp, result, sizeof(struct tm));
			return temp;
#endif
		}

		template<class T>
		static bool GetNumericToken(const T* string, std::size_t length, std::size_t& i, std::size_t width, std::int32_t* number, std::size_t* scannedDigitsCount = nullptr) noexcept
		{
			std::size_t n = 1;
			*number = 0;
			while (i < length && string[i] >= '0' && string[i] <= '9') {
				*number *= 10;
				*number += string[i] - '0';

				i++;
				n++;

				if (width > 0 && n > width) {
					break;
				}
			}

			if (scannedDigitsCount != nullptr) {
				*scannedDigitsCount = n - 1;
			}

			return (n > 1);
		}

		template<class T, class U>
		std::size_t TranslateFromUnicodeFormat(T* dest, std::size_t destLength, const U* src, std::size_t srcLength) noexcept
		{
			static const char* formatchars =
				"dghHmMsSy"
#if defined(DEATH_TARGET_WINDOWS)
				"t"
#else
				"EcLawD"
#endif
				;

			U chLast = '\0';
			std::size_t lastCount = 0;
			std::size_t j = 0;

			for (std::size_t i = 0; /* Handled inside the loop */; i++) {
				if (i < srcLength) {
					if (src[i] == chLast) {
						lastCount++;
						continue;
					}

					if (src[i] >= 'A' && src[i] <= 'z' && strchr(formatchars, (char)src[i])) {
						chLast = src[i];
						lastCount = 1;
						continue;
					}
				}

				if (lastCount > 0) {
					switch (chLast) {
						case 'd':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'd';
									break;
#if defined(DEATH_TARGET_WINDOWS)
								case 3:
									dest[j++] = '%'; dest[j++] = 'a';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'A';
									break;
#endif
							}
							break;
#if !defined(DEATH_TARGET_WINDOWS)
						case 'D':
							switch (lastCount) {
								case 1:
								case 2:
								case 3:
									dest[j++] = '%'; dest[j++] = 'j';
									break;
							}
							break;
						case 'w':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'W';
									break;
							}
							break;
						case 'E':
							switch (lastCount) {
								case 1:
								case 2:
								case 3:
									dest[j++] = '%'; dest[j++] = 'a';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'A';
									break;
								case 5:
								case 6:
									// No "narrow form" in strftime(), use abbreviation instead
									dest[j++] = '%'; dest[j++] = 'a';
									break;
							}
							break;
						case 'c':
							switch (lastCount) {
								case 1:
									// First day of week as numeric value
									dest[j++] = '1';
									break;
								case 3:
									dest[j++] = '%'; dest[j++] = 'a';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'A';
									break;
								case 5:
									// No "narrow form" in strftime(), use abbreviation instead
									dest[j++] = '%'; dest[j++] = 'a';
									break;
							}
							break;
						case 'L':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'm';
									break;
								case 3:
									dest[j++] = '%'; dest[j++] = 'b';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'B';
									break;
								case 5:
									//No "narrow form" in strftime(), use abbreviation instead
									dest[j++] = '%'; dest[j++] = 'b';
									break;
							}
							break;
#endif
						case 'M':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'm';
									break;
								case 3:
									dest[j++] = '%'; dest[j++] = 'n';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'B';
									break;
								case 5:
									// No "narrow form" in strftime(), use abbreviation instead
									dest[j++] = '%'; dest[j++] = 'b';
									break;
							}
							break;
						case 'y':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'y';
									break;
								case 4:
									dest[j++] = '%'; dest[j++] = 'Y';
									break;
							}
							break;
						case 'H':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'H';
									break;
							}
							break;
						case 'h':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'I';
									break;
							}
							break;
						case 'm':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'M';
									break;
							}
							break;
						case 's':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'S';
									break;
							}
							break;
						case 'g':
							// This format is not supported in strftime(), ignore it
							break;
#if !defined(DEATH_TARGET_WINDOWS)
						case 'a':
							dest[j++] = '%'; dest[j++] = 'p';
							break;
#else
						case 't':
							switch (lastCount) {
								case 1:
								case 2:
									dest[j++] = '%'; dest[j++] = 'p';
									break;
							}
							break;
#endif
					}

					chLast = '\0';
					lastCount = 0;
				}

				if (i >= srcLength) {
					break;
				}

				if (i + 1 < srcLength && src[i] == '\'' && src[i + 1] == '\'') {
					dest[j++] = '\'';
					i++;
					continue;
				}

				bool isEndQuote = false;
				if (src[i] == '\'') {
					i++;
					while (i < srcLength) {
						if (i + 1 < srcLength && src[i] == '\'' && src[i + 1] == '\'') {
							dest[j++] = '\'';
							i += 2;
							continue;
						}

						if (src[i] == '\'') {
							isEndQuote = true;
							break;
						}

						dest[j++] = (T)src[i];
						i++;
					}
				}

				if (i >= srcLength) {
					break;
				}

				if (!isEndQuote) {
					if (src[i] == '%') {
						dest[j++] = '%';
					}

					dest[j++] = (T)src[i];
				}
			}

			return j;
		}

		template<class T>
		static bool TryParseFormat(DateTime& result, const T* input, std::size_t inputLength, const T* format, std::size_t formatLength, std::size_t* endIndex) noexcept
		{
			std::int32_t msec = 0, sec = 0, min = 0, hour = 0, wday = -1, yday = 0, day = 0, mon = -1, year = 0, timeZone = 0, num;
			bool haveWDay = false, haveYDay = false, haveDay = false, haveMon = false, haveYear = false, haveHour = false,
				haveMin = false, haveSec = false, haveMsec = false, hourIsIn12hFormat = false, isPM = false, haveTimeZone = false;

			std::size_t j = 0;
			for (std::size_t i = 0; i < formatLength; i++) {
				if (format[i] != '%') {
					if (format[i] == ' ') {
						// Whitespace in the format string matches zero or more whitespaces in the input
						while (j < inputLength && input[j] == ' ') {
							j++;
						}
					} else {
						// Any other character (except whitespace and '%') must be matched by itself in the input
						if (j >= inputLength || input[j++] != format[i]) {
							return false;
						}
					}
					continue;
				}

				// Start of a format specification
				i++;

				// Parse the optional width
				std::size_t width = 0;
				while (format[i] >= '0' && format[i] <= '9') {
					width *= 10;
					width += format[i] - '0';
					i++;
				}

				if (width == 0) {
					switch (format[i]) {
						case 'Y':			// Year has 4 digits
							width = 4;
							break;
						case 'j':			// Day of year has 3 digits
						case 'l':			// Milliseconds have 3 digits
							width = 3;
							break;
						case 'w':			// Week day as number has only one
							width = 1;
							break;
						default:
							width = 2;		// Default for all other fields
							break;
					}
				}

				switch (format[i]) {
					case 'c': {		// Locale default date and time representation
						DateTime dt;
						T format2[64];
						bool success2 = false;
#if defined(DEATH_TARGET_WINDOWS)
						wchar_t buffer[64];
						std::int32_t bufferLength = ::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, buffer, int(arraySize(buffer)));
						if (bufferLength > 0) {
							if (buffer[bufferLength - 1] == '\0') {
								bufferLength--;
							}
							buffer[bufferLength++] = ' ';
							std::int32_t timeLength = ::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_STIMEFORMAT, buffer + bufferLength, int(arraySize(buffer)) - bufferLength);
							if (timeLength > 0) {
								bufferLength += timeLength;
								if (buffer[bufferLength - 1] == '\0') {
									bufferLength--;
								}
								std::size_t format2Length = TranslateFromUnicodeFormat(format2, arraySize(format2), buffer, bufferLength);
								if (format2Length > 0) {
									success2 = TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex);
								}
							}
						}
#endif
						if (!success2) {
							std::size_t format2Length = 0;	// yyyy/mm/dd HH:MM:SS
							format2[format2Length++] = '%'; format2[format2Length++] = 'Y'; format2[format2Length++] = '/';
							format2[format2Length++] = '%'; format2[format2Length++] = 'm'; format2[format2Length++] = '/';
							format2[format2Length++] = '%'; format2[format2Length++] = 'd'; format2[format2Length++] = ' ';
							format2[format2Length++] = '%'; format2[format2Length++] = 'H'; format2[format2Length++] = ':';
							format2[format2Length++] = '%'; format2[format2Length++] = 'M'; format2[format2Length++] = ':';
							format2[format2Length++] = '%'; format2[format2Length++] = 'S';
							if (!TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex)) {
								return false;
							}
						}

						j = *endIndex;

						const DateTime::Tm tm = dt.Partitioned();

						haveDay = haveMon = haveYear = haveHour = haveMin = haveSec = true;

						hour = tm.Hour;
						min = tm.Minute;
						sec = tm.Second;

						year = tm.Year;
						mon = tm.Month;
						day = tm.Day;
						break;
					}
					case 'd':		// Day of a month (01-31)
					case 'e': {		// Day of a month (1-31) (GNU extension)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num < 1 || num > 31) {
							return false;
						}

						haveDay = true;
						day = num;
						break;
					}
					case 'H': {		// Hour in 24h format (00-23)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num > 23) {
							return false;
						}

						haveHour = true;
						hour = num;
						break;
					}
					case 'I': {		// Hour in 12h format (01-12)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num == 0 || num > 12) {
							return false;
						}

						haveHour = true;
						hourIsIn12hFormat = true;
						hour = (num % 12);
						break;
					}
					case 'j': {		// Day of the year
						if (!GetNumericToken(input, inputLength, j, width, &num) || num == 0 || num > 366) {
							return false;
						}

						haveYDay = true;
						yday = num;
						break;
					}
					case 'l': {		// Milliseconds (0-999)
						if (!GetNumericToken(input, inputLength, j, width, &num)) {
							return false;
						}

						haveMsec = true;
						msec = num;
						break;
					}
					case 'm': {		// Month as a number (01-12)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num == 0 || num > 12) {
							return false;
						}

						haveMon = true;
						mon = (num - 1);
						break;
					}
					case 'M': {		// Minute as a decimal number (00-59)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num > 59) {
							return false;
						}

						haveMin = true;
						min = num;
						break;
					}
					case 'p': {		// AM or PM string
						if (j + 1 >= inputLength || input[j + 1] != 'M') {
							return false;
						}
						switch (input[j]) {
							case 'P': isPM = true; break;
							case 'A': break;
							default: return false;
						}
						j += 2;
						break;
					}
					case 'S': {		// Second as a decimal number (00-61)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num > 61) {
							return false;
						}

						haveSec = true;
						sec = num;
						break;
					}
					case 'w': {		// Weekday as a number (0-6), Sunday = 0
						if (!GetNumericToken(input, inputLength, j, width, &num) || wday > 6) {
							return false;
						}

						haveWDay = true;
						wday = num;
						break;
					}
					case 'x': {		// Locale default date representation
						DateTime dt;
						T format2[64];
						bool success2 = false;
#if defined(DEATH_TARGET_WINDOWS)
						wchar_t buffer[64];
						std::int32_t bufferLength = ::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, buffer, int(arraySize(buffer)));
						if (bufferLength > 0) {
							if (buffer[bufferLength - 1] == '\0') {
								bufferLength--;
							}
							std::size_t format2Length = TranslateFromUnicodeFormat(format2, arraySize(format2), buffer, bufferLength);
							if (format2Length > 0) {
								success2 = TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex);
								if (!success2) {
									std::int32_t bufferLength = ::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SLONGDATE, buffer, int(arraySize(buffer)));
									if (bufferLength > 0) {
										if (buffer[bufferLength - 1] == '\0') {
											bufferLength--;
										}
										format2Length = TranslateFromUnicodeFormat(format2, arraySize(format2), buffer, bufferLength);
										if (format2Length > 0) {
											success2 = TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex);
										}
									}
								}
							}
						}
#endif
						if (!success2) {
							std::size_t format2Length = 0;	// yyyy/mm/dd
							format2[format2Length++] = '%'; format2[format2Length++] = 'Y'; format2[format2Length++] = '/';
							format2[format2Length++] = '%'; format2[format2Length++] = 'm'; format2[format2Length++] = '/';
							format2[format2Length++] = '%'; format2[format2Length++] = 'd';
							if (!TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex)) {
								return false;
							}
						}

						j = *endIndex;

						const DateTime::Tm tm = dt.Partitioned();

						haveDay = haveMon = haveYear = true;

						year = tm.Year;
						mon = tm.Month;
						day = tm.Day;
						break;
					}
					case 'X': {		// Locale default time representation
						DateTime dt;
						T format2[64];
						bool success2 = false;
#if defined(DEATH_TARGET_WINDOWS)
						wchar_t buffer[64];
						std::int32_t bufferLength = ::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_STIMEFORMAT, buffer, int(arraySize(buffer)));
						if (bufferLength > 0) {
							if (buffer[bufferLength - 1] == '\0') {
								bufferLength--;
							}
							std::size_t format2Length = TranslateFromUnicodeFormat(format2, arraySize(format2), buffer, bufferLength);
							if (format2Length > 0) {
								success2 = TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex);
							}
						}
#endif
						if (!success2) {
							std::size_t format2Length = 0;	// HH:MM:SS
							format2[format2Length++] = '%'; format2[format2Length++] = 'H'; format2[format2Length++] = ':';
							format2[format2Length++] = '%'; format2[format2Length++] = 'M'; format2[format2Length++] = ':';
							format2[format2Length++] = '%'; format2[format2Length++] = 'S';
							if (!TryParseFormat(dt, input + j, inputLength - j, format2, format2Length, endIndex)) {
								return false;
							}
						}

						j = *endIndex;

						const DateTime::Tm tm = dt.Partitioned();

						haveHour = haveMin = haveSec = true;

						hour = tm.Hour;
						min = tm.Minute;
						sec = tm.Second;
						break;
					}
					case 'y': {		// Year without century (00-99)
						if (!GetNumericToken(input, inputLength, j, width, &num) || num > 99) {
							return false;
						}

						haveYear = true;
						year = (num > 30 ? 1900 : 2000) + num;
						break;
					}
					case 'Y': {		// Year with century
						if (!GetNumericToken(input, inputLength, j, width, &num)) {
							return false;
						}

						haveYear = true;
						year = num;
						break;
					}
					case 'z': {
						if (j >= inputLength) {
							return false;
						}

						if (input[j] == 'Z') {
							// Time is in UTC
							j++;
							haveTimeZone = true;
							break;
						}

						bool minusFound;
						switch (input[j]) {
							case '+': minusFound = false; break;
							case '-': minusFound = true; break;
							default: return false;
						}

						j++;

						// Here should follow exactly 2 digits for hours (HH)
						std::size_t numScannedDigits;

						std::int32_t hours;
						if (!GetNumericToken(input, inputLength, j, 2, &hours, &numScannedDigits) || numScannedDigits != 2) {
							return false;
						}

						// Optionally followed by a colon separator
						bool mustHaveMinutes = false;
						if (j < inputLength && input[j] == ':') {
							mustHaveMinutes = true;
							j++;
						}

						// Optionally followed by exactly 2 digits for minutes (MM)
						std::int32_t minutes = 0;
						if (!GetNumericToken(input, inputLength, j, 2, &minutes, &numScannedDigits) || numScannedDigits != 2) {
							if (mustHaveMinutes || numScannedDigits) {
								return false;
							}
						}

						if (hours > 15 || minutes > 59) {
							return false;
						}

						timeZone = 3600 * hours + 60 * minutes;
						if (minusFound) {
							timeZone = -timeZone;
						}

						haveTimeZone = true;
						break;
					}
					case 'Z': {		// Timezone name
						// TODO: Named timezones are not supported, skip it for now
						while (j < inputLength && ((input[j] >= 'a' && input[j] <= 'z') || (input[j] >= 'A' && input[j] <= 'Z'))) {
							j++;
						}
						break;
					}
					case '%': {
						if (j >= inputLength || input[j++] != '%') {
							return false;
						}
						break;
					}
					case '\0': {
						LOGD("Unexpected format end in DateTime::TryParse()");
						return false;
					}
					default: {
						return false;
					}
				}
			}

			DateTime::Tm tm;
			if (result.IsValid()) {
				tm = result.Partitioned();
			} else {
				tm = DateTime::UtcNow().ResetTime().Partitioned();
			}

			if (haveMon) {
				tm.Month = mon;
			}
			if (haveYear) {
				tm.Year = year;
			}

			if (haveDay) {
				if (day > GetNumberOfDaysInMonth(tm.Month, tm.Year)) {
					return false;
				}

				tm.Day = day;
			} else if (haveYDay) {
				bool isLeap = IsLeapYear(tm.Year);
				if (yday > (isLeap ? 366 : 365)) {
					return false;
				}

				std::int32_t month = 0;
				while (yday - DaysInMonth[isLeap][month] > 0) {
					yday -= DaysInMonth[isLeap][month];
					month++;
				}

				DateTime::Tm tm2 = DateTime(tm.Year, month, yday).Partitioned();
				tm.Month = tm2.Month;
				tm.Day = tm2.Day;
			}

			if (haveHour && hourIsIn12hFormat && isPM) {
				// Translate to 24-hour format
				hour += 12;
			}

			if (haveHour) {
				tm.Hour = hour;
			}
			if (haveMin) {
				tm.Minute = min;
			}
			if (haveSec) {
				tm.Second = sec;
			}
			if (haveMsec) {
				tm.Millisecond = msec;
			}

			result.Set(tm);

			if (haveTimeZone) {
				result.AdjustFromTimezone(timeZone);
			}

			if (haveWDay && result.GetWeekDay() != wday) {
				return false;
			}

			*endIndex = j;
			return true;
		}
	}

	DateTime::TimeZone::TimeZone(DateTime::Tz tz) noexcept
	{
		switch (tz) {
			case DateTime::Local:
				// Use a special value for local timezone
				_offset = INT32_MIN;
				break;

			case DateTime::GMT_12:
			case DateTime::GMT_11:
			case DateTime::GMT_10:
			case DateTime::GMT_9:
			case DateTime::GMT_8:
			case DateTime::GMT_7:
			case DateTime::GMT_6:
			case DateTime::GMT_5:
			case DateTime::GMT_4:
			case DateTime::GMT_3:
			case DateTime::GMT_2:
			case DateTime::GMT_1:
				_offset = -3600 * (DateTime::GMT0 - tz);
				break;

			case DateTime::GMT0:
			case DateTime::GMT1:
			case DateTime::GMT2:
			case DateTime::GMT3:
			case DateTime::GMT4:
			case DateTime::GMT5:
			case DateTime::GMT6:
			case DateTime::GMT7:
			case DateTime::GMT8:
			case DateTime::GMT9:
			case DateTime::GMT10:
			case DateTime::GMT11:
			case DateTime::GMT12:
			case DateTime::GMT13:
				_offset = 3600 * (tz - DateTime::GMT0);
				break;

			case DateTime::A_CST:
				// Central Standard Time in use in Australia (UTC + 9.5)
				_offset = 60L * (9 * MIN_PER_HOUR + MIN_PER_HOUR / 2);
				break;

			default:
				DEATH_ASSERT_UNREACHABLE();
				break;
		}
	}

	std::int32_t DateTime::TimeZone::GetOffset() const noexcept
	{
		// Get the offset using standard library
		// It returns the difference GMT-local while we want to have the offset _from_ GMT, hence the '-'
		return (_offset == INT32_MIN ? -Implementation::GetTimeZone() : _offset);
	}

	DateTime::Tm::Tm() noexcept
		: Millisecond(0), Second(0), Minute(0), Hour(0), Day(0), DayOfYear(0), Month(0), Year(-1), _dayOfWeek(-1)
	{
	}

	DateTime::Tm::Tm(const struct tm& tm, TimeZone tz) noexcept
		: _tz(tz), Millisecond(0), Second((std::int32_t)tm.tm_sec), Minute((std::int32_t)tm.tm_min), Hour((std::int32_t)tm.tm_hour),
			Day((std::int32_t)tm.tm_mday), DayOfYear((std::int32_t)tm.tm_yday), Month((std::int32_t)tm.tm_mon),
			Year(1900 + (std::int32_t)tm.tm_year), _dayOfWeek((std::int32_t)tm.tm_wday)
	{
	}

	bool DateTime::Tm::IsValid() const noexcept
	{
		return (Year != -1 && Month >= 0 && Month < 12 && (Day > 0 && Day <= Implementation::GetNumberOfDaysInMonth(Month, Year)) &&
			(Hour >= 0 && Hour < 24) && (Minute >= 0 && Minute < 60) && (Second >= 0 && Second < 62) && (Millisecond >= 0 && Millisecond < 1000));
	}

	void DateTime::Tm::AddMonths(std::int32_t monDiff) noexcept
	{
		while (monDiff < -Month) {
			Year--;
			monDiff += MONTHS_IN_YEAR;
		}

		while (monDiff + Month >= MONTHS_IN_YEAR) {
			Year++;
			monDiff -= MONTHS_IN_YEAR;
		}

		Month += monDiff;
	}

	void DateTime::Tm::AddDays(std::int32_t dayDiff) noexcept
	{
		while (dayDiff + Day < 1) {
			AddMonths(-1);
			dayDiff += Implementation::GetNumberOfDaysInMonth(Month, Year);
		}

		Day += dayDiff;

		while (Day > Implementation::GetNumberOfDaysInMonth(Month, Year)) {
			Day -= Implementation::GetNumberOfDaysInMonth(Month, Year);
			AddMonths(1);
		}
	}

	void DateTime::Tm::ComputeWeekDay() noexcept
	{
		_dayOfWeek = (Implementation::GetTruncatedJDN(Day, Month, Year) + 2) % 7;
	}

	DateTime DateTime::UtcNow() noexcept
	{
#if defined(DEATH_TARGET_WINDOWS)
		FILETIME ft;
		::GetSystemTimeAsFileTime(&ft);
		return DateTime(ft);
#else
		struct timeval tp{};
		gettimeofday(&tp, nullptr);
		return DateTime::FromUnixMilliseconds((tp.tv_sec * 1000) + (tp.tv_usec / 1000));
#endif
	}

#if defined(DEATH_TARGET_WINDOWS)
	DateTime::DateTime(const struct _SYSTEMTIME& st) noexcept
	{
		Set(st.wYear, st.wMonth - 1, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
	}

	DateTime::DateTime(const struct _FILETIME& ft) noexcept
	{
		ULARGE_INTEGER value;
		value.LowPart = ft.dwLowDateTime;
		value.HighPart = ft.dwHighDateTime;
		_time = ((value.QuadPart - 116444736000000000LL) / 10000LL);
	}
#endif

	DateTime& DateTime::Set(const struct tm& tm) noexcept
	{
		struct tm tm2(tm);
		time_t timet = mktime(&tm2);

		if (timet == (time_t)-1) {
			// mktime() fails for Jan 1, 1970 if the hour is less than timezone - try to make it work for this case
			if (tm2.tm_year == 70 && tm2.tm_mon == 0 && tm2.tm_mday == 1) {
				return Set((time_t)(
					Implementation::GetTimeZone() +
					tm2.tm_hour * MIN_PER_HOUR * SEC_PER_MIN +
					tm2.tm_min * SEC_PER_MIN +
					tm2.tm_sec));
			}

			LOGE("mktime() failed");

			_time = INT64_MIN;
			return *this;
		}

		// Standardize on moving the time forwards to have consistent behaviour under all platforms and to avoid problems
		if (tm2.tm_hour != tm.tm_hour) {
			tm2 = tm;
			tm2.tm_hour++;
			if (tm2.tm_hour == 24) {
				// This shouldn't normally happen as the DST never starts at 23:00 but if it does, we have a problem as we need to adjust the day
				// as well. However we stop here, i.e. we don't adjust the month (or the year) because mktime() is supposed to take care of this for us.
				tm2.tm_hour = 0;
				tm2.tm_mday++;
			}

			timet = mktime(&tm2);
		}

		return Set(timet);
	}

	DateTime& DateTime::Set(std::int32_t year, std::int32_t month, std::int32_t day, std::int32_t hour, std::int32_t minute, std::int32_t second, std::int32_t millisec) noexcept
	{
		DEATH_ASSERT(month >= 0 && month < 12 && day > 0 && day <= Implementation::GetNumberOfDaysInMonth(month, year) &&
			hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 61 /* with leap second */ &&
			millisec >= 0 && millisec < 1000, "Invalid date specified", *this);

		// The range of time_t type (inclusive)
		constexpr std::int32_t YearMinInRange = 1970;
		constexpr std::int32_t YearMaxInRange = 2037;

		if (year >= YearMinInRange && year <= YearMaxInRange) {
			// Use the standard library version if the date is in range
			struct tm tm;
			tm.tm_year = year - 1900;
			tm.tm_mon = month;
			tm.tm_mday = day;
			tm.tm_hour = hour;
			tm.tm_min = minute;
			tm.tm_sec = second;
			tm.tm_isdst = -1;		// mktime() will guess it
			Set(tm);

			if (IsValid()) {
				SetMillisecond(millisec);
			}
		} else {
			// Get the JDN for the midnight of this day
			_time = Implementation::GetTruncatedJDN(day, month, year);
			_time -= EPOCH_JDN;
			_time *= SECONDS_PER_DAY * 1000;

			// JDN corresponds to GMT, we take localtime
			_time += TimeSpan(hour, minute, second + Implementation::GetTimeZone(), millisec).GetValue();
		}

		return *this;
	}

	DateTime::Tm DateTime::Partitioned(TimeZone tz) const noexcept
	{
		if (!IsValid()) {
			return {};
		}

		time_t time = GetTicks();
		if (time != (time_t)-1) {
			struct tm temp;
			if (const tm* tm = Implementation::TryGetTm(time, tz, &temp)) {
				Tm tm2(*tm, tz);
				std::int32_t timeOnly = (std::int32_t)(_time % MILLISECONDS_PER_DAY);
				tm2.Millisecond = (std::int32_t)(timeOnly % 1000);
				return tm2;
			}
		}

		// If standard library functions cannot be used, try to guess it
		std::int32_t secDiff = tz.GetOffset();
		std::int64_t timeMidnight = _time + (secDiff * 1000);
		std::int32_t timeOnly = (std::int32_t)(timeMidnight % MILLISECONDS_PER_DAY);

		if (timeOnly < 0) {
			timeOnly = MILLISECONDS_PER_DAY + timeOnly;
		}

		timeMidnight -= timeOnly;

		std::int32_t jdn = (std::int32_t)(timeMidnight / MILLISECONDS_PER_DAY) + EPOCH_JDN;

		// Calculate the century
		std::int32_t temp = (jdn + JDN_OFFSET) * 4 - 1;
		std::int32_t century = temp / DAYS_PER_400_YEARS;

		// Then the year and day of year (1 <= dayOfYear <= 366)
		temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3;
		std::int32_t year = (century * 100) + (temp / DAYS_PER_4_YEARS);
		std::int32_t dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1;

		// And finally the month and day of the month
		temp = dayOfYear * 5 - 3;
		std::int32_t month = temp / DAYS_PER_5_MONTHS;
		std::int32_t day = (temp % DAYS_PER_5_MONTHS) / 5 + 1;

		// Month is counted from March, so shift it back
		if (month < 10) {
			month += 3;
		} else {
			year += 1;
			month -= 9;
		}

		// Year is offset by 4800
		year -= 4800;

		// Construct Tm from these values
		Tm tm;
		tm.Year = year;
		tm.DayOfYear = dayOfYear - 1;
		tm.Month = month - 1;
		tm.Day = day;
		tm.Millisecond = timeOnly % 1000;
		timeOnly -= tm.Millisecond;
		timeOnly /= 1000;

		tm.Second = timeOnly % SEC_PER_MIN;
		timeOnly -= tm.Second;
		timeOnly /= SEC_PER_MIN;

		tm.Minute = timeOnly % MIN_PER_HOUR;
		timeOnly -= tm.Minute;

		tm.Hour = timeOnly / MIN_PER_HOUR;

		return tm;
	}

	DateTime& DateTime::SetYear(std::int32_t year) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Year = year;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetMonth(std::int32_t month) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Month = month;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetDay(std::int32_t day) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Day = day;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetHour(std::int32_t hour) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Hour = hour;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetMinute(std::int32_t minute) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Minute = minute;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetSecond(std::int32_t second) noexcept
	{
		if (IsValid()) {
			Tm tm(Partitioned());
			tm.Second = second;
			Set(tm);
		}
		return *this;
	}

	DateTime& DateTime::SetMillisecond(std::int32_t millisecond) noexcept
	{
		if (IsValid()) {
			_time -= _time % 1000;
			_time += millisecond;
		}
		return *this;
	}

	DateTime& DateTime::ResetTime() noexcept
	{
		Tm tm(Partitioned());
		if (tm.Hour != 0 || tm.Minute != 0 || tm.Second != 0 || tm.Millisecond != 0) {
			tm.Millisecond = tm.Second = tm.Minute = tm.Hour = 0;
			Set(tm);
		}
		return *this;
	}

	void DateTime::AdjustToTimezone(TimeZone tz, bool noDST) noexcept
	{
		std::int32_t secDiff = Implementation::GetTimeZone() + tz.GetOffset();
		std::int32_t bias = 0;

		// We are converting from the local time to some other time zone, but local time zone does not include the DST offset
		// (as it varies depending on the date), so we have to handle DST manually, unless a special flag inhibiting this was specified.
		if (!tz.IsLocal() && !noDST && Implementation::IsDST(*this, bias)) {
			secDiff -= bias;
		}

		_time += secDiff * 1000;
	}

	void DateTime::AdjustFromTimezone(TimeZone tz, bool noDST) noexcept
	{
		std::int32_t secDiff = Implementation::GetTimeZone() + tz.GetOffset();
		std::int32_t bias = 0;

		if (!tz.IsLocal() && !noDST && Implementation::IsDST(*this, bias)) {
			secDiff -= bias;
		}

		_time -= secDiff * 1000;	// To milliseconds
	}

#if defined(DEATH_TARGET_WINDOWS)
	struct _SYSTEMTIME DateTime::ToWin32() const noexcept
	{
		Tm tm(Partitioned());

		struct _SYSTEMTIME st;
		st.wYear = (WORD)tm.Year;
		st.wMonth = (WORD)(tm.Month + 1);
		st.wDay = (WORD)tm.Day;

		st.wDayOfWeek = 0;
		st.wHour = (WORD)tm.Hour;
		st.wMinute = (WORD)tm.Minute;
		st.wSecond = (WORD)tm.Second;
		st.wMilliseconds = (WORD)tm.Millisecond;
		return st;
	}
#endif

	bool DateTime::TryParse(StringView input, StringView format, StringView* endParse) noexcept
	{
		std::size_t endIndex;
		if (!Implementation::TryParseFormat(*this, input.data(), input.size(), format.data(), format.size(), &endIndex)) {
			return false;
		}
		if (endParse != nullptr) {
			*endParse = input.exceptPrefix(endIndex);
		}
		return true;
	}

#if defined(DEATH_USE_WCHAR)
	bool DateTime::TryParse(std::wstring_view input, std::wstring_view format, std::wstring_view::const_iterator* endParse) noexcept
	{
		std::size_t endIndex;
		if (!Implementation::TryParseFormat(*this, input.data(), input.size(), format.data(), format.size(), &endIndex)) {
			return false;
		}
		if (endParse != nullptr) {
			*endParse = input.begin() + endIndex;
		}
		return true;
	}
#endif

	String DateTime::ToString() const noexcept
	{
		auto p = Partitioned();

		char result[64];
		std::int32_t length = snprintf(result, sizeof(result), "%04d/%02d/%02d %02d:%02d:%02d.%03d", p.Year, p.Month + 1, p.Day, p.Hour, p.Minute, p.Second, p.Millisecond);
		return String(result, length);
	}

	String TimeSpan::ToString() const noexcept
	{
		std::int64_t v = _value;
		std::int32_t milliseconds = (std::int32_t)(v % 1000);
		v /= 1000;
		std::int32_t seconds = (std::int32_t)(v % 60);
		v /= 60;
		std::int32_t minutes = (std::int32_t)(v % 60);
		v /= 60;

		char result[64];
		std::int32_t length = snprintf(result, sizeof(result), "%d:%02d:%02d.%03d", std::int32_t(v), minutes, seconds, milliseconds);
		return String(result, length);
	}

}}