1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1993-08-26 04:43:03 +04:00
|
|
|
/*static char *sccsid = "from: @(#)strftime.c 5.11 (Berkeley) 2/24/91";*/
|
1994-04-23 02:11:52 +04:00
|
|
|
static char *rcsid = "$Id: strftime.c,v 1.6 1994/04/22 22:11:52 jtc Exp $";
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <string.h>
|
1994-04-23 02:11:52 +04:00
|
|
|
#include <tzfile.h>
|
|
|
|
#include <time.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-23 02:11:52 +04:00
|
|
|
static const char *const abday[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
|
|
|
|
};
|
1994-04-23 02:11:52 +04:00
|
|
|
static const char *const day[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
|
|
|
"Saturday",
|
|
|
|
};
|
1994-04-23 02:11:52 +04:00
|
|
|
static const char *const abmon[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
|
|
|
|
"Oct", "Nov", "Dec",
|
|
|
|
};
|
1994-04-23 02:11:52 +04:00
|
|
|
static const char *const mon[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"January", "February", "March", "April", "May", "June", "July",
|
|
|
|
"August", "September", "October", "November", "December",
|
|
|
|
};
|
1994-04-23 02:11:52 +04:00
|
|
|
static const char *const ampm[] = {
|
1993-08-19 20:39:47 +04:00
|
|
|
"AM", "PM",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *d_t_fmt = "%a %b %e %H:%M:%S %Y";
|
|
|
|
static const char *d_fmt = "%m/%d/%y";
|
|
|
|
static const char *t_fmt = "%H:%M:%S";
|
|
|
|
static const char *t_fmt_ampm = "%I:%M:%S %p";
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
static size_t gsize;
|
|
|
|
static char *pt;
|
|
|
|
static int _add(), _conv(), _secs();
|
1993-08-19 20:39:47 +04:00
|
|
|
static size_t _fmt();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
size_t
|
|
|
|
strftime(s, maxsize, format, t)
|
|
|
|
char *s;
|
|
|
|
size_t maxsize;
|
|
|
|
const char *format;
|
|
|
|
const struct tm *t;
|
|
|
|
{
|
1994-04-23 02:11:52 +04:00
|
|
|
tzset();
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
pt = s;
|
|
|
|
if ((gsize = maxsize) < 1)
|
|
|
|
return(0);
|
|
|
|
if (_fmt(format, t)) {
|
|
|
|
*pt = '\0';
|
|
|
|
return(maxsize - gsize);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-08-19 20:39:47 +04:00
|
|
|
#define SUN_WEEK(t) (((t)->tm_yday + 7 - \
|
|
|
|
((t)->tm_wday)) / 7)
|
|
|
|
#define MON_WEEK(t) (((t)->tm_yday + 7 - \
|
|
|
|
((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
|
1993-03-21 12:45:37 +03:00
|
|
|
static size_t
|
|
|
|
_fmt(format, t)
|
|
|
|
register char *format;
|
|
|
|
struct tm *t;
|
|
|
|
{
|
|
|
|
for (; *format; ++format) {
|
1993-08-19 20:39:47 +04:00
|
|
|
if (*format == '%') {
|
|
|
|
++format;
|
|
|
|
if (*format == 'E') {
|
|
|
|
/* Alternate Era */
|
|
|
|
++format;
|
|
|
|
} else if (*format == 'O') {
|
|
|
|
/* Alternate numeric symbols */
|
|
|
|
++format;
|
|
|
|
}
|
|
|
|
switch(*format) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case '\0':
|
|
|
|
--format;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
if (t->tm_wday < 0 || t->tm_wday > 6)
|
|
|
|
return(0);
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_add(day[t->tm_wday]))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'a':
|
|
|
|
if (t->tm_wday < 0 || t->tm_wday > 6)
|
|
|
|
return(0);
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_add(abday[t->tm_wday]))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'B':
|
|
|
|
if (t->tm_mon < 0 || t->tm_mon > 11)
|
|
|
|
return(0);
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_add(mon[t->tm_mon]))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'b':
|
|
|
|
case 'h':
|
|
|
|
if (t->tm_mon < 0 || t->tm_mon > 11)
|
|
|
|
return(0);
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_add(abmon[t->tm_mon]))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'C':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
|
|
|
|
2, '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'c':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_fmt(d_t_fmt, t))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'D':
|
|
|
|
if (!_fmt("%m/%d/%y", t))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'd':
|
|
|
|
if (!_conv(t->tm_mday, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'e':
|
|
|
|
if (!_conv(t->tm_mday, 2, ' '))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'H':
|
|
|
|
if (!_conv(t->tm_hour, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'I':
|
|
|
|
if (!_conv(t->tm_hour % 12 ?
|
|
|
|
t->tm_hour % 12 : 12, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'j':
|
|
|
|
if (!_conv(t->tm_yday + 1, 3, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'k':
|
|
|
|
if (!_conv(t->tm_hour, 2, ' '))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'l':
|
|
|
|
if (!_conv(t->tm_hour % 12 ?
|
1993-08-19 20:39:47 +04:00
|
|
|
t->tm_hour % 12: 12, 2, ' '))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'M':
|
|
|
|
if (!_conv(t->tm_min, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'm':
|
|
|
|
if (!_conv(t->tm_mon + 1, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'n':
|
|
|
|
if (!_add("\n"))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'p':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_add(ampm[t->tm_hour >= 12]))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'R':
|
|
|
|
if (!_fmt("%H:%M", t))
|
|
|
|
return(0);
|
1994-04-07 10:52:14 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'r':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_fmt(t_fmt_ampm, t))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'S':
|
|
|
|
if (!_conv(t->tm_sec, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 's':
|
|
|
|
if (!_secs(t))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'T':
|
|
|
|
if (!_fmt("%H:%M:%S", t))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 't':
|
|
|
|
if (!_add("\t"))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'U':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_conv(SUN_WEEK(t), 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'u':
|
|
|
|
if (!_conv(t->tm_wday ? t->tm_wday : 7, 2, '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
1993-08-19 20:39:47 +04:00
|
|
|
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. */
|
|
|
|
|
|
|
|
int week = MON_WEEK(t);
|
|
|
|
|
|
|
|
if (((t->tm_yday + 7 - (t->tm_wday + 1)) % 7) >= 4) {
|
|
|
|
week++;
|
|
|
|
} else if (week == 0) {
|
|
|
|
week = 53;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_conv(week, 2, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'W':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_conv(MON_WEEK(t), 2, '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'w':
|
|
|
|
if (!_conv(t->tm_wday, 1, '0'))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'x':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_fmt(d_fmt, t))
|
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'X':
|
|
|
|
if (!_fmt(t_fmt, t))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'y':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
|
|
|
|
2, '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'Y':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case 'Z':
|
1993-08-19 20:39:47 +04:00
|
|
|
if (t->tm_zone && !_add(t->tm_zone))
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
continue;
|
|
|
|
case '%':
|
|
|
|
/*
|
|
|
|
* X311J/88-090 (4.12.3.5): if conversion char is
|
|
|
|
* undefined, behavior is undefined. Print out the
|
|
|
|
* character itself as printf(3) does.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
break;
|
1993-08-19 20:39:47 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (!gsize--)
|
|
|
|
return(0);
|
|
|
|
*pt++ = *format;
|
|
|
|
}
|
|
|
|
return(gsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
_secs(t)
|
|
|
|
struct tm *t;
|
|
|
|
{
|
|
|
|
static char buf[15];
|
|
|
|
register time_t s;
|
|
|
|
register char *p;
|
|
|
|
struct tm tmp;
|
|
|
|
|
|
|
|
/* Make a copy, mktime(3) modifies the tm struct. */
|
|
|
|
tmp = *t;
|
|
|
|
s = mktime(&tmp);
|
|
|
|
for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
|
|
|
|
*p-- = s % 10 + '0';
|
|
|
|
return(_add(++p));
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
_conv(n, digits, pad)
|
|
|
|
int n, digits;
|
|
|
|
char pad;
|
|
|
|
{
|
|
|
|
static char buf[10];
|
|
|
|
register char *p;
|
|
|
|
|
|
|
|
for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
|
|
|
|
*p-- = n % 10 + '0';
|
|
|
|
while (p > buf && digits-- > 0)
|
|
|
|
*p-- = pad;
|
|
|
|
return(_add(++p));
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
_add(str)
|
|
|
|
register char *str;
|
|
|
|
{
|
|
|
|
for (;; ++pt, --gsize) {
|
|
|
|
if (!gsize)
|
|
|
|
return(0);
|
|
|
|
if (!(*pt = *str++))
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|