-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdates.c
1408 lines (1211 loc) · 41.5 KB
/
dates.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
/** @file dates.c
* All functions are Multi Thread safe (MT-Safe), unless specified.
*/
/*******
* Copyright 2019 Laurent Farhi
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*****/
//#define _GNU_SOURCE
//#define _POSIX_SOURCE
#define _DEFAULT_SOURCE // for additional field const char *tm_zone in struct tm (_BSD_SOURCE has been deprecated)
#define _XOPEN_SOURCE // for strptime
#define _POSIX_C_SOURCE // for tzset, gmtime_r, localtime_r, setenv, unsetenv
#include <time.h> // Comment to catch syscalls to time.h
//#include <bits/types/struct_tm.h> // Comment out to catch syscalls to time.h
#include "dates.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
int tm_is_TZ_owner = 0; // Not optimized by default
static const char tm_ref_localtime = 0;
const char *const TM_REF_LOCALTIME = &tm_ref_localtime; // Local time representation
const char *const TM_REF_SYSTEMTIME = 0; // Local time representation as defined by the system
static const char tm_ref_utc = 0;
const char *const TM_REF_UTC = &tm_ref_utc; // Coordinated Universal Time representation
static const char tm_ref_unchanged = 0;
const char *const TM_REF_UNCHANGED = &tm_ref_unchanged;
static const char tm_ref_undefined = 0;
const char *const TM_REF_UNDEFINED = &tm_ref_undefined;
#ifndef WALLCLOCK_MAX_NB
# define WALLCLOCK_MAX_NB 2000 // there are currently 1829 entries in TZ database /usrshare/zoneinfo
#endif
#define WALLCLOCK_MAX_LENGTH 200
static size_t nb_registered_wallclocks = 0;
static char registered_wallclock[WALLCLOCK_MAX_LENGTH][WALLCLOCK_MAX_NB] = { 0 }; // Set everything to 0 (even though already the default behavior for static variables)
static const char *TM_LOCALTIMEZONE_NAME = 0; // 0 means system timezone
// Make the API Thread safe (MT-Safe env locale to be precise, see see man attributes(7)):
static pthread_mutex_t tzset_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t wallclock_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t localtimename_mutex = PTHREAD_MUTEX_INITIALIZER;
static const char *
tm_utctimezone (void)
{
return "UTC";
}
static int
tm_isutctimezone (const char *tm_zone)
{
return (tm_zone == TM_REF_UTC || tm_zone == tm_utctimezone () || (tm_zone && tm_utctimezone () && !strcmp (tm_zone, tm_utctimezone ())))? 1 : 0;
}
static const char *
tm_systemtimezone (void)
{
return TM_REF_SYSTEMTIME;
}
static const char *
tm_localtimezone (void)
{
const char *ret = 0;
pthread_mutex_lock (&localtimename_mutex);
ret = TM_LOCALTIMEZONE_NAME;
pthread_mutex_unlock (&localtimename_mutex);
if (ret)
return ret;
else
return tm_systemtimezone ();
}
static void
tm_unregisterwallclock (const char *wc)
{
pthread_mutex_lock (&wallclock_mutex);
if (wc && *wc)
for (size_t i = 0; i < nb_registered_wallclocks; i++)
if (!strcmp (registered_wallclock[i], wc))
{
*registered_wallclock[i] = 0;
break;
}
pthread_mutex_unlock (&wallclock_mutex);
}
static const char *
tm_getregisteredwallclock (const char *wc, int add)
{
if (wc == TM_REF_SYSTEMTIME)
return tm_systemtimezone ();
else if (tm_isutctimezone (wc))
return tm_utctimezone ();
else if (wc == TM_REF_LOCALTIME)
return tm_localtimezone ();
else if (wc == 0 || *wc == 0)
return (errno = EINVAL), TM_REF_UNDEFINED;
pthread_mutex_lock (&wallclock_mutex);
size_t islot = nb_registered_wallclocks;
const char *ret = 0;
for (size_t i = 0; i < nb_registered_wallclocks; i++)
if (!strcmp (registered_wallclock[i], wc))
{
ret = registered_wallclock[i];
pthread_mutex_unlock (&wallclock_mutex);
return ret;
}
else if (islot == nb_registered_wallclocks && *registered_wallclock[i] == 0)
islot = i;
if (!add)
{
pthread_mutex_unlock (&wallclock_mutex);
return TM_REF_UNDEFINED;
}
if (islot < nb_registered_wallclocks)
{
ret = strncpy (registered_wallclock[islot], wc, WALLCLOCK_MAX_LENGTH - 1);
pthread_mutex_unlock (&wallclock_mutex);
return ret;
}
if (nb_registered_wallclocks == WALLCLOCK_MAX_NB || strlen (wc) >= WALLCLOCK_MAX_LENGTH)
{
errno = ENOMEM;
perror ("Wallclock registration");
pthread_mutex_unlock (&wallclock_mutex);
return tm_systemtimezone ();
}
ret = strncpy (registered_wallclock[nb_registered_wallclocks++], wc, WALLCLOCK_MAX_LENGTH - 1);
pthread_mutex_unlock (&wallclock_mutex);
return ret;
}
int
tm_isdefinedinwallclock (struct tm dt, const char *wc)
{
wc = tm_getregisteredwallclock (wc, 0);
if (wc == TM_REF_UNDEFINED)
return 0;
else if (dt.tm_zone == wc)
return 1;
else if (dt.tm_zone && wc)
return !strcmp (dt.tm_zone, wc);
else
return 0;
}
static void
tm_tzunset (const char *old_tz)
{
if (tm_is_TZ_owner)
return;
if (!old_tz)
unsetenv ("TZ");
else
setenv ("TZ", old_tz, 1);
tzset ();
}
static tm_status
tm_tzset (const char *wc, const char * *p_old_tz)
{
char *old_tz = getenv ("TZ");
if (p_old_tz)
*p_old_tz = old_tz;
if (wc && !*wc)
return TM_ERROR;
// if the TZ variable does appear in the environment, but its value is empty,
// then Coordinated Universal Time (UTC) is used as default.
if (wc && !strcmp (tm_utctimezone (), wc)) // Specific code for UTC
wc = "";
if (tm_is_TZ_owner && !old_tz && !wc)
return TM_OK;
else if (!wc)
unsetenv ("TZ");
else if (tm_is_TZ_owner && old_tz && !strcmp (old_tz, wc))
return TM_OK;
else if (setenv ("TZ", wc, 1) != 0)
return TM_ERROR;
tm_status ret = TM_OK;
int saveerrno = errno;
errno = 0;
// N.B.: tzset is automatically called by the other time conversion functions that depend on the timezone.
tzset (); // time syscall, seems to set errno if TZ is empty or cannot be interpreted.
// if the TZ variable does appear in the environment, but its value cannot be interpreted,
// then Coordinated Universal Time (UTC) is used as default.
if (errno && wc && strlen (wc)) // wc = "" is not an error
{
tm_unregisterwallclock (wc);
if (!old_tz)
unsetenv ("TZ");
else
setenv ("TZ", old_tz, 1);
tzset ();
ret = TM_ERROR;
}
else
errno = saveerrno;
return ret;
}
// wc should conform to format accepted by tzset (see man tzset)
// On Linux, possible values can be listed with command 'find /usr/share/zoneinfo \! -type d | sort'
// Thread safety : MT-Unsafe const:env (see attributes(7))
tm_status
tm_setlocalwallclock (const char *wc)
{
if (wc == TM_REF_LOCALTIME || wc == TM_REF_UNCHANGED)
return TM_OK;
pthread_mutex_lock (&tzset_mutex);
const char *old_tz;
if (tm_tzset (wc = tm_getregisteredwallclock (wc, 1), &old_tz) == TM_ERROR) // tm_tzset simply checks if wc is a valid timezone here.
{
pthread_mutex_unlock (&tzset_mutex);
return TM_ERROR;
}
pthread_mutex_lock (&localtimename_mutex);
TM_LOCALTIMEZONE_NAME = wc;
pthread_mutex_unlock (&localtimename_mutex);
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
return TM_OK;
}
// Thread safety : MT-Safe env (see attributes(7))
const char *
tm_getlocalwallclock (void)
{
const char *wc = 0;
pthread_mutex_lock (&localtimename_mutex);
wc = TM_LOCALTIMEZONE_NAME;
pthread_mutex_unlock (&localtimename_mutex);
if (tm_isutctimezone (wc))
return TM_REF_UTC;
else
return tm_localtimezone ();
}
// Thread safety : MT-Safe env (see attributes(7))
int
tm_islocalwallclock (const char *wc)
{
return tm_getregisteredwallclock (tm_localtimezone (), 0) == tm_getregisteredwallclock (wc, 0);
}
/// Normalizes instant in time.
/// @param [in,out] date Pointer to broken-down time structure
/// @returns Absolute calendar time
/// @remark Calls mktime. The tm_normalizetolocal() function is equivalent to the POSIX standard function mktime()
static tm_status /* might set errno to EINVAL */
tm_normalize (struct tm *tm, time_t *t)
{
/* (mktime man page)
The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation.
The function ignores the values supplied by the caller in the tm_wday, tm_yday and tm_gmtoff fields.
The value specified in the tm_isdst field informs mktime() whether or not daylight saving time (DST) is in effect for the time supplied in
the tm structure:
- a positive value means DST is in effect;
- zero means that DST is not in effect;
- and a negative value means that mktime() should (use timezone information and system databases to) attempt to determine whether DST is in
effect at the specified time.
The mktime() function modifies the fields of the tm structure as follows:
- tm_wday and tm_yday are set to values determined from the contents of the other fields;
- tm_gmtoff is set according to the current timezone of the system (environment variable TZ).
- If structure members are outside their valid interval, they will be normalized (so that, for example, 40 October is changed into 9 November).
- tm_isdst is set (regardless of its initial value) to a positive value or to 0, respectively, to indicate whether DST is or is not in effect
at the specified time.
Calling mktime() also sets the external variable tzname with information about the current timezone.
*/
struct tm oldtm = *tm;
const char *wc = tm->tm_zone;
pthread_mutex_lock (&tzset_mutex);
const char *old_tz;
if (tm_tzset (wc, &old_tz) == TM_ERROR)
{
pthread_mutex_unlock (&tzset_mutex);
return TM_ERROR;
}
int saveerrno = errno;
errno = 0;
// May apply daylight saving if tm_isdst is not negative before function call:
time_t ret = mktime (tm); // time syscall, calls tzset ; if structure members are outside their valid interval, they will be normalized.
if (errno)
{
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
*tm = oldtm;
return TM_ERROR;
}
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
tm->tm_zone = wc;
if (tm->tm_year + 1900 + 1 < tm->tm_year)
return (*tm = oldtm), (errno = EINVAL), TM_ERROR;
if (t)
*t = ret;
errno = saveerrno;
return TM_OK;
}
const char *
tm_getwallclock (struct tm date)
{
return date.tm_zone;
}
static int
tm_isdaylightsavingextrasummertime (struct tm date)
{
if (tm_isdefinedinutc (date) || !date.tm_isdst)
return 0;
date.tm_isdst = 0;
tm_normalize (&date, 0); // date was already normalized at creation, it should not set errno
// Returns 1 during the hour before DST loses effect (from summer to winter time switch), 0 otherwise.
return (date.tm_isdst ? 0 : 1);
}
static int
tm_isdaylightsavingextrawintertime (struct tm date)
{
if (tm_isdefinedinutc (date) || date.tm_isdst)
return 0;
date.tm_isdst = 1;
tm_normalize (&date, 0); // date was already normalized at creation, it should not set errno
// Returns 1 during the hour after DST loses effect (from summer to winter time switch), 0 otherwise.
return (date.tm_isdst ? 1 : 0);
}
static tm_status
tm_todaylightsavingextrawintertime (struct tm *date)
{
int saveerrno = errno;
errno = 0;
if (!tm_isdaylightsavingextrasummertime (*date) || errno)
return (errno = saveerrno), TM_ERROR;
date->tm_isdst = 0;
return tm_normalize (date, 0) == TM_ERROR ? TM_ERROR : date->tm_isdst ? TM_ERROR : TM_OK;
}
static tm_status
tm_todaylightsavingextrasummertime (struct tm *date)
{
int saveerrno = errno;
errno = 0;
if (!tm_isdaylightsavingextrawintertime (*date) || errno)
return (errno = saveerrno), TM_ERROR;
date->tm_isdst = 1;
return tm_normalize (date, 0) == TM_ERROR ? TM_ERROR : date->tm_isdst ? TM_OK : TM_ERROR;
}
/*****************************************************
* CONSTRUCTORS *
*****************************************************/
tm_status
tm_make_ir (struct tm *tm, tm_predefined_instant instant, const char *rep)
{
time_t now = time (0); // time syscall
if (rep == TM_REF_UNCHANGED)
rep = tm_getwallclock (*tm);
if (tm_isutctimezone (rep))
{
rep = tm_getregisteredwallclock (rep, 0);
gmtime_r (&now, tm); // time syscall
}
else
{
rep = tm_getregisteredwallclock (rep, 1);
pthread_mutex_lock (&tzset_mutex);
const char *old_tz;
if (tm_tzset (rep, &old_tz) == TM_ERROR)
{
pthread_mutex_unlock (&tzset_mutex);
return (errno = EINVAL), TM_ERROR;
}
localtime_r (&now, tm); // time syscall. For portable code, tzset(3) should be called before localtime_r().
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
}
tm->tm_zone = rep;
if (instant == TM_TODAY)
{
tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
tm->tm_isdst = -1; // Let timezone information and system databases define DST flag.
return tm_normalize (tm, 0);
}
else if (instant == TM_NOW)
return TM_OK;
else
return (errno = EINVAL), TM_ERROR;
}
tm_status
tm_make_dtrc (struct tm *tm, int year, tm_month month, int day, int hour, int min, int sec, const char *rep, tm_time_precedence clock)
{
if (rep == TM_REF_UNCHANGED)
rep = tm_getwallclock (*tm);
rep = tm_getregisteredwallclock (rep, 1);
tm->tm_year = year - 1900;
if (tm->tm_year > year)
return (errno = EINVAL), TM_ERROR;
tm->tm_mon = (typeof (tm->tm_mon)) (month - 1);
tm->tm_mday = day;
tm->tm_hour = hour;
tm->tm_min = min;
tm->tm_sec = sec;
tm->tm_zone = rep;
tm->tm_isdst = -1; // Let timezone information and system databases define DST flag.
time_t ret;
if (tm_normalize (tm, &ret) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (!tm_isutctimezone (rep))
{
if (clock == TM_DST_OVER_ST)
tm_todaylightsavingextrasummertime (tm);
else if (clock == TM_ST_OVER_DST)
tm_todaylightsavingextrawintertime (tm);
else
return (errno = EINVAL), TM_ERROR;
}
if (tm->tm_year == year - 1900 && tm->tm_mon == (typeof (tm->tm_mon)) (month - 1) && tm->tm_mday == day && tm->tm_hour == hour && tm->tm_min == min && tm->tm_sec == sec)
return TM_OK;
else
return (errno = EINVAL), TM_ERROR;
}
// tm should have been initialized with tm_set first.
tm_status
tm_setdatefromstring (struct tm *tm, const char *buf, const char *rep, tm_time_precedence clock)
{
char *ret;
ret = strptime (buf, "%x", tm); // time syscall
if (!ret || *ret)
ret = strptime (buf, "%Ex", tm);
if (!ret || *ret)
ret = strptime (buf, "%Y-%m-%d", tm);
if (!ret || *ret)
ret = strptime (buf, "%Y−%m−%d", tm);
if (!ret || *ret)
ret = strptime (buf, "%Y %m %d", tm);
if (!ret || *ret)
return (errno = EINVAL), TM_ERROR;
int year = tm->tm_year + 1900; /* tm_year is the number of years since 1900. */
if (year >= 0 && year < 100)
{
time_t now = time (0); // time syscall
struct tm tm_now;
tzset ();
localtime_r (&now, &tm_now); // time syscall
int current_year = tm_now.tm_year + 1900; /* tm_year is the number of years since 1900. */
tm->tm_year += (typeof (tm->tm_year)) lround ((current_year - year) / 100.) * 100;
}
if (rep == TM_REF_UNCHANGED)
rep = tm_getwallclock (*tm);
rep = tm_getregisteredwallclock (rep, 1);
tm->tm_zone = rep;
tm->tm_isdst = -1; // Let timezone information and system databases define DST flag.
struct tm copy = *tm;
time_t retval;
if (tm_normalize (tm, &retval) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (!tm_isutctimezone (rep))
{
if (clock == TM_DST_OVER_ST)
tm_todaylightsavingextrasummertime (tm);
else if (clock == TM_ST_OVER_DST)
tm_todaylightsavingextrawintertime (tm);
else
return (errno = EINVAL), TM_ERROR;
}
if (tm->tm_year == copy.tm_year && tm->tm_mon == copy.tm_mon && tm->tm_mday == copy.tm_mday
&& tm->tm_hour == copy.tm_hour && tm->tm_min == copy.tm_min && tm->tm_sec == copy.tm_sec)
return TM_OK;
else
return (errno = EINVAL), TM_ERROR;
}
// tm should have been initialized with tm_set first.
tm_status
tm_settimefromstring (struct tm *tm, const char *buf, const char *rep, tm_time_precedence clock)
{
char *ret;
tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
ret = strptime (buf, "%X", tm); // time syscall
if (!ret || *ret)
ret = strptime (buf, "%EX", tm);
if (!ret || *ret)
ret = strptime (buf, "%H:%M:%S", tm);
if (!ret || *ret)
ret = strptime (buf, "%H %M %S", tm);
if (!ret || *ret)
ret = strptime (buf, "%H:%M", tm);
if (!ret || *ret)
ret = strptime (buf, "%H %M", tm);
if (!ret || *ret)
return (errno = EINVAL), TM_ERROR;
if (rep == TM_REF_UNCHANGED)
rep = tm_getwallclock (*tm);
rep = tm_getregisteredwallclock (rep, 1);
tm->tm_zone = rep;
tm->tm_isdst = -1; // Let timezone information and system databases define DST flag.
struct tm copy = *tm;
time_t retval;
if (tm_normalize (tm, &retval) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (!tm_isutctimezone (rep))
{
if (clock == TM_DST_OVER_ST)
tm_todaylightsavingextrasummertime (tm);
else if (clock == TM_ST_OVER_DST)
tm_todaylightsavingextrawintertime (tm);
else
return (errno = EINVAL), TM_ERROR;
}
if (tm->tm_year == copy.tm_year && tm->tm_mon == copy.tm_mon && tm->tm_mday == copy.tm_mday
&& tm->tm_hour == copy.tm_hour && tm->tm_min == copy.tm_min && tm->tm_sec == copy.tm_sec)
return TM_OK;
else
return (errno = EINVAL), TM_ERROR;
}
tm_status
tm_setfromiso8601 (struct tm *dt, char *str)
{
// <date>T<time><tz>
// <date> is YYYY-MM-DD or YYYYMMDD,
// YYYY indicates a four-digit year, 0000 through 9999.
// MM indicates a two-digit month of the year, 01 through 12.
// DD indicates a two-digit day of that month, 01 through 31.
// <time> is hh:mm:ss.sss or hhmmss.sss or hh:mm:ss or hhmmss or hh:mm or hhmm or hh
// hh refers to a zero-padded hour between 00 and 24 (where 24 is only used to denote midnight at the end of a calendar day).
// mm refers to a zero-padded minute between 00 and 59.
// ss refers to a zero-padded second between 00 and 60.
// <tz> is Z or ±hh:mm or ±hhmm or ±hh
// - is '-' or '−'
const char *regex = "^" "(([0-9][0-9][0-9][0-9])[-−]?([0-9][0-9])[-−]?([0-9][0-9]))" // AAAAMMDD or AAAA-MM-DD
"(T([0-9][0-9])([:]?([0-9][0-9])([:]?([0-9][0-9])([.,](([0-9])+))?)?)?)?" // hhmmss or hh:mm:ss
"((([-−+])([0-9][0-9]))([:]?([0-9][0-9]))?|Z)?" // +/-hhmm or +/-hh:mm
"$";
regex_t preg;
if (regcomp (&preg, regex, REG_EXTENDED | REG_NEWLINE))
abort ();
regmatch_t pmatch[19 + 1]; // an item for each open parenthesis of regex + 1
if (regexec (&preg, str, sizeof (pmatch) / sizeof (*pmatch), pmatch, 0) == REG_NOMATCH)
{
regfree (&preg);
return (errno = EINVAL), TM_ERROR;
}
regfree (&preg);
int annee, mois, jour, heure, min, sec, gsign, gheure, gmin;
int fsec;
const char *wc;
heure = min = sec = fsec = gheure = gmin = 0;
const size_t UTCOFFSET = 14; // This hard-coded value depend on the variable regex.
const size_t UTCOFFSETSIGN = 16; // This hard-coded value depend on the variable regex.
if (pmatch[UTCOFFSET].rm_so < 0 || pmatch[UTCOFFSET].rm_so >= pmatch[UTCOFFSET].rm_eo) // no UTC offset information
wc = TM_REF_LOCALTIME; // If no UTC relation information is given with a time representation, the time is assumed to be in local time.
else
{
wc = TM_REF_UTC;
if (strncmp ("Z", str + pmatch[UTCOFFSET].rm_so, (size_t) (pmatch[UTCOFFSET].rm_eo - pmatch[UTCOFFSET].rm_so)) == 0) // UTC time zone
gheure = gmin = 0; // If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. An offset of zero, in addition to having the special representation "Z", can also be stated numerically as "+00:00", "+0000", or "+00"
if (pmatch[UTCOFFSETSIGN].rm_so >= 0 && pmatch[UTCOFFSETSIGN].rm_so < pmatch[UTCOFFSETSIGN].rm_eo && // if UTC offset does not start with '+'
strncmp ("+", str + pmatch[UTCOFFSETSIGN].rm_so, (size_t) (pmatch[UTCOFFSETSIGN].rm_eo - pmatch[UTCOFFSETSIGN].rm_so)) != 0)
gsign = -1; // '-' or '−'
else
gsign = 1; // '+'
}
// relevant subexpression indexes
/* *INDENT-OFF* */
struct
{
size_t index;
int *pvar;
size_t len;
} parts[] =
{
[0] = {2, &annee}, [1] = {3, &mois}, [2] = {4, &jour},
[3] = {6, &heure}, [4] = {8, &min}, [5] = {10, &sec}, [6] = {12, &fsec},
[7] = {17, &gheure},[8] = {19, &gmin},
};
/* *INDENT-ON* */
for (size_t part = 0; part < sizeof (parts) / sizeof (*parts); part++)
{
#define MAXLEN ((size_t)6)
char substr[MAXLEN + 1];
substr[MAXLEN] = 0;
parts[part].len = 0;
if (pmatch[parts[part].index].rm_so < 0 || pmatch[parts[part].index].rm_so >= pmatch[parts[part].index].rm_eo)
continue;
strncpy (substr, str + pmatch[parts[part].index].rm_so, MAXLEN); // at most MAXLEN bytes are copied, including the terminating null byte ('\0').
if ((size_t) (pmatch[parts[part].index].rm_eo - pmatch[parts[part].index].rm_so) > MAXLEN)
substr[parts[part].len = MAXLEN] = 0;
else
substr[parts[part].len = (size_t) (pmatch[parts[part].index].rm_eo - pmatch[parts[part].index].rm_so)] = 0;
*(parts[part].pvar) = atoi (substr);
}
float msec = (float) fsec;
for (size_t i = parts[6].len; i > 0; i--)
msec /= 10;
int deltad = 0;
// Midnight is a special case and may be referred to as either "00:00" or "24:00". "2007-04-05T24:00" is the same instant as "2007-04-06T00:00"
if (heure == 24)
{
deltad = 1;
heure = 0;
}
// 60 is only used to denote an added leap second managed by the system, and is ignored here
if (sec == 60)
sec = 59;
if (tm_set (dt, annee, mois, jour, heure, min, sec, wc) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (tm_adddays (dt, deltad) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (wc == TM_REF_UTC && tm_addseconds (dt, -1 * gsign * (3600 * gheure + 60 * gmin)) == TM_ERROR) // To calculate UTC time one has to subtract the offset from the local time
return (errno = EINVAL), TM_ERROR;
if (tm_isdefinedinutc (*dt))
{
if (tm_changetowallclock (dt, TM_REF_LOCALTIME) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
if (tm_getutcoffset (*dt) != gsign * (3600 * gheure + 60 * gmin) && // Does UTC offset match with local time ?
tm_changetowallclock (dt, TM_REF_UTC) == TM_ERROR)
return (errno = EINVAL), TM_ERROR;
}
return TM_OK;
}
static tm_status
tm_tostring_fmt (struct tm dt, size_t max, char *str, const char *fmt)
{
assert (fmt && *fmt);
if (!str || !max)
return (errno = EINVAL), TM_ERROR;
// Provided that the result string, including the terminating null byte, does not exceed max bytes, strftime() returns the number of bytes (excluding the terminating null byte) placed in the array str.
// If the length of the result string (including the terminating null byte) would exceed max bytes, then strftime() returns 0, and the contents of the array are undefined.
// The return value 0 does not necessarily indicate an error. But POSIX.1-2001 does not specify any errno settings.
// This makes it impossible to distinguish this error case from cases where the format string legitimately produces a zero-length output string. For example, in many locales %p yields an empty string.
return strftime (str, max, fmt, &dt) ? TM_OK : TM_ERROR; // time syscall.
}
tm_status
tm_toiso8601 (struct tm dt, size_t max, char *str, int sep)
{
if (sep)
return tm_tostring_fmt (dt, max, str, "%Y-%m-%dT%H:%M:%S%z");
else
return tm_tostring_fmt (dt, max, str, "%Y%m%dT%H%M%S%z");
}
tm_status
tm_timetostring (struct tm dt, size_t max, char *str)
{
const char *fmt[] = { "%X", "%X (%Z)", "%X (UTC%z)", "%X (%Z,UTC%z)" };
size_t index = 2U * (tm_isinsidedaylightsavingtimeoverlap (dt) ? 1 : 0) + (tm_isdefinedinlocaltime (dt) ? 0 : 1);
return tm_tostring_fmt (dt, max, str, fmt[index]);
}
tm_status
tm_datetostring (struct tm dt, size_t max, char *str)
{
const char *fmt = "%x";
if (!tm_isdefinedinlocaltime (dt))
fmt = "%x (%Z)";
return tm_tostring_fmt (dt, max, str, fmt);
}
tm_status
tm_tostring (struct tm dt, size_t max, char *str)
{
const char *fmt[] = { "%x %X", "%x %X (%Z)", "%x %X (UTC%z)", "%x %X (%Z,UTC%z)" };
size_t index = 2U * (tm_isinsidedaylightsavingtimeoverlap (dt) ? 1 : 0) + (tm_isdefinedinlocaltime (dt) ? 0 : 1);
return tm_tostring_fmt (dt, max, str, fmt[index]);
}
tm_status
dt_tostring (struct tm dt, size_t max, char *str)
{
return tm_tostring_fmt (dt, max, str, "%x");
}
tm_status
dt_toiso8601 (struct tm dt, size_t max, char *str, int sep)
{
if (sep)
return tm_tostring_fmt (dt, max, str, "%Y-%m-%d");
else
return tm_tostring_fmt (dt, max, str, "%Y%m%d");
}
tm_status
dt_setfromiso8601 (struct tm *dt, char *str)
{
if (tm_setfromiso8601 (dt, str) == TM_ERROR)
return TM_ERROR;
dt->tm_zone = tm_getregisteredwallclock (TM_REF_UTC, 0);
return TM_OK;
}
tm_status
dt_make_ir (struct tm *date, const char *wc)
{
if (tm_make_ir (date, TM_TODAY, wc) == TM_ERROR)
return TM_ERROR;
date->tm_zone = tm_getregisteredwallclock (TM_REF_UTC, 0);
return TM_OK;
}
/*****************************************************
* CONVERTERS *
**************************************************** */
static tm_status
tm_toutcrepresentation (struct tm *date)
{
if (!tm_isdefinedinutc (*date))
{
time_t local;
if (tm_normalize (date, &local) != TM_ERROR && gmtime_r (&local, date) // time syscall
&& date->tm_year + 1900 + 1 > date->tm_year)
{
date->tm_zone = tm_getregisteredwallclock (TM_REF_UTC, 0);
return TM_OK;
}
else
return (errno = EINVAL), TM_ERROR;
}
else
return TM_OK;
}
static tm_status
tm_totimezonerepresentation (struct tm *date, const char *rep)
{
time_t utc;
if (tm_normalize (date, &utc) != TM_ERROR)
{
rep = tm_getregisteredwallclock (rep, 1);
pthread_mutex_lock (&tzset_mutex);
const char *old_tz;
if (tm_tzset (rep, &old_tz) == TM_ERROR)
{
pthread_mutex_unlock (&tzset_mutex);
return TM_ERROR;
}
if (localtime_r (&utc, date) // time syscall
&& date->tm_year + 1900 + 1 > date->tm_year)
{
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
date->tm_zone = rep;
return TM_OK;
}
else
{
tm_tzunset (old_tz);
pthread_mutex_unlock (&tzset_mutex);
return (errno = EINVAL), TM_ERROR;
}
}
else
return (errno = EINVAL), TM_ERROR;
}
// wc should conform to format accepted by tzset (see man tzset)
// On Linux, possible values can be listed with command 'find /usr/share/zoneinfo \! -type d | sort'
tm_status
tm_changetowallclock (struct tm *date, const char *wc)
{
if (tm_isutctimezone (wc))
return tm_toutcrepresentation (date);
else
return tm_totimezonerepresentation (date, wc);
}
/*****************************************************
* CALENDAR PROPERTIES *
*****************************************************/
int
tm_getdaysinyear (int year)
{
struct tm debut, fin;
if (tm_set (&debut, year, TM_JANUARY, 1, 0, 0, 0) == TM_OK && tm_set (&fin, year + 1, TM_JANUARY, 1, 0, 0, 0) == TM_OK)
return tm_diffcalendardays (debut, fin);
else
return (errno = EINVAL), 0;
}
int
tm_isleapyear (int year)
{
// Nonzero if year is a leap year (every 4 years, except every 100th isn't, and every 400th is).
return tm_getdaysinyear (year) == 366 ? 1 : 0;
//return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
int /* set_errno */
tm_getweeksinisoyear (int isoyear)
{
struct tm date;
if (isoyear + 1 < isoyear || tm_make_dtrc (&date, isoyear + 1, TM_JANUARY, 4, 0, 0, 0, TM_REF_LOCALTIME, TM_ST_OVER_DST) == TM_ERROR || tm_adddays (&date, -7) == TM_ERROR)
return (errno = EINVAL), 0;
return tm_getisoweek (date);
}
int /* set_errno */
tm_getdaysinmonth (int year, tm_month month)
{
int ret = 1; // not 0
struct tm date;
if (tm_make_dtrc (&date, year, month, 1, 0, 0, 0, TM_REF_LOCALTIME, TM_ST_OVER_DST) == TM_ERROR)
return (errno = EINVAL), 0;
ret -= tm_getdayofyear (date);
if (tm_addmonths (&date, 1) == TM_ERROR || tm_adddays (&date, -1) == TM_ERROR)
return (errno = EINVAL), 0;
ret += tm_getdayofyear (date);
return ret;
}
int /* set_errno */
tm_getsecondsinday (int year, tm_month month, int day, const char *rep)
{
if (rep == TM_REF_UNCHANGED)
return (errno = EINVAL), 0;
struct tm date;
if (tm_make_dtrc (&date, year, month, day, 0, 0, 0, rep, TM_ST_OVER_DST) == TM_ERROR)
return (errno = EINVAL), 0;
time_t deb, fin;
if (tm_normalize (&date, &deb) == TM_ERROR || tm_adddays (&date, 1) == TM_ERROR || tm_normalize (&date, &fin) == TM_ERROR)
return (errno = EINVAL), 0;
return (int) (fin - deb);
}
int /* set_errno */
tm_getfirstweekdayinmonth (int year, tm_month month, tm_dayofweek dow)
{
if (dow < 1 || dow > 7)
return (errno = EINVAL), 0;
struct tm date;
if (tm_make_dtrc (&date, year, month, 1, 0, 0, 0, TM_REF_LOCALTIME, TM_ST_OVER_DST) == TM_ERROR)
return (errno = EINVAL), 0;
return (int) ((dow - tm_getdayofweek (date) + 7U) % 7U + 1U);
}
int /* set_errno */
tm_getlastweekdayinmonth (int year, tm_month month, tm_dayofweek dow)
{
if (dow < 1 || dow > 7)
return (errno = EINVAL), 0;
struct tm date;
int savederrno = errno;
errno = 0;
int last = tm_getdaysinmonth (year, month);
if (errno)
return 0;
errno = savederrno;
if (tm_make_dtrc (&date, year, month, last, 0, 0, 0, TM_REF_LOCALTIME, TM_ST_OVER_DST) == TM_ERROR)
return (errno = EINVAL), 0;
int diff = (int) (dow - tm_getdayofweek (date));
return last + diff + (diff > 0 ? -7 : 0);
}
int /* set_errno */
tm_getfirstweekdayinisoyear (int isoyear, tm_dayofweek dow)
{
if (dow < 1 || dow > 7)
return (errno = EINVAL), 0;
struct tm date;
int savederrno = errno;
errno = 0;
int ret = tm_getfirstweekdayinmonth (isoyear, TM_JANUARY, dow) + 7;
if (errno)
return 0;
errno = savederrno;
if (tm_make_dtrc (&date, isoyear, TM_JANUARY, ret, 0, 0, 0, TM_REF_LOCALTIME, TM_ST_OVER_DST) == TM_ERROR)
return (errno = EINVAL), 0;
return ret + 7 - 7 * tm_getisoweek (date);
}
/*****************************************************
* DATE PROPERTIES *
*****************************************************/
tm_dayofweek
tm_getdayofweek (struct tm date)
{
return (date.tm_wday + 6) % 7 + 1; /* Monday = 1, Sunday = 7 */
}
tm_month
tm_getmonth (struct tm date)
{
return date.tm_mon + 1; /* January = 1, December = 12 */
}
int
tm_getyear (struct tm date)
{
if (date.tm_year + 1900 < date.tm_year)
return (errno = EINVAL), 0;
return date.tm_year + 1900;
}
int
tm_getday (struct tm date)
{
return date.tm_mday;