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