-
Notifications
You must be signed in to change notification settings - Fork 1
/
timefns.c
1314 lines (1220 loc) · 38.5 KB
/
timefns.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
/* -*- Mode: C; Character-encoding: utf-8; -*- */
/* Copyright (C) 2004-2019 beingmeta, inc.
Copyright (C) 2020-2022 Kenneth Haase ([email protected])
This file is part of the libu8 UTF-8 unicode library.
This program comes with absolutely NO WARRANTY, including implied
warranties of merchantability or fitness for any particular
purpose.
Use, modification, and redistribution of this program is permitted
under any of the licenses found in the the 'licenses' directory
accompanying this distribution, including the GNU General Public License
(GPL) Version 2 or the GNU Lesser General Public License.
*/
#include "libu8/u8source.h"
#include "libu8/libu8.h"
typedef unsigned long long u8ull;
typedef unsigned int u8uint;
#ifndef WORDS_BIGENDIAN
#define WORDS_BIGENDIAN 0
#endif
#ifndef _FILEINFO
#define _FILEINFO __FILE__
#endif
#include "libu8/u8streamio.h"
#include "libu8/u8printf.h"
#include "libu8/u8timefns.h"
#include <math.h>
/* We just include this for sscanf */
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#if HAVE_UUID_UUID_H
#include <uuid/uuid.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if U8_THREADS_ENABLED
static u8_mutex timefns_lock;
#endif
struct U8_XTIME u8_start_time;
/* Utility functions */
static U8_MAYBE_UNUSED double getprecfactor(enum u8_timestamp_precision precision)
{
switch (precision) {
case u8_year: return 1000000000.0*3600*24*365;
case u8_month: return 1000000000.0*3600*24*30;
case u8_day: return 1000000000.0*3600*24;
case u8_hour: return 1000000000.0*3600;
case u8_minute: return 1000000000.0*60;
case u8_second: return 1000000000.0;
case u8_millisecond: return 1000000.0;
case u8_microsecond: return 1000.0;
case u8_nanosecond: return 1.0;
default: return 1.0;}
}
static void copy_tm2xt(struct tm *tptr,struct U8_XTIME *xt)
{
memset(xt,0,sizeof(struct U8_XTIME));
xt->u8_sec=tptr->tm_sec;
xt->u8_min=tptr->tm_min;
xt->u8_hour=tptr->tm_hour;
xt->u8_mday=tptr->tm_mday;
xt->u8_mon=tptr->tm_mon;
xt->u8_year=tptr->tm_year+1900;
xt->u8_wday=tptr->tm_wday;
xt->u8_yday=tptr->tm_yday;
xt->u8_wnum=(tptr->tm_yday/7);
}
static void copy_xt2tm(struct U8_XTIME *xt,struct tm *tptr)
{
memset(tptr,0,sizeof(struct tm));
tptr->tm_sec=xt->u8_sec;
tptr->tm_min=xt->u8_min;
tptr->tm_hour=xt->u8_hour;
tptr->tm_mday=xt->u8_mday;
tptr->tm_mon=xt->u8_mon;
tptr->tm_year=xt->u8_year-1900;
tptr->tm_wday=xt->u8_wday;
tptr->tm_yday=xt->u8_yday;
}
/* Finer grained time */
U8_EXPORT
/** Returns the number of milliseconds since the epoch.
This returns a value with whatever accuracy it can get.
@returns a long long counting milliseconds
*/
long long u8_millitime()
{
#if HAVE_GETTIMEOFDAY
struct timeval now;
if (gettimeofday(&now,NULL) < 0)
return -1;
else {
long long secs = now.tv_sec;
secs=(secs*1000)+(now.tv_usec/1000);
return secs;}
#elif HAVE_FTIME
struct timeb now;
#if WIN32
/* In WIN32, ftime doesn't return an error value.
?? We should really do something respectable here.*/
ftime(&now);
#else
if (ftime(&now) < 0) return -1;
else return now.time*1000+now.millitm;
#endif
#else
return ((int)time(NULL))*1000;
#endif
}
/* New core functionality */
unsigned int u8_precision_secs[12]=
{1,3600*24*365,3600*24*30,3600*24,3600,60,1,1,1,1,1,1};
U8_EXPORT u8_xtime u8_init_xtime
(struct U8_XTIME *xt,time_t tick,u8_tmprec prec,int nsecs,
int tzoff,int dstoff)
{
time_t offtick;
unsigned int prec_secs;
struct tm _tptr, *tptr=&_tptr;
if (xt==NULL) xt=u8_malloc(sizeof(struct U8_XTIME));
memset(xt,0,sizeof(struct U8_XTIME));
if (tick<0) {
#if HAVE_GETTIMEOFDAY
struct timeval tv; struct timezone tz;
if (gettimeofday(&tv,&tz) < 0) {
u8_graberr(errno,"u8_xlocaltime/gettimeofday",NULL);
return NULL;}
tick=tv.tv_sec;
nsecs=tv.tv_usec*1000;
if (prec==u8_maxtmprec) xt->u8_prec=u8_microsecond;
#elif HAVE_FTIME
struct timeb tb;
if (ftime(&tb)<0) {
u8_graberr(errno,"u8_xlocaltime/ftime",NULL);
return NULL;}
tick=tb.time;
nsecs=tb.millitm*1000000;
if (prec==u8_maxtmprec) xt->u8_prec=u8_millisecond;
#else
if ((tick=time(NULL))<0) {
u8_graberr(errno,"u8_xlocaltime/time",NULL);
return NULL;}
if (prec==u8_maxtmprec) xt->u8_prec=u8_second;
nsecs=0;
#endif
}
/* Offset the tick to get a "fake" gmtime */
offtick=tick+tzoff+dstoff;
/* Adjust for the precision, rounding to the middle of the range */
prec_secs=u8_precision_secs[prec];
offtick=offtick-(offtick%prec_secs)+prec_secs/2;
/* Get the broken down representation for the "fake" time. */
#if HAVE_GMTIME_R
gmtime_r(&offtick,&_tptr);
#else
u8_lock_mutex(&timefns_lock);
tptr=gmtime(&offtick);
#endif
copy_tm2xt(tptr,xt);
#if (!(HAVE_GMTIME_R))
u8_unlock_mutex(&timefns_lock);
#endif
/* Set the real time and the offset */
xt->u8_tick=tick; xt->u8_tzoff=tzoff; xt->u8_dstoff=dstoff;
xt->u8_nsecs=nsecs;
/* Initialize the precision */
if (prec==u8_maxtmprec) prec=u8_second;
xt->u8_prec=prec;
/* Don't have redundant nanoseconds */
if (xt->u8_prec<=u8_second) xt->u8_nsecs=0;
return xt;
}
U8_EXPORT u8_xtime u8_local_xtime
(struct U8_XTIME *xt,time_t tick,u8_tmprec prec,int nsecs)
{
struct tm _tptr, *tptr=&_tptr;
if (xt==NULL) xt=u8_malloc(sizeof(struct U8_XTIME));
memset(xt,0,sizeof(struct U8_XTIME));
if (tick<0) {
#if HAVE_GETTIMEOFDAY
struct timeval tv; struct timezone tz;
if (gettimeofday(&tv,&tz) < 0) {
u8_graberr(errno,"u8_xlocaltime/gettimeofday",NULL);
return NULL;}
tick=tv.tv_sec;
nsecs=tv.tv_usec*1000;
if ((prec==u8_maxtmprec) || (prec>u8_microsecond))
prec=u8_microsecond;
#elif HAVE_FTIME
struct timeb tb;
if (ftime(&tb)<0) {
u8_graberr(errno,"u8_xlocaltime/ftime",NULL);
return NULL;}
tick=tb.time;
nsecs=tb.millitm*1000000;
if ((prec==u8_maxtmprec) || (prec>u8_millisecond))
prec=u8_millisecond;
#else
if ((tick=time(NULL))<0) {
u8_graberr(errno,"u8_xlocaltime/time",NULL);
return NULL;}
nsecs=0;
if ((prec<0) || (prec>u8_second))
prec=u8_second;
#endif
}
/* Get the broken down representation for the "fake" time. */
#if HAVE_LOCALTIME_R
localtime_r(&tick,&_tptr);
#else
u8_lock_mutex(&timefns_lock);
tptr=localtime(&tick);
#endif
copy_tm2xt(tptr,xt);
#if (!(HAVE_LOCALTIME_R))
u8_unlock_mutex(&timefns_lock);
#endif
/* Set the real time and the offset */
xt->u8_tick=tick;
xt->u8_nsecs=nsecs;
#if HAVE_TM_GMTOFF
if (tptr->tm_isdst) {
xt->u8_tzoff=tptr->tm_gmtoff-3600;
xt->u8_dstoff=3600;}
else xt->u8_tzoff=tptr->tm_gmtoff;
#elif ((HAVE_GETTIMEOFDAY) || (HAVE_FTIME))
if (daylight) {
xt->u8_tzoff=-timezone-3600;
xt->u8_dstoff=3600;}
else xt->u8_tzoff=-timezone;
#else
xt->u8_tzoff=0;
#endif
/* Initialize the precision */
if (prec==u8_maxtmprec) prec=u8_second;
xt->u8_prec=prec;
/* Don't have redundant nanoseconds */
if (xt->u8_prec<=u8_second) xt->u8_nsecs=0;
return xt;
}
static time_t mktime_x(struct U8_XTIME *xt,int islocal)
{
time_t tick; struct tm tptr; int localoff;
memset(&tptr,0,sizeof(struct tm));
copy_xt2tm(xt,&tptr);
/* This tells it to figure it out. */
tick=mktime(&tptr);
if (tick<0) {
u8_graberrno("u8_mktime",NULL);
return tick;}
xt->u8_wday=tptr.tm_wday;
xt->u8_yday=tptr.tm_yday;
if (islocal) {
#if (HAVE_TM_GMTOFF)
if (tptr.tm_isdst>0) {
xt->u8_dstoff=3600;
xt->u8_tzoff=tptr.tm_gmtoff-3600;}
else {
xt->u8_dstoff=0;
xt->u8_tzoff=tptr.tm_gmtoff;}
#else
if (daylight) xt->u8_dstoff=0;
xt->u8_tzoff=timezone;
#endif
}
else {
#if HAVE_TM_GMTOFF
localoff=tptr.tm_gmtoff+((tptr.tm_isdst)?(-3600):(0));
#else
localoff=(timezone+((daylight>1)?(daylight):(daylight*3600)));
#endif
xt->u8_tick=tick=tick+localoff-(xt->u8_tzoff+xt->u8_dstoff);}
return tick;
}
U8_EXPORT
/* u8_mktime_x
Fills out an XTIME structure based on the broken down time representation
Arguments: a pointer to a tm struct and a time_t value
Returns: the time_t value or -1 if it failed
*/
time_t u8_mktime(struct U8_XTIME *xt)
{
return mktime_x(xt,0);
}
U8_EXPORT
/* u8_mktime
Fills out an XTIME structure based on the broken down time representation
Arguments: a pointer to a tm struct and a time_t value
Returns: the time_t value or -1 if it failed
*/
time_t u8_mklocaltime(struct U8_XTIME *xt)
{
return mktime_x(xt,1);
}
/* Initializing xtime structures */
U8_EXPORT
/* u8_localtime_x
Arguments: a pointer to a tm struct, a time_t value, and a nanosecond value (or -1)
Returns: the time_t value or -1 if it failed
*/
time_t u8_localtime_x(struct U8_XTIME *xt,time_t tick,int nsecs)
{
struct tm _now, *now=&_now;
#if HAVE_LOCALTIME_R
localtime_r(&tick,&_now);
#else
u8_lock_mutex(&timefns_lock);
now=localtime(&tick);
if (now == NULL) {
u8_unlock_mutex(&timefns_lock);
return -1;}
#endif
copy_tm2xt(now,xt);
#if HAVE_TM_GMTOFF
if (now->tm_isdst) {
xt->u8_dstoff=3600;
xt->u8_tzoff=now->tm_gmtoff-3600;}
else {
xt->u8_tzoff=now->tm_gmtoff;
xt->u8_dstoff=0;}
#endif
#if (!(HAVE_LOCALTIME_R))
u8_unlock_mutex(&timefns_lock);
#endif
xt->u8_tick=tick;
if (nsecs<0) {
xt->u8_nsecs=0;
xt->u8_prec=u8_second;}
else {xt->u8_nsecs=nsecs; xt->u8_prec=u8_nanosecond;}
return tick;
}
U8_EXPORT
/* u8_localtime
Arguments: a pointer to a tm struct and a time_t value
Returns: the time_t value or -1 if it failed
*/
time_t u8_localtime(struct U8_XTIME *xt,time_t tick)
{
return u8_localtime_x(xt,tick,-1);
}
U8_EXPORT
/* u8_set_xtime_precision
Arguments: a pointer to an xtime struct and an integer
Returns: void
*/
void u8_set_xtime_precision(struct U8_XTIME *xt,u8_tmprec prec)
{
time_t secs=xt->u8_tick;
if (prec==u8_microsecond) {
double nsecs=(double)xt->u8_nsecs;
double usecs=1000*round(nsecs/1000);
xt->u8_nsecs=usecs;}
else if (prec==u8_millisecond) {
double nsecs=(double)xt->u8_nsecs;
double msecs=1000000*round(nsecs/1000000);
xt->u8_nsecs=msecs;}
else if (prec>=u8_second) {
xt->u8_nsecs=0;
if (prec==u8_minute) secs=(secs/60)*60;
else if (prec==u8_hour) secs=(secs/3600)*3600;
else if (prec==u8_day) secs=(secs/(24*3600))*(24*3600);
else if (prec==u8_month) secs=(secs/(30*24*3600))*(30*24*3600);
else if (prec==u8_year) secs=(secs/(365*24*3600))*(365*24*3600);
else NO_ELSE;}
else NO_ELSE;
xt->u8_prec=prec;
}
U8_EXPORT
/* u8_set_xtime_gmtoff
Arguments: a pointer to an xtime struct, a tzoff (int), and a dstoff (int)
Returns: void
*/
void u8_set_xtime_gmtoff(struct U8_XTIME *xt,int tzoff,int dstoff)
{
u8_init_xtime(xt,((xt->u8_tick)-tzoff),xt->u8_prec,
xt->u8_nsecs,tzoff,dstoff);
}
U8_EXPORT
/* u8_xtime_plus
Arguments: a pointer to an xtime struct and an integer
Returns: void
*/
void u8_xtime_plus(struct U8_XTIME *xt,double delta)
{
int sign=(delta>=0.0);
double absval=((delta<0.0) ? (-delta) : (delta));
double secs=floor(absval);
double ndelta=(delta-absval)*1000000000.0;
u8_tmprec precision=xt->u8_prec;
time_t tick=xt->u8_tick; unsigned int nsecs=xt->u8_nsecs;
int tzoff=xt->u8_tzoff, dstoff=xt->u8_dstoff;
tick=((sign) ? (tick+secs) : (tick-secs));
if ((sign) && (nsecs+ndelta>1000000000)) {
tick++; nsecs=(nsecs+ndelta)-1000000000;}
if ((!(sign)) && (xt->u8_nsecs-ndelta<0)) {
tick--; nsecs=(nsecs-ndelta)+1000000000;}
u8_init_xtime(xt,tick,precision,nsecs,tzoff,dstoff);
/* Reset the precision */
xt->u8_prec=precision;
}
U8_EXPORT
/* u8_xtime_diff
Arguments: pointers to two xtime structs
Returns: void
*/
double u8_xtime_diff(struct U8_XTIME *xt,struct U8_XTIME *yt)
{
double xsecs=((double)(xt->u8_tick))+(0.000000001*xt->u8_nsecs);
double ysecs=((double)(yt->u8_tick))+(0.000000001*yt->u8_nsecs);
double totaldiff=xsecs-ysecs;
#if 0 /* This doesn't seem to work, but the idea is to only return a result
as precise as is justified. */
enum u8_timestamp_precision result_precision=
((xt->u8_prec<yt->u8_prec) ? (xt->u8_prec) : (yt->u8_prec));
double precfactor=getprecfactor(result_precision);
return trunc(precfactor*totaldiff)/precfactor;
#endif
return totaldiff;
}
U8_EXPORT
/* u8_get_tm_zone:
Arguments: a pointer to an extended time pointer
Returns: a time_t or -1 if it fails for some reason
This will try and get the finest precision time it can.
*/
u8_string u8_get_tm_zone(int tzoff,int dstoff)
{
if ( (tzoff == 0) && (dstoff == 0) )
return "UTC";
else if ( (tzoff == -5*3600) && (dstoff == 0) )
return "EST";
else if ( (tzoff == -5*3600) && (dstoff == 3600) )
return "EDT";
else if ( (tzoff == -6*3600) && (dstoff == 0) )
return "CST";
else if ( (tzoff == -6*3600) && (dstoff == 3600) )
return "CDT";
else if ( (tzoff == -7*3600) && (dstoff == 0) )
return "MST";
else if ( (tzoff == -7*3600) && (dstoff == 3600) )
return "MDT";
else if ( (tzoff == -8*3600) && (dstoff == 0) )
return "PST";
else if ( (tzoff == -8*3600) && (dstoff == 3600) )
return "PDT";
else if ( (tzoff == 0) && (dstoff == 3600) )
return "BDST";
else if ( (tzoff == 3600) && (dstoff == 0) )
return "WET";
else if ( (tzoff == 3600) && (dstoff == 0) )
return "WEST";
else if ( (tzoff == 3*3600) && (dstoff == 0) )
return "MSK";
else if ( (tzoff == 3*3600) && (dstoff == 3600) )
return "MSD";
else if ( (tzoff == (3*3600+1800)) && (dstoff == 0) )
return "NST";
else if ( (tzoff == 2*3600) && (dstoff == 0) )
return "EET";
else if ( (tzoff == 2*3600) && (dstoff == 3600) )
return "EEST";
else if ( (tzoff == 13*3600) && (dstoff == 0) )
return "NZST";
else if ( (tzoff == 13*3600) && (dstoff == 3600) )
return "NZDT";
else return NULL;
}
U8_EXPORT
/* u8_xtime_to_tptr
Arguments: pointers to an xtime struct and a tm struct
Returns: void
*/
struct tm *u8_xtime_to_tptr(struct U8_XTIME *xt,struct tm *tm)
{
if (tm == NULL) tm = u8_alloc(struct tm);
memset(tm,0,sizeof(struct tm));
copy_xt2tm(xt,tm);
tm->tm_gmtoff=xt->u8_tzoff;
if (xt->u8_dstoff) tm->tm_isdst=1;
tm->tm_zone = (char *) u8_get_tm_zone(xt->u8_tzoff,xt->u8_dstoff);
return tm;
}
U8_EXPORT
/* u8_now:
Arguments: a pointer to an extended time pointer
Returns: a time_t or -1 if it fails for some reason
This will try and get the finest precision time it can.
*/
time_t u8_now(struct U8_XTIME *xtp)
{
if (xtp) {
u8_xtime result=u8_local_xtime(xtp,-1,u8_femtosecond,0);
if (result) return result->u8_tick;
else return -1;}
else {
time_t result=time(NULL);
if (result<0) {
u8_graberr(errno,"u8_now",NULL); errno=0;}
return result;}
}
/* Time and strings */
struct TZENTRY {char *name; int tzoff; int dstoff;};
static struct TZENTRY tzones[]= {
{"Z",0,0},
{"GMT",0,0},
{"UT",0,0},
{"UTC",0,0},
{"EST",-5*3600,0},
{"EDT",-5*3600,3600},
{"CST",-6*3600,0},
{"CDT",-6*3600,3600},
{"MST",-7*3600,0},
{"MDT",-7*3600,0},
{"PST",-8*3600,0},
{"PDT",-8*3600,3600},
{"CET",1*3600,0},
{"EET",2*3600,0},
{NULL,0,0}};
static int lookup_tzname(u8_string string,int dflt)
{
struct TZENTRY *zones=tzones;
while ((*zones).name)
if (strcasecmp(string,(*zones).name) == 0)
return (*zones).tzoff;
else zones++;
return dflt;
}
U8_EXPORT
/* u8_parse_tzspec:
Arguments: a string and a default offset
Returns: an offset from UTC
This uses a built in table but should really use operating system
facilities if they were even remotely standardized.
*/
int u8_parse_tzspec(u8_string s,int dflt)
{
int hours=0, mins=0, secs=0, sign=1;
char *offstart=strchr(s,'+');
if (offstart == NULL) {
offstart=strchr(s,'-');
if (offstart) sign=-1;
else return lookup_tzname(s,dflt);}
sscanf(offstart+1,"%d:%d:%d",&hours,&mins,&secs);
return sign*(hours*3600+mins*60+secs);
}
U8_EXPORT
/* u8_use_tzspec:
Arguments: a pointer to a U8_XTIME structure, a string, and a default offset
Returns: an offset from UTC
This uses a built in table but should really use operating system
facilities if they were even remotely standardized.
*/
void u8_apply_tzspec(struct U8_XTIME *xt,u8_string s)
{
int hours=0, mins=0, secs=0, sign=1;
int dhours=0, dmins=0, dsecs=0, dsign=1;
char *offstart=strchr(s,'+');
if (offstart == NULL) {
offstart=strchr(s,'-');
if (offstart) sign=-1;}
if (offstart) {
char *dstart=strchr(offstart+1,'+');
sscanf(offstart+1,"%d:%d:%d",&hours,&mins,&secs);
xt->u8_tzoff=sign*(hours*3600+mins*60+secs);
if (!(dstart)) {
dstart=strchr(offstart+1,'-');
if (dstart) dsign=-1;}
if (dstart) {
sscanf(dstart+1,"%d:%d:%d",&dhours,&dmins,&dsecs);
xt->u8_dstoff=dsign*(dhours*3600+dmins*60+dsecs);}}
else {
struct TZENTRY *zones=tzones;
while ((*zones).name)
if (strcasecmp(s,(*zones).name) == 0) {
xt->u8_tzoff=(*zones).tzoff;
xt->u8_dstoff=(*zones).dstoff;
return;}
else zones++;}
}
U8_EXPORT
/* u8_iso8601_to_xtime:
Arguments: a string and a pointer to a timestamp structure
Returns: -1 on error, the time as a time_t otherwise
This takes an iso8601 string and fills out an extended time pointer which
includes possible timezone and precision information.
*/
time_t u8_iso8601_to_xtime(u8_string s,struct U8_XTIME *xtp)
{
const char *tzstart;
int stdpos[]={-1,4,7,10,13,16,19,20}, *pos=stdpos;
int basicpos[]={-1,4,6,8,11,13,15,17};
int nsecs=0, n_elts, len=strlen(s);
if (strchr(s,'/')) return (time_t) -1;
memset(xtp,0,sizeof(struct U8_XTIME));
n_elts=sscanf(s,"%04u-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu.%u",
&xtp->u8_year,&xtp->u8_mon,
&xtp->u8_mday,&xtp->u8_hour,
&xtp->u8_min,&xtp->u8_sec,
&nsecs);
if ((n_elts == 1)&&(len>4)) {
/* Assume basic format */
n_elts=sscanf(s,"%04u%02hhu%02hhuT%02hhu%02hhu%02hhu.%u",
&xtp->u8_year,&xtp->u8_mon,
&xtp->u8_mday,&xtp->u8_hour,
&xtp->u8_min,&xtp->u8_sec,
&nsecs);
pos=basicpos;}
/* Give up if you can't parse anything */
if (n_elts == 0) return (time_t) -1;
/* Adjust month */
xtp->u8_mon--;
/* Set precision */
xtp->u8_prec=n_elts;
if (n_elts <= 6) xtp->u8_nsecs=0;
if (n_elts == 7) {
const char *start=s+pos[n_elts], *scan=start;
int n_digits=0;
while (isdigit(*scan)) scan++;
n_digits=scan-start;
if (n_digits==9) {
xtp->u8_nsecs=nsecs;
xtp->u8_prec=u8_nanosecond;}
else if (n_digits<9) {
int multiplier=1, missing=9-n_digits, i=0;
while (i<missing) {multiplier=multiplier*10; i++;}
if (n_digits<=3) xtp->u8_prec=u8_millisecond;
else if (n_digits<=6) xtp->u8_prec=u8_microsecond;
else xtp->u8_prec=u8_nanosecond;
xtp->u8_nsecs=nsecs*multiplier;}
else if (n_digits>9) {
int divisor=1, extra=n_digits-9, i=0;
while (i<extra) {divisor=divisor*10; i++;}
xtp->u8_nsecs=nsecs/divisor;
xtp->u8_prec=u8_nanosecond;}
tzstart=scan;}
else tzstart=s+pos[n_elts];
if ((tzstart)&&(*tzstart)) {
u8_apply_tzspec(xtp,tzstart);
xtp->u8_tick=mktime_x(xtp,0);}
else xtp->u8_tick=mktime_x(xtp,1);
return xtp->u8_tick;
}
void xtime_to_iso8601(u8_output ss,struct U8_XTIME *xt,int flags)
{
char buf[128], tzbuf[128];
struct U8_XTIME utc, *xtptr;
char *dash="-", *colon=":";
u8_tmprec prec=xt->u8_prec;
if ((flags)&(U8_ISO8601_BASIC)) {dash=""; colon="";}
if (((flags)&(U8_ISO8601_BASIC))&&(prec>u8_second)) prec=u8_second;
else if (((flags)&(U8_ISO8601_NOMSECS))&&(prec>u8_second)) prec=u8_second;
else NO_ELSE;
if ((flags)&(U8_ISO8601_UTC)&&
((xt->u8_tzoff!=0)||(xt->u8_dstoff!=0))) {
u8_init_xtime(&utc,xt->u8_tick,prec,
((prec>u8_second)?(xt->u8_nsecs):(0)),
0,0);
xtptr=&utc;}
else xtptr=xt;
switch (prec) {
case u8_year:
sprintf(buf,"%04d",xtptr->u8_year); break;
case u8_month:
sprintf(buf,"%04d%s%02d",
xtptr->u8_year,dash,xtptr->u8_mon+1); break;
case u8_day:
sprintf(buf,"%04d%s%02d%s%02d",
xtptr->u8_year,dash,xtptr->u8_mon+1,dash,xtptr->u8_mday); break;
case u8_hour:
sprintf(buf,"%04d%s%02d%s%02dT%02d",
xtptr->u8_year,dash,xtptr->u8_mon+1,dash,xtptr->u8_mday,
xtptr->u8_hour);
break;
case u8_minute:
sprintf(buf,"%04d%s%02d%s%02dT%02d%s%02d",
xtptr->u8_year,dash,xtptr->u8_mon+1,dash,xtptr->u8_mday,
xtptr->u8_hour,colon,xtptr->u8_min);
break;
/* u8_maxtmprec should never get this var */
case u8_maxtmprec:
u8_log(LOG_WARN,"xtime_to_iso8601","Invalid precision %d for tick=%ld",
(int)prec,(long long)(xtptr->u8_tick));
case u8_second:
sprintf(buf,"%04d%s%02d%s%02dT%02d%s%02d%s%02d",
xtptr->u8_year,dash,xtptr->u8_mon+1,dash,xtptr->u8_mday,
xtptr->u8_hour,colon,xtptr->u8_min,colon,xtptr->u8_sec);
break;
case u8_millisecond:
/* The cases here and below don't exist for the basic format */
sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02d.%03d",
xtptr->u8_year,xtptr->u8_mon+1,xtptr->u8_mday,
xtptr->u8_hour,xtptr->u8_min,xtptr->u8_sec,
xtptr->u8_nsecs/1000000); break;
case u8_microsecond:
sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02d.%06d",
xtptr->u8_year,xtptr->u8_mon+1,xtptr->u8_mday,
xtptr->u8_hour,xtptr->u8_min,xtptr->u8_sec,
xtptr->u8_nsecs/1000); break;
case u8_nanosecond: case u8_picosecond: case u8_femtosecond:
sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02d.%09d",
xtptr->u8_year,xtptr->u8_mon+1,xtptr->u8_mday,
xtptr->u8_hour,xtptr->u8_min,xtptr->u8_sec,
xtptr->u8_nsecs); break;
default:
sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02d",
xtptr->u8_year,xtptr->u8_mon+1,xtptr->u8_mday,
xtptr->u8_hour,xtptr->u8_min,xtptr->u8_sec);
break;}
if ((flags)&(U8_ISO8601_NOZONE)) tzbuf[0]='\0';
else if ((xtptr->u8_tzoff) || (xtptr->u8_dstoff)) {
int off=xtptr->u8_tzoff+xtptr->u8_dstoff;
char *sign=((off<0) ? "-" : "+");
int tzoff=((off<0) ? (-((off))) : (off));
int hours=tzoff/3600,
minutes=(tzoff%3600)/60,
seconds=tzoff%3600-minutes*60;
if (seconds)
sprintf(tzbuf,"%s%d:%02d:%02d",sign,hours,minutes,seconds);
else sprintf(tzbuf,"%s%d:%02d",sign,hours,minutes);}
else strcpy(tzbuf,"Z");
if (xtptr->u8_prec > u8_day)
u8_printf(ss,"%s%s",buf,tzbuf);
else u8_printf(ss,"%s",buf);
}
U8_EXPORT
/* u8_xtime_to_iso8601:
Arguments: a timestamp and a pointer to a string stream
Returns: -1 on error, the time as a time_t otherwise
This takes xtime pointer and outputs an ISO8601 representation of it,
obeying the precision of the XTIME structure
*/
void u8_xtime_to_iso8601(u8_output ss,struct U8_XTIME *xt)
{
xtime_to_iso8601(ss,xt,0);
}
U8_EXPORT
/* u8_xtime_to_iso8601_x:
Arguments: a timestamp, a pointer to a string stream, and a flags arg
Returns: -1 on error, the time as a time_t otherwise
This takes xtime pointer and outputs an ISO8601 representation of it,
obeying the precision of the XTIME structure
The flags arg provides display options, which can be ORd together:
U8_ISO8601_BASIC: use the more compact BASIC format)
U8_ISO8601_NOZONE: don't show the timezone information
U8_ISO8601_NOMSECS: don't show milli/micro/nano seconds
U8_ISO8601_UTC: display time as UTC
*/
void u8_xtime_to_iso8601_x(u8_output ss,struct U8_XTIME *xt,int flags)
{
xtime_to_iso8601(ss,xt,flags);
}
static u8_string month_names[12]=
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static u8_string dow_names[7]=
{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
static int getmonthnum(u8_string s)
{
int i=0;
while (i<12)
if (strncasecmp(s,month_names[i],3)==0)
return i;
else i++;
return -1;
}
U8_EXPORT
/* u8_rfc822_to_xtime:
Arguments: a string and a pointer to a timestamp structure
Returns: -1 on error, the time as a time_t otherwise
This takes an rfc822 string and fills out an extended time pointer which
includes possible timezone and precision information.
*/
time_t u8_rfc822_to_xtime(u8_string s,struct U8_XTIME *xtp)
{
char tzspec[128], dow[128], mon[128]; int n_elts;
if (strchr(s,'/')) return (time_t) -1;
tzspec[0]='\0'; dow[0]='\0'; mon[0]='\0';
memset(xtp,0,sizeof(struct U8_XTIME));
if (isdigit(*s))
n_elts=sscanf(s,"%hhd %s %d %hhd:%hhd:%hhd %s",
&xtp->u8_mday,(char *)&mon,
&xtp->u8_year,
&xtp->u8_hour,
&xtp->u8_min,&xtp->u8_sec,
(char *)tzspec);
else {
n_elts=sscanf(s,"%s %hhd %s %d %hhd:%hhd:%hhd %s",
(char *)&dow,&xtp->u8_mday,(char *)&mon,
&xtp->u8_year,
&xtp->u8_hour,
&xtp->u8_min,&xtp->u8_sec,
(char *)tzspec);
if (n_elts<2)
n_elts=sscanf(s,"%s %s %hhd %d %hhd:%hhd:%hhd %s",
(char *)&dow,&xtp->u8_mday,(char *)&mon,
&xtp->u8_year,
&xtp->u8_hour,
&xtp->u8_min,&xtp->u8_sec,
(char *)tzspec);
xtp->u8_mon=getmonthnum(mon);}
/* Give up if you can't parse anything */
if (n_elts == 0) return (time_t) -1;
/* Set precision */
xtp->u8_prec=u8_second;
xtp->u8_nsecs=0;
if (tzspec[0]) u8_apply_tzspec(xtp,tzspec);
xtp->u8_tick=u8_mktime(xtp); xtp->u8_nsecs=0;
return xtp->u8_tick;
}
U8_EXPORT
/* u8_xtime_to_rfc822:
Arguments: a timestamp and a pointer to a string stream
Returns: -1 on error, the time as a time_t otherwise
This takes an iso8601 string and fills out an extended time pointer which
includes possible timezone and precision information.
*/
void u8_xtime_to_rfc822(u8_output ss,struct U8_XTIME *xtp)
{
struct U8_XTIME asgmt;
char buf[128];
u8_init_xtime(&asgmt,xtp->u8_tick,xtp->u8_prec,xtp->u8_nsecs,0,0);
sprintf(buf,"%s, %d %s %04d %02d:%02d:%02d",
dow_names[xtp->u8_wday],
xtp->u8_mday,
month_names[xtp->u8_mon],
xtp->u8_year,
xtp->u8_hour,
xtp->u8_min,
xtp->u8_sec);
u8_string tz_name = u8_get_tm_zone(xtp->u8_tzoff,xtp->u8_dstoff);
if (tz_name)
u8_printf(ss,"%s %s",buf,tz_name);
else {
int toff = xtp->u8_tzoff + xtp->u8_dstoff;
u8_printf(ss,"%s +%d:%02d",buf,toff/3600,toff%3600);}
}
U8_EXPORT
/* u8_xtime_to_rfc822_x:
Arguments: a timestamp and a pointer to a string stream
Returns: -1 on error, the time as a time_t otherwise
This takes an iso8601 string and fills out an extended time pointer which
includes possible timezone and precision information.
*/
void u8_xtime_to_rfc822_x(u8_output ss,struct U8_XTIME *xtp,int zone,int flags)
{
struct U8_XTIME inzone;
char buf[128];
if (zone==0)
u8_init_xtime(&inzone,xtp->u8_tick,xtp->u8_prec,xtp->u8_nsecs,0,0);
else if (zone==-1)
u8_init_xtime
(&inzone,xtp->u8_tick,xtp->u8_prec,xtp->u8_nsecs,
xtp->u8_tzoff,xtp->u8_dstoff);
else if (zone==1)
u8_local_xtime(&inzone,xtp->u8_tick,xtp->u8_prec,xtp->u8_nsecs);
else u8_init_xtime(&inzone,xtp->u8_tick,xtp->u8_prec,xtp->u8_nsecs,zone,0);
if (xtp->u8_prec >= u8_second)
u8_sprintf(buf,128,"%s, %d %s %04d %02d:%02d:%02d",
dow_names[inzone.u8_wday],
inzone.u8_mday,
month_names[inzone.u8_mon],
(inzone.u8_year),
inzone.u8_hour,inzone.u8_min,inzone.u8_sec);
else if (xtp->u8_prec >= u8_minute)
u8_sprintf(buf,128,"%s, %d %s %04d %02d:%02d",
dow_names[inzone.u8_wday],
inzone.u8_mday,
month_names[inzone.u8_mon],
(inzone.u8_year),
inzone.u8_hour,inzone.u8_min);
else if (xtp->u8_prec >= u8_hour)
u8_sprintf(buf,128,"%s, %d %s %04d %02d:00",
dow_names[inzone.u8_wday],
inzone.u8_mday,
month_names[inzone.u8_mon],
(inzone.u8_year),
inzone.u8_hour);
else if (xtp->u8_prec <= u8_day)
u8_sprintf(buf,128,"%s, %d %s %04d",
dow_names[inzone.u8_wday],
inzone.u8_mday,
month_names[inzone.u8_mon],
(inzone.u8_year));
else u8_sprintf(buf,128,"%s, %d %s %04d %02d:%02d:%02d",
dow_names[inzone.u8_wday],
inzone.u8_mday,
month_names[inzone.u8_mon],
(inzone.u8_year),
inzone.u8_hour,inzone.u8_min,inzone.u8_sec);
if ( (flags) & (U8_RFC822_NOZONE) )
u8_puts(ss,buf);
else {
int minus=(inzone.u8_tzoff+inzone.u8_dstoff)<0;
int off=((minus)?(-(inzone.u8_tzoff+inzone.u8_dstoff)):
(inzone.u8_tzoff+inzone.u8_dstoff));
int hroff=off/3600, minoff=off%3600/60;
u8_printf(ss,"%s %s%02d%02d",buf,((minus)?"-":"+"),hroff,minoff);}
}
/* printf handling for time related values */
static u8_string time_printf_handler
(u8_output s,char *cmd,u8_byte *buf,int bufsz,va_list *args)
{
struct U8_XTIME xt; memset(&xt,0,sizeof(struct U8_XTIME));
if (strchr(cmd,'*'))
/* Uses the current time, doesn't consume an argument. */
if (strchr(cmd+1,'G'))
u8_init_xtime(&xt,-1,u8_femtosecond,0,0,0);
else u8_local_xtime(&xt,-1,u8_femtosecond,0);
else if (strchr(cmd,'X'))
/* The argument is an U8_XTIME pointer. */
if (strchr(cmd,'G')) {
/* Convert the result to GMT/UTC */
struct U8_XTIME *xtarg=va_arg(*args,u8_xtime);
memcpy(&xt,xtarg,sizeof(struct U8_XTIME));}
else {
/* Use the time intrinsic to the XTIME structure. */
struct U8_XTIME *xtarg=va_arg(*args,u8_xtime);
memcpy(&xt,xtarg,sizeof(struct U8_XTIME));}
/* Otherwise, the argument is time_t value. */
else if (strchr(cmd,'G'))
/* Use it as GMT. */
u8_init_xtime(&xt,va_arg(*args,time_t),u8_second,0,0,0);
else /* Display it as local time */
u8_local_xtime(&xt,va_arg(*args,time_t),u8_second,0);
if (strchr(cmd,'i')) {
/* With this argument, specify the precision to be used. */
u8_tmprec precision=u8_second;
if (strchr(cmd,'Y')) precision=u8_year;
else if (strchr(cmd,'D')) precision=u8_day;
else if (strstr(cmd,"HM")) precision=u8_minute;
else if (strstr(cmd,"MS")) precision=u8_millisecond;
else if (strchr(cmd,'H')) precision=u8_hour;
else if (strchr(cmd,'S')) precision=u8_second;