aa3fc850ec25b8b2dc1f1526e23d7d7ddc9392ba
[platform/upstream/bash.git] / lib / sh / strftime.c
1 /* strftime - formatted time and date to a string */
2 /*
3  * Modified slightly by Chet Ramey for inclusion in Bash
4  */
5 /*
6  * strftime.c
7  *
8  * Public-domain implementation of ISO C library routine.
9  *
10  * If you can't do prototypes, get GCC.
11  *
12  * The C99 standard now specifies just about all of the formats
13  * that were additional in the earlier versions of this file.
14  *
15  * For extensions from SunOS, add SUNOS_EXT.
16  * For extensions from HP/UX, add HPUX_EXT.
17  * For VMS dates, add VMS_EXT.
18  * For complete POSIX semantics, add POSIX_SEMANTICS.
19  *
20  * The code for %c, %x, and %X follows the C99 specification for
21  * the "C" locale.
22  *
23  * This version ignores LOCALE information.
24  * It also doesn't worry about multi-byte characters.
25  * So there.
26  *
27  * Arnold Robbins
28  * January, February, March, 1991
29  * Updated March, April 1992
30  * Updated April, 1993
31  * Updated February, 1994
32  * Updated May, 1994
33  * Updated January, 1995
34  * Updated September, 1995
35  * Updated January, 1996
36  * Updated July, 1997
37  * Updated October, 1999
38  * Updated September, 2000
39  * Updated December, 2001
40  * Updated January, 2011
41  *
42  * Fixes from ado@elsie.nci.nih.gov,
43  * February 1991, May 1992
44  * Fixes from Tor Lillqvist tml@tik.vtt.fi,
45  * May 1993
46  * Further fixes from ado@elsie.nci.nih.gov,
47  * February 1994
48  * %z code from chip@chinacat.unicom.com,
49  * Applied September 1995
50  * %V code fixed (again) and %G, %g added,
51  * January 1996
52  * %v code fixed, better configuration,
53  * July 1997
54  * Moved to C99 specification.
55  * September 2000
56  * Fixes from Tanaka Akira <akr@m17n.org>
57  * December 2001
58  */
59 #include <config.h>
60
61 #include <stdio.h>
62 #include <ctype.h>
63 #include <time.h>
64
65 #if defined(TM_IN_SYS_TIME)
66 #include <sys/types.h>
67 #include <sys/time.h>
68 #endif
69
70 #include <stdlib.h>
71 #include <string.h>
72
73 /* defaults: season to taste */
74 #define SUNOS_EXT       1       /* stuff in SunOS strftime routine */
75 #define VMS_EXT         1       /* include %v for VMS date format */
76 #define HPUX_EXT        1       /* non-conflicting stuff in HP-UX date */
77 #define POSIX_SEMANTICS 1       /* call tzset() if TZ changes */
78
79 #undef strchr   /* avoid AIX weirdness */
80
81 #if defined (SHELL)
82 extern char *get_string_value (const char *);
83 #endif
84
85 extern void tzset(void);
86 static int weeknumber(const struct tm *timeptr, int firstweekday);
87 static int iso8601wknum(const struct tm *timeptr);
88
89 #ifndef inline
90 #ifdef __GNUC__
91 #define inline  __inline__
92 #else
93 #define inline  /**/
94 #endif
95 #endif
96
97 #define range(low, item, hi)    max(low, min(item, hi))
98
99 #if !defined(OS2) && !defined(MSDOS) && defined(HAVE_TZNAME)
100 extern char *tzname[2];
101 extern int daylight;
102 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX)
103 extern long int timezone, altzone;
104 #else
105 #  if defined (HPUX)
106 extern long int timezone;
107 #  else
108 extern int timezone, altzone;
109 #  endif /* !HPUX */
110 #endif /* !SOLARIS && !mips && !M_UNIX */
111 #endif
112
113 #undef min      /* just in case */
114
115 /* format for %+ -- currently unused */
116 #ifndef NATIONAL_FORMAT
117 #define NATIONAL_FORMAT "%a %b %e %H:%M:%S %Z %Y"
118 #endif
119
120 /* min --- return minimum of two numbers */
121
122 static inline int
123 min(int a, int b)
124 {
125         return (a < b ? a : b);
126 }
127
128 #undef max      /* also, just in case */
129
130 /* max --- return maximum of two numbers */
131
132 static inline int
133 max(int a, int b)
134 {
135         return (a > b ? a : b);
136 }
137
138 /* strftime --- produce formatted time */
139
140 size_t
141 strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
142 {
143         char *endp = s + maxsize;
144         char *start = s;
145         auto char tbuf[100];
146         long off;
147         int i, w;
148         long y;
149         static short first = 1;
150 #ifdef POSIX_SEMANTICS
151         static char *savetz = NULL;
152         static int savetzlen = 0;
153         char *tz;
154 #endif /* POSIX_SEMANTICS */
155 #ifndef HAVE_TM_ZONE
156 #ifndef HAVE_TM_NAME
157 #ifndef HAVE_TZNAME
158         extern char *timezone();
159         struct timeval tv;
160         struct timezone zone;
161 #endif /* HAVE_TZNAME */
162 #endif /* HAVE_TM_NAME */
163 #endif /* HAVE_TM_ZONE */
164
165         /* various tables, useful in North America */
166         static const char *days_a[] = {
167                 "Sun", "Mon", "Tue", "Wed",
168                 "Thu", "Fri", "Sat",
169         };
170         static const char *days_l[] = {
171                 "Sunday", "Monday", "Tuesday", "Wednesday",
172                 "Thursday", "Friday", "Saturday",
173         };
174         static const char *months_a[] = {
175                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
176                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
177         };
178         static const char *months_l[] = {
179                 "January", "February", "March", "April",
180                 "May", "June", "July", "August", "September",
181                 "October", "November", "December",
182         };
183         static const char *ampm[] = { "AM", "PM", };
184
185         if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
186                 return 0;
187
188         /* quick check if we even need to bother */
189         if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
190                 return 0;
191
192 #ifndef POSIX_SEMANTICS
193         if (first) {
194                 tzset();
195                 first = 0;
196         }
197 #else   /* POSIX_SEMANTICS */
198 #if defined (SHELL)
199         tz = get_string_value ("TZ");
200 #else
201         tz = getenv("TZ");
202 #endif
203         if (first) {
204                 if (tz != NULL) {
205                         int tzlen = strlen(tz);
206
207                         savetz = (char *) malloc(tzlen + 1);
208                         if (savetz != NULL) {
209                                 savetzlen = tzlen + 1;
210                                 strcpy(savetz, tz);
211                         }
212                 }
213                 tzset();
214                 first = 0;
215         }
216         /* if we have a saved TZ, and it is different, recapture and reset */
217         if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
218                 i = strlen(tz) + 1;
219                 if (i > savetzlen) {
220                         savetz = (char *) realloc(savetz, i);
221                         if (savetz) {
222                                 savetzlen = i;
223                                 strcpy(savetz, tz);
224                         }
225                 } else
226                         strcpy(savetz, tz);
227                 tzset();
228         }
229 #endif  /* POSIX_SEMANTICS */
230
231         for (; *format && s < endp - 1; format++) {
232                 tbuf[0] = '\0';
233                 if (*format != '%') {
234                         *s++ = *format;
235                         continue;
236                 }
237         again:
238                 switch (*++format) {
239                 case '\0':
240                         *s++ = '%';
241                         goto out;
242
243                 case '%':
244                         *s++ = '%';
245                         continue;
246
247                 case 'a':       /* abbreviated weekday name */
248                         if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
249                                 strcpy(tbuf, "?");
250                         else
251                                 strcpy(tbuf, days_a[timeptr->tm_wday]);
252                         break;
253
254                 case 'A':       /* full weekday name */
255                         if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
256                                 strcpy(tbuf, "?");
257                         else
258                                 strcpy(tbuf, days_l[timeptr->tm_wday]);
259                         break;
260
261                 case 'b':       /* abbreviated month name */
262                 short_month:
263                         if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
264                                 strcpy(tbuf, "?");
265                         else
266                                 strcpy(tbuf, months_a[timeptr->tm_mon]);
267                         break;
268
269                 case 'B':       /* full month name */
270                         if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
271                                 strcpy(tbuf, "?");
272                         else
273                                 strcpy(tbuf, months_l[timeptr->tm_mon]);
274                         break;
275
276                 case 'c':       /* appropriate date and time representation */
277                         /*
278                          * This used to be:
279                          *
280                          * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
281                          *
282                          * Now, per the ISO 1999 C standard, it this:
283                          */
284                         strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr);
285                         break;
286
287                 case 'C':
288                 century:
289                         sprintf(tbuf, "%02ld", (timeptr->tm_year + 1900L) / 100);
290                         break;
291
292                 case 'd':       /* day of the month, 01 - 31 */
293                         i = range(1, timeptr->tm_mday, 31);
294                         sprintf(tbuf, "%02d", i);
295                         break;
296
297                 case 'D':       /* date as %m/%d/%y */
298                         strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
299                         break;
300
301                 case 'e':       /* day of month, blank padded */
302                         sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
303                         break;
304
305                 case 'E':
306                         /* POSIX (now C99) locale extensions, ignored for now */
307                         goto again;
308
309                 case 'F':       /* ISO 8601 date representation */
310                         strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr);
311                         break;
312
313                 case 'g':
314                 case 'G':
315                         /*
316                          * Year of ISO week.
317                          *
318                          * If it's December but the ISO week number is one,
319                          * that week is in next year.
320                          * If it's January but the ISO week number is 52 or
321                          * 53, that week is in last year.
322                          * Otherwise, it's this year.
323                          */
324                         w = iso8601wknum(timeptr);
325                         if (timeptr->tm_mon == 11 && w == 1)
326                                 y = 1900L + timeptr->tm_year + 1;
327                         else if (timeptr->tm_mon == 0 && w >= 52)
328                                 y = 1900L + timeptr->tm_year - 1;
329                         else
330                                 y = 1900L + timeptr->tm_year;
331
332                         if (*format == 'G')
333                                 sprintf(tbuf, "%ld", y);
334                         else
335                                 sprintf(tbuf, "%02ld", y % 100);
336                         break;
337
338                 case 'h':       /* abbreviated month name */
339                         goto short_month;
340
341                 case 'H':       /* hour, 24-hour clock, 00 - 23 */
342                         i = range(0, timeptr->tm_hour, 23);
343                         sprintf(tbuf, "%02d", i);
344                         break;
345
346                 case 'I':       /* hour, 12-hour clock, 01 - 12 */
347                         i = range(0, timeptr->tm_hour, 23);
348                         if (i == 0)
349                                 i = 12;
350                         else if (i > 12)
351                                 i -= 12;
352                         sprintf(tbuf, "%02d", i);
353                         break;
354
355                 case 'j':       /* day of the year, 001 - 366 */
356                         sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
357                         break;
358
359                 case 'm':       /* month, 01 - 12 */
360                         i = range(0, timeptr->tm_mon, 11);
361                         sprintf(tbuf, "%02d", i + 1);
362                         break;
363
364                 case 'M':       /* minute, 00 - 59 */
365                         i = range(0, timeptr->tm_min, 59);
366                         sprintf(tbuf, "%02d", i);
367                         break;
368
369                 case 'n':       /* same as \n */
370                         tbuf[0] = '\n';
371                         tbuf[1] = '\0';
372                         break;
373
374                 case 'O':
375                         /* POSIX (now C99) locale extensions, ignored for now */
376                         goto again;
377
378                 case 'p':       /* am or pm based on 12-hour clock */
379                         i = range(0, timeptr->tm_hour, 23);
380                         if (i < 12)
381                                 strcpy(tbuf, ampm[0]);
382                         else
383                                 strcpy(tbuf, ampm[1]);
384                         break;
385
386                 case 'r':       /* time as %I:%M:%S %p */
387                         strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
388                         break;
389
390                 case 'R':       /* time as %H:%M */
391                         strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
392                         break;
393
394 #if defined(HAVE_MKTIME)
395                 case 's':       /* time as seconds since the Epoch */
396                 {
397                         struct tm non_const_timeptr;
398
399                         non_const_timeptr = *timeptr;
400                         sprintf(tbuf, "%ld", mktime(& non_const_timeptr));
401                         break;
402                 }
403 #endif /* defined(HAVE_MKTIME) */
404
405                 case 'S':       /* second, 00 - 60 */
406                         i = range(0, timeptr->tm_sec, 60);
407                         sprintf(tbuf, "%02d", i);
408                         break;
409
410                 case 't':       /* same as \t */
411                         tbuf[0] = '\t';
412                         tbuf[1] = '\0';
413                         break;
414
415                 case 'T':       /* time as %H:%M:%S */
416                 the_time:
417                         strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
418                         break;
419
420                 case 'u':
421                 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
422                         sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
423                                         timeptr->tm_wday);
424                         break;
425
426                 case 'U':       /* week of year, Sunday is first day of week */
427                         sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
428                         break;
429
430                 case 'V':       /* week of year according ISO 8601 */
431                         sprintf(tbuf, "%02d", iso8601wknum(timeptr));
432                         break;
433
434                 case 'w':       /* weekday, Sunday == 0, 0 - 6 */
435                         i = range(0, timeptr->tm_wday, 6);
436                         sprintf(tbuf, "%d", i);
437                         break;
438
439                 case 'W':       /* week of year, Monday is first day of week */
440                         sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
441                         break;
442
443                 case 'x':       /* appropriate date representation */
444                         strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr);
445                         break;
446
447                 case 'X':       /* appropriate time representation */
448                         goto the_time;
449                         break;
450
451                 case 'y':       /* year without a century, 00 - 99 */
452                 year:
453                         i = timeptr->tm_year % 100;
454                         sprintf(tbuf, "%02d", i);
455                         break;
456
457                 case 'Y':       /* year with century */
458                 fullyear:
459                         sprintf(tbuf, "%ld", 1900L + timeptr->tm_year);
460                         break;
461
462                 /*
463                  * From: Chip Rosenthal <chip@chinacat.unicom.com>
464                  * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
465                  * 
466                  * Warning: the %z [code] is implemented by inspecting the
467                  * timezone name conditional compile settings, and
468                  * inferring a method to get timezone offsets. I've tried
469                  * this code on a couple of machines, but I don't doubt
470                  * there is some system out there that won't like it.
471                  * Maybe the easiest thing to do would be to bracket this
472                  * with an #ifdef that can turn it off. The %z feature
473                  * would be an admittedly obscure one that most folks can
474                  * live without, but it would be a great help to those of
475                  * us that muck around with various message processors.
476                  */
477                 case 'z':       /* time zone offset east of GMT e.g. -0600 */
478                         if (timeptr->tm_isdst < 0)
479                                 break;
480 #ifdef HAVE_TM_NAME
481                         /*
482                          * Systems with tm_name probably have tm_tzadj as
483                          * secs west of GMT.  Convert to mins east of GMT.
484                          */
485                         off = -timeptr->tm_tzadj / 60;
486 #else /* !HAVE_TM_NAME */
487 #ifdef HAVE_TM_ZONE
488                         /*
489                          * Systems with tm_zone probably have tm_gmtoff as
490                          * secs east of GMT.  Convert to mins east of GMT.
491                          */
492                         off = timeptr->tm_gmtoff / 60;
493 #else /* !HAVE_TM_ZONE */
494 #if HAVE_TZNAME
495                         /*
496                          * Systems with tzname[] probably have timezone as
497                          * secs west of GMT.  Convert to mins east of GMT.
498                          */
499 #  ifdef HPUX
500                         off = -timezone / 60;
501 #  else
502                         /* ADR: 4 August 2001, fixed this per gazelle@interaccess.com */
503                         off = -(daylight ? altzone : timezone) / 60;
504 #  endif /* !HPUX */
505 #else /* !HAVE_TZNAME */
506                         gettimeofday(& tv, & zone);
507                         off = -zone.tz_minuteswest;
508 #endif /* !HAVE_TZNAME */
509 #endif /* !HAVE_TM_ZONE */
510 #endif /* !HAVE_TM_NAME */
511                         if (off < 0) {
512                                 tbuf[0] = '-';
513                                 off = -off;
514                         } else {
515                                 tbuf[0] = '+';
516                         }
517                         sprintf(tbuf+1, "%02ld%02ld", off/60, off%60);
518                         break;
519
520                 case 'Z':       /* time zone name or abbrevation */
521 #ifdef HAVE_TZNAME
522                         i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
523                         strcpy(tbuf, tzname[i]);
524 #else
525 #ifdef HAVE_TM_ZONE
526                         strcpy(tbuf, timeptr->tm_zone);
527 #else
528 #ifdef HAVE_TM_NAME
529                         strcpy(tbuf, timeptr->tm_name);
530 #else
531                         gettimeofday(& tv, & zone);
532                         strcpy(tbuf, timezone(zone.tz_minuteswest,
533                                                 timeptr->tm_isdst > 0));
534 #endif /* HAVE_TM_NAME */
535 #endif /* HAVE_TM_ZONE */
536 #endif /* HAVE_TZNAME */
537                         break;
538
539 #ifdef SUNOS_EXT
540                 case 'k':       /* hour, 24-hour clock, blank pad */
541                         sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
542                         break;
543
544                 case 'l':       /* hour, 12-hour clock, 1 - 12, blank pad */
545                         i = range(0, timeptr->tm_hour, 23);
546                         if (i == 0)
547                                 i = 12;
548                         else if (i > 12)
549                                 i -= 12;
550                         sprintf(tbuf, "%2d", i);
551                         break;
552 #endif
553
554 #ifdef HPUX_EXT
555                 case 'N':       /* Emperor/Era name */
556                         /* this is essentially the same as the century */
557                         goto century;   /* %C */
558
559                 case 'o':       /* Emperor/Era year */
560                         goto year;      /* %y */
561 #endif /* HPUX_EXT */
562
563
564 #ifdef VMS_EXT
565                 case 'v':       /* date as dd-bbb-YYYY */
566                         sprintf(tbuf, "%2d-%3.3s-%4ld",
567                                 range(1, timeptr->tm_mday, 31),
568                                 months_a[range(0, timeptr->tm_mon, 11)],
569                                 timeptr->tm_year + 1900L);
570                         for (i = 3; i < 6; i++)
571                                 if (islower(tbuf[i]))
572                                         tbuf[i] = toupper(tbuf[i]);
573                         break;
574 #endif
575
576                 default:
577                         tbuf[0] = '%';
578                         tbuf[1] = *format;
579                         tbuf[2] = '\0';
580                         break;
581                 }
582                 i = strlen(tbuf);
583                 if (i) {
584                         if (s + i < endp - 1) {
585                                 strcpy(s, tbuf);
586                                 s += i;
587                         } else
588                                 return 0;
589                 }
590         }
591 out:
592         if (s < endp && *format == '\0') {
593                 *s = '\0';
594                 return (s - start);
595         } else
596                 return 0;
597 }
598
599 /* isleap --- is a year a leap year? */
600
601 static int
602 isleap(long year)
603 {
604         return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
605 }
606
607
608 /* iso8601wknum --- compute week number according to ISO 8601 */
609
610 static int
611 iso8601wknum(const struct tm *timeptr)
612 {
613         /*
614          * From 1003.2:
615          *      If the week (Monday to Sunday) containing January 1
616          *      has four or more days in the new year, then it is week 1;
617          *      otherwise it is the highest numbered week of the previous
618          *      year (52 or 53), and the next week is week 1.
619          *
620          * ADR: This means if Jan 1 was Monday through Thursday,
621          *      it was week 1, otherwise week 52 or 53.
622          *
623          * XPG4 erroneously included POSIX.2 rationale text in the
624          * main body of the standard. Thus it requires week 53.
625          */
626
627         int weeknum, jan1day, diff;
628
629         /* get week number, Monday as first day of the week */
630         weeknum = weeknumber(timeptr, 1);
631
632         /*
633          * With thanks and tip of the hatlo to tml@tik.vtt.fi
634          *
635          * What day of the week does January 1 fall on?
636          * We know that
637          *      (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
638          *              (timeptr->tm_wday - jan1.tm_wday) MOD 7
639          * and that
640          *      jan1.tm_yday == 0
641          * and that
642          *      timeptr->tm_wday MOD 7 == timeptr->tm_wday
643          * from which it follows that. . .
644          */
645         jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
646         if (jan1day < 0)
647                 jan1day += 7;
648
649         /*
650          * If Jan 1 was a Monday through Thursday, it was in
651          * week 1.  Otherwise it was last year's highest week, which is
652          * this year's week 0.
653          *
654          * What does that mean?
655          * If Jan 1 was Monday, the week number is exactly right, it can
656          *      never be 0.
657          * If it was Tuesday through Thursday, the weeknumber is one
658          *      less than it should be, so we add one.
659          * Otherwise, Friday, Saturday or Sunday, the week number is
660          * OK, but if it is 0, it needs to be 52 or 53.
661          */
662         switch (jan1day) {
663         case 1:         /* Monday */
664                 break;
665         case 2:         /* Tuesday */
666         case 3:         /* Wednesday */
667         case 4:         /* Thursday */
668                 weeknum++;
669                 break;
670         case 5:         /* Friday */
671         case 6:         /* Saturday */
672         case 0:         /* Sunday */
673                 if (weeknum == 0) {
674 #ifdef USE_BROKEN_XPG4
675                         /* XPG4 (as of March 1994) says 53 unconditionally */
676                         weeknum = 53;
677 #else
678                         /* get week number of last week of last year */
679                         struct tm dec31ly;      /* 12/31 last year */
680                         dec31ly = *timeptr;
681                         dec31ly.tm_year--;
682                         dec31ly.tm_mon = 11;
683                         dec31ly.tm_mday = 31;
684                         dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
685                         dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
686                         weeknum = iso8601wknum(& dec31ly);
687 #endif
688                 }
689                 break;
690         }
691
692         if (timeptr->tm_mon == 11) {
693                 /*
694                  * The last week of the year
695                  * can be in week 1 of next year.
696                  * Sigh.
697                  *
698                  * This can only happen if
699                  *      M   T  W
700                  *      29  30 31
701                  *      30  31
702                  *      31
703                  */
704                 int wday, mday;
705
706                 wday = timeptr->tm_wday;
707                 mday = timeptr->tm_mday;
708                 if (   (wday == 1 && (mday >= 29 && mday <= 31))
709                     || (wday == 2 && (mday == 30 || mday == 31))
710                     || (wday == 3 &&  mday == 31))
711                         weeknum = 1;
712         }
713
714         return weeknum;
715 }
716
717 /* weeknumber --- figure how many weeks into the year */
718
719 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
720
721 static int
722 weeknumber(const struct tm *timeptr, int firstweekday)
723 {
724         int wday = timeptr->tm_wday;
725         int ret;
726
727         if (firstweekday == 1) {
728                 if (wday == 0)  /* sunday */
729                         wday = 6;
730                 else
731                         wday--;
732         }
733         ret = ((timeptr->tm_yday + 7 - wday) / 7);
734         if (ret < 0)
735                 ret = 0;
736         return ret;
737 }
738
739 #if 0
740 /* ADR --- I'm loathe to mess with ado's code ... */
741
742 Date:         Wed, 24 Apr 91 20:54:08 MDT
743 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
744 To: arnold@audiofax.com
745
746 Hi Arnold,
747 in a process of fixing of strftime() in libraries on Atari ST I grabbed
748 some pieces of code from your own strftime.  When doing that it came
749 to mind that your weeknumber() function compiles a little bit nicer
750 in the following form:
751 /*
752  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
753  */
754 {
755     return (timeptr->tm_yday - timeptr->tm_wday +
756             (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
757 }
758 How nicer it depends on a compiler, of course, but always a tiny bit.
759
760    Cheers,
761    Michal
762    ntomczak@vm.ucs.ualberta.ca
763 #endif
764
765 #ifdef  TEST_STRFTIME
766
767 /*
768  * NAME:
769  *      tst
770  *
771  * SYNOPSIS:
772  *      tst
773  *
774  * DESCRIPTION:
775  *      "tst" is a test driver for the function "strftime".
776  *
777  * OPTIONS:
778  *      None.
779  *
780  * AUTHOR:
781  *      Karl Vogel
782  *      Control Data Systems, Inc.
783  *      vogelke@c-17igp.wpafb.af.mil
784  *
785  * BUGS:
786  *      None noticed yet.
787  *
788  * COMPILE:
789  *      cc -o tst -DTEST_STRFTIME strftime.c
790  */
791
792 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
793
794 #ifndef NULL
795 #include        <stdio.h>
796 #endif
797 #include        <sys/time.h>
798 #include        <string.h>
799
800 #define         MAXTIME         132
801
802 /*
803  * Array of time formats.
804  */
805
806 static char *array[] =
807 {
808         "(%%A)      full weekday name, var length (Sunday..Saturday)  %A",
809         "(%%B)       full month name, var length (January..December)  %B",
810         "(%%C)                                               Century  %C",
811         "(%%D)                                       date (%%m/%%d/%%y)  %D",
812         "(%%E)                           Locale extensions (ignored)  %E",
813         "(%%F)       full month name, var length (January..December)  %F",
814         "(%%H)                          hour (24-hour clock, 00..23)  %H",
815         "(%%I)                          hour (12-hour clock, 01..12)  %I",
816         "(%%M)                                       minute (00..59)  %M",
817         "(%%N)                                      Emporer/Era Name  %N",
818         "(%%O)                           Locale extensions (ignored)  %O",
819         "(%%R)                                 time, 24-hour (%%H:%%M)  %R",
820         "(%%S)                                       second (00..60)  %S",
821         "(%%T)                              time, 24-hour (%%H:%%M:%%S)  %T",
822         "(%%U)    week of year, Sunday as first day of week (00..53)  %U",
823         "(%%V)                    week of year according to ISO 8601  %V",
824         "(%%W)    week of year, Monday as first day of week (00..53)  %W",
825         "(%%X)     appropriate locale time representation (%H:%M:%S)  %X",
826         "(%%Y)                           year with century (1970...)  %Y",
827         "(%%Z) timezone (EDT), or blank if timezone not determinable  %Z",
828         "(%%a)          locale's abbreviated weekday name (Sun..Sat)  %a",
829         "(%%b)            locale's abbreviated month name (Jan..Dec)  %b",
830         "(%%c)           full date (Sat Nov  4 12:02:33 1989)%n%t%t%t  %c",
831         "(%%d)                             day of the month (01..31)  %d",
832         "(%%e)               day of the month, blank-padded ( 1..31)  %e",
833         "(%%h)                                should be same as (%%b)  %h",
834         "(%%j)                            day of the year (001..366)  %j",
835         "(%%k)               hour, 24-hour clock, blank pad ( 0..23)  %k",
836         "(%%l)               hour, 12-hour clock, blank pad ( 0..12)  %l",
837         "(%%m)                                        month (01..12)  %m",
838         "(%%o)                                      Emporer/Era Year  %o",
839         "(%%p)              locale's AM or PM based on 12-hour clock  %p",
840         "(%%r)                   time, 12-hour (same as %%I:%%M:%%S %%p)  %r",
841         "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7]   %u",
842         "(%%v)                                VMS date (dd-bbb-YYYY)  %v",
843         "(%%w)                       day of week (0..6, Sunday == 0)  %w",
844         "(%%x)                appropriate locale date representation  %x",
845         "(%%y)                      last two digits of year (00..99)  %y",
846         "(%%z)      timezone offset east of GMT as HHMM (e.g. -0500)  %z",
847         (char *) NULL
848 };
849
850 /* main routine. */
851
852 int
853 main(argc, argv)
854 int argc;
855 char **argv;
856 {
857         long time();
858
859         char *next;
860         char string[MAXTIME];
861
862         int k;
863         int length;
864
865         struct tm *tm;
866
867         long clock;
868
869         /* Call the function. */
870
871         clock = time((long *) 0);
872         tm = localtime(&clock);
873
874         for (k = 0; next = array[k]; k++) {
875                 length = strftime(string, MAXTIME, next, tm);
876                 printf("%s\n", string);
877         }
878
879         exit(0);
880 }
881 #endif  /* TEST_STRFTIME */