-
Notifications
You must be signed in to change notification settings - Fork 0
/
strftime.c
1318 lines (1179 loc) · 39.5 KB
/
strftime.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
/* strftime.c
==========
Note that the function defined here is called ya_strftime() to avoid issues with tryng to rename an existing strftime() function (especially in C++).
See comments at the start of strptime.c for the format options supported (basically C99 in the C locale with a couple of extensions).
This file was created by starting from strftime.c obtained from https://github.com/arnoldrobbins/strftime on 3/4/2022
A significant number of changes mave been made, initially to make it compile for Windows with C99 using TDM-GCC 10.3.0, but then to expand the functionality and remove bugs.
This file now compiles and runs correctly with devC++/TDMgcc 10.3.0 on Windows, Builder C++ (10.2) on Windows and gcc on Linux.
This file uses the structure struct strp_tz_struct strp_tz to pass information between strptime() and strftime() and to support timezones in a way thats independent of the operating system.
In general if strptime() is not used then strftime() will get things like the timezone from the OS [ Windows/Linux ], but if strptime() is called the last strptime() call may provide these.
This allows for example strptime() to be used to decode "time stamps" in a "Log file" recorded at a different time (and even time zone),
with strftime() used to display values from these timestamps as if it was in the locale where the log was created.
If you want to guarantee that no information is passed into strftime() from strptime(), then call init_strp_tz() before calling strptime().
An extensive test program is also provided (main.c).
For gcc under Linux compile test program with :
gcc -Wall -O3 -o date-time main.c strftime.c strptime.c
./date-time
For dev-C++ (tested with tdmgcc 10.3.0) there is a *.dev file, and the project files (*.cbproj) for Builder C++ are also present.
You should see no errors or warnings when compiling these files.
Thanks to Arnold Robbins and the other contributors to the original file who are all listed below in the original file header.
*/
/*----------------------------------------------------------------------------
* Copyright (c) 2022 Peter Miller
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*--------------------------------------------------------------------------*/
/* Header from file as obtained from github: https://github.com/arnoldrobbins/strftime on 3/4/2022 */
/*
* strftime.c
*
* Public-domain implementation of ISO C library routine.
*
* If you can't do prototypes, get GCC.
*
* The C99 standard now specifies just about all of the formats
* that were additional in the earlier versions of this file.
*
* For extensions from SunOS, add SUNOS_EXT.
* For extensions from HP/UX, add HPUX_EXT.
* For VMS dates, add VMS_EXT.
* For complete POSIX semantics, add POSIX_SEMANTICS.
*
* The code for %X follows the C99 specification for
* the "C" locale.
*
* The code for %c, and %x follows the C11 specification for
* the "C" locale.
*
* With HAVE_NL_LANGINFO defined, locale-based values are used.
*
* This version doesn't worry about multi-byte characters.
*
* Arnold Robbins
* January, February, March, 1991
* Updated March, April 1992
* Updated April, 1993
* Updated February, 1994
* Updated May, 1994
* Updated January, 1995
* Updated September, 1995
* Updated January, 1996
* Updated July, 1997
* Updated October, 1999
* Updated September, 2000
* Updated December, 2001
* Updated January, 2011
* Updated April, 2012
* Updated March, 2015
* Updated June, 2015
*
* Fixes from ado@elsie.nci.nih.gov,
* February 1991, May 1992
* Fixes from Tor Lillqvist tml@tik.vtt.fi,
* May 1993
* Further fixes from ado@elsie.nci.nih.gov,
* February 1994
* %z code from chip@chinacat.unicom.com,
* Applied September 1995
* %V code fixed (again) and %G, %g added,
* January 1996
* %v code fixed, better configuration,
* July 1997
* Moved to C99 specification.
* September 2000
* Fixes from Tanaka Akira <akr@m17n.org>
* December 2001
*/
/* end of original header */
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdint.h>
// #include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// #include <math.h>
#include "time_local.h"
#define YEAR0LEAP /* if defined make year 0 a leap year (if not defined then its not) */
/* defaults: season to taste , note tests below use the fact if these are defined or not, the value does not matter */
// #define SUNOS_EXT /* stuff in SunOS strftime routine */
// #define VMS_EXT /* include %v for VMS date format */
// #define HPUX_EXT /* non-conflicting stuff in HP-UX date */
// #define POSIX_SEMANTICS /* call tzset() if TZ changes */
// #define POSIX_2008 /* flag and fw for C, F, G, Y formats */
// #define HAVE_NL_LANGINFO /* locale-based values [ does not work for Windows at present ] */
// #define HAVE_TZNAME 1 /* has to be defined as 1 ! */
#ifdef HAVE_NL_LANGINFO
#include <locale.h>
#endif
#ifdef __BORLANDC__
/* builder C++ */
#define timezone _timezone /* _timezone is the difference, in seconds, between the current local time and Greenwich mean time */
#define daylight _daylight /* _daylight is set to 1 if the daylight savings time conversion should be applied */
#endif
#ifdef __linux
/* linux */
#define _tzname tzname
#endif
extern const char * strp_weekdays[] ;/* full names of weekdays and months - defined in strptime.c */
extern const char * strp_monthnames[];
extern void tzset(void);
static int weeknumber(const struct tm *timeptr, int firstweekday);
static int iso8601wknum(const struct tm *timeptr);
#define range(low, item, hi) max(low, min(item, hi))
#undef min /* just in case */
/* min --- return minimum of two numbers */
static inline int
min(int a, int b)
{
return (a < b ? a : b);
}
#undef max /* also, just in case */
/* max --- return maximum of two numbers */
static inline int
max(int a, int b)
{
return (a > b ? a : b);
}
#ifdef __linux
/* need strnicmp() , note we just need equal & != here the ordering does not matter
if char is signed, you use chars with more than 7 bits and you are using this function for ordering check if it does what you want !
*/
int strnicmp( const char * s1, const char * s2, size_t n )
{
while ( n && *s1 && ( tolower(*s1) == tolower(*s2) ) )
{
++s1;
++s2;
--n;
}
if ( n == 0 ) return 0;
return tolower(*s1) - tolower(*s2);
}
#endif
// 3 functions below based on those in K&R 2nd ed pp 111, changed so month is 0..11 to match the rest of the code
static char daytab[2][13]={ /* table of days in a month for non leap years and leap years*/
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
bool is_leap(int64_t year) /* returns true if year [with no offset] is a leap year */
{if(year==0)
#ifdef YEAR0LEAP
return true; // year 0 IS a leap year
#else
return false; // year 0 is a NOT a leap year
#endif
else if(year<0) year= -year; // just in case % operator does silly things with negative arguments
return (year%4==0 && year%100!=0) || year%400==0; // no need to worry about sign of year here as just check ==0 or !=0
}
void month_day(int64_t year, int yearday, int*pmonth, int *pday)
/* set month, day from year (actual year with no offset) and day of year (actually days since jan 1st, 0-365) */
/* returns pmonth as 0->11, 0-Jan, and pday=1->31 */
{int i;
bool leap =is_leap(year);
++yearday; // supplied as 0=jan 1st, below treats that as the 1st day of the year
for(i=0;yearday>daytab[leap][i] && i<12;i++) // test for i<12 avoids overrunning the array if yearday is too big
yearday-=daytab[leap][i];
*pmonth=i;// 0->11
*pday=yearday;
}
int day_of_year(int64_t year, int month, int day) // year without offset, month 0->11, day 1->31, returns days since 1st jan (0>365)
{bool leap =is_leap(year);
if(month>11) month=11;// ensure we don't overrun the array
for(int i=0;i<month;++i)
day+=daytab[leap][i];
return day-1; // days since 1st jan start at 0
}
static inline time_t year_to_s(int64_t year)
{ // converts year to seconds since 1st Jan 1970.
// While year is a int64_t its assumed to come from an int (32 bits) with a 1900 offset, so we don't need to worry about overflow in the conversion
int64_t lyears;
/* we want to factor in leap years to the year before noting year 0 is not a leap year, so 0,1,2,3,4,5,6,.. we want to count 1st leap year when year=5 (as year 4 is a leap year)
for negative years the first one we want to count is the year -5 */
if(year>=0) lyears=year-1; /* 5-1=4 so lyears/4=1. 0 => -1 but -1/4 is still 0 so thats OK */
else lyears=year+1;/* -5 +1 = -4 so lyears/4= -1 */
// calculation now just needs to use yday, year and lyears
#ifdef YEAR0LEAP
/* if year>0 then 1 more leap year to add, also adds 1 to offset for 1970 so overall this cancels out for 1970 */
return ( (time_t)(lyears/4) - (time_t)(lyears/100) + (time_t)(lyears/400 ) + (time_t)(year>0?1:0)+
(time_t)year*365 - (time_t)719528 /* offset to make 1st Jan 1970=0 */
)*(time_t)86400; /* 86400=24*60*60; hours->minutes->seconds */
#else
return ( (time_t)(lyears/4) - (time_t)(lyears/100) + (time_t)(lyears/400 ) +
(time_t)year*365 - (time_t)719527 /* offset to make 1st Jan 1970=0 */
)*(time_t)86400; /* 86400=24*60*60; hours->minutes->seconds */
#endif
}
static inline time_t ya_mktime_s(int64_t year, int month, int mday, int yday, int hour, int min, int sec ) /* version of mktime() that returns secs */
{// Converts Gregorian date to seconds since 1970-01-01 00:00:00.
// Gregorian date has been used in the UK since 1752 ,but in most other places since 15th Oct 1582
// leap seconds allowed in sec, and midnight=24:00:00 is also allowed (but this is not allowed by strptime() ).
// ignores timezone
// ignores leap seconds in previous years (so is currently 37 sec in error) - as the timing of future leap seconds is unknown these would be impossible to allow for!
// used by %s format
// year is actual year (no offset) - its assume year is basically an int, we use int64_t to allow year+1900 to be passed as an argument without overflowing as tm_year=0 for year 1900.
// month 0..11, mday 1..31.
// yday is the number of days since 1st Jan 0-365
// if mday<=0 (which is invalid) then use yday, otherwise use month, mday. As all values in tm are initialised to 0 mday==0 means its not been set
if(mday>0 && yday <=0)
{// get yday from month & mday [ yday might already be valid, but we have no way to know ]
yday=day_of_year(year,month,mday);
}
return year_to_s(year)+((( (time_t)yday
)*24 + hour /* now have hours - midnight tomorrow handled here */
)*60 + min /* now have minutes */
)*60 + sec; /* finally seconds */
}
time_t ya_mktime_tm(const struct tm *tp) /* version of mktime() that returns secs and takes (but does not change) timeptr */
{// Converts Gregorian date to seconds since 1970-01-01 00:00:00.
// just calls ya_mktime() with the corect arguments
return ya_mktime_s((int64_t)tp->tm_year+1900,tp->tm_mon,tp->tm_mday,tp->tm_yday,tp->tm_hour,tp->tm_min,tp->tm_sec);
}
int day_of_week_yd(int64_t year,int yday) /* year with no offset eg 1900 and yday is days since 1st Jan 0->365 */
{// time_t ya_mktime_s(int64_t year, int month,int mday,const int yday,const int hour,const int min, const int sec ) /* version of mktime() that returns secs */
int64_t sy=ya_mktime_s(year, 0,0,yday,0,0,0 ); /* version of mktime() that returns secs since 1st Jan 1970 */
int64_t days=sy/(3600*24);
days=(days+4)%7; // +4 as 1st jan 1970 (secs=0) was a Thursday .
if(days<0)days+=7;
return (int)days;
}
int day_of_week(int64_t y, int m, int d) /* 0 = Sunday */
{ // year with no offset, m 0=jan..11=dec, d = day of month 1..31
int yday=day_of_year(y,m,d);
return day_of_week_yd(y,yday);
}
int cmp_tm(struct tm *tm1,struct tm *tm2) /* returns sign(tm1-tm2) */
{time_t s1,s2;
s1=ya_mktime_tm(tm1);
s2=ya_mktime_tm(tm2);
if(s1==s2) return 0;
else if(s1<s2) return -1;
return 1;
}
#if 0 /* use a binary search to find the year This takes 11.4 secs for test program vs 10.3 secs for approximation below, so #if 0 is recommended */
void sec_to_tm(time_t t,struct tm *tp) // reverse of ya_mktime_tm, converts secs since epoch to the numbers of tp
{
int64_t year;
int month=0, mday=1, hour=0, min=0, sec=0,yday=0;
time_t t_y;
/* binary search for year .. */
time_t high=INT_MAX,low= -INT_MAX,mid;
bool found=false;
high*=2; low*=2;// widen range to allow for offset from int in struct tm
while(low<=high)
{mid=(low+high)/2;// no need to worry about overflow as int 32 bits and maths in 64 bits
t_y=year_to_s(mid);// jan 1st 0:0:0 of year mid
if(t_y >t)
high=mid-1;// we are using integers
else if(t_y<t)
low=mid+1;
else
{found=true;
break; // t_y=t
}
}
if(!found)
{ // low <=high is false to cause termination, so high is the lower
year=high;
t_y=year_to_s(high);
}
else year=mid;// found exactly
t-=t_y;// get remainder = secs into year
yday=t/(24*3600);// 24 hrs in a day so this gives us days in the year
t-=yday*24*3600; // whats left is seconds in the day
month_day(year,yday,&month,&mday);
hour=t/3600;
t-=hour*3600;
min=t/60;
sec=t-min*60;
if(year-1900 < -INT_MAX ) year=-INT_MAX+1900; // clip at min (subtract 1900 below)
else if(year-1900 > INT_MAX ) year=(int64_t)INT_MAX+1900; // clip at max (subtract 1900 below)
tp->tm_year=year-1900;
tp->tm_mon=month;
tp->tm_mday=mday;
tp->tm_hour=hour;
tp->tm_min=min;
tp->tm_sec=sec;
tp->tm_yday=yday;
tp->tm_wday=day_of_week_yd(year,yday);
// does not set tm_isdst
}
#else /* use an approximation to find the year */
void sec_to_tm(time_t t,struct tm *tp) // reverse of ya_mktime_tm, converts secs since epoch to the numbers of tp
{/* note that sec_to_tm() sets all fields in tm (except tz), whereas ya_mktime_tm() does not need all fields set to work, so calling ya_mktime_tm() then sec_to_tm() will ensure all fields are set */
int64_t year;
int month, mday, hour, min, sec,yday;
time_t t_y;
#ifdef YEAR0LEAP
year=(t+INT64_C(62167219200))/INT64_C(31556952); // +719528*86400=62,167,219,200 removes "1970" offset added in year_to_s(), 31,556,952=365.2425*24*60*60 which approximates leap years (100-4+1=97 year years in 400 years so 97/400=.2425)
#else
year=(t+INT64_C(62167132800))/INT64_C(31556952); // +719527*86400=62,167,132,800 removes "1970" offset added in year_to_s(), 31,556,952=365.2425*24*60*60 which approximates leap years (100-4+1=97 year years in 400 years so 97/400=.2425)
#endif
t_y=year_to_s(year);
for(int i=0; t_y<t && i<10000;++i)// i stops infinite loops, should only go around this a few times as our initial guess should be quite accurate
{t_y=year_to_s(++year); // have to start 1 higher
}
for(int i=0; t_y>t && i<10000;++i) // i stops infinite loops , our initial guess should be out by a small amount as our approximation above is quite accurate
{
t_y=year_to_s(--year); // keep reducing year till its smaller
}
t-=t_y;// get remainder = secs into year
yday=(int)(t/(24*3600));// 24 hrs in a day so this gives us days in the year
t-=yday*24*3600; // whats left is seconds in the day
month_day(year,yday,&month,&mday);
hour=(int)(t/3600);
t-=hour*3600;
min=(int)(t/60);
sec=(int)(t-min*60);
if(year-1900 < -INT_MAX ) year=-INT_MAX+1900; // clip at min (subtract 1900 below)
else if(year-1900 > INT_MAX ) year=(int64_t)INT_MAX+1900; // clip at max (subtract 1900 below)
tp->tm_year=(int)(year-1900);
tp->tm_mon=month;
tp->tm_mday=mday;
tp->tm_hour=hour;
tp->tm_min=min;
tp->tm_sec=sec;
tp->tm_yday=yday;
tp->tm_wday=day_of_week_yd(year,yday);
// does not set tm_isdst
}
#endif
time_t ya_mktime(struct tm *tp) /* fully functional version of mktime() that returns secs and takes (and changes if necessary) timeptr */
{time_t s;
s=ya_mktime_tm(tp); // convert tp to secs
sec_to_tm(s,tp); // convert secs back to tp - setting all fields of tp to standardised values
return s;
}
time_t UTC_mktime(struct tm *tp,struct strp_tz_struct *tz ) /* version of mktime() that also uses struct strp_tz_struct to adjust secs returned for timezones. Returns UTC secs since epoch (time_t) */
{time_t s;
s=ya_mktime_tm(tp); // convert tp to secs
sec_to_tm(s,tp); // convert secs back to tp - setting all fields of tp to standardised values
if(tp->tm_isdst>0) s-=3600; // if in summer time (daylight savings time) local time has an extra 1 hour added, so subtract that here.
if(tz->tz_off_mins!=strp_tz_default) s-=60*tz->tz_off_mins; // correct for time zone ofset
return s;
}
void UTC_sec_to_tm(time_t t,struct tm *tp,struct strp_tz_struct *tz ) // reverse of UTC_mktime(), converts UTC time as secs since epoch to the numbers of tp, takinginto account struct strp_tz to adjust secs for timezones
{time_t s=t;
if(tp->tm_isdst>0) s+=3600; // if in summer time (daylight savings time) local time has an extra 1 hour added, so add that here.
if(tz->tz_off_mins!=strp_tz_default) s+=60*tz->tz_off_mins; // correct for time zone ofset
sec_to_tm(s,tp);
}
#ifdef POSIX_2008
/* iso_8601_2000_year --- format a year per ISO 8601:2000 as in 1003.1 */
static void
iso_8601_2000_year(char *buf, int year, size_t fw)
{
int extra;
char sign = '\0';
if (year >= -9999 && year <= 9999) {
sprintf(buf, "%0*d", (int) fw, year);
return;
}
/* now things get weird */
if (year > 9999) {
sign = '+';
} else {
sign = '-';
year = -year;
}
extra = year / 10000;
year %= 10000;
sprintf(buf, "%c_%04d_%d", sign, extra, year);
}
#endif /* POSIX_2008 */
#ifdef HAVE_NL_LANGINFO
/* days_a() --- return the short name for the day of the week */
static const char *
days_a(int index)
{
static const nl_item data[] = {
ABDAY_1,
ABDAY_2,
ABDAY_3,
ABDAY_4,
ABDAY_5,
ABDAY_6,
ABDAY_7,
};
return nl_langinfo(data[index]);
}
/* days_l() --- return the long name for the day of the week */
static const char *
days_l(int index)
{
static const nl_item data[] = {
DAY_1,
DAY_2,
DAY_3,
DAY_4,
DAY_5,
DAY_6,
DAY_7,
};
return nl_langinfo(data[index]);
}
/* months_a() --- return the short name for the month */
static const char *
months_a(int index)
{
static const nl_item data[] = {
ABMON_1,
ABMON_2,
ABMON_3,
ABMON_4,
ABMON_5,
ABMON_6,
ABMON_7,
ABMON_8,
ABMON_9,
ABMON_10,
ABMON_11,
ABMON_12,
};
return nl_langinfo(data[index]);
}
/* months_l() --- return the short name for the month */
static const char *
months_l(int index)
{
static const nl_item data[] = {
MON_1,
MON_2,
MON_3,
MON_4,
MON_5,
MON_6,
MON_7,
MON_8,
MON_9,
MON_10,
MON_11,
MON_12,
};
return nl_langinfo(data[index]);
}
/* ampm() --- return am/pm string */
static const char *
ampm(int index)
{
static const nl_item data[] = {
AM_STR,
PM_STR,
};
return nl_langinfo(data[index]);
}
#endif /* ifdef HAVE_NL_LANGINFO */
/* strftime() --- produce formatted time */
size_t ya_strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
{
char *endp = s + maxsize;
char *start = s;
char tbuf[100];
long off;
int i, w;
long y;
static short first = 1;
#ifdef POSIX_SEMANTICS
static char *savetz = NULL;
static int savetzlen = 0;
char *tz;
#endif /* POSIX_SEMANTICS */
#ifdef POSIX_2008
int pad;
size_t fw;
char flag;
#endif /* POSIX_2008 */
if(strp_tz.initialised==0)
init_strp_tz(&strp_tz);
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wnonnull-compare" /* Peter Miller - "fix" incorrect gcc warning 321 26 strftime.c [Warning] 'nonnull' argument 'format' compared to NULL [-Wnonnull-compare] */
#endif
if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
return 0;
#ifdef __GNUC__
#pragma GCC diagnostic warning "-Wnonnull-compare" /* turn warning back on again */
#endif
/* quick check if we even need to bother */
if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
return 0;
#ifndef POSIX_SEMANTICS
if (first) {
tzset();
first = 0;
}
#else /* POSIX_SEMANTICS */
tz = getenv("TZ");
if (first) {
if (tz != NULL) {
int tzlen = strlen(tz);
savetz = (char *) malloc(tzlen + 1);
if (savetz != NULL) {
savetzlen = tzlen + 1;
strcpy(savetz, tz);
}
}
tzset();
first = 0;
}
/* if we have a saved TZ, and it is different, recapture and reset */
if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
i = strlen(tz) + 1;
if (i > savetzlen) {
savetz = (char *) realloc(savetz, i);
if (savetz) {
savetzlen = i;
strcpy(savetz, tz);
}
} else
strcpy(savetz, tz);
tzset();
}
#endif /* POSIX_SEMANTICS */
for (; *format && s < endp - 1; format++) {
tbuf[0] = '\0';
if (*format != '%') {
*s++ = *format;
continue;
}
#ifdef POSIX_2008
pad = '\0';
fw = 0;
flag = '\0';
switch (*++format) {
case '+':
flag = '+';
/* fall through */
case '0':
pad = '0';
format++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
default:
format--;
goto again;
}
for (; isdigit(*format); format++) {
fw = fw * 10 + (*format - '0');
}
format--;
#endif /* POSIX_2008 */
again:
switch (*++format) {
case '\0':
*s++ = '%';
goto out;
case '%':
*s++ = '%';
continue;
case 'a': /* abbreviated weekday name */
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
strcpy(tbuf, "?");
else
#ifndef HAVE_NL_LANGINFO
{
strncpy(tbuf,strp_weekdays[timeptr->tm_wday],3);
tbuf[3]=0;// make null terminated after 1st 3 chars
}
#else
strcpy(tbuf, days_a(timeptr->tm_wday));
#endif
break;
case 'A': /* full weekday name */
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
strcpy(tbuf, "?");
else
#ifndef HAVE_NL_LANGINFO
strcpy(tbuf, strp_weekdays[timeptr->tm_wday]);
#else
strcpy(tbuf, days_l(timeptr->tm_wday));
#endif
break;
case 'b': /* abbreviated month name */
short_month:
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
strcpy(tbuf, "?");
else
#ifndef HAVE_NL_LANGINFO
{
strncpy(tbuf,strp_monthnames[timeptr->tm_mon],3);
tbuf[3]=0;// make null terminated after 1st 3 chars
}
#else
strcpy(tbuf, months_a(timeptr->tm_mon));
#endif
break;
case 'B': /* full month name */
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
strcpy(tbuf, "?");
else
#ifndef HAVE_NL_LANGINFO
strcpy(tbuf, strp_monthnames[timeptr->tm_mon]);
#else
strcpy(tbuf, months_l(timeptr->tm_mon));
#endif
break;
case 'c': /* appropriate date and time representation */
/*
* This used to be:
*
* strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
*
* Per the ISO 1999 C standard, it was this:
* strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr);
*
* Per the ISO 2011 C standard, it is now this:
*/
#ifdef HAVE_NL_LANGINFO
ya_strftime(tbuf, sizeof tbuf, nl_langinfo(D_T_FMT), timeptr);
#else
{ const char *f="%a %b %e %T %Y";// done this way to avoid gcc -Wformat= errors in line below
ya_strftime(tbuf, sizeof tbuf, f, timeptr);
}
#endif
break;
case 'C':
#ifdef POSIX_2008
if (pad != '\0' && fw > 0) {
size_t min_fw = (flag ? 3 : 2);
fw = max(fw, min_fw);
snprintf(tbuf,sizeof(tbuf), flag
? "%+0*ld"
: "%0*ld", (int) fw,
(timeptr->tm_year + 1900L) / 100);
} else
#endif /* POSIX_2008 */
#ifdef HPUX_EXT
century:
#endif
snprintf(tbuf,sizeof(tbuf), "%02ld", (timeptr->tm_year + 1900L) / 100);
break;
case 'd': /* day of the month, 01 - 31 */
i = range(1, timeptr->tm_mday, 31);
snprintf(tbuf,sizeof(tbuf), "%02d", i);
break;
case 'D': /* date as %m/%d/%y */
ya_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
break;
case 'e': /* day of month, blank padded */
snprintf(tbuf,sizeof(tbuf), "%2d", range(1, timeptr->tm_mday, 31));
break;
case 'E':
/* POSIX (now C99) locale extensions, ignored for now */
goto again;
case 'f': /* local extension - fractional part of seconds */
{
if(strp_tz.f_secs_p10>=0)
{// -ve values for f_secs_p10 are not allowed (default is big negative)
#if 1
snprintf(tbuf,sizeof(tbuf),"%0.*f",strp_tz.f_secs_p10,strp_tz.f_secs);
if(tbuf[0]=='0' && tbuf[1]=='.')
{ // delete leading 0. from number as we only want digits after decimal point
char *s1,*s2;
for(s1=tbuf,s2=tbuf+2;*s1!=0 && *s2!=0;)*s1++=*s2++; // copy backwards so we overwrite 0. at start which we don't want
*s1=0;// null terminate resultant string (above copy should have stopped with *s2==0)
}
#else
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wformat=" /* Peter Miller - "fix" [Warning] unknown conversion type character '#' in format [-Wformat=] */
#pragma GCC diagnostic ignored "-Wformat-extra-args" /* Peter Miller - "fix" [Warning] too many arguments for format [-Wformat-extra-args] */
#endif
snprintf(tbuf,sizeof(tbuf),"%0.*#f",strp_tz.f_secs_p10,strp_tz.f_secs);// the # in this line causes gcc compiler warnings which are turned off before and on again after this line.
#ifdef __GNUC__
#pragma GCC diagnostic warning "-Wformat" /* turn warning back on again. If this says "-Wformat=" as you might expect to match "ignored" above, GCCgives an error and fails compilation! */
#pragma GCC diagnostic warning "-Wformat-extra-args" /* turn warning back on again */
#endif
char *s1,*s2;
for(s1=tbuf,s2=tbuf+2;*s1!=0 && *s2!=0;)*s1++=*s2++; // copy backwards so we overwrite 0. at start which we don't want
*s1=0;// null terminate resultant string (above copy should have stopped with *s2==0)
#endif
}
else
{tbuf[0]='?'; // produce "?" for invalid inputs (otherwise test program fails)
tbuf[1]=0;
}
}
break;
case 'F': /* ISO 8601 date representation */
{
#ifdef POSIX_2008
/*
* Field width for %F is for the whole thing.
* It must be at least 10.
*/
char m_d[10];
ya_strftime(m_d, sizeof m_d, "-%m-%d", timeptr);
size_t min_fw = 10;
if (pad != '\0' && fw > 0) {
fw = max(fw, min_fw);
} else {
fw = min_fw;
}
fw -= 6; /* -XX-XX at end are invariant */
iso_8601_2000_year(tbuf, timeptr->tm_year + 1900, fw);
strcat(tbuf, m_d);
#else
ya_strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr);
#endif /* POSIX_2008 */
}
break;
case 'g':
case 'G':
/*
* Year of ISO week.
* If strp_tz.year_G!= strp_tz_default we already have it, otherwsie calculate it as below
* If it's December but the ISO week number is one,
* that week is in next year.
* If it's January but the ISO week number is 52 or
* 53, that week is in last year.
* Otherwise, it's this year.
*/
if(strp_tz.year_G== strp_tz_default)
{
w = iso8601wknum(timeptr);
if (timeptr->tm_mon == 11 && w == 1)
y = 1900L + timeptr->tm_year + 1;
else if (timeptr->tm_mon == 0 && w >= 52)
y = 1900L + timeptr->tm_year - 1;
else
y = 1900L + timeptr->tm_year;
}
else
y=strp_tz.year_G; // simple case - we already have the correct year
if (*format == 'G') {
#ifdef POSIX_2008
if (pad != '\0' && fw > 0) {
size_t min_fw = 4;
fw = max(fw, min_fw);
snprintf(tbuf,sizeof(tbuf), flag
? "%+0*ld"
: "%0*ld", (int) fw,
y);
} else
#endif /* POSIX_2008 */
snprintf(tbuf,sizeof(tbuf), "%ld", y);
}
else
snprintf(tbuf,sizeof(tbuf), "%02ld", y % 100);
break;
case 'h': /* abbreviated month name */
goto short_month;
case 'H': /* hour, 24-hour clock, 00 - 23 */
i = range(0, timeptr->tm_hour, 23);
snprintf(tbuf,sizeof(tbuf), "%02d", i);
break;
case 'I': /* hour, 12-hour clock, 01 - 12 */
i = range(0, timeptr->tm_hour, 23);
if (i == 0)
i = 12;
else if (i > 12)
i -= 12;
snprintf(tbuf,sizeof(tbuf), "%02d", i);
break;
case 'j': /* day of the year, 001 - 366 */
snprintf(tbuf,sizeof(tbuf), "%03d", timeptr->tm_yday + 1);
break;
case 'm': /* month, 01 - 12 */
i = range(0, timeptr->tm_mon, 11);
snprintf(tbuf,sizeof(tbuf), "%02d", i + 1);
break;
case 'M': /* minute, 00 - 59 */
i = range(0, timeptr->tm_min, 59);
snprintf(tbuf,sizeof(tbuf), "%02d", i);
break;
case 'n': /* same as \n */
tbuf[0] = '\n';
tbuf[1] = '\0';
break;
case 'O':
/* POSIX (now C99) locale extensions, ignored for now */
goto again;
case 'p': /* am or pm based on 12-hour clock */
i = range(0, timeptr->tm_hour, 23);
#ifndef HAVE_NL_LANGINFO
if (i < 12)
strcpy(tbuf, "AM");
else
strcpy(tbuf, "PM");
#else
if (i < 12)
strcpy(tbuf, ampm(0));
else
strcpy(tbuf, ampm(1));
#endif
break;
case 'r': /* time as %I:%M:%S %p */
ya_strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
break;
case 'R': /* time as %H:%M */
ya_strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
break;
case 's': /* time as seconds since the Epoch */
snprintf(tbuf,sizeof(tbuf), "%lld", (long long)ya_mktime_tm(timeptr)); // (long long) added as time_t might only be long
break;
case 'S': /* second, 00 - 60 */
i = range(0, timeptr->tm_sec, 60);
snprintf(tbuf,sizeof(tbuf), "%02d", i);
break;
case 't': /* same as \t */
tbuf[0] = '\t';
tbuf[1] = '\0';
break;
case 'T': /* time as %H:%M:%S */
the_time:
ya_strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
break;
case 'u':
/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] {very similar to %w which outputs 0->6 with sunday as 0 }*/
i = range(0, timeptr->tm_wday, 6);
snprintf(tbuf,sizeof(tbuf), "%d", i== 0 ? 7 :i);
break;
case 'U': /* week of year, Sunday is first day of week */
// if a value has been set in strp_tz.week_nos_U then use that, otherwise calculate it from other entries
if(strp_tz.week_nos_U== strp_tz_default)
snprintf(tbuf,sizeof(tbuf), "%02d", weeknumber(timeptr, 0));
else
snprintf(tbuf,sizeof(tbuf), "%02d", strp_tz.week_nos_U);
break;
case 'V': /* week of year according ISO 8601 */
// if a value has been set in strp_tz.week_nos_V then use that, otherwise calculate it from other entries
if(strp_tz.week_nos_V== strp_tz_default)
snprintf(tbuf,sizeof(tbuf), "%02d", iso8601wknum(timeptr));
else
snprintf(tbuf,sizeof(tbuf), "%02d", strp_tz.week_nos_V);
break;
case 'w': /* weekday, Sunday == 0, 0 - 6 */
i = range(0, timeptr->tm_wday, 6);
snprintf(tbuf,sizeof(tbuf), "%d", i);
break;
case 'W': /* week of year, Monday is first day of week */
// if a value has been set in strp_tz.week_nos_U then use that, otherwise calculate it from other entries
if(strp_tz.week_nos_W== strp_tz_default)
snprintf(tbuf,sizeof(tbuf), "%02d", weeknumber(timeptr, 1));
else
snprintf(tbuf,sizeof(tbuf), "%02d", strp_tz.week_nos_W);
break;
case 'x': /* appropriate date representation */
/*
* Up to the 2011 standard, this code used:
* strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr);
*
* Now, per the 2011 C standard (C99), this is: "%m/%d/%y"
*/
#ifdef HAVE_NL_LANGINFO
ya_strftime(tbuf, sizeof tbuf, nl_langinfo(D_FMT), timeptr);
#else
ya_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
#endif