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