#ifndef lint
#ifndef NOID
static const char elsieid[] = "@(#)strftime.c 7.64";
#endif
#endif
#include "xlocale_private.h"
#include "namespace.h"
#include "private.h"
#if defined(LIBC_SCCS) && !defined(lint)
static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.40 2004/06/14 10:31:52 stefanf Exp $");
#include "tzfile.h"
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "un-namespace.h"
#include "timelocal.h"
#if !BUILDING_VARIANT
static char * _add(const char *, char *, const char *);
static char * _conv(int, const char *, char *, const char *, locale_t);
#endif
#define _fmt _st_fmt
__private_extern__ char *_fmt(const char *, const struct tm *, char *, const char *, int *, struct lc_time_T *, locale_t);
size_t strftime(char * __restrict, size_t, const char * __restrict,
const struct tm * __restrict);
extern char * tzname[];
__private_extern__ long __darwin_altzone;
#define altzone __darwin_altzone
__private_extern__ long _st_get_timezone(void);
#ifndef YEAR_2000_NAME
#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
#endif
#define IN_NONE 0
#define IN_SOME 1
#define IN_THIS 2
#define IN_ALL 3
size_t
strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
const struct tm * __restrict t, locale_t loc)
{
char * p;
int warn;
#if __DARWIN_UNIX03
struct tm t2;
#endif
NORMALIZE_LOCALE(loc);
tzset();
warn = IN_NONE;
#if __DARWIN_UNIX03
if (t->tm_isdst >= 0) {
t2 = *t;
t2.tm_gmtoff = t->tm_isdst ? -__darwin_altzone : -_st_get_timezone();
t = &t2;
}
#endif
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, __get_current_time_locale(loc), loc);
#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
(void) fputs("\n", stderr);
if (format == NULL)
(void) fputs("NULL strftime format ", stderr);
else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
format);
(void) fputs("yields only two digits of years in ", stderr);
if (warn == IN_SOME)
(void) fputs("some locales", stderr);
else if (warn == IN_THIS)
(void) fputs("the current locale", stderr);
else (void) fputs("all locales", stderr);
(void) fputs("\n", stderr);
}
#endif
if (p == s + maxsize)
return 0;
*p = '\0';
return p - s;
}
size_t
strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
const struct tm * __restrict t)
{
return strftime_l(s, maxsize, format, t, __current_locale());
}
#ifndef BUILDING_VARIANT
__private_extern__ char *
_fmt(format, t, pt, ptlim, warnp, tptr, loc)
const char * format;
const struct tm * const t;
char * pt;
const char * const ptlim;
int * warnp;
struct lc_time_T * tptr;
locale_t loc;
{
int Ealternative, Oalternative;
for ( ; *format; ++format) {
if (*format == '%') {
Ealternative = 0;
Oalternative = 0;
label:
switch (*++format) {
case '\0':
--format;
break;
case 'A':
pt = _add((t->tm_wday < 0 ||
t->tm_wday >= DAYSPERWEEK) ?
"?" : tptr->weekday[t->tm_wday],
pt, ptlim);
continue;
case 'a':
pt = _add((t->tm_wday < 0 ||
t->tm_wday >= DAYSPERWEEK) ?
"?" : tptr->wday[t->tm_wday],
pt, ptlim);
continue;
case 'B':
pt = _add((t->tm_mon < 0 ||
t->tm_mon >= MONSPERYEAR) ?
"?" : (Oalternative ? tptr->alt_month :
tptr->month)[t->tm_mon],
pt, ptlim);
continue;
case 'b':
case 'h':
pt = _add((t->tm_mon < 0 ||
t->tm_mon >= MONSPERYEAR) ?
"?" : tptr->mon[t->tm_mon],
pt, ptlim);
continue;
case 'C':
pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
"%02d", pt, ptlim, loc);
continue;
case 'c':
{
int warn2 = IN_SOME;
pt = _fmt(tptr->c_fmt, t, pt, ptlim, warnp, tptr, loc);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
*warnp = warn2;
}
continue;
case 'D':
pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, tptr, loc);
continue;
case 'd':
pt = _conv(t->tm_mday, "%02d", pt, ptlim, loc);
continue;
case 'E':
if (Ealternative || Oalternative)
break;
Ealternative++;
goto label;
case 'O':
if (Ealternative || Oalternative)
break;
Oalternative++;
goto label;
case 'e':
pt = _conv(t->tm_mday, "%2d", pt, ptlim, loc);
continue;
case 'F':
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, tptr, loc);
continue;
case 'H':
pt = _conv(t->tm_hour, "%02d", pt, ptlim, loc);
continue;
case 'I':
pt = _conv((t->tm_hour % 12) ?
(t->tm_hour % 12) : 12,
"%02d", pt, ptlim, loc);
continue;
case 'j':
pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim, loc);
continue;
case 'k':
pt = _conv(t->tm_hour, "%2d", pt, ptlim, loc);
continue;
#ifdef KITCHEN_SINK
case 'K':
pt = _add("kitchen sink", pt, ptlim);
continue;
#endif
case 'l':
pt = _conv((t->tm_hour % 12) ?
(t->tm_hour % 12) : 12,
"%2d", pt, ptlim, loc);
continue;
case 'M':
pt = _conv(t->tm_min, "%02d", pt, ptlim, loc);
continue;
case 'm':
pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim, loc);
continue;
case 'n':
pt = _add("\n", pt, ptlim);
continue;
case 'p':
pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
tptr->pm :
tptr->am,
pt, ptlim);
continue;
case 'R':
pt = _fmt("%H:%M", t, pt, ptlim, warnp, tptr, loc);
continue;
case 'r':
pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
warnp, tptr, loc);
continue;
case 'S':
pt = _conv(t->tm_sec, "%02d", pt, ptlim, loc);
continue;
case 's':
{
struct tm tm;
char buf[INT_STRLEN_MAXIMUM(
time_t) + 1];
time_t mkt;
tm = *t;
mkt = mktime(&tm);
if (TYPE_SIGNED(time_t))
(void) sprintf_l(buf, loc, "%ld",
(long) mkt);
else (void) sprintf_l(buf, loc, "%lu",
(unsigned long) mkt);
pt = _add(buf, pt, ptlim);
}
continue;
case 'T':
pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, tptr, loc);
continue;
case 't':
pt = _add("\t", pt, ptlim);
continue;
case 'U':
pt = _conv((t->tm_yday + DAYSPERWEEK -
t->tm_wday) / DAYSPERWEEK,
"%02d", pt, ptlim, loc);
continue;
case 'u':
pt = _conv((t->tm_wday == 0) ?
DAYSPERWEEK : t->tm_wday,
"%d", pt, ptlim, loc);
continue;
case 'V':
case 'G':
case 'g':
{
int year;
int yday;
int wday;
int w;
year = t->tm_year + TM_YEAR_BASE;
yday = t->tm_yday;
wday = t->tm_wday;
for ( ; ; ) {
int len;
int bot;
int top;
len = isleap(year) ?
DAYSPERLYEAR :
DAYSPERNYEAR;
bot = ((yday + 11 - wday) %
DAYSPERWEEK) - 3;
top = bot -
(len % DAYSPERWEEK);
if (top < -3)
top += DAYSPERWEEK;
top += len;
if (yday >= top) {
++year;
w = 1;
break;
}
if (yday >= bot) {
w = 1 + ((yday - bot) /
DAYSPERWEEK);
break;
}
--year;
yday += isleap(year) ?
DAYSPERLYEAR :
DAYSPERNYEAR;
}
#ifdef XPG4_1994_04_09
if ((w == 52
&& t->tm_mon == TM_JANUARY)
|| (w == 1
&& t->tm_mon == TM_DECEMBER))
w = 53;
#endif
if (*format == 'V')
pt = _conv(w, "%02d",
pt, ptlim, loc);
else if (*format == 'g') {
*warnp = IN_ALL;
pt = _conv(year % 100, "%02d",
pt, ptlim, loc);
} else pt = _conv(year, "%04d",
pt, ptlim, loc);
}
continue;
case 'v':
pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, tptr, loc);
continue;
case 'W':
pt = _conv((t->tm_yday + DAYSPERWEEK -
(t->tm_wday ?
(t->tm_wday - 1) :
(DAYSPERWEEK - 1))) / DAYSPERWEEK,
"%02d", pt, ptlim, loc);
continue;
case 'w':
pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
continue;
case 'X':
pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, tptr, loc);
continue;
case 'x':
{
int warn2 = IN_SOME;
pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, tptr, loc);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
*warnp = warn2;
}
continue;
case 'y':
*warnp = IN_ALL;
pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
"%02d", pt, ptlim, loc);
continue;
case 'Y':
pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
pt, ptlim, loc);
continue;
case 'Z':
#ifdef TM_ZONE
if (t->TM_ZONE != NULL)
pt = _add(t->TM_ZONE, pt, ptlim);
else
#endif
if (t->tm_isdst >= 0)
pt = _add(tzname[t->tm_isdst != 0],
pt, ptlim);
continue;
case 'z':
{
int diff;
char const * sign;
if (t->tm_isdst < 0)
continue;
#ifdef TM_GMTOFF
diff = t->TM_GMTOFF;
#else
if (t->tm_isdst == 0)
#ifdef USG_COMPAT
diff = -_st_get_timezone();
#else
continue;
#endif
else
#ifdef ALTZONE
diff = -altzone;
#else
continue;
#endif
#endif
if (diff < 0) {
sign = "-";
diff = -diff;
} else sign = "+";
pt = _add(sign, pt, ptlim);
diff /= 60;
pt = _conv((diff/60)*100 + diff%60,
"%04d", pt, ptlim, loc);
}
continue;
case '+':
pt = _fmt(tptr->date_fmt, t, pt, ptlim,
warnp, tptr, loc);
continue;
case '%':
default:
break;
}
}
if (pt == ptlim)
break;
*pt++ = *format;
}
return pt;
}
static char *
_conv(n, format, pt, ptlim, loc)
const int n;
const char * const format;
char * const pt;
const char * const ptlim;
locale_t loc;
{
char buf[INT_STRLEN_MAXIMUM(int) + 1];
(void) sprintf_l(buf, loc, format, n);
return _add(buf, pt, ptlim);
}
static char *
_add(str, pt, ptlim)
const char * str;
char * pt;
const char * const ptlim;
{
while (pt < ptlim && (*pt = *str++) != '\0')
++pt;
return pt;
}
#endif