1 /* exp_strp.c - functions for exp_timestamp */
5 * Public-domain implementation of ANSI C library routine.
7 * It's written in old-style C for maximal portability.
8 * However, since I'm used to prototypes, I've included them too.
10 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
11 * For extensions from SunOS, add SUNOS_EXT.
12 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
13 * For VMS dates, add VMS_EXT.
14 * For complete POSIX semantics, add POSIX_SEMANTICS.
16 * The code for %c, %x, and %X now follows the 1003.2 specification for
18 * This version ignores LOCALE information.
19 * It also doesn't worry about multi-byte characters.
22 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
23 * code are included if GAWK is defined.
25 * Arnold Robbins <arnold@skeeve.atl.ga.us>
26 * January, February, March, 1991
27 * Updated March, April 1992
29 * Updated February, 1994
31 * Updated January 1995
32 * Updated September 1995
34 * Fixes from ado@elsie.nci.nih.gov
35 * February 1991, May 1992
36 * Fixes from Tor Lillqvist tml@tik.vtt.fi
38 * Further fixes from ado@elsie.nci.nih.gov
40 * %z code from chip@chinacat.unicom.com
41 * Applied September 1995
44 * Modified by Don Libes for Expect, 10/93 and 12/95.
45 * Forced POSIX semantics.
46 * Replaced inline/min/max stuff with a single range function.
47 * Removed tzset stuff.
48 * Commented out tzname stuff.
50 * According to Arnold, the current version of this code can ftp'd from
51 * ftp.mathcs.emory.edu:/pub/arnold/strftime.shar.gz
55 #include "expect_cf.h"
62 /* according to Karl Vogel, time.h is insufficient on Pyramid */
63 /* following is recommended by autoconf */
65 #ifdef TIME_WITH_SYS_TIME
66 # include <sys/time.h>
69 # ifdef HAVE_SYS_TIME_H
70 # include <sys/time.h>
78 #include <sys/types.h>
80 #define SYSV_EXT 1 /* stuff in System V ascftime routine */
81 #define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
83 #if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
87 #if defined(POSIX2_DATE)
88 #define adddecl(stuff) stuff
90 #define adddecl(stuff)
96 extern char *getenv();
97 static int weeknumber();
98 adddecl(static int iso8601wknum();)
102 extern char *strchr(const char *str, int ch);
104 extern char *getenv(const char *v);
105 static int weeknumber(const struct tm *timeptr, int firstweekday);
106 adddecl(static int iso8601wknum(const struct tm *timeptr);)
109 /* attempt to use strftime to compute timezone, else fallback to */
110 /* less portable ways */
111 #if !defined(HAVE_STRFTIME)
112 # if defined(HAVE_SV_TIMEZONE)
113 extern char *tzname[2];
116 # if defined(HAVE_TIMEZONE)
126 gettimeofday (&tv, &tz);
128 return timezone (tz.tz_minuteswest, tp->tm_isdst);
131 # endif /* HAVE_TIMEZONE */
132 # endif /* HAVE_SV_TIMEZONE */
133 #endif /* HAVE_STRFTIME */
139 if (item < low) return low;
140 if (item > hi) return hi;
144 /* strftime --- produce formatted time */
149 exp_strftime(/*s,*/ format, timeptr, dstring)
152 const struct tm *timeptr;
153 Tcl_DString *dstring;
155 /*exp_strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)*/
156 exp_strftime(char *format, const struct tm *timeptr,Tcl_DString *dstring)
159 int copied; /* used to suppress copying when called recursively */
162 char *endp = s + maxsize;
170 /* various tables, useful in North America */
171 static char *days_a[] = {
172 "Sun", "Mon", "Tue", "Wed",
175 static char *days_l[] = {
176 "Sunday", "Monday", "Tuesday", "Wednesday",
177 "Thursday", "Friday", "Saturday",
179 static char *months_a[] = {
180 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
181 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
183 static char *months_l[] = {
184 "January", "February", "March", "April",
185 "May", "June", "July", "August", "September",
186 "October", "November", "December",
188 static char *ampm[] = { "AM", "PM", };
190 /* for (; *format && s < endp - 1; format++) {*/
191 for (; *format ; format++) {
193 copied = 0; /* has not been copied yet */
194 percentptr = strchr(format,'%');
195 if (percentptr == 0) {
196 Tcl_DStringAppend(dstring,format,-1);
198 } else if (percentptr != format) {
199 Tcl_DStringAppend(dstring,format,percentptr - format);
203 if (*format != '%') {
211 Tcl_DStringAppend(dstring,"%",1);
218 Tcl_DStringAppend(dstring,"%",1);
226 case 'a': /* abbreviated weekday name */
227 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
230 strcpy(tbuf, days_a[timeptr->tm_wday]);
233 case 'A': /* full weekday name */
234 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
237 strcpy(tbuf, days_l[timeptr->tm_wday]);
241 case 'h': /* abbreviated month name */
243 case 'b': /* abbreviated month name */
244 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
247 strcpy(tbuf, months_a[timeptr->tm_mon]);
250 case 'B': /* full month name */
251 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
254 strcpy(tbuf, months_l[timeptr->tm_mon]);
257 case 'c': /* appropriate date and time representation */
258 sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
259 days_a[range(0, timeptr->tm_wday, 6)],
260 months_a[range(0, timeptr->tm_mon, 11)],
261 range(1, timeptr->tm_mday, 31),
262 range(0, timeptr->tm_hour, 23),
263 range(0, timeptr->tm_min, 59),
264 range(0, timeptr->tm_sec, 61),
265 timeptr->tm_year + 1900);
268 case 'd': /* day of the month, 01 - 31 */
269 i = range(1, timeptr->tm_mday, 31);
270 sprintf(tbuf, "%02d", i);
273 case 'H': /* hour, 24-hour clock, 00 - 23 */
274 i = range(0, timeptr->tm_hour, 23);
275 sprintf(tbuf, "%02d", i);
278 case 'I': /* hour, 12-hour clock, 01 - 12 */
279 i = range(0, timeptr->tm_hour, 23);
284 sprintf(tbuf, "%02d", i);
287 case 'j': /* day of the year, 001 - 366 */
288 sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
291 case 'm': /* month, 01 - 12 */
292 i = range(0, timeptr->tm_mon, 11);
293 sprintf(tbuf, "%02d", i + 1);
296 case 'M': /* minute, 00 - 59 */
297 i = range(0, timeptr->tm_min, 59);
298 sprintf(tbuf, "%02d", i);
301 case 'p': /* am or pm based on 12-hour clock */
302 i = range(0, timeptr->tm_hour, 23);
304 strcpy(tbuf, ampm[0]);
306 strcpy(tbuf, ampm[1]);
309 case 'S': /* second, 00 - 61 */
310 i = range(0, timeptr->tm_sec, 61);
311 sprintf(tbuf, "%02d", i);
314 case 'U': /* week of year, Sunday is first day of week */
315 sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
318 case 'w': /* weekday, Sunday == 0, 0 - 6 */
319 i = range(0, timeptr->tm_wday, 6);
320 sprintf(tbuf, "%d", i);
323 case 'W': /* week of year, Monday is first day of week */
324 sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
327 case 'x': /* appropriate date representation */
328 sprintf(tbuf, "%s %s %2d %d",
329 days_a[range(0, timeptr->tm_wday, 6)],
330 months_a[range(0, timeptr->tm_mon, 11)],
331 range(1, timeptr->tm_mday, 31),
332 timeptr->tm_year + 1900);
335 case 'X': /* appropriate time representation */
336 sprintf(tbuf, "%02d:%02d:%02d",
337 range(0, timeptr->tm_hour, 23),
338 range(0, timeptr->tm_min, 59),
339 range(0, timeptr->tm_sec, 61));
342 case 'y': /* year without a century, 00 - 99 */
343 i = timeptr->tm_year % 100;
344 sprintf(tbuf, "%02d", i);
347 case 'Y': /* year with century */
348 sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
351 case 'Z': /* time zone name or abbrevation */
352 #if defined(HAVE_STRFTIME)
353 strftime(tbuf,sizeof tbuf,"%Z",timeptr);
355 # if defined(HAVE_SV_TIMEZONE)
357 if (daylight && timeptr->tm_isdst)
359 strcpy(tbuf, tzname[i]);
361 strcpy(tbuf, zone_name (timeptr));
362 # if defined(HAVE_TIMEZONE)
363 # endif /* HAVE_TIMEZONE */
364 /* no timezone available */
365 /* feel free to add others here */
366 # endif /* HAVE_SV_TIMEZONE */
367 #endif /* HAVE STRFTIME */
371 case 'n': /* same as \n */
376 case 't': /* same as \t */
381 case 'D': /* date as %m/%d/%y */
382 exp_strftime("%m/%d/%y", timeptr, dstring);
384 /* exp_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);*/
387 case 'e': /* day of month, blank padded */
388 sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
391 case 'r': /* time as %I:%M:%S %p */
392 exp_strftime("%I:%M:%S %p", timeptr, dstring);
394 /* exp_strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);*/
397 case 'R': /* time as %H:%M */
398 exp_strftime("%H:%M", timeptr, dstring);
400 /* exp_strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);*/
403 case 'T': /* time as %H:%M:%S */
404 exp_strftime("%H:%M:%S", timeptr, dstring);
406 /* exp_strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);*/
412 sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
418 /* POSIX locale extensions, ignored for now */
420 case 'V': /* week of year according ISO 8601 */
421 sprintf(tbuf, "%02d", iso8601wknum(timeptr));
425 /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
426 sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
429 #endif /* POSIX2_DATE */
437 Tcl_DStringAppend(dstring,tbuf,-1);
441 if (s + i < endp - 1) {
450 if (s < endp && *format == '\0') {
458 /* isleap --- is a year a leap year? */
469 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
473 /* iso8601wknum --- compute week number according to ISO 8601 */
477 iso8601wknum(timeptr)
478 const struct tm *timeptr;
481 iso8601wknum(const struct tm *timeptr)
486 * If the week (Monday to Sunday) containing January 1
487 * has four or more days in the new year, then it is week 1;
488 * otherwise it is the highest numbered week of the previous
489 * (52 or 53) year, and the next week is week 1.
491 * ADR: This means if Jan 1 was Monday through Thursday,
492 * it was week 1, otherwise week 53.
494 * XPG4 erroneously included POSIX.2 rationale text in the
495 * main body of the standard. Thus it requires week 53.
498 int weeknum, jan1day, diff;
500 /* get week number, Monday as first day of the week */
501 weeknum = weeknumber(timeptr, 1);
504 * With thanks and tip of the hatlo to tml@tik.vtt.fi
506 * What day of the week does January 1 fall on?
508 * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
509 * (timeptr->tm_wday - jan1.tm_wday) MOD 7
513 * timeptr->tm_wday MOD 7 == timeptr->tm_wday
514 * from which it follows that. . .
516 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
521 * If Jan 1 was a Monday through Thursday, it was in
522 * week 1. Otherwise it was last year's highest week, which is
523 * this year's week 0.
525 * What does that mean?
526 * If Jan 1 was Monday, the week number is exactly right, it can
528 * If it was Tuesday through Thursday, the weeknumber is one
529 * less than it should be, so we add one.
530 * Otherwise, Friday, Saturday or Sunday, the week number is
531 * OK, but if it is 0, it needs to be 52 or 53.
536 case 2: /* Tuesday */
537 case 3: /* Wednesday */
538 case 4: /* Thursday */
542 case 6: /* Saturday */
545 #ifdef USE_BROKEN_XPG4
546 /* XPG4 (as of March 1994) says 53 unconditionally */
549 /* get week number of last week of last year */
550 struct tm dec31ly; /* 12/31 last year */
554 dec31ly.tm_mday = 31;
555 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
556 dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
557 weeknum = iso8601wknum(& dec31ly);
563 if (timeptr->tm_mon == 11) {
565 * The last week of the year
566 * can be in week 1 of next year.
569 * This can only happen if
577 wday = timeptr->tm_wday;
578 mday = timeptr->tm_mday;
579 if ( (wday == 1 && (mday >= 29 && mday <= 31))
580 || (wday == 2 && (mday == 30 || mday == 31))
581 || (wday == 3 && mday == 31))
589 /* weeknumber --- figure how many weeks into the year */
591 /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
595 weeknumber(timeptr, firstweekday)
596 const struct tm *timeptr;
600 weeknumber(const struct tm *timeptr, int firstweekday)
603 int wday = timeptr->tm_wday;
606 if (firstweekday == 1) {
607 if (wday == 0) /* sunday */
612 ret = ((timeptr->tm_yday + 7 - wday) / 7);