-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_api.c
executable file
·1727 lines (1394 loc) · 57.3 KB
/
time_api.c
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
/*****************************************************************************\
* *
* FILENAME: time_api.c *
* *
* --------------------------------------------------------------------------- *
* *
* DESCRIPTION: Implementation of several time handling functions. *
* *
* --------------------------------------------------------------------------- *
* *
* COPYRIGHT: (c) 2024 Dipl.-Ing. Klaus Lux (Aachen, Germany) *
* *
* --------------------------------------------------------------------------- *
* *
* ORIGIN: https://github/klux21/limitless_times *
* *
* --------------------------------------------------------------------------- *
* *
* Civil Usage Public License, Version 1.1, January 2024 *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions, the explanation of terms *
* and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation or other materials provided with the distribution. *
* *
* 3. All modified files must carry prominent notices stating that the *
* files have been changed. *
* *
* 4. The source code and binary forms and any derivative works are not *
* stored or executed in systems or devices which are designed or *
* intended to harm, to kill or to forcibly immobilize people. *
* *
* 5. The source code and binary forms and any derivative works are not *
* stored or executed in systems or devices which are intended to *
* monitor, to track, to change or to control the behavior, the *
* constitution, the location or the communication of any people or *
* their property without the explicit and prior agreement of those *
* people except those devices and systems are solely designed for *
* saving or protecting peoples life or health. *
* *
* 6. The source code and binary forms and any derivative works are not *
* stored or executed in any systems or devices that are intended *
* for the production of any of the systems or devices that *
* have been stated before except the ones for saving or protecting *
* peoples life or health only. *
* *
* The term 'systems' in all clauses shall include all types and combinations *
* of physical, virtualized or simulated hardware and software and any kind *
* of data storage. *
* *
* The term 'devices' shall include any kind of local or non-local control *
* system of the stated devices as part of that device als well. Any assembly *
* of more than one device is one and the same device regarding this license. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN *
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) *
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
\*****************************************************************************/
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h> /* open */
#include <stdio.h> /* sprintf */
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#pragma warning(disable : 4100 4127)
#include <io.h>
#else
#include <unistd.h>
#include <sys/types.h>
#ifdef __CYGWIN__
#include <windows.h>
#undef _WIN32
#endif
#endif/* #ifdef _WIN32 */
#include <time_api.h>
#include <tz_value.h>
#if defined (_WIN32) || defined (__CYGWIN__)
/* ------------------------------------------------------------------------- *\
unix_time returns the Unix time stamp in microsecond.
(UTC time since 01/01/1970) The precision depends on the system.
\* ------------------------------------------------------------------------- */
static void (WINAPI * vGetSystemTimePreciseAsFileTime)(LPFILETIME lpSystemTimeAsFileTime);
static HMODULE hmKernel32Dll = (HMODULE) -1;
int64_t unix_time()
{
int64_t iRet;
FILETIME CurrentTime;
if(vGetSystemTimePreciseAsFileTime)
{
vGetSystemTimePreciseAsFileTime(&CurrentTime);
}
else if(hmKernel32Dll == (HMODULE) -1)
{ /* 1rst call */
hmKernel32Dll = LoadLibrary("Kernel32.dll");
if(hmKernel32Dll)
vGetSystemTimePreciseAsFileTime = (void (WINAPI * )(LPFILETIME)) GetProcAddress(hmKernel32Dll, "GetSystemTimePreciseAsFileTime");
if(vGetSystemTimePreciseAsFileTime)
vGetSystemTimePreciseAsFileTime(&CurrentTime);
else
GetSystemTimeAsFileTime(&CurrentTime);
}
else
{
GetSystemTimeAsFileTime(&CurrentTime);
}
iRet = ((int64_t) CurrentTime.dwHighDateTime << 32);
iRet += (int64_t) CurrentTime.dwLowDateTime;
iRet -= (int64_t) 116444736 * 1000000 * 1000; /* offset of Windows FileTime to start of Unix time */
return (iRet / 10);
}/* int64_t unix_time() */
#else
int64_t unix_time()
{
int64_t tRet;
struct timeval tv;
gettimeofday(&tv, NULL);
tRet = (int64_t) tv.tv_sec;
/* Try to turn the year 2038 problem into a year 2106 problem. */
if((sizeof(time_t) <= 4) && (tv.tv_sec < 0))
tRet += (int64_t) 0x80000000ul + (int64_t) 0x80000000ul;
tRet *= 1000000ul;
tRet += tv.tv_usec;
return (tRet);
}/* int64_t unix_time() */
#endif
/* ========================================================================= *\
Thread safety helpers
\* ========================================================================= */
/* ------------------------------------------------------------------------- *\
Optional thread lock stuff for ensuring thread safety in multi-threaded
programs. The lock callbacks must be set before calling
update_time_zone_info, new_mktime, get_local_zone_info or new_localtime_r.
The context is a user defined pointer which is uses as argument of the
callbacks. It can be a pointer to a global program mutex for instance.
The provided pfn_lock and pfn_unlock functions are called for guarding
the updates of the internal time zone information updates in
multi-threaded programs but may slow down those functions according to the
time the lock or unlock functions require. For disabling subsequent calls
of the lock and unlock function e.g. before the program termination call
set_time_api_lock with null pointer arguments instead of callback functions.
The used mutex needs to be callable recursively.
Because the function pointers are remembered in static variables you need
to ensure to call this function in all of your program modules which don't
share the same statics and ensure the usage of the same mutex in all of
those modules.
Be aware that the other standard C time functions beside of those
functions aren't required to be thread safe implemented while changing the
global time settings e.g. if changing the TZ environment variable of your
process!
\* ------------------------------------------------------------------------- */
static TIME_API_LOCK pta_lock = NULL;
static TIME_API_LOCK pta_unlock = NULL;
static void * pv_lock_context = NULL;
void init_time_api_lock(TIME_API_LOCK pfn_lock, /* pointer to a user provided mutex lock callback function */
TIME_API_LOCK pfn_unlock, /* pointer to a user provided mutex unlock callback function */
void * pv_context) /* user provided context, e.g. pointer to the mutex. */
{
TIME_API_LOCK old_unlock;
void * old_context;
if(pfn_lock)
pfn_lock(pv_context);
if(pta_lock)
pta_lock(pv_lock_context);
old_context = pv_lock_context;
old_unlock = pta_unlock;
pv_lock_context = pv_context;
pta_lock = pfn_lock;
pta_unlock = pfn_unlock;
if(old_unlock)
old_unlock(old_context);
if(pfn_unlock)
pfn_unlock(pv_context);
} /* void set_time_api_lock(...) */
/* ========================================================================= *\
Routines for calculating calendar week of a given date
\* ========================================================================= */
/* ------------------------------------------------------------------------- *\
calendar_week_of_year returns the calender week of the year for a struct tm
\* ------------------------------------------------------------------------- */
int calendar_week_of_year(const struct tm * ptm)
{
int wday_01_01;
int kw_ret = 0;
int epoch = 0;
int year = 0;
if(!ptm)
goto Exit;
year = ptm->tm_year + 1900;
epoch = year / 400;
if (year < 0)
--epoch;
year -= epoch * 400; /* year is between 0 and 399 now (every 400 years epoch epoch starts on a Saturday) */
/* we have to subtract one because the calendar week starts
on Monday instead on Sunday as in struct ptm specified. */
wday_01_01 = (700 + ptm->tm_wday - ptm->tm_yday - 1) % 7;
if(wday_01_01 >= 4)
wday_01_01 -= 7; /* subtract a week because the 01/01 belongs to KW53 of last year. */
kw_ret = (ptm->tm_yday + wday_01_01 + 7) / 7;
if (kw_ret == 53)
{ /* KW53 is KW1 of next year if the 12/31 isn't a Thursday */
long wday_12_31;
if(!(year & 3) && ((year % 100) || !year))
wday_12_31 = (wday_01_01 + 366) % 7; /* leap year */
else
wday_12_31 = (wday_01_01 + 365) % 7; /* normal year */
if(wday_12_31 < 4)
kw_ret = 1;
}
if(!kw_ret)
kw_ret = 53; /* KW0 indicates KW53 of previous year. */
Exit:;
return (kw_ret);
}/* calendar_week_of_year(...) */
/* ------------------------------------------------------------------------- *\
week_of_year returns the calendar week of a given date
\* ------------------------------------------------------------------------- */
int week_of_year(int year, int month, int day)
{
int kw_ret = 0;
struct tm stm;
time_t timestamp;
memset(&stm, 0, sizeof(stm)); /* clear data */
if((month < 1) || (month > 12))
month = 1;
if((day < 1) || (day > 31))
day = 1;
stm.tm_year = year - 1900;
stm.tm_mon = month - 1;
stm.tm_mday = day;
stm.tm_hour = 11;
timestamp = new_mkgmtime(&stm);
new_gmtime_r(×tamp, &stm);
kw_ret = calendar_week_of_year(&stm);
return (kw_ret);
}/* week_of_year(...) */
/* ------------------------------------------------------------------------- *\
calendar_week_of_time returns the calender week of the year for a time_t
\* ------------------------------------------------------------------------- */
int calendar_week_of_time(time_t tt)
{
int kw_ret = 0;
struct tm stm;
memset(&stm, 0, sizeof(stm));
new_gmtime_r(&tt, &stm);
kw_ret = calendar_week_of_year(&stm);
return (kw_ret);
} /* int calendar_week_of_time(time_t tt) */
/* ------------------------------------------------------------------------- *\
new_mkgmtime is a mkgmtime (timegm) implementation
\* ------------------------------------------------------------------------- */
time_t new_mkgmtime(const struct tm * ptm)
{
int64_t tt = -1;
int64_t year;
int64_t epoch;
int32_t time_of_year;
int leap_year = 0;
if(!ptm)
{
errno = EINVAL;
goto Exit;
}
if ( ((ptm->tm_sec < 0) || (ptm->tm_sec > 60)) /* allow specification of a positive leap second */
|| ((ptm->tm_min < 0) || (ptm->tm_min > 59))
|| ((ptm->tm_hour < 0) || (ptm->tm_hour > 23))
|| (ptm->tm_mday < 1)
|| ((ptm->tm_mon < 0) || (ptm->tm_mon > 11)))
{
errno = ERANGE;
goto Exit;
}
year = (int64_t) ptm->tm_year + 1900;
epoch = year / 400;
if (year < 0)
--epoch;
year -= epoch * 400; /* year is between 0 and 399 now */
tt = epoch * ((int64_t) 146097 * 86400); /* time of the 400 year epochs */
if (year >= 100)
{
if (year >= 300)
{
tt += (int64_t) 86400 * (36525 + 36524 + 36524);
year -= 300;
}
else if (year >= 200)
{
tt += (int64_t) 86400 * (36525 + 36524);
year -= 200;
}
else
{
tt += (int64_t) 86400 * 36525;
year -= 100;
}
if (year >= 4)
{
tt += (int64_t) 86400 * (365 * 4); /* add the time of the first 4 years of the century */
year -= 4;
tt += (year >> 2) * (1461 * 86400); /* time of full four year epochs */
year &= 3;
if(year == 3)
tt += (366 + 365 + 365) * 86400; /* add time till the year */
else if(year == 2)
tt += (366 + 365) * 86400; /* add time till the year */
else if(year == 1)
tt += 366 * 86400; /* add time of the leap year */
else
leap_year = 1;
}
else
{
tt += year * (86400 * 365); /* add the time of the full years */
}
}
else
{
tt += (year >> 2) * (1461 * 86400); /* time of full four year epochs */
year &= 3;
if(year == 3)
tt += (366 + 365 + 365) * 86400; /* add time till the year */
else if(year == 2)
tt += (366 + 365) * 86400; /* add time till the year */
else if(year == 1)
tt += 366 * 86400; /* add time of the leap year */
else
leap_year = 1;
}
if(!leap_year)
{ /* month 1 2 3 4 5 6 7 8 9 10 11 12 */
static const uint8_t days_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* number of the days in the month */
static const uint16_t startday_of_month[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; /* offset of the first day of a month to the begin of the year that is not a leap year */
if (ptm->tm_mday > days_of_month[ptm->tm_mon])
{
errno = ERANGE;
goto Exit;
}
time_of_year = (startday_of_month[ptm->tm_mon] + (ptm->tm_mday - 1)) * 86400;
}
else
{
static const int8_t days_of_month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* number of the days in the month */
static const int16_t startday_of_month[12] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; /* offset of the first day of a month from the begin of the year in a leap year */
if (ptm->tm_mday > days_of_month[ptm->tm_mon])
{
errno = ERANGE;
goto Exit;
}
time_of_year = (startday_of_month[ptm->tm_mon] + (ptm->tm_mday - 1)) * 86400;
}
time_of_year += ptm->tm_sec;
time_of_year += ptm->tm_min * 60;
time_of_year += ptm->tm_hour * 3600;
tt += (int64_t) time_of_year;
tt -= (int64_t) 719528 * 86400; /* subtract the time from 1/1/0000 until 1/1/1970 */
if(tt != (time_t) tt)
{ /* handle overflow of 32 bit time_t values */
tt = -1;
errno = ERANGE;
goto Exit;
}
Exit:;
return ((time_t) tt);
} /* time_t new_mkgmtime(struct tm * ptm) */
/* ------------------------------------------------------------------------- *\
new_gmtime_r an own implementation of gmtime_r
Note: new_gmtime_r returns the atronomical date that has a year 0.
If you need the historical date you can do this as following
...
new_gmtime_r(&t, ptm);
if(ptm->year <= -1900)
--ptm->year;
printf ( "The historical year was %s%i%s", ptm->year > -1900 ? "AD" : "",
ptm->year + 1900, ptm->year < -1900 ? " BC" : "");
...
\* ------------------------------------------------------------------------- */
struct tm * new_gmtime_r(const time_t * pt, struct tm * ptm)
{
int64_t time = 0; /* unix time in micro seconds */
int64_t year;
uint32_t day;
uint32_t time_of_day;
uint32_t tmp;
int leap_year = 0;
int ignore_leap_year = 0;
static uint8_t mday[366] = /* day of a month of in a leap year */
{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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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};
static uint8_t mon[366] = /* month of a day in a leap year */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11};
if(!ptm)
{
errno = EINVAL;
goto Exit; /* destination missing */
}
if(pt)
time = (int64_t) *pt;
memset(ptm, 0, sizeof(*ptm));
/* ptm->tm_isdst = 0; -> ignore summer time flag for UTC */
time += ((int64_t) 719528 * 86400); /* add the time from 1/1/0000 to 1/1/1970 */
if (time < 0)
year = time / ((int64_t) 146097 * 86400) - 1; /* calculate the number of full 400 year epochs BC */
else
year = time / ((int64_t) 146097 * 86400); /* calculate the number of full 400 year epochs */
time -= year * ((int64_t) 146097 * 86400); /* subtract the 400 year epochs from time */
year *= 400; /* year contains the Gregorian 400 year epoch of the time now e.g 400 for 753 AD */
day = (uint32_t) (time / 86400); /* calculate the days within the epoch */
time_of_day = (uint32_t) (time - (int64_t) day * 86400); /* time of the day in seconds */
ptm->tm_wday = (day + 6 /* 6 is offset at 1.1.0000 */) % 7; /* day of the week the year starts with 0=Sunday ... 6=Saturday */
if (day >= 36525)
{ /* if the time is more than 100 years after the start of a 400 years epoch */
day -= 36525;
year += 100;
if (day >= 36524)
{
day -= 36524;
year += 100;
if (day >= 36524)
{
day -= 36524;
year += 100;
}
}
/* handle the first non leap years at begin of the century and ensure all remaining 4 year epochs start with a leap year */
if (day >= 1460)
{
year += 4;
day -= 1460;
}
else
{
ignore_leap_year = 1; /* we have to ignore the leap year within the first four years of a century */
while (day >= 365)
{
++year;
day -= 365;
}
}
}
/* now we handle the rest of the years */
tmp = (day / 1461); /* calculate the number of full 4 year epochs that start with a leap year */
year += tmp * 4;
day -= tmp * 1461;
if (day >= 1096)
{ /* last year of the remaining 4 year block */
year += 3;
day -= 1096;
}
else if (day >= 731)
{ /* 3rd year of the 4 year block */
year += 2;
day -= 731;
}
else if (day >= 366)
{ /* 2nd year of the 4 year block */
year += 1;
day -= 366;
}
else if (!ignore_leap_year)
{ /* 1rst year of the 4 year block */
leap_year = 1;
}
ptm->tm_year = (int) (year - 1900);
ptm->tm_yday = (int) day;
if(!leap_year && (day >= 59))
++day; /* we have to skip the 29th of February in our tables */
ptm->tm_mon = mon[day];
ptm->tm_mday = mday[day];
ptm->tm_hour = (time_of_day / 3600);
ptm->tm_min = (time_of_day % (3600)) / 60;
ptm->tm_sec = (time_of_day % (60));
Exit:;
return (ptm);
}/* new_gmtime_r */
static TIME_ZONE_INFO ti; /* static time zone information as returned by the system functions */
/* ------------------------------------------------------------------------- *\
read_TZ_zone_data is a helper of read_TZ for reading time zone name and
time offsets
\* ------------------------------------------------------------------------- */
static int read_TZ_zone_data (TIME_ZONE_RULE * ptr, int is_dst, char * psrc, char ** ppend)
{
int bRet = 0;
size_t size = 0;
int32_t offset = 1;
uint32_t hours = 0;
uint32_t minutes = 0;
uint32_t seconds = 0;
char * ps = psrc;
if (*ps == '<')
{
while(*ps && (*ps != '>'))
++ps;
if(*ps++ != '>')
goto Exit; /* invalid format because the required termination of the name string is missing */
}
else
{
while(*ps && (*ps != ',') && (*ps != ':') && (*ps != '-') && (*ps != '+') && ((*ps < '0') || (*ps > '9')))
++ps;
}
if(!*ps || ((ps - psrc) < 3))
goto Exit; /* time zone name too short or either time specification or rules missing */
size = ps - psrc; /* time zone name */
if((*ps != '-') && (*ps != '+') && ((*ps < '0') || (*ps > '9')))
{ /* no time specified */
offset = 0x7fffffff;
bRet = is_dst; /* in case of the summer time the specification of an offset is optional */
goto Exit;
}
if(*ps == '+')
{
offset = 1;
++ps;
}
else if(*ps == '-')
{
offset = -1;
++ps;
}
if((*ps < '0') || (*ps > '9'))
goto Exit; /* invalid format */
hours = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
hours = (hours * 10) + (*ps++ - '0');
if(hours > 24)
goto Exit; /* TZ format error :o( */
if(*ps == ':')
{ /* minutes of the offset specified */
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
minutes = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
minutes = (minutes * 10) + (*ps++ - '0');
if(minutes >= 59)
goto Exit; /* TZ format error :o( */
if(*ps == ':')
{ /* seconds of the offset specified for what ever */
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
seconds = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
seconds = (seconds * 10) + (*ps++ - '0');
if(seconds > 59)
goto Exit; /* TZ format error :o( */
}
}
offset *= hours * 3600 + minutes * 60 + seconds;
bRet = 1;
Exit:;
if(bRet)
{
*ppend = ps;
ptr->bias = offset;
if(size >= sizeof(ptr->zone_name))
size = sizeof(ptr->zone_name) -1; /* limit the zone name to the size of our buffer */
ps = ptr->zone_name;
while(size--)
*ps++ = *psrc++; /* copy zone name string */
*ps = '\0'; /* terminate zone name string */
}
return (bRet);
} /* read_TZ_zone_data */
/* ------------------------------------------------------------------------- *\
read_TZ_rules is a helper of b_read_TZ for reading the daylight saving
rules
\* ------------------------------------------------------------------------- */
static int read_TZ_rules (TIME_ZONE_RULE * ptr, char * psrc, char ** ppend)
{
int bRet = 0;
char * ps = psrc;
int32_t mode = 0;
int32_t month = 0;
int32_t mweek = 0;
int32_t wday = 0;
int32_t year_day = 0;
int32_t hour = 2; /* default is 02:00:00 */
int32_t minutes = 0;
int32_t seconds = 0;
if(*ps++ != ',')
goto Exit;
if(*ps == 'M')
{
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
month = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
month = (month * 10) + (*ps++ - '0');
if((month < 1) || (month > 12))
goto Exit; /* TZ format error :o( */
month--; /* January is 0 */
if(*ps++ != '.')
goto Exit; /* TZ format error :o( */
if ((*ps < '1') || (*ps > '5'))
goto Exit; /* TZ format error :o( */
mweek = *ps++ - '0';
if(*ps++ != '.')
goto Exit; /* TZ format error :o( */
if ((*ps < '0') || (*ps > '6'))
goto Exit; /* TZ format error :o( */
wday = *ps++ - '0';
mode = 0;
}
else if(*ps == 'J')
{
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
year_day = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
year_day = (year_day * 10) + (*ps++ - '0');
if ((*ps >= '0') && (*ps <= '9'))
year_day = (year_day * 10) + (*ps++ - '0');
if(year_day > 365) /* 364 is maximum but J365/23 is used for termination of daylight saving or a previous rule as a day that never comes */
goto Exit; /* TZ format error :o( */
mode = 1;
}
else
{
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
year_day = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
year_day = (year_day * 10) + (*ps++ - '0');
if ((*ps >= '0') && (*ps <= '9'))
year_day = (year_day * 10) + (*ps++ - '0');
if(year_day > 365)
goto Exit; /* TZ format error :o( */
mode = 2;
}
if(*ps == '/')
{ /* activation time specified */
int32_t sign = 0;
++ps;
if(*ps == '-')
{
sign = 1;
++ps;
}
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
hour = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
hour = (hour * 10) + (*ps++ - '0');
if(hour > 75)
goto Exit;
if(*ps == ':')
{ /* minutes specified */
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
minutes = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
minutes = (minutes * 10) + (*ps++ - '0');
if(minutes > 59)
goto Exit;
if(*ps == ':')
{ /* seconds specified */
++ps;
if ((*ps < '0') || (*ps > '9'))
goto Exit; /* TZ format error :o( */
seconds = *ps++ - '0';
if ((*ps >= '0') && (*ps <= '9'))
seconds = (seconds * 10) + (*ps++ - '0');
if(seconds > 59)
goto Exit;
}
}
if(sign)
{
hour *= -1;
seconds *= -1;
minutes *= -1;
}
}
ptr->mode = mode;
ptr->year_day = year_day;
ptr->month = month;
ptr->mweek = mweek;
ptr->wday = wday;
ptr->time = (hour * 3600) + (minutes * 60) + seconds;
*ppend = ps;
bRet = 1;
Exit:;
return (bRet);
} /* read_TZ_rules */
/* ------------------------------------------------------------------------- *\
read_TZ parses a Unix conform TZ evironment variable conform string for
the time zone rules and stores this rules in success case in a struct
TIME_ZONE_INFO. The function returns nonzero in success case only.
If the function fails because of an invalid string then the strage that
ptzi points to is unchanged.
\* ------------------------------------------------------------------------- */
int read_TZ (TIME_ZONE_INFO * pzi, const char * pTZ)
{
int bRet = 0;
TIME_ZONE_INFO zi;
char * ps = (char *) pTZ;
memset(&zi, 0, sizeof(zi));
if (!pzi || !ps)
goto Exit; /* internal program error :o( */
if (*ps == ':')
goto Exit; /* unknown user defined TZ format according to the Unix standard :o( */
/* Valid TZ format sample for Central European Time "CET-1CEST,M3.5.0,M10.5.0/3"
Please check the unix standard for the format description. */
if(!read_TZ_zone_data (&zi.standard, 0 /* STD time */, ps, &ps))
goto Exit; /* TZ format error :o( */
zi.type = 1; /* standard time if there is no following data */
if(read_TZ_zone_data (&zi.daylight, 1 /* STD time */, ps, &ps))
{
if(zi.daylight.bias == 0x7fffffff)
zi.daylight.bias = zi.standard.bias - 3600;
if(!read_TZ_rules (&zi.daylight, ps, &ps))
goto Exit; /* TZ format error :o( */
if(!read_TZ_rules (&zi.standard, ps, &ps))
goto Exit; /* TZ format error :o( */
zi.type = 2; /* standard time if there is no following data */
}
pzi->type = 0;
*pzi = zi;
bRet = 1;
Exit:;
return(bRet);
} /* read_TZ() */
/* ------------------------------------------------------------------------- *\
update_time_zone_info initializes or reinitializes the timezone information
that is used for new_mktime and new_localtime_r according to the current
system settings. The function is not thread safe regarding our usage of
static timezone information and requires TZ being set as specified in the
Unix standard.
\* ------------------------------------------------------------------------- */
void update_time_zone_info()
{/* Get time zone information from system */
static char last_TZ[128]="###";
char * pTZ = getenv("TZ");
struct stat st;
if(pta_lock)
pta_lock(pv_lock_context);
if(!pTZ)
{
tzset(); /* mktime should call that. */
pTZ = getenv("TZ");
}
if(pTZ)
{
if(!strncmp(pTZ, last_TZ, sizeof(last_TZ) - 1))
goto Exit; /* timezone unchanged */
strncpy(last_TZ, pTZ, sizeof(last_TZ) - 1);
if (read_TZ(&ti, pTZ))
goto Exit;
}
#ifndef _WIN32
if(!pTZ || !*pTZ)
{