Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestTimeUtils.cpp
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *      This file implements a process to effect a functional test for
23  *      the CHIP date and time support utilities.
24  *
25  */
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <time.h>
31
32 #include <support/TimeUtils.h>
33 #include <support/UnitTestRegistration.h>
34 #include <support/logging/CHIPLogging.h>
35
36 using namespace chip;
37
38 static void Abort()
39 {
40     abort();
41 }
42
43 void TestAssert(bool assert, const char * msg)
44 {
45     if (!assert)
46     {
47         printf("%s\n", msg);
48         Abort();
49     }
50 }
51
52 struct OrdinalDateTestValue
53 {
54     uint16_t DayOfYear;
55     uint8_t Month;
56     uint8_t DayOfMonth;
57 };
58
59 // clang-format off
60 OrdinalDateTestValue StandardYearOrdinalDates[] =
61 {
62     { 1, 1, 1 },
63     { 2, 1, 2 },
64     { 3, 1, 3 },
65     { 4, 1, 4 },
66     { 5, 1, 5 },
67     { 6, 1, 6 },
68     { 7, 1, 7 },
69     { 8, 1, 8 },
70     { 9, 1, 9 },
71     { 10, 1, 10 },
72     { 11, 1, 11 },
73     { 12, 1, 12 },
74     { 13, 1, 13 },
75     { 14, 1, 14 },
76     { 15, 1, 15 },
77     { 16, 1, 16 },
78     { 17, 1, 17 },
79     { 18, 1, 18 },
80     { 19, 1, 19 },
81     { 20, 1, 20 },
82     { 21, 1, 21 },
83     { 22, 1, 22 },
84     { 23, 1, 23 },
85     { 24, 1, 24 },
86     { 25, 1, 25 },
87     { 26, 1, 26 },
88     { 27, 1, 27 },
89     { 28, 1, 28 },
90     { 29, 1, 29 },
91     { 30, 1, 30 },
92     { 31, 1, 31 },
93     { 32, 2, 1 },
94     { 33, 2, 2 },
95     { 34, 2, 3 },
96     { 35, 2, 4 },
97     { 36, 2, 5 },
98     { 37, 2, 6 },
99     { 38, 2, 7 },
100     { 39, 2, 8 },
101     { 40, 2, 9 },
102     { 41, 2, 10 },
103     { 42, 2, 11 },
104     { 43, 2, 12 },
105     { 44, 2, 13 },
106     { 45, 2, 14 },
107     { 46, 2, 15 },
108     { 47, 2, 16 },
109     { 48, 2, 17 },
110     { 49, 2, 18 },
111     { 50, 2, 19 },
112     { 51, 2, 20 },
113     { 52, 2, 21 },
114     { 53, 2, 22 },
115     { 54, 2, 23 },
116     { 55, 2, 24 },
117     { 56, 2, 25 },
118     { 57, 2, 26 },
119     { 58, 2, 27 },
120     { 59, 2, 28 },
121     { 60, 3, 1 },
122     { 61, 3, 2 },
123     { 62, 3, 3 },
124     { 63, 3, 4 },
125     { 64, 3, 5 },
126     { 65, 3, 6 },
127     { 66, 3, 7 },
128     { 67, 3, 8 },
129     { 68, 3, 9 },
130     { 69, 3, 10 },
131     { 70, 3, 11 },
132     { 71, 3, 12 },
133     { 72, 3, 13 },
134     { 73, 3, 14 },
135     { 74, 3, 15 },
136     { 75, 3, 16 },
137     { 76, 3, 17 },
138     { 77, 3, 18 },
139     { 78, 3, 19 },
140     { 79, 3, 20 },
141     { 80, 3, 21 },
142     { 81, 3, 22 },
143     { 82, 3, 23 },
144     { 83, 3, 24 },
145     { 84, 3, 25 },
146     { 85, 3, 26 },
147     { 86, 3, 27 },
148     { 87, 3, 28 },
149     { 88, 3, 29 },
150     { 89, 3, 30 },
151     { 90, 3, 31 },
152     { 91, 4, 1 },
153     { 92, 4, 2 },
154     { 93, 4, 3 },
155     { 94, 4, 4 },
156     { 95, 4, 5 },
157     { 96, 4, 6 },
158     { 97, 4, 7 },
159     { 98, 4, 8 },
160     { 99, 4, 9 },
161     { 100, 4, 10 },
162     { 101, 4, 11 },
163     { 102, 4, 12 },
164     { 103, 4, 13 },
165     { 104, 4, 14 },
166     { 105, 4, 15 },
167     { 106, 4, 16 },
168     { 107, 4, 17 },
169     { 108, 4, 18 },
170     { 109, 4, 19 },
171     { 110, 4, 20 },
172     { 111, 4, 21 },
173     { 112, 4, 22 },
174     { 113, 4, 23 },
175     { 114, 4, 24 },
176     { 115, 4, 25 },
177     { 116, 4, 26 },
178     { 117, 4, 27 },
179     { 118, 4, 28 },
180     { 119, 4, 29 },
181     { 120, 4, 30 },
182     { 121, 5, 1 },
183     { 122, 5, 2 },
184     { 123, 5, 3 },
185     { 124, 5, 4 },
186     { 125, 5, 5 },
187     { 126, 5, 6 },
188     { 127, 5, 7 },
189     { 128, 5, 8 },
190     { 129, 5, 9 },
191     { 130, 5, 10 },
192     { 131, 5, 11 },
193     { 132, 5, 12 },
194     { 133, 5, 13 },
195     { 134, 5, 14 },
196     { 135, 5, 15 },
197     { 136, 5, 16 },
198     { 137, 5, 17 },
199     { 138, 5, 18 },
200     { 139, 5, 19 },
201     { 140, 5, 20 },
202     { 141, 5, 21 },
203     { 142, 5, 22 },
204     { 143, 5, 23 },
205     { 144, 5, 24 },
206     { 145, 5, 25 },
207     { 146, 5, 26 },
208     { 147, 5, 27 },
209     { 148, 5, 28 },
210     { 149, 5, 29 },
211     { 150, 5, 30 },
212     { 151, 5, 31 },
213     { 152, 6, 1 },
214     { 153, 6, 2 },
215     { 154, 6, 3 },
216     { 155, 6, 4 },
217     { 156, 6, 5 },
218     { 157, 6, 6 },
219     { 158, 6, 7 },
220     { 159, 6, 8 },
221     { 160, 6, 9 },
222     { 161, 6, 10 },
223     { 162, 6, 11 },
224     { 163, 6, 12 },
225     { 164, 6, 13 },
226     { 165, 6, 14 },
227     { 166, 6, 15 },
228     { 167, 6, 16 },
229     { 168, 6, 17 },
230     { 169, 6, 18 },
231     { 170, 6, 19 },
232     { 171, 6, 20 },
233     { 172, 6, 21 },
234     { 173, 6, 22 },
235     { 174, 6, 23 },
236     { 175, 6, 24 },
237     { 176, 6, 25 },
238     { 177, 6, 26 },
239     { 178, 6, 27 },
240     { 179, 6, 28 },
241     { 180, 6, 29 },
242     { 181, 6, 30 },
243     { 182, 7, 1 },
244     { 183, 7, 2 },
245     { 184, 7, 3 },
246     { 185, 7, 4 },
247     { 186, 7, 5 },
248     { 187, 7, 6 },
249     { 188, 7, 7 },
250     { 189, 7, 8 },
251     { 190, 7, 9 },
252     { 191, 7, 10 },
253     { 192, 7, 11 },
254     { 193, 7, 12 },
255     { 194, 7, 13 },
256     { 195, 7, 14 },
257     { 196, 7, 15 },
258     { 197, 7, 16 },
259     { 198, 7, 17 },
260     { 199, 7, 18 },
261     { 200, 7, 19 },
262     { 201, 7, 20 },
263     { 202, 7, 21 },
264     { 203, 7, 22 },
265     { 204, 7, 23 },
266     { 205, 7, 24 },
267     { 206, 7, 25 },
268     { 207, 7, 26 },
269     { 208, 7, 27 },
270     { 209, 7, 28 },
271     { 210, 7, 29 },
272     { 211, 7, 30 },
273     { 212, 7, 31 },
274     { 213, 8, 1 },
275     { 214, 8, 2 },
276     { 215, 8, 3 },
277     { 216, 8, 4 },
278     { 217, 8, 5 },
279     { 218, 8, 6 },
280     { 219, 8, 7 },
281     { 220, 8, 8 },
282     { 221, 8, 9 },
283     { 222, 8, 10 },
284     { 223, 8, 11 },
285     { 224, 8, 12 },
286     { 225, 8, 13 },
287     { 226, 8, 14 },
288     { 227, 8, 15 },
289     { 228, 8, 16 },
290     { 229, 8, 17 },
291     { 230, 8, 18 },
292     { 231, 8, 19 },
293     { 232, 8, 20 },
294     { 233, 8, 21 },
295     { 234, 8, 22 },
296     { 235, 8, 23 },
297     { 236, 8, 24 },
298     { 237, 8, 25 },
299     { 238, 8, 26 },
300     { 239, 8, 27 },
301     { 240, 8, 28 },
302     { 241, 8, 29 },
303     { 242, 8, 30 },
304     { 243, 8, 31 },
305     { 244, 9, 1 },
306     { 245, 9, 2 },
307     { 246, 9, 3 },
308     { 247, 9, 4 },
309     { 248, 9, 5 },
310     { 249, 9, 6 },
311     { 250, 9, 7 },
312     { 251, 9, 8 },
313     { 252, 9, 9 },
314     { 253, 9, 10 },
315     { 254, 9, 11 },
316     { 255, 9, 12 },
317     { 256, 9, 13 },
318     { 257, 9, 14 },
319     { 258, 9, 15 },
320     { 259, 9, 16 },
321     { 260, 9, 17 },
322     { 261, 9, 18 },
323     { 262, 9, 19 },
324     { 263, 9, 20 },
325     { 264, 9, 21 },
326     { 265, 9, 22 },
327     { 266, 9, 23 },
328     { 267, 9, 24 },
329     { 268, 9, 25 },
330     { 269, 9, 26 },
331     { 270, 9, 27 },
332     { 271, 9, 28 },
333     { 272, 9, 29 },
334     { 273, 9, 30 },
335     { 274, 10, 1 },
336     { 275, 10, 2 },
337     { 276, 10, 3 },
338     { 277, 10, 4 },
339     { 278, 10, 5 },
340     { 279, 10, 6 },
341     { 280, 10, 7 },
342     { 281, 10, 8 },
343     { 282, 10, 9 },
344     { 283, 10, 10 },
345     { 284, 10, 11 },
346     { 285, 10, 12 },
347     { 286, 10, 13 },
348     { 287, 10, 14 },
349     { 288, 10, 15 },
350     { 289, 10, 16 },
351     { 290, 10, 17 },
352     { 291, 10, 18 },
353     { 292, 10, 19 },
354     { 293, 10, 20 },
355     { 294, 10, 21 },
356     { 295, 10, 22 },
357     { 296, 10, 23 },
358     { 297, 10, 24 },
359     { 298, 10, 25 },
360     { 299, 10, 26 },
361     { 300, 10, 27 },
362     { 301, 10, 28 },
363     { 302, 10, 29 },
364     { 303, 10, 30 },
365     { 304, 10, 31 },
366     { 305, 11, 1 },
367     { 306, 11, 2 },
368     { 307, 11, 3 },
369     { 308, 11, 4 },
370     { 309, 11, 5 },
371     { 310, 11, 6 },
372     { 311, 11, 7 },
373     { 312, 11, 8 },
374     { 313, 11, 9 },
375     { 314, 11, 10 },
376     { 315, 11, 11 },
377     { 316, 11, 12 },
378     { 317, 11, 13 },
379     { 318, 11, 14 },
380     { 319, 11, 15 },
381     { 320, 11, 16 },
382     { 321, 11, 17 },
383     { 322, 11, 18 },
384     { 323, 11, 19 },
385     { 324, 11, 20 },
386     { 325, 11, 21 },
387     { 326, 11, 22 },
388     { 327, 11, 23 },
389     { 328, 11, 24 },
390     { 329, 11, 25 },
391     { 330, 11, 26 },
392     { 331, 11, 27 },
393     { 332, 11, 28 },
394     { 333, 11, 29 },
395     { 334, 11, 30 },
396     { 335, 12, 1 },
397     { 336, 12, 2 },
398     { 337, 12, 3 },
399     { 338, 12, 4 },
400     { 339, 12, 5 },
401     { 340, 12, 6 },
402     { 341, 12, 7 },
403     { 342, 12, 8 },
404     { 343, 12, 9 },
405     { 344, 12, 10 },
406     { 345, 12, 11 },
407     { 346, 12, 12 },
408     { 347, 12, 13 },
409     { 348, 12, 14 },
410     { 349, 12, 15 },
411     { 350, 12, 16 },
412     { 351, 12, 17 },
413     { 352, 12, 18 },
414     { 353, 12, 19 },
415     { 354, 12, 20 },
416     { 355, 12, 21 },
417     { 356, 12, 22 },
418     { 357, 12, 23 },
419     { 358, 12, 24 },
420     { 359, 12, 25 },
421     { 360, 12, 26 },
422     { 361, 12, 27 },
423     { 362, 12, 28 },
424     { 363, 12, 29 },
425     { 364, 12, 30 },
426     { 365, 12, 31 },
427     { 0, 0, 0 }
428 };
429
430 OrdinalDateTestValue LeapYearOrdinalDates[] =
431 {
432     { 1, 1, 1 },
433     { 2, 1, 2 },
434     { 3, 1, 3 },
435     { 4, 1, 4 },
436     { 5, 1, 5 },
437     { 6, 1, 6 },
438     { 7, 1, 7 },
439     { 8, 1, 8 },
440     { 9, 1, 9 },
441     { 10, 1, 10 },
442     { 11, 1, 11 },
443     { 12, 1, 12 },
444     { 13, 1, 13 },
445     { 14, 1, 14 },
446     { 15, 1, 15 },
447     { 16, 1, 16 },
448     { 17, 1, 17 },
449     { 18, 1, 18 },
450     { 19, 1, 19 },
451     { 20, 1, 20 },
452     { 21, 1, 21 },
453     { 22, 1, 22 },
454     { 23, 1, 23 },
455     { 24, 1, 24 },
456     { 25, 1, 25 },
457     { 26, 1, 26 },
458     { 27, 1, 27 },
459     { 28, 1, 28 },
460     { 29, 1, 29 },
461     { 30, 1, 30 },
462     { 31, 1, 31 },
463     { 32, 2, 1 },
464     { 33, 2, 2 },
465     { 34, 2, 3 },
466     { 35, 2, 4 },
467     { 36, 2, 5 },
468     { 37, 2, 6 },
469     { 38, 2, 7 },
470     { 39, 2, 8 },
471     { 40, 2, 9 },
472     { 41, 2, 10 },
473     { 42, 2, 11 },
474     { 43, 2, 12 },
475     { 44, 2, 13 },
476     { 45, 2, 14 },
477     { 46, 2, 15 },
478     { 47, 2, 16 },
479     { 48, 2, 17 },
480     { 49, 2, 18 },
481     { 50, 2, 19 },
482     { 51, 2, 20 },
483     { 52, 2, 21 },
484     { 53, 2, 22 },
485     { 54, 2, 23 },
486     { 55, 2, 24 },
487     { 56, 2, 25 },
488     { 57, 2, 26 },
489     { 58, 2, 27 },
490     { 59, 2, 28 },
491     { 60, 2, 29 },
492     { 61, 3, 1 },
493     { 62, 3, 2 },
494     { 63, 3, 3 },
495     { 64, 3, 4 },
496     { 65, 3, 5 },
497     { 66, 3, 6 },
498     { 67, 3, 7 },
499     { 68, 3, 8 },
500     { 69, 3, 9 },
501     { 70, 3, 10 },
502     { 71, 3, 11 },
503     { 72, 3, 12 },
504     { 73, 3, 13 },
505     { 74, 3, 14 },
506     { 75, 3, 15 },
507     { 76, 3, 16 },
508     { 77, 3, 17 },
509     { 78, 3, 18 },
510     { 79, 3, 19 },
511     { 80, 3, 20 },
512     { 81, 3, 21 },
513     { 82, 3, 22 },
514     { 83, 3, 23 },
515     { 84, 3, 24 },
516     { 85, 3, 25 },
517     { 86, 3, 26 },
518     { 87, 3, 27 },
519     { 88, 3, 28 },
520     { 89, 3, 29 },
521     { 90, 3, 30 },
522     { 91, 3, 31 },
523     { 92, 4, 1 },
524     { 93, 4, 2 },
525     { 94, 4, 3 },
526     { 95, 4, 4 },
527     { 96, 4, 5 },
528     { 97, 4, 6 },
529     { 98, 4, 7 },
530     { 99, 4, 8 },
531     { 100, 4, 9 },
532     { 101, 4, 10 },
533     { 102, 4, 11 },
534     { 103, 4, 12 },
535     { 104, 4, 13 },
536     { 105, 4, 14 },
537     { 106, 4, 15 },
538     { 107, 4, 16 },
539     { 108, 4, 17 },
540     { 109, 4, 18 },
541     { 110, 4, 19 },
542     { 111, 4, 20 },
543     { 112, 4, 21 },
544     { 113, 4, 22 },
545     { 114, 4, 23 },
546     { 115, 4, 24 },
547     { 116, 4, 25 },
548     { 117, 4, 26 },
549     { 118, 4, 27 },
550     { 119, 4, 28 },
551     { 120, 4, 29 },
552     { 121, 4, 30 },
553     { 122, 5, 1 },
554     { 123, 5, 2 },
555     { 124, 5, 3 },
556     { 125, 5, 4 },
557     { 126, 5, 5 },
558     { 127, 5, 6 },
559     { 128, 5, 7 },
560     { 129, 5, 8 },
561     { 130, 5, 9 },
562     { 131, 5, 10 },
563     { 132, 5, 11 },
564     { 133, 5, 12 },
565     { 134, 5, 13 },
566     { 135, 5, 14 },
567     { 136, 5, 15 },
568     { 137, 5, 16 },
569     { 138, 5, 17 },
570     { 139, 5, 18 },
571     { 140, 5, 19 },
572     { 141, 5, 20 },
573     { 142, 5, 21 },
574     { 143, 5, 22 },
575     { 144, 5, 23 },
576     { 145, 5, 24 },
577     { 146, 5, 25 },
578     { 147, 5, 26 },
579     { 148, 5, 27 },
580     { 149, 5, 28 },
581     { 150, 5, 29 },
582     { 151, 5, 30 },
583     { 152, 5, 31 },
584     { 153, 6, 1 },
585     { 154, 6, 2 },
586     { 155, 6, 3 },
587     { 156, 6, 4 },
588     { 157, 6, 5 },
589     { 158, 6, 6 },
590     { 159, 6, 7 },
591     { 160, 6, 8 },
592     { 161, 6, 9 },
593     { 162, 6, 10 },
594     { 163, 6, 11 },
595     { 164, 6, 12 },
596     { 165, 6, 13 },
597     { 166, 6, 14 },
598     { 167, 6, 15 },
599     { 168, 6, 16 },
600     { 169, 6, 17 },
601     { 170, 6, 18 },
602     { 171, 6, 19 },
603     { 172, 6, 20 },
604     { 173, 6, 21 },
605     { 174, 6, 22 },
606     { 175, 6, 23 },
607     { 176, 6, 24 },
608     { 177, 6, 25 },
609     { 178, 6, 26 },
610     { 179, 6, 27 },
611     { 180, 6, 28 },
612     { 181, 6, 29 },
613     { 182, 6, 30 },
614     { 183, 7, 1 },
615     { 184, 7, 2 },
616     { 185, 7, 3 },
617     { 186, 7, 4 },
618     { 187, 7, 5 },
619     { 188, 7, 6 },
620     { 189, 7, 7 },
621     { 190, 7, 8 },
622     { 191, 7, 9 },
623     { 192, 7, 10 },
624     { 193, 7, 11 },
625     { 194, 7, 12 },
626     { 195, 7, 13 },
627     { 196, 7, 14 },
628     { 197, 7, 15 },
629     { 198, 7, 16 },
630     { 199, 7, 17 },
631     { 200, 7, 18 },
632     { 201, 7, 19 },
633     { 202, 7, 20 },
634     { 203, 7, 21 },
635     { 204, 7, 22 },
636     { 205, 7, 23 },
637     { 206, 7, 24 },
638     { 207, 7, 25 },
639     { 208, 7, 26 },
640     { 209, 7, 27 },
641     { 210, 7, 28 },
642     { 211, 7, 29 },
643     { 212, 7, 30 },
644     { 213, 7, 31 },
645     { 214, 8, 1 },
646     { 215, 8, 2 },
647     { 216, 8, 3 },
648     { 217, 8, 4 },
649     { 218, 8, 5 },
650     { 219, 8, 6 },
651     { 220, 8, 7 },
652     { 221, 8, 8 },
653     { 222, 8, 9 },
654     { 223, 8, 10 },
655     { 224, 8, 11 },
656     { 225, 8, 12 },
657     { 226, 8, 13 },
658     { 227, 8, 14 },
659     { 228, 8, 15 },
660     { 229, 8, 16 },
661     { 230, 8, 17 },
662     { 231, 8, 18 },
663     { 232, 8, 19 },
664     { 233, 8, 20 },
665     { 234, 8, 21 },
666     { 235, 8, 22 },
667     { 236, 8, 23 },
668     { 237, 8, 24 },
669     { 238, 8, 25 },
670     { 239, 8, 26 },
671     { 240, 8, 27 },
672     { 241, 8, 28 },
673     { 242, 8, 29 },
674     { 243, 8, 30 },
675     { 244, 8, 31 },
676     { 245, 9, 1 },
677     { 246, 9, 2 },
678     { 247, 9, 3 },
679     { 248, 9, 4 },
680     { 249, 9, 5 },
681     { 250, 9, 6 },
682     { 251, 9, 7 },
683     { 252, 9, 8 },
684     { 253, 9, 9 },
685     { 254, 9, 10 },
686     { 255, 9, 11 },
687     { 256, 9, 12 },
688     { 257, 9, 13 },
689     { 258, 9, 14 },
690     { 259, 9, 15 },
691     { 260, 9, 16 },
692     { 261, 9, 17 },
693     { 262, 9, 18 },
694     { 263, 9, 19 },
695     { 264, 9, 20 },
696     { 265, 9, 21 },
697     { 266, 9, 22 },
698     { 267, 9, 23 },
699     { 268, 9, 24 },
700     { 269, 9, 25 },
701     { 270, 9, 26 },
702     { 271, 9, 27 },
703     { 272, 9, 28 },
704     { 273, 9, 29 },
705     { 274, 9, 30 },
706     { 275, 10, 1 },
707     { 276, 10, 2 },
708     { 277, 10, 3 },
709     { 278, 10, 4 },
710     { 279, 10, 5 },
711     { 280, 10, 6 },
712     { 281, 10, 7 },
713     { 282, 10, 8 },
714     { 283, 10, 9 },
715     { 284, 10, 10 },
716     { 285, 10, 11 },
717     { 286, 10, 12 },
718     { 287, 10, 13 },
719     { 288, 10, 14 },
720     { 289, 10, 15 },
721     { 290, 10, 16 },
722     { 291, 10, 17 },
723     { 292, 10, 18 },
724     { 293, 10, 19 },
725     { 294, 10, 20 },
726     { 295, 10, 21 },
727     { 296, 10, 22 },
728     { 297, 10, 23 },
729     { 298, 10, 24 },
730     { 299, 10, 25 },
731     { 300, 10, 26 },
732     { 301, 10, 27 },
733     { 302, 10, 28 },
734     { 303, 10, 29 },
735     { 304, 10, 30 },
736     { 305, 10, 31 },
737     { 306, 11, 1 },
738     { 307, 11, 2 },
739     { 308, 11, 3 },
740     { 309, 11, 4 },
741     { 310, 11, 5 },
742     { 311, 11, 6 },
743     { 312, 11, 7 },
744     { 313, 11, 8 },
745     { 314, 11, 9 },
746     { 315, 11, 10 },
747     { 316, 11, 11 },
748     { 317, 11, 12 },
749     { 318, 11, 13 },
750     { 319, 11, 14 },
751     { 320, 11, 15 },
752     { 321, 11, 16 },
753     { 322, 11, 17 },
754     { 323, 11, 18 },
755     { 324, 11, 19 },
756     { 325, 11, 20 },
757     { 326, 11, 21 },
758     { 327, 11, 22 },
759     { 328, 11, 23 },
760     { 329, 11, 24 },
761     { 330, 11, 25 },
762     { 331, 11, 26 },
763     { 332, 11, 27 },
764     { 333, 11, 28 },
765     { 334, 11, 29 },
766     { 335, 11, 30 },
767     { 336, 12, 1 },
768     { 337, 12, 2 },
769     { 338, 12, 3 },
770     { 339, 12, 4 },
771     { 340, 12, 5 },
772     { 341, 12, 6 },
773     { 342, 12, 7 },
774     { 343, 12, 8 },
775     { 344, 12, 9 },
776     { 345, 12, 10 },
777     { 346, 12, 11 },
778     { 347, 12, 12 },
779     { 348, 12, 13 },
780     { 349, 12, 14 },
781     { 350, 12, 15 },
782     { 351, 12, 16 },
783     { 352, 12, 17 },
784     { 353, 12, 18 },
785     { 354, 12, 19 },
786     { 355, 12, 20 },
787     { 356, 12, 21 },
788     { 357, 12, 22 },
789     { 358, 12, 23 },
790     { 359, 12, 24 },
791     { 360, 12, 25 },
792     { 361, 12, 26 },
793     { 362, 12, 27 },
794     { 363, 12, 28 },
795     { 364, 12, 29 },
796     { 365, 12, 30 },
797     { 366, 12, 31 },
798     { 0, 0, 0 }
799 };
800 // clang-format on
801
802 void TestOrdinalDateConversion()
803 {
804     for (uint16_t year = 0; year <= 10000; year++)
805     {
806         OrdinalDateTestValue * testValue = (IsLeapYear(year)) ? LeapYearOrdinalDates : StandardYearOrdinalDates;
807
808         for (; testValue->DayOfYear != 0; testValue++)
809         {
810             uint16_t outDayOfYear = 0;
811             uint8_t outMonth = 0, outDayOfMonth = 0;
812
813             OrdinalDateToCalendarDate(year, testValue->DayOfYear, outMonth, outDayOfMonth);
814
815             TestAssert(outMonth == testValue->Month, "OrdinalDateToCalendarDate returned invalid month");
816             TestAssert(outDayOfMonth == testValue->DayOfMonth, "OrdinalDateToCalendarDate returned invalid day-of-month");
817
818             CalendarDateToOrdinalDate(year, testValue->Month, testValue->DayOfMonth, outDayOfYear);
819
820             TestAssert(outDayOfYear == testValue->DayOfYear, "CalendarDateToOrdinalDate returned invalid day-of-year");
821         }
822     }
823 }
824
825 void TestDaysSinceEpochConversion()
826 {
827     uint32_t daysSinceEpoch = 0;
828
829     for (uint16_t year = kEpochYear; year <= kMaxYearInDaysSinceEpoch32; year++)
830     {
831         for (uint8_t month = kJanuary; month <= kDecember; month++)
832         {
833             for (uint8_t dayOfMonth = 1; dayOfMonth <= DaysInMonth(year, month); dayOfMonth++)
834             {
835                 // Test CalendarDateToDaysSinceEpoch()
836                 {
837                     uint32_t calculatedDaysSinceEpoch;
838
839                     CalendarDateToDaysSinceEpoch(year, month, dayOfMonth, calculatedDaysSinceEpoch);
840
841                     if (calculatedDaysSinceEpoch != daysSinceEpoch)
842                         printf("%04u/%02u/%02u %u %u\n", year, month, dayOfMonth, daysSinceEpoch, calculatedDaysSinceEpoch);
843
844                     TestAssert(calculatedDaysSinceEpoch == daysSinceEpoch,
845                                "CalendarDateToDaysSinceEpoch() returned unexpected value");
846                 }
847
848                 // Test DaysSinceEpochToCalendarDate()
849                 {
850                     uint16_t calculatedYear;
851                     uint8_t calculatedMonth, calculatedDayOfMonth;
852
853                     DaysSinceEpochToCalendarDate(daysSinceEpoch, calculatedYear, calculatedMonth, calculatedDayOfMonth);
854
855                     if (calculatedYear != year || calculatedMonth != month || calculatedDayOfMonth != dayOfMonth)
856                         printf("%04u/%02u/%02u %04u/%02u/%02u\n", year, month, dayOfMonth, calculatedYear, calculatedMonth,
857                                calculatedDayOfMonth);
858
859                     TestAssert(calculatedYear == year, "DaysSinceEpochToCalendarDate() returned unexpected year value");
860                     TestAssert(calculatedMonth == month, "DaysSinceEpochToCalendarDate() returned unexpected month value");
861                     TestAssert(calculatedDayOfMonth == dayOfMonth,
862                                "DaysSinceEpochToCalendarDate() returned unexpected dayOfMonth value");
863                 }
864
865                 daysSinceEpoch++;
866             }
867         }
868     }
869 }
870
871 void TestSecondsSinceEpochConversion()
872 {
873     uint32_t daysSinceEpoch = 0;
874     uint32_t timeOfDay      = 0; // in seconds
875
876     for (uint16_t year = kEpochYear; year <= kMaxYearInSecondsSinceEpoch32; year++)
877     {
878         for (uint8_t month = kJanuary; month <= kDecember; month++)
879         {
880             for (uint8_t dayOfMonth = 1; dayOfMonth <= DaysInMonth(year, month); dayOfMonth++)
881             {
882                 // Test 10 different times of day per calendar day (this keeps the total runtime manageable).
883                 for (uint8_t i = 0; i < 10; i++)
884                 {
885                     uint32_t secondsSinceEpoch = daysSinceEpoch * kSecondsPerDay + timeOfDay;
886
887                     uint8_t hour   = static_cast<uint8_t>(timeOfDay / kSecondsPerHour);
888                     uint8_t minute = static_cast<uint8_t>((timeOfDay - (hour * kSecondsPerHour)) / kSecondsPerMinute);
889                     uint8_t second = static_cast<uint8_t>(timeOfDay - (hour * kSecondsPerHour + minute * kSecondsPerMinute));
890
891 #define VERIFY_TEST_AGAINST_GMTTIME 0
892 #if VERIFY_TEST_AGAINST_GMTTIME
893                     {
894                         time_t epochTimeT = static_cast<time_t>(secondsSinceEpoch);
895
896                         struct tm * gmt = gmtime(&epochTimeT);
897
898                         TestAssert(year == gmt->tm_year + 1900, "gmtime() mismatch: year");
899                         TestAssert(month == gmt->tm_mon + 1, "gmtime() mismatch: month");
900                         TestAssert(dayOfMonth == gmt->tm_mday, "gmtime() mismatch: dayOfMonth");
901                         TestAssert(hour == gmt->tm_hour, "gmtime() mismatch: hour");
902                         TestAssert(minute == gmt->tm_min, "gmtime() mismatch: minute");
903                         TestAssert(second == gmt->tm_sec, "gmtime() mismatch: second");
904                     }
905 #endif
906
907                     // Test SecondsSinceEpochToCalendarTime()
908                     {
909                         uint16_t calculatedYear;
910                         uint8_t calculatedMonth, calculatedDayOfMonth, calculatedHour, calculatedMinute, calculatedSecond;
911
912                         SecondsSinceEpochToCalendarTime(secondsSinceEpoch, calculatedYear, calculatedMonth, calculatedDayOfMonth,
913                                                         calculatedHour, calculatedMinute, calculatedSecond);
914
915                         TestAssert(calculatedYear == year, "SecondsSinceEpochToCalendarTime() returned unexpected year value");
916                         TestAssert(calculatedMonth == month, "SecondsSinceEpochToCalendarTime() returned unexpected month value");
917                         TestAssert(calculatedDayOfMonth == dayOfMonth,
918                                    "SecondsSinceEpochToCalendarTime() returned unexpected dayOfMonth value");
919                         TestAssert(calculatedHour == hour, "SecondsSinceEpochToCalendarTime() returned unexpected hour value");
920                         TestAssert(calculatedMinute == minute,
921                                    "SecondsSinceEpochToCalendarTime() returned unexpected minute value");
922                         TestAssert(calculatedSecond == second,
923                                    "SecondsSinceEpochToCalendarTime() returned unexpected second value");
924                     }
925
926                     // Test CalendarTimeToSecondsSinceEpoch()
927
928                     {
929                         uint32_t calculatedSecondsSinceEpoch;
930
931                         CalendarTimeToSecondsSinceEpoch(year, month, dayOfMonth, hour, minute, second, calculatedSecondsSinceEpoch);
932
933                         TestAssert(calculatedSecondsSinceEpoch == secondsSinceEpoch,
934                                    "CalendarTimeToSecondsSinceEpoch() returned unexpected value");
935                     }
936
937                     // Iterate through times of day by skipping a large prime number of seconds.
938                     timeOfDay = (timeOfDay + 10007) % kSecondsPerDay;
939                 }
940
941                 daysSinceEpoch++;
942             }
943         }
944     }
945 }
946
947 void TestChipEpochTimeConversion()
948 {
949     uint32_t daysSinceEpoch = 0;
950     uint32_t timeOfDay      = 0; // in seconds
951
952     for (uint16_t year = kChipEpochBaseYear; year <= kChipEpochMaxYear; year++)
953     {
954         for (uint8_t month = kJanuary; month <= kDecember; month++)
955         {
956             for (uint8_t dayOfMonth = 1; dayOfMonth <= DaysInMonth(year, month); dayOfMonth++)
957             {
958                 // Test 10 different times of day per calendar day (this keeps the total runtime manageable).
959                 for (uint8_t i = 0; i < 10; i++)
960                 {
961                     uint32_t chipEpochTime = daysSinceEpoch * kSecondsPerDay + timeOfDay;
962
963                     uint8_t hour   = static_cast<uint8_t>(timeOfDay / kSecondsPerHour);
964                     uint8_t minute = static_cast<uint8_t>((timeOfDay - (hour * kSecondsPerHour)) / kSecondsPerMinute);
965                     uint8_t second = static_cast<uint8_t>(timeOfDay - (hour * kSecondsPerHour + minute * kSecondsPerMinute));
966
967 #if VERIFY_TEST_AGAINST_GMTTIME
968                     {
969                         time_t epochTimeT = static_cast<time_t>(chipEpochTime) + kChipEpochSecondsSinceUnixEpoch;
970
971                         struct tm * gmt = gmtime(&epochTimeT);
972
973                         TestAssert(year == gmt->tm_year + 1900, "chip epoch gmtime() mismatch: year");
974                         TestAssert(month == gmt->tm_mon + 1, "chip epoch gmtime() mismatch: month");
975                         TestAssert(dayOfMonth == gmt->tm_mday, "chip epoch gmtime() mismatch: dayOfMonth");
976                         TestAssert(hour == gmt->tm_hour, "chip epoch gmtime() mismatch: hour");
977                         TestAssert(minute == gmt->tm_min, "chip epoch gmtime() mismatch: minute");
978                         TestAssert(second == gmt->tm_sec, "chip epoch gmtime() mismatch: second");
979                     }
980 #endif
981
982                     // Test ChipEpochToCalendarTime()
983                     {
984                         uint16_t calculatedYear;
985                         uint8_t calculatedMonth, calculatedDayOfMonth, calculatedHour, calculatedMinute, calculatedSecond;
986
987                         ChipEpochToCalendarTime(chipEpochTime, calculatedYear, calculatedMonth, calculatedDayOfMonth,
988                                                 calculatedHour, calculatedMinute, calculatedSecond);
989
990                         TestAssert(calculatedYear == year, "ChipEpochToCalendarTime() returned unexpected year value");
991                         TestAssert(calculatedMonth == month, "ChipEpochToCalendarTime() returned unexpected month value");
992                         TestAssert(calculatedDayOfMonth == dayOfMonth,
993                                    "ChipEpochToCalendarTime() returned unexpected dayOfMonth value");
994                         TestAssert(calculatedHour == hour, "ChipEpochToCalendarTime() returned unexpected hour value");
995                         TestAssert(calculatedMinute == minute, "ChipEpochToCalendarTime() returned unexpected minute value");
996                         TestAssert(calculatedSecond == second, "ChipEpochToCalendarTime() returned unexpected second value");
997                     }
998
999                     // Test CalendarTimeToSecondsSinceEpoch()
1000
1001                     {
1002                         uint32_t calculatedChipEpochTime;
1003
1004                         CalendarToChipEpochTime(year, month, dayOfMonth, hour, minute, second, calculatedChipEpochTime);
1005
1006                         TestAssert(calculatedChipEpochTime == chipEpochTime,
1007                                    "CalendarTimeToSecondsSinceEpoch() returned unexpected value");
1008                     }
1009
1010                     // Iterate through times of day by skipping a large prime number of seconds.
1011                     timeOfDay = (timeOfDay + 10007) % kSecondsPerDay;
1012                 }
1013
1014                 daysSinceEpoch++;
1015             }
1016         }
1017     }
1018 }
1019
1020 int TestTimeUtils(void)
1021 {
1022     TestOrdinalDateConversion();
1023     TestDaysSinceEpochConversion();
1024     TestSecondsSinceEpochConversion();
1025     TestChipEpochTimeConversion();
1026
1027     printf("All tests passed\n");
1028
1029     return (0);
1030 }
1031
1032 CHIP_REGISTER_TEST_SUITE(TestTimeUtils);