Ruby  1.9.3p392(2013-02-22revision39386)
date_strftime.c
Go to the documentation of this file.
1 /* -*- c-file-style: "linux" -*- */
2 
3 /*
4  * strftime.c
5  *
6  * Public-domain implementation of ANSI C library routine.
7  *
8  * It's written in old-style C for maximal portability.
9  * However, since I'm used to prototypes, I've included them too.
10  *
11  * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
12  * For extensions from SunOS, add SUNOS_EXT.
13  * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
14  * For VMS dates, add VMS_EXT.
15  * For a an RFC822 time format, add MAILHEADER_EXT.
16  * For ISO week years, add ISO_DATE_EXT.
17  * For complete POSIX semantics, add POSIX_SEMANTICS.
18  *
19  * The code for %c, %x, and %X now follows the 1003.2 specification for
20  * the POSIX locale.
21  * This version ignores LOCALE information.
22  * It also doesn't worry about multi-byte characters.
23  * So there.
24  *
25  * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
26  * code are included if GAWK is defined.
27  *
28  * Arnold Robbins
29  * January, February, March, 1991
30  * Updated March, April 1992
31  * Updated April, 1993
32  * Updated February, 1994
33  * Updated May, 1994
34  * Updated January, 1995
35  * Updated September, 1995
36  * Updated January, 1996
37  *
38  * Fixes from ado@elsie.nci.nih.gov
39  * February 1991, May 1992
40  * Fixes from Tor Lillqvist tml@tik.vtt.fi
41  * May, 1993
42  * Further fixes from ado@elsie.nci.nih.gov
43  * February 1994
44  * %z code from chip@chinacat.unicom.com
45  * Applied September 1995
46  * %V code fixed (again) and %G, %g added,
47  * January 1996
48  */
49 
50 #include "ruby/ruby.h"
51 #include "date_tmx.h"
52 
53 #ifndef GAWK
54 #include <stdio.h>
55 #include <ctype.h>
56 #include <string.h>
57 #include <time.h>
58 #include <sys/types.h>
59 #include <errno.h>
60 #endif
61 #if defined(TM_IN_SYS_TIME) || !defined(GAWK)
62 #include <sys/types.h>
63 #if HAVE_SYS_TIME_H
64 #include <sys/time.h>
65 #endif
66 #endif
67 #include <math.h>
68 
69 /* defaults: season to taste */
70 #define SYSV_EXT 1 /* stuff in System V ascftime routine */
71 #define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
72 #define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
73 #define VMS_EXT 1 /* include %v for VMS date format */
74 #define MAILHEADER_EXT 1 /* add %z for HHMM format */
75 #define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
76 
77 #if defined(ISO_DATE_EXT)
78 #if ! defined(POSIX2_DATE)
79 #define POSIX2_DATE 1
80 #endif
81 #endif
82 
83 #if defined(POSIX2_DATE)
84 #if ! defined(SYSV_EXT)
85 #define SYSV_EXT 1
86 #endif
87 #if ! defined(SUNOS_EXT)
88 #define SUNOS_EXT 1
89 #endif
90 #endif
91 
92 #if defined(POSIX2_DATE)
93 #define adddecl(stuff) stuff
94 #else
95 #define adddecl(stuff)
96 #endif
97 
98 #undef strchr /* avoid AIX weirdness */
99 
100 #if 0
101 #if !defined __STDC__ && !defined _WIN32
102 #define const
103 static int weeknumber();
104 adddecl(static int iso8601wknum();)
105 static int weeknumber_v();
106 adddecl(static int iso8601wknum_v();)
107 #else
108 static int weeknumber(const struct tm *timeptr, int firstweekday);
109 adddecl(static int iso8601wknum(const struct tm *timeptr);)
110 static int weeknumber_v(const struct tmx *tmx, int firstweekday);
111 adddecl(static int iso8601wknum_v(const struct tmx *tmx);)
112 #endif
113 #endif
114 
115 #ifdef STDC_HEADERS
116 #include <stdlib.h>
117 #include <string.h>
118 #else
119 extern void *malloc();
120 extern void *realloc();
121 extern char *getenv();
122 extern char *strchr();
123 #endif
124 
125 #define range(low, item, hi) max((low), min((item), (hi)))
126 
127 #undef min /* just in case */
128 
129 /* min --- return minimum of two numbers */
130 
131 #ifndef __STDC__
132 static inline int
133 min(a, b)
134 int a, b;
135 #else
136 static inline int
137 min(int a, int b)
138 #endif
139 {
140  return (a < b ? a : b);
141 }
142 
143 #undef max /* also, just in case */
144 
145 /* max --- return maximum of two numbers */
146 
147 #ifndef __STDC__
148 static inline int
149 max(a, b)
150 int a, b;
151 #else
152 static inline int
153 max(int a, int b)
154 #endif
155 {
156  return (a > b ? a : b);
157 }
158 
159 #ifdef NO_STRING_LITERAL_CONCATENATION
160 #error No string literal concatenation
161 #endif
162 
163 #define add(x,y) (rb_funcall((x), '+', 1, (y)))
164 #define sub(x,y) (rb_funcall((x), '-', 1, (y)))
165 #define mul(x,y) (rb_funcall((x), '*', 1, (y)))
166 #define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
167 #define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
168 #define mod(x,y) (rb_funcall((x), '%', 1, (y)))
169 
170 /* strftime --- produce formatted time */
171 
172 static size_t
173 date_strftime_with_tmx(char *s, size_t maxsize, const char *format,
174  const struct tmx *tmx)
175 {
176  char *endp = s + maxsize;
177  char *start = s;
178  const char *sp, *tp;
179  auto char tbuf[100];
180  long off;
181  ptrdiff_t i;
182  int w;
183  int precision, flags, colons;
184  char padding;
185  enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E};
186 #define BIT_OF(n) (1U<<(n))
187 
188  /* various tables, useful in North America */
189  static const char days_l[][10] = {
190  "Sunday", "Monday", "Tuesday", "Wednesday",
191  "Thursday", "Friday", "Saturday",
192  };
193  static const char months_l[][10] = {
194  "January", "February", "March", "April",
195  "May", "June", "July", "August", "September",
196  "October", "November", "December",
197  };
198  static const char ampm[][3] = { "AM", "PM", };
199 
200  if (s == NULL || format == NULL || tmx == NULL || maxsize == 0)
201  return 0;
202 
203  /* quick check if we even need to bother */
204  if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) {
205  err:
206  errno = ERANGE;
207  return 0;
208  }
209 
210  for (; *format && s < endp - 1; format++) {
211 #define FLAG_FOUND() do { \
212  if (precision > 0 || flags & (BIT_OF(LOCALE_E)|BIT_OF(LOCALE_O))) \
213  goto unknown; \
214  } while (0)
215 #define NEEDS(n) do if (s >= endp || (n) >= endp - s - 1) goto err; while (0)
216 #define FILL_PADDING(i) do { \
217  if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
218  NEEDS(precision); \
219  memset(s, padding ? padding : ' ', precision - (i)); \
220  s += precision - (i); \
221  } \
222  else { \
223  NEEDS(i); \
224  } \
225 } while (0);
226 #define FMT(def_pad, def_prec, fmt, val) \
227  do { \
228  int l; \
229  if (precision <= 0) precision = (def_prec); \
230  if (flags & BIT_OF(LEFT)) precision = 1; \
231  l = snprintf(s, endp - s, \
232  ((padding == '0' || (!padding && (def_pad) == '0')) ? "%0*"fmt : "%*"fmt), \
233  precision, (val)); \
234  if (l < 0) goto err; \
235  s += l; \
236  } while (0)
237 #define STRFTIME(fmt) \
238  do { \
239  i = date_strftime_with_tmx(s, endp - s, (fmt), tmx); \
240  if (!i) return 0; \
241  if (precision > i) {\
242  if (start + maxsize < s + precision) { \
243  errno = ERANGE; \
244  return 0; \
245  } \
246  memmove(s + precision - i, s, i);\
247  memset(s, padding ? padding : ' ', precision - i); \
248  s += precision; \
249  }\
250  else s += i; \
251  } while (0)
252 #define FMTV(def_pad, def_prec, fmt, val) \
253  do { \
254  VALUE tmp = (val); \
255  if (FIXNUM_P(tmp)) { \
256  FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
257  } \
258  else { \
259  VALUE args[2], result; \
260  size_t l; \
261  if (precision <= 0) precision = (def_prec); \
262  if (flags & BIT_OF(LEFT)) precision = 1; \
263  args[0] = INT2FIX(precision); \
264  args[1] = (val); \
265  if (padding == '0' || (!padding && (def_pad) == '0')) \
266  result = rb_str_format(2, args, rb_str_new2("%0*"fmt)); \
267  else \
268  result = rb_str_format(2, args, rb_str_new2("%*"fmt)); \
269  l = strlcpy(s, StringValueCStr(result), endp-s); \
270  if ((size_t)(endp-s) <= l) \
271  goto err; \
272  s += l; \
273  } \
274  } while (0)
275 
276  if (*format != '%') {
277  *s++ = *format;
278  continue;
279  }
280  tp = tbuf;
281  sp = format;
282  precision = -1;
283  flags = 0;
284  padding = 0;
285  colons = 0;
286  again:
287  switch (*++format) {
288  case '\0':
289  format--;
290  goto unknown;
291 
292  case '%':
293  FILL_PADDING(1);
294  *s++ = '%';
295  continue;
296 
297  case 'a': /* abbreviated weekday name */
298  if (flags & BIT_OF(CHCASE)) {
299  flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
300  flags |= BIT_OF(UPPER);
301  }
302  {
303  int wday = tmx_wday;
304  if (wday < 0 || wday > 6)
305  i = 1, tp = "?";
306  else
307  i = 3, tp = days_l[wday];
308  }
309  break;
310 
311  case 'A': /* full weekday name */
312  if (flags & BIT_OF(CHCASE)) {
313  flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
314  flags |= BIT_OF(UPPER);
315  }
316  {
317  int wday = tmx_wday;
318  if (wday < 0 || wday > 6)
319  i = 1, tp = "?";
320  else
321  i = strlen(tp = days_l[wday]);
322  }
323  break;
324 
325 #ifdef SYSV_EXT
326  case 'h': /* abbreviated month name */
327 #endif
328  case 'b': /* abbreviated month name */
329  if (flags & BIT_OF(CHCASE)) {
330  flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
331  flags |= BIT_OF(UPPER);
332  }
333  {
334  int mon = tmx_mon;
335  if (mon < 1 || mon > 12)
336  i = 1, tp = "?";
337  else
338  i = 3, tp = months_l[mon-1];
339  }
340  break;
341 
342  case 'B': /* full month name */
343  if (flags & BIT_OF(CHCASE)) {
344  flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
345  flags |= BIT_OF(UPPER);
346  }
347  {
348  int mon = tmx_mon;
349  if (mon < 1 || mon > 12)
350  i = 1, tp = "?";
351  else
352  i = strlen(tp = months_l[mon-1]);
353  }
354  break;
355 
356  case 'c': /* appropriate date and time representation */
357  STRFTIME("%a %b %e %H:%M:%S %Y");
358  continue;
359 
360  case 'd': /* day of the month, 01 - 31 */
361  i = range(1, tmx_mday, 31);
362  FMT('0', 2, "d", (int)i);
363  continue;
364 
365  case 'H': /* hour, 24-hour clock, 00 - 23 */
366  i = range(0, tmx_hour, 23);
367  FMT('0', 2, "d", (int)i);
368  continue;
369 
370  case 'I': /* hour, 12-hour clock, 01 - 12 */
371  i = range(0, tmx_hour, 23);
372  if (i == 0)
373  i = 12;
374  else if (i > 12)
375  i -= 12;
376  FMT('0', 2, "d", (int)i);
377  continue;
378 
379  case 'j': /* day of the year, 001 - 366 */
380  FMT('0', 3, "d", tmx_yday);
381  continue;
382 
383  case 'm': /* month, 01 - 12 */
384  i = range(1, tmx_mon, 12);
385  FMT('0', 2, "d", (int)i);
386  continue;
387 
388  case 'M': /* minute, 00 - 59 */
389  i = range(0, tmx_min, 59);
390  FMT('0', 2, "d", (int)i);
391  continue;
392 
393  case 'p': /* AM or PM based on 12-hour clock */
394  case 'P': /* am or pm based on 12-hour clock */
395  if ((*format == 'p' && (flags & BIT_OF(CHCASE))) ||
396  (*format == 'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
397  flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
398  flags |= BIT_OF(LOWER);
399  }
400  i = range(0, tmx_hour, 23);
401  if (i < 12)
402  tp = ampm[0];
403  else
404  tp = ampm[1];
405  i = 2;
406  break;
407 
408  case 's':
409  FMTV('0', 1, "d", tmx_secs);
410  continue;
411 
412  case 'Q':
413  FMTV('0', 1, "d", tmx_msecs);
414  continue;
415 
416  case 'S': /* second, 00 - 59 */
417  i = range(0, tmx_sec, 59);
418  FMT('0', 2, "d", (int)i);
419  continue;
420 
421  case 'U': /* week of year, Sunday is first day of week */
422  FMT('0', 2, "d", tmx_wnum0);
423  continue;
424 
425  case 'w': /* weekday, Sunday == 0, 0 - 6 */
426  i = range(0, tmx_wday, 6);
427  FMT('0', 1, "d", (int)i);
428  continue;
429 
430  case 'W': /* week of year, Monday is first day of week */
431  FMT('0', 2, "d", tmx_wnum1);
432  continue;
433 
434  case 'x': /* appropriate date representation */
435  STRFTIME("%m/%d/%y");
436  continue;
437 
438  case 'X': /* appropriate time representation */
439  STRFTIME("%H:%M:%S");
440  continue;
441 
442  case 'y': /* year without a century, 00 - 99 */
443  i = NUM2INT(mod(tmx_year, INT2FIX(100)));
444  FMT('0', 2, "d", (int)i);
445  continue;
446 
447  case 'Y': /* year with century */
448  {
449  VALUE year = tmx_year;
450  if (FIXNUM_P(year)) {
451  long y = FIX2LONG(year);
452  FMT('0', 0 <= y ? 4 : 5, "ld", y);
453  }
454  else {
455  FMTV('0', 4, "d", year);
456  }
457  }
458  continue;
459 
460 #ifdef MAILHEADER_EXT
461  case 'z': /* time zone offset east of GMT e.g. -0600 */
462  {
463  long aoff;
464  int hl, hw;
465 
466  off = NUM2LONG(rb_funcall(tmx_offset, rb_intern("round"), 0));
467 
468  aoff = off;
469  if (aoff < 0)
470  aoff = -off;
471 
472  if ((aoff / 3600) < 10)
473  hl = 1;
474  else
475  hl = 2;
476  hw = 2;
477  if (flags & BIT_OF(LEFT) && hl == 1)
478  hw = 1;
479 
480  switch (colons) {
481  case 0: /* %z -> +hhmm */
482  precision = precision <= (3 + hw) ? hw : precision-3;
483  NEEDS(precision + 3);
484  break;
485 
486  case 1: /* %:z -> +hh:mm */
487  precision = precision <= (4 + hw) ? hw : precision-4;
488  NEEDS(precision + 4);
489  break;
490 
491  case 2: /* %::z -> +hh:mm:ss */
492  precision = precision <= (7 + hw) ? hw : precision-7;
493  NEEDS(precision + 7);
494  break;
495 
496  case 3: /* %:::z -> +hh[:mm[:ss]] */
497  {
498  if (aoff % 3600 == 0) {
499  precision = precision <= (1 + hw) ? hw : precision-1;
500  NEEDS(precision + 3);
501  }
502  else if (aoff % 60 == 0) {
503  precision = precision <= (4 + hw) ? hw : precision-4;
504  NEEDS(precision + 4);
505  }
506  else {
507  precision = precision <= (7 + hw) ? hw : precision-7;
508  NEEDS(precision + 7);
509  }
510  }
511  break;
512 
513  default:
514  format--;
515  goto unknown;
516  }
517  if (padding == ' ' && precision > hl) {
518  i = snprintf(s, endp - s, "%*s", precision - hl, "");
519  precision = hl;
520  if (i < 0) goto err;
521  s += i;
522  }
523  if (off < 0) {
524  off = -off;
525  *s++ = '-';
526  } else {
527  *s++ = '+';
528  }
529  i = snprintf(s, endp - s, "%.*ld", precision, off / 3600);
530  if (i < 0) goto err;
531  s += i;
532  off = off % 3600;
533  if (colons == 3 && off == 0)
534  continue;
535  if (1 <= colons)
536  *s++ = ':';
537  i = snprintf(s, endp - s, "%02d", (int)(off / 60));
538  if (i < 0) goto err;
539  s += i;
540  off = off % 60;
541  if (colons == 3 && off == 0)
542  continue;
543  if (2 <= colons) {
544  *s++ = ':';
545  i = snprintf(s, endp - s, "%02d", (int)off);
546  if (i < 0) goto err;
547  s += i;
548  }
549  }
550  continue;
551 #endif /* MAILHEADER_EXT */
552 
553  case 'Z': /* time zone name or abbreviation */
554  if (flags & BIT_OF(CHCASE)) {
555  flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
556  flags |= BIT_OF(LOWER);
557  }
558  {
559  char *zone = tmx_zone;
560  if (zone == NULL)
561  tp = "";
562  else
563  tp = zone;
564  i = strlen(tp);
565  }
566  break;
567 
568 #ifdef SYSV_EXT
569  case 'n': /* same as \n */
570  FILL_PADDING(1);
571  *s++ = '\n';
572  continue;
573 
574  case 't': /* same as \t */
575  FILL_PADDING(1);
576  *s++ = '\t';
577  continue;
578 
579  case 'D': /* date as %m/%d/%y */
580  STRFTIME("%m/%d/%y");
581  continue;
582 
583  case 'e': /* day of month, blank padded */
584  FMT(' ', 2, "d", range(1, tmx_mday, 31));
585  continue;
586 
587  case 'r': /* time as %I:%M:%S %p */
588  STRFTIME("%I:%M:%S %p");
589  continue;
590 
591  case 'R': /* time as %H:%M */
592  STRFTIME("%H:%M");
593  continue;
594 
595  case 'T': /* time as %H:%M:%S */
596  STRFTIME("%H:%M:%S");
597  continue;
598 #endif
599 
600 #ifdef SUNOS_EXT
601  case 'k': /* hour, 24-hour clock, blank pad */
602  i = range(0, tmx_hour, 23);
603  FMT(' ', 2, "d", (int)i);
604  continue;
605 
606  case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
607  i = range(0, tmx_hour, 23);
608  if (i == 0)
609  i = 12;
610  else if (i > 12)
611  i -= 12;
612  FMT(' ', 2, "d", (int)i);
613  continue;
614 #endif
615 
616 #ifdef VMS_EXT
617  case 'v': /* date as dd-bbb-YYYY */
618  STRFTIME("%e-%b-%Y");
619  continue;
620 #endif
621 
622 #ifdef POSIX2_DATE
623  case 'C':
624  FMTV('0', 2, "d", div(tmx_year, INT2FIX(100)));
625  continue;
626 
627  case 'E':
628  /* POSIX locale extensions, ignored for now */
629  flags |= BIT_OF(LOCALE_E);
630  if (*(format + 1) && strchr("cCxXyY", *(format + 1)))
631  goto again;
632  goto unknown;
633  case 'O':
634  /* POSIX locale extensions, ignored for now */
635  flags |= BIT_OF(LOCALE_O);
636  if (*(format + 1) && strchr("deHImMSuUVwWy",
637  *(format + 1)))
638  goto again;
639  goto unknown;
640  case 'V': /* week of year according ISO 8601 */
641  FMT('0', 2, "d", tmx_cweek);
642  continue;
643 
644  case 'u':
645  /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
646  FMT('0', 1, "d", tmx_cwday);
647  continue;
648 #endif /* POSIX2_DATE */
649 
650 #ifdef ISO_DATE_EXT
651  case 'g': /* year of ISO week without a century */
652  i = NUM2INT(mod(tmx_cwyear, INT2FIX(100)));
653  FMT('0', 2, "d", (int)i);
654  continue;
655 
656  case 'G': /* year of ISO week with century */
657  {
658  VALUE year = tmx_cwyear;
659  if (FIXNUM_P(year)) {
660  long y = FIX2LONG(year);
661  FMT('0', 0 <= y ? 4 : 5, "ld", y);
662  }
663  else {
664  FMTV('0', 4, "d", year);
665  }
666  continue;
667  }
668 
669 #endif /* ISO_DATE_EXT */
670 
671  case 'L':
672  w = 3;
673  goto subsec;
674 
675  case 'N':
676  /*
677  * fractional second digits. default is 9 digits
678  * (nanosecond).
679  *
680  * %3N millisecond (3 digits)
681  * %6N microsecond (6 digits)
682  * %9N nanosecond (9 digits)
683  */
684  w = 9;
685  subsec:
686  if (precision <= 0) {
687  precision = w;
688  }
689  NEEDS(precision);
690 
691  {
692  VALUE subsec = tmx_sec_fraction;
693  int ww;
694  long n;
695 
696  ww = precision;
697  while (9 <= ww) {
698  subsec = mul(subsec, INT2FIX(1000000000));
699  ww -= 9;
700  }
701  n = 1;
702  for (; 0 < ww; ww--)
703  n *= 10;
704  if (n != 1)
705  subsec = mul(subsec, INT2FIX(n));
706  subsec = div(subsec, INT2FIX(1));
707 
708  if (FIXNUM_P(subsec)) {
709  (void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec));
710  s += precision;
711  }
712  else {
713  VALUE args[2], result;
714  args[0] = INT2FIX(precision);
715  args[1] = subsec;
716  result = rb_str_format(2, args, rb_str_new2("%0*d"));
717  (void)strlcpy(s, StringValueCStr(result), endp-s);
718  s += precision;
719  }
720  }
721  continue;
722 
723  case 'F': /* Equivalent to %Y-%m-%d */
724  STRFTIME("%Y-%m-%d");
725  continue;
726  case '+':
727  STRFTIME("%a %b %e %H:%M:%S %Z %Y");
728  continue;
729 
730  case '-':
731  FLAG_FOUND();
732  flags |= BIT_OF(LEFT);
733  padding = precision = 0;
734  goto again;
735 
736  case '^':
737  FLAG_FOUND();
738  flags |= BIT_OF(UPPER);
739  goto again;
740 
741  case '#':
742  FLAG_FOUND();
743  flags |= BIT_OF(CHCASE);
744  goto again;
745 
746  case '_':
747  FLAG_FOUND();
748  padding = ' ';
749  goto again;
750 
751  case ':':
752  colons++;
753  goto again;
754 
755  case '0':
756  padding = '0';
757  case '1': case '2': case '3': case '4':
758  case '5': case '6': case '7': case '8': case '9':
759  {
760  char *e;
761  precision = (int)strtoul(format, &e, 10);
762  format = e - 1;
763  goto again;
764  }
765 
766  default:
767  unknown:
768  i = format - sp + 1;
769  tp = sp;
770  precision = -1;
771  flags = 0;
772  padding = 0;
773  colons = 0;
774  break;
775  }
776  if (i) {
777  FILL_PADDING(i);
778  memcpy(s, tp, i);
779  switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
780  case BIT_OF(UPPER):
781  do {
782  if (ISLOWER(*s)) *s = TOUPPER(*s);
783  } while (s++, --i);
784  break;
785  case BIT_OF(LOWER):
786  do {
787  if (ISUPPER(*s)) *s = TOLOWER(*s);
788  } while (s++, --i);
789  break;
790  default:
791  s += i;
792  break;
793  }
794  }
795  }
796  if (s >= endp) {
797  goto err;
798  }
799  if (*format == '\0') {
800  *s = '\0';
801  return (s - start);
802  } else
803  return 0;
804 }
805 
806 size_t
807 date_strftime(char *s, size_t maxsize, const char *format,
808  const struct tmx *tmx)
809 {
810  return date_strftime_with_tmx(s, maxsize, format, tmx);
811 }
812 
813 #if 0
814 /* isleap --- is a year a leap year? */
815 
816 #ifndef __STDC__
817 static int
818 isleap(year)
819 long year;
820 #else
821 static int
822 isleap(long year)
823 #endif
824 {
825  return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
826 }
827 
828 static void
829 tmx2tm_noyear(const struct tmx *tmx, struct tm *result)
830 {
831  struct tm tm;
832 
833  /* for isleap() in iso8601wknum. +100 is -1900 (mod 400). */
834  tm.tm_year = FIX2INT(mod(tmx_year, INT2FIX(400))) + 100;
835 
836  tm.tm_mon = tmx_mon-1;
837  tm.tm_mday = tmx_mday;
838  tm.tm_hour = tmx_hour;
839  tm.tm_min = tmx_min;
840  tm.tm_sec = tmx_sec;
841  tm.tm_wday = tmx_wday;
842  tm.tm_yday = tmx_yday-1;
843  tm.tm_isdst = 0;
844 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
845  tm.tm_gmtoff = NUM2LONG(tmx_offset);
846 #endif
847 #if defined(HAVE_TM_ZONE)
848  tm.tm_zone = (char *)tmx_zone;
849 #endif
850  *result = tm;
851 }
852 
853 #ifdef POSIX2_DATE
854 /* iso8601wknum --- compute week number according to ISO 8601 */
855 
856 #ifndef __STDC__
857 static int
858 iso8601wknum(timeptr)
859 const struct tm *timeptr;
860 #else
861 static int
862 iso8601wknum(const struct tm *timeptr)
863 #endif
864 {
865  /*
866  * From 1003.2:
867  * If the week (Monday to Sunday) containing January 1
868  * has four or more days in the new year, then it is week 1;
869  * otherwise it is the highest numbered week of the previous
870  * year (52 or 53), and the next week is week 1.
871  *
872  * ADR: This means if Jan 1 was Monday through Thursday,
873  * it was week 1, otherwise week 52 or 53.
874  *
875  * XPG4 erroneously included POSIX.2 rationale text in the
876  * main body of the standard. Thus it requires week 53.
877  */
878 
879  int weeknum, jan1day;
880 
881  /* get week number, Monday as first day of the week */
882  weeknum = weeknumber(timeptr, 1);
883 
884  /*
885  * With thanks and tip of the hatlo to tml@tik.vtt.fi
886  *
887  * What day of the week does January 1 fall on?
888  * We know that
889  * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
890  * (timeptr->tm_wday - jan1.tm_wday) MOD 7
891  * and that
892  * jan1.tm_yday == 0
893  * and that
894  * timeptr->tm_wday MOD 7 == timeptr->tm_wday
895  * from which it follows that. . .
896  */
897  jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
898  if (jan1day < 0)
899  jan1day += 7;
900 
901  /*
902  * If Jan 1 was a Monday through Thursday, it was in
903  * week 1. Otherwise it was last year's highest week, which is
904  * this year's week 0.
905  *
906  * What does that mean?
907  * If Jan 1 was Monday, the week number is exactly right, it can
908  * never be 0.
909  * If it was Tuesday through Thursday, the weeknumber is one
910  * less than it should be, so we add one.
911  * Otherwise, Friday, Saturday or Sunday, the week number is
912  * OK, but if it is 0, it needs to be 52 or 53.
913  */
914  switch (jan1day) {
915  case 1: /* Monday */
916  break;
917  case 2: /* Tuesday */
918  case 3: /* Wednesday */
919  case 4: /* Thursday */
920  weeknum++;
921  break;
922  case 5: /* Friday */
923  case 6: /* Saturday */
924  case 0: /* Sunday */
925  if (weeknum == 0) {
926 #ifdef USE_BROKEN_XPG4
927  /* XPG4 (as of March 1994) says 53 unconditionally */
928  weeknum = 53;
929 #else
930  /* get week number of last week of last year */
931  struct tm dec31ly; /* 12/31 last year */
932  dec31ly = *timeptr;
933  dec31ly.tm_year--;
934  dec31ly.tm_mon = 11;
935  dec31ly.tm_mday = 31;
936  dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
937  dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
938  weeknum = iso8601wknum(& dec31ly);
939 #endif
940  }
941  break;
942  }
943 
944  if (timeptr->tm_mon == 11) {
945  /*
946  * The last week of the year
947  * can be in week 1 of next year.
948  * Sigh.
949  *
950  * This can only happen if
951  * M T W
952  * 29 30 31
953  * 30 31
954  * 31
955  */
956  int wday, mday;
957 
958  wday = timeptr->tm_wday;
959  mday = timeptr->tm_mday;
960  if ( (wday == 1 && (mday >= 29 && mday <= 31))
961  || (wday == 2 && (mday == 30 || mday == 31))
962  || (wday == 3 && mday == 31))
963  weeknum = 1;
964  }
965 
966  return weeknum;
967 }
968 
969 static int
970 iso8601wknum_v(const struct tmx *tmx)
971 {
972  struct tm tm;
973  tmx2tm_noyear(tmx, &tm);
974  return iso8601wknum(&tm);
975 }
976 
977 #endif
978 
979 /* weeknumber --- figure how many weeks into the year */
980 
981 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
982 
983 #ifndef __STDC__
984 static int
985 weeknumber(timeptr, firstweekday)
986 const struct tm *timeptr;
987 int firstweekday;
988 #else
989 static int
990 weeknumber(const struct tm *timeptr, int firstweekday)
991 #endif
992 {
993  int wday = timeptr->tm_wday;
994  int ret;
995 
996  if (firstweekday == 1) {
997  if (wday == 0) /* sunday */
998  wday = 6;
999  else
1000  wday--;
1001  }
1002  ret = ((timeptr->tm_yday + 7 - wday) / 7);
1003  if (ret < 0)
1004  ret = 0;
1005  return ret;
1006 }
1007 
1008 static int
1009 weeknumber_v(const struct tmx *tmx, int firstweekday)
1010 {
1011  struct tm tm;
1012  tmx2tm_noyear(tmx, &tm);
1013  return weeknumber(&tm, firstweekday);
1014 }
1015 #endif
1016 
1017 #if 0
1018 /* ADR --- I'm loathe to mess with ado's code ... */
1019 
1020 Date: Wed, 24 Apr 91 20:54:08 MDT
1021 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
1022 To: arnold@audiofax.com
1023 
1024 Hi Arnold,
1025 in a process of fixing of strftime() in libraries on Atari ST I grabbed
1026 some pieces of code from your own strftime. When doing that it came
1027 to mind that your weeknumber() function compiles a little bit nicer
1028 in the following form:
1029 /*
1030  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
1031  */
1032 {
1033  return (timeptr->tm_yday - timeptr->tm_wday +
1034  (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
1035 }
1036 How nicer it depends on a compiler, of course, but always a tiny bit.
1037 
1038  Cheers,
1039  Michal
1040  ntomczak@vm.ucs.ualberta.ca
1041 #endif
1042 
1043 #ifdef TEST_STRFTIME
1044 
1045 /*
1046  * NAME:
1047  * tst
1048  *
1049  * SYNOPSIS:
1050  * tst
1051  *
1052  * DESCRIPTION:
1053  * "tst" is a test driver for the function "strftime".
1054  *
1055  * OPTIONS:
1056  * None.
1057  *
1058  * AUTHOR:
1059  * Karl Vogel
1060  * Control Data Systems, Inc.
1061  * vogelke@c-17igp.wpafb.af.mil
1062  *
1063  * BUGS:
1064  * None noticed yet.
1065  *
1066  * COMPILE:
1067  * cc -o tst -DTEST_STRFTIME strftime.c
1068  */
1069 
1070 /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
1071 
1072 #ifndef NULL
1073 #include <stdio.h>
1074 #endif
1075 #include <time.h>
1076 #include <sys/time.h>
1077 #include <string.h>
1078 
1079 #define MAXTIME 132
1080 
1081 /*
1082  * Array of time formats.
1083  */
1084 
1085 static char *array[] =
1086 {
1087  "(%%A) full weekday name, var length (Sunday..Saturday) %A",
1088  "(%%B) full month name, var length (January..December) %B",
1089  "(%%C) Century %C",
1090  "(%%D) date (%%m/%%d/%%y) %D",
1091  "(%%E) Locale extensions (ignored) %E",
1092  "(%%H) hour (24-hour clock, 00..23) %H",
1093  "(%%I) hour (12-hour clock, 01..12) %I",
1094  "(%%M) minute (00..59) %M",
1095  "(%%O) Locale extensions (ignored) %O",
1096  "(%%R) time, 24-hour (%%H:%%M) %R",
1097  "(%%S) second (00..60) %S",
1098  "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
1099  "(%%U) week of year, Sunday as first day of week (00..53) %U",
1100  "(%%V) week of year according to ISO 8601 %V",
1101  "(%%W) week of year, Monday as first day of week (00..53) %W",
1102  "(%%X) appropriate locale time representation (%H:%M:%S) %X",
1103  "(%%Y) year with century (1970...) %Y",
1104  "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
1105  "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
1106  "(%%b) locale's abbreviated month name (Jan..Dec) %b",
1107  "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
1108  "(%%d) day of the month (01..31) %d",
1109  "(%%e) day of the month, blank-padded ( 1..31) %e",
1110  "(%%h) should be same as (%%b) %h",
1111  "(%%j) day of the year (001..366) %j",
1112  "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
1113  "(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
1114  "(%%m) month (01..12) %m",
1115  "(%%p) locale's AM or PM based on 12-hour clock %p",
1116  "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
1117  "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
1118  "(%%v) VMS date (dd-bbb-YYYY) %v",
1119  "(%%w) day of week (0..6, Sunday == 0) %w",
1120  "(%%x) appropriate locale date representation %x",
1121  "(%%y) last two digits of year (00..99) %y",
1122  "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
1123  (char *) NULL
1124 };
1125 
1126 /* main routine. */
1127 
1128 int
1129 main(argc, argv)
1130 int argc;
1131 char **argv;
1132 {
1133  char *next;
1134  char string[MAXTIME];
1135 
1136  int k;
1137  int length;
1138 
1139  struct tm *tm;
1140 
1141  time_t clock;
1142 
1143  /* Call the function. */
1144 
1145  clock = time(NULL);
1146  tm = localtime(&clock);
1147 
1148  for (k = 0; next = array[k]; k++) {
1149  length = strftime(string, MAXTIME, next, tm);
1150  printf("%s\n", string);
1151  }
1152 
1153  exit(0);
1154 }
1155 #endif /* TEST_STRFTIME */
1156