#include "expect_cf.h"
#include "tcl.h"
#include <stdio.h>
#include <ctype.h>
#include "string.h"
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <sys/types.h>
#define SYSV_EXT 1
#define POSIX2_DATE 1
#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
#define SYSV_EXT 1
#endif
#if defined(POSIX2_DATE)
#define adddecl(stuff) stuff
#else
#define adddecl(stuff)
#endif
#ifndef __STDC__
#define const
extern char *getenv();
static int weeknumber();
adddecl(static int iso8601wknum();)
#else
#ifndef strchr
extern char *strchr(const char *str, int ch);
#endif
extern char *getenv(const char *v);
static int weeknumber(const struct tm *timeptr, int firstweekday);
adddecl(static int iso8601wknum(const struct tm *timeptr);)
#endif
#if !defined(HAVE_STRFTIME)
# if defined(HAVE_SV_TIMEZONE)
extern char *tzname[2];
extern int daylight;
# else
# if defined(HAVE_TIMEZONE)
char *
zone_name (tp)
struct tm *tp;
{
char *timezone ();
struct timeval tv;
struct timezone tz;
gettimeofday (&tv, &tz);
return timezone (tz.tz_minuteswest, tp->tm_isdst);
}
# endif
# endif
#endif
static int
range(low,item,hi)
int low, item, hi;
{
if (item < low) return low;
if (item > hi) return hi;
return item;
}
void
#ifndef __STDC__
exp_strftime( format, timeptr, dstring)
char *format;
const struct tm *timeptr;
Tcl_DString *dstring;
#else
exp_strftime(char *format, const struct tm *timeptr,Tcl_DString *dstring)
#endif
{
int copied;
#if 0
char *endp = s + maxsize;
char *start = s;
#endif
char *percentptr;
char tbuf[100];
int i;
static char *days_a[] = {
"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat",
};
static char *days_l[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
};
static char *months_a[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
static char *months_l[] = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December",
};
static char *ampm[] = { "AM", "PM", };
for (; *format ; format++) {
tbuf[0] = '\0';
copied = 0;
percentptr = strchr(format,'%');
if (percentptr == 0) {
Tcl_DStringAppend(dstring,format,-1);
goto out;
} else if (percentptr != format) {
Tcl_DStringAppend(dstring,format,percentptr - format);
format = percentptr;
}
#if 0
if (*format != '%') {
*s++ = *format;
continue;
}
#endif
again:
switch (*++format) {
case '\0':
Tcl_DStringAppend(dstring,"%",1);
#if 0
*s++ = '%';
#endif
goto out;
case '%':
Tcl_DStringAppend(dstring,"%",1);
copied = 1;
break;
#if 0
*s++ = '%';
continue;
#endif
case 'a':
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
strcpy(tbuf, "?");
else
strcpy(tbuf, days_a[timeptr->tm_wday]);
break;
case 'A':
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
strcpy(tbuf, "?");
else
strcpy(tbuf, days_l[timeptr->tm_wday]);
break;
#ifdef SYSV_EXT
case 'h':
#endif
case 'b':
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
strcpy(tbuf, "?");
else
strcpy(tbuf, months_a[timeptr->tm_mon]);
break;
case 'B':
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
strcpy(tbuf, "?");
else
strcpy(tbuf, months_l[timeptr->tm_mon]);
break;
case 'c':
sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
days_a[range(0, timeptr->tm_wday, 6)],
months_a[range(0, timeptr->tm_mon, 11)],
range(1, timeptr->tm_mday, 31),
range(0, timeptr->tm_hour, 23),
range(0, timeptr->tm_min, 59),
range(0, timeptr->tm_sec, 61),
timeptr->tm_year + 1900);
break;
case 'd':
i = range(1, timeptr->tm_mday, 31);
sprintf(tbuf, "%02d", i);
break;
case 'H':
i = range(0, timeptr->tm_hour, 23);
sprintf(tbuf, "%02d", i);
break;
case 'I':
i = range(0, timeptr->tm_hour, 23);
if (i == 0)
i = 12;
else if (i > 12)
i -= 12;
sprintf(tbuf, "%02d", i);
break;
case 'j':
sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
break;
case 'm':
i = range(0, timeptr->tm_mon, 11);
sprintf(tbuf, "%02d", i + 1);
break;
case 'M':
i = range(0, timeptr->tm_min, 59);
sprintf(tbuf, "%02d", i);
break;
case 'p':
i = range(0, timeptr->tm_hour, 23);
if (i < 12)
strcpy(tbuf, ampm[0]);
else
strcpy(tbuf, ampm[1]);
break;
case 'S':
i = range(0, timeptr->tm_sec, 61);
sprintf(tbuf, "%02d", i);
break;
case 'U':
sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
break;
case 'w':
i = range(0, timeptr->tm_wday, 6);
sprintf(tbuf, "%d", i);
break;
case 'W':
sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
break;
case 'x':
sprintf(tbuf, "%s %s %2d %d",
days_a[range(0, timeptr->tm_wday, 6)],
months_a[range(0, timeptr->tm_mon, 11)],
range(1, timeptr->tm_mday, 31),
timeptr->tm_year + 1900);
break;
case 'X':
sprintf(tbuf, "%02d:%02d:%02d",
range(0, timeptr->tm_hour, 23),
range(0, timeptr->tm_min, 59),
range(0, timeptr->tm_sec, 61));
break;
case 'y':
i = timeptr->tm_year % 100;
sprintf(tbuf, "%02d", i);
break;
case 'Y':
sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
break;
case 'Z':
#if defined(HAVE_STRFTIME)
strftime(tbuf,sizeof tbuf,"%Z",timeptr);
#else
# if defined(HAVE_SV_TIMEZONE)
i = 0;
if (daylight && timeptr->tm_isdst)
i = 1;
strcpy(tbuf, tzname[i]);
# else
strcpy(tbuf, zone_name (timeptr));
# if defined(HAVE_TIMEZONE)
# endif
# endif
#endif
break;
#ifdef SYSV_EXT
case 'n':
tbuf[0] = '\n';
tbuf[1] = '\0';
break;
case 't':
tbuf[0] = '\t';
tbuf[1] = '\0';
break;
case 'D':
exp_strftime("%m/%d/%y", timeptr, dstring);
copied = 1;
break;
case 'e':
sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
break;
case 'r':
exp_strftime("%I:%M:%S %p", timeptr, dstring);
copied = 1;
break;
case 'R':
exp_strftime("%H:%M", timeptr, dstring);
copied = 1;
break;
case 'T':
exp_strftime("%H:%M:%S", timeptr, dstring);
copied = 1;
break;
#endif
#ifdef POSIX2_DATE
case 'C':
sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
break;
case 'E':
case 'O':
goto again;
case 'V':
sprintf(tbuf, "%02d", iso8601wknum(timeptr));
break;
case 'u':
sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
timeptr->tm_wday);
break;
#endif
default:
tbuf[0] = '%';
tbuf[1] = *format;
tbuf[2] = '\0';
break;
}
if (!copied)
Tcl_DStringAppend(dstring,tbuf,-1);
#if 0
i = strlen(tbuf);
if (i) {
if (s + i < endp - 1) {
strcpy(s, tbuf);
s += i;
} else
return 0;
#endif
}
out:;
#if 0
if (s < endp && *format == '\0') {
*s = '\0';
return (s - start);
} else
return 0;
#endif
}
#ifndef __STDC__
static int
isleap(year)
int year;
#else
static int
isleap(int year)
#endif
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
#ifdef POSIX2_DATE
#ifndef __STDC__
static int
iso8601wknum(timeptr)
const struct tm *timeptr;
#else
static int
iso8601wknum(const struct tm *timeptr)
#endif
{
int weeknum, jan1day, diff;
weeknum = weeknumber(timeptr, 1);
jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
if (jan1day < 0)
jan1day += 7;
switch (jan1day) {
case 1:
break;
case 2:
case 3:
case 4:
weeknum++;
break;
case 5:
case 6:
case 0:
if (weeknum == 0) {
#ifdef USE_BROKEN_XPG4
weeknum = 53;
#else
struct tm dec31ly;
dec31ly = *timeptr;
dec31ly.tm_year--;
dec31ly.tm_mon = 11;
dec31ly.tm_mday = 31;
dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
weeknum = iso8601wknum(& dec31ly);
#endif
}
break;
}
if (timeptr->tm_mon == 11) {
int wday, mday;
wday = timeptr->tm_wday;
mday = timeptr->tm_mday;
if ( (wday == 1 && (mday >= 29 && mday <= 31))
|| (wday == 2 && (mday == 30 || mday == 31))
|| (wday == 3 && mday == 31))
weeknum = 1;
}
return weeknum;
}
#endif
#ifndef __STDC__
static int
weeknumber(timeptr, firstweekday)
const struct tm *timeptr;
int firstweekday;
#else
static int
weeknumber(const struct tm *timeptr, int firstweekday)
#endif
{
int wday = timeptr->tm_wday;
int ret;
if (firstweekday == 1) {
if (wday == 0)
wday = 6;
else
wday--;
}
ret = ((timeptr->tm_yday + 7 - wday) / 7);
if (ret < 0)
ret = 0;
return ret;
}