1989-06-12 15:22:14 +00:00
|
|
|
/*
|
|
|
|
* strftime - convert a structure to a string, controlled by an argument
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-06-12 15:22:14 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include "loc_time.h"
|
1989-06-12 15:22:14 +00:00
|
|
|
|
1989-12-18 15:33:48 +00:00
|
|
|
/* The width can be -1 in both s_prnt() as in u_prnt(). This
|
1989-06-12 15:22:14 +00:00
|
|
|
* indicates that as many characters as needed should be printed.
|
|
|
|
*/
|
2018-06-21 20:33:47 +00:00
|
|
|
static char*
|
|
|
|
s_prnt(char* s, size_t maxsize, const char* str, int width)
|
1989-06-12 15:22:14 +00:00
|
|
|
{
|
2018-06-21 20:33:47 +00:00
|
|
|
while (width > 0 || (width < 0 && *str))
|
|
|
|
{
|
|
|
|
if (!maxsize)
|
|
|
|
break;
|
1989-06-12 15:22:14 +00:00
|
|
|
*s++ = *str++;
|
|
|
|
maxsize--;
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
static char*
|
|
|
|
u_prnt(char* s, size_t maxsize, unsigned val, int width)
|
1989-06-12 15:22:14 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = val % 10;
|
|
|
|
val = val / 10;
|
|
|
|
if (--width > 0 || (width < 0 && val != 0))
|
|
|
|
s = u_prnt(s, (maxsize ? maxsize - 1 : 0), val, width);
|
2018-06-21 20:33:47 +00:00
|
|
|
if (maxsize)
|
|
|
|
*s++ = c + '0';
|
1989-06-12 15:22:14 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-06-21 20:33:47 +00:00
|
|
|
strftime(char* s, size_t maxsize,
|
|
|
|
const char* format, const struct tm* timeptr)
|
1989-06-12 15:22:14 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *firsts, *olds;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!format)
|
|
|
|
return 0;
|
1989-06-12 15:22:14 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
_tzset(); /* for %Z conversion */
|
1989-06-12 15:22:14 +00:00
|
|
|
firsts = s;
|
2018-06-21 20:33:47 +00:00
|
|
|
while (maxsize && *format)
|
|
|
|
{
|
|
|
|
while (maxsize && *format && *format != '%')
|
|
|
|
{
|
1989-06-12 15:22:14 +00:00
|
|
|
*s++ = *format++;
|
|
|
|
maxsize--;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!maxsize || !*format)
|
|
|
|
break;
|
1989-12-18 15:33:48 +00:00
|
|
|
format++;
|
1989-06-12 15:22:14 +00:00
|
|
|
|
|
|
|
olds = s;
|
2018-06-21 20:33:47 +00:00
|
|
|
switch (*format++)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
s = s_prnt(s, maxsize,
|
|
|
|
_days[timeptr->tm_wday], ABB_LEN);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
s = s_prnt(s, maxsize, _days[timeptr->tm_wday], -1);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
s = s_prnt(s, maxsize,
|
|
|
|
_months[timeptr->tm_mon], ABB_LEN);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
s = s_prnt(s, maxsize, _months[timeptr->tm_mon], -1);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
n = strftime(s, maxsize,
|
|
|
|
"%a %b %d %H:%M:%S %Y", timeptr);
|
|
|
|
if (n)
|
|
|
|
maxsize -= n;
|
|
|
|
else
|
|
|
|
maxsize = 0;
|
|
|
|
s += n;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_mday, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_hour, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
s = u_prnt(s, maxsize,
|
|
|
|
(timeptr->tm_hour + 11) % 12 + 1, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_yday + 1, 3);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_mon + 1, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_min, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
s = s_prnt(s, maxsize,
|
1989-06-12 15:22:14 +00:00
|
|
|
(timeptr->tm_hour < 12) ? "AM" : "PM", 2);
|
2018-06-21 20:33:47 +00:00
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_sec, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
s = u_prnt(s, maxsize, /* ??? */
|
|
|
|
(timeptr->tm_yday + 7 - timeptr->tm_wday) / 7, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_wday, 1);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
s = u_prnt(s, maxsize, /* ??? */
|
|
|
|
(timeptr->tm_yday + 7 - (timeptr->tm_wday + 6) % 7) / 7, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
n = strftime(s, maxsize, "%a %b %d %Y", timeptr);
|
|
|
|
if (n)
|
|
|
|
maxsize -= n;
|
|
|
|
else
|
|
|
|
maxsize = 0;
|
|
|
|
s += n;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
n = strftime(s, maxsize, "%H:%M:%S", timeptr);
|
|
|
|
if (n)
|
|
|
|
maxsize -= n;
|
|
|
|
else
|
|
|
|
maxsize = 0;
|
|
|
|
s += n;
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_year % 100, 2);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
s = u_prnt(s, maxsize, timeptr->tm_year + YEAR0, -1);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
s = s_prnt(s, maxsize,
|
|
|
|
_tzname[(timeptr->tm_isdst > 0)], -1);
|
|
|
|
maxsize -= s - olds;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
*s++ = '%';
|
|
|
|
maxsize--;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* A conversion error. Leave the loop. */
|
|
|
|
while (*format)
|
|
|
|
format++;
|
|
|
|
break;
|
1989-06-12 15:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (maxsize)
|
|
|
|
{
|
1989-06-12 15:22:14 +00:00
|
|
|
*s = '\0';
|
|
|
|
return s - firsts;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
return 0; /* The buffer is full */
|
1989-06-12 15:22:14 +00:00
|
|
|
}
|