* POSIX: use tzname for the %Z conversion, not tm->tm_zone.

* KNF, RCSid police.
This commit is contained in:
kleink 1997-04-29 20:00:14 +00:00
parent 4fd0a6d595
commit 891c1283c5

View File

@ -1,3 +1,5 @@
/* $NetBSD: strftime.c,v 1.2 1997/04/29 20:00:14 kleink Exp $ */
/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
@ -33,7 +35,7 @@
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)strftime.c 5.11 (Berkeley) 2/24/91";*/
static char *rcsid = "$Id: strftime.c,v 1.1 1997/04/23 01:18:05 mrg Exp $";
static char *rcsid = "$NetBSD: strftime.c,v 1.2 1997/04/29 20:00:14 kleink Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/localedef.h>
@ -44,8 +46,11 @@ static char *rcsid = "$Id: strftime.c,v 1.1 1997/04/23 01:18:05 mrg Exp $";
static size_t gsize;
static char *pt;
static int _add(), _conv(), _secs();
static size_t _fmt();
static int _add __P((const char *));
static int _conv __P((int, int, char));
static int _secs __P((const struct tm *));
static size_t _fmt __P((const char *, const struct tm *));
size_t
strftime(s, maxsize, format, t)
@ -58,12 +63,12 @@ strftime(s, maxsize, format, t)
pt = s;
if ((gsize = maxsize) < 1)
return(0);
return (0);
if (_fmt(format, t)) {
*pt = '\0';
return(maxsize - gsize);
return (maxsize - gsize);
}
return(0);
return (0);
}
#define SUN_WEEK(t) (((t)->tm_yday + 7 - \
@ -85,139 +90,140 @@ _fmt(format, t)
/* Alternate numeric symbols */
++format;
}
switch(*format) {
switch (*format) {
case '\0':
--format;
break;
case 'A':
if (t->tm_wday < 0 || t->tm_wday > 6)
return(0);
return (0);
if (!_add(_CurrentTimeLocale->day[t->tm_wday]))
return(0);
return (0);
continue;
case 'a':
if (t->tm_wday < 0 || t->tm_wday > 6)
return(0);
return (0);
if (!_add(_CurrentTimeLocale->abday[t->tm_wday]))
return(0);
return (0);
continue;
case 'B':
if (t->tm_mon < 0 || t->tm_mon > 11)
return(0);
return (0);
if (!_add(_CurrentTimeLocale->mon[t->tm_mon]))
return(0);
return (0);
continue;
case 'b':
case 'h':
if (t->tm_mon < 0 || t->tm_mon > 11)
return(0);
return (0);
if (!_add(_CurrentTimeLocale->abmon[t->tm_mon]))
return(0);
return (0);
continue;
case 'C':
if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
2, '0'))
return(0);
return (0);
continue;
case 'c':
if (!_fmt(_CurrentTimeLocale->d_t_fmt, t))
return(0);
return (0);
continue;
case 'D':
if (!_fmt("%m/%d/%y", t))
return(0);
return (0);
continue;
case 'd':
if (!_conv(t->tm_mday, 2, '0'))
return(0);
return (0);
continue;
case 'e':
if (!_conv(t->tm_mday, 2, ' '))
return(0);
return (0);
continue;
case 'H':
if (!_conv(t->tm_hour, 2, '0'))
return(0);
return (0);
continue;
case 'I':
if (!_conv(t->tm_hour % 12 ?
t->tm_hour % 12 : 12, 2, '0'))
return(0);
return (0);
continue;
case 'j':
if (!_conv(t->tm_yday + 1, 3, '0'))
return(0);
return (0);
continue;
case 'k':
if (!_conv(t->tm_hour, 2, ' '))
return(0);
return (0);
continue;
case 'l':
if (!_conv(t->tm_hour % 12 ?
t->tm_hour % 12: 12, 2, ' '))
return(0);
return (0);
continue;
case 'M':
if (!_conv(t->tm_min, 2, '0'))
return(0);
return (0);
continue;
case 'm':
if (!_conv(t->tm_mon + 1, 2, '0'))
return(0);
return (0);
continue;
case 'n':
if (!_add("\n"))
return(0);
return (0);
continue;
case 'p':
if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour >= 12]))
return(0);
return (0);
continue;
case 'R':
if (!_fmt("%H:%M", t))
return(0);
return (0);
continue;
case 'r':
if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t))
return(0);
return (0);
continue;
case 'S':
if (!_conv(t->tm_sec, 2, '0'))
return(0);
return (0);
continue;
case 's':
if (!_secs(t))
return(0);
return (0);
continue;
case 'T':
if (!_fmt("%H:%M:%S", t))
return(0);
return (0);
continue;
case 't':
if (!_add("\t"))
return(0);
return (0);
continue;
case 'U':
if (!_conv(SUN_WEEK(t), 2, '0'))
return(0);
return (0);
continue;
case 'u':
if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0'))
return(0);
return (0);
continue;
case 'V':
{
/* ISO 8601 Week Of Year:
If the week (Monday - Sunday) containing
January 1 has four or more days in the new
year, then it is week 1; otherwise it is
week 53 of the previous year and the next
week is week one. */
* If the week (Monday - Sunday) containing
* January 1 has four or more days in the new
* year, then it is week 1; otherwise it is
* week 53 of the previous year and the next
* week is week one.
*/
int week = MON_WEEK(t);
int days = (((t)->tm_yday + 7 - \
((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
int days = (((t)->tm_yday + 7 -
((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
if (days >= 4) {
@ -227,37 +233,38 @@ _fmt(format, t)
}
if (!_conv(week, 2, '0'))
return(0);
return (0);
continue;
}
case 'W':
if (!_conv(MON_WEEK(t), 2, '0'))
return(0);
return (0);
continue;
case 'w':
if (!_conv(t->tm_wday, 1, '0'))
return(0);
return (0);
continue;
case 'x':
if (!_fmt(_CurrentTimeLocale->d_fmt, t))
return(0);
return (0);
continue;
case 'X':
if (!_fmt(_CurrentTimeLocale->t_fmt, t))
return(0);
return (0);
continue;
case 'y':
if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
2, '0'))
return(0);
return (0);
continue;
case 'Y':
if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0'))
return(0);
return (0);
continue;
case 'Z':
if (t->tm_zone && !_add(t->tm_zone))
return(0);
if (tzname[t->tm_isdst ? 1 : 0] &&
!_add(tzname[t->tm_isdst ? 1 : 0]))
return (0);
continue;
case '%':
/*
@ -270,10 +277,10 @@ _fmt(format, t)
}
}
if (!gsize--)
return(0);
return (0);
*pt++ = *format;
}
return(gsize);
return (gsize);
}
static int
@ -290,7 +297,7 @@ _secs(t)
s = mktime(&tmp);
for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
*p-- = s % 10 + '0';
return(_add(++p));
return (_add(++p));
}
static int
@ -305,7 +312,7 @@ _conv(n, digits, pad)
*p-- = n % 10 + '0';
while (p > buf && digits-- > 0)
*p-- = pad;
return(_add(++p));
return (_add(++p));
}
static int
@ -314,8 +321,8 @@ _add(str)
{
for (;; ++pt, --gsize) {
if (!gsize)
return(0);
return (0);
if (!(*pt = *str++))
return(1);
return (1);
}
}