ack/lang/cem/libcc.ansi/time/asctime.c

62 lines
1.1 KiB
C
Raw Normal View History

1989-06-12 15:22:14 +00:00
/*
* asctime - print a date
*/
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 <string.h>
#include <time.h>
#include "loc_time.h"
1989-06-12 15:22:14 +00:00
2018-06-21 20:33:47 +00:00
#define DATE_STR "??? ??? ?? ??:??:?? ????\n"
1989-06-12 15:22:14 +00:00
2018-06-21 20:33:47 +00:00
static char*
two_digits(register char* pb, int i, int nospace)
1989-06-12 15:22:14 +00:00
{
*pb = (i / 10) % 10 + '0';
2018-06-21 20:33:47 +00:00
if (!nospace && *pb == '0')
*pb = ' ';
1989-06-12 15:22:14 +00:00
pb++;
*pb++ = (i % 10) + '0';
return ++pb;
}
2018-06-21 20:33:47 +00:00
static char*
four_digits(register char* pb, int i)
1989-06-12 15:22:14 +00:00
{
i %= 10000;
*pb++ = (i / 1000) + '0';
i %= 1000;
*pb++ = (i / 100) + '0';
i %= 100;
*pb++ = (i / 10) + '0';
*pb++ = (i % 10) + '0';
return ++pb;
}
2018-06-21 20:33:47 +00:00
char* asctime(const struct tm* timeptr)
1989-06-12 15:22:14 +00:00
{
static char buf[26];
2018-06-21 20:33:47 +00:00
register char* pb = buf;
register const char* ps;
1989-06-12 15:22:14 +00:00
register int n;
strcpy(pb, DATE_STR);
ps = _days[timeptr->tm_wday];
n = ABB_LEN;
2018-06-21 20:33:47 +00:00
while (--n >= 0)
*pb++ = *ps++;
1989-06-12 15:22:14 +00:00
pb++;
ps = _months[timeptr->tm_mon];
n = ABB_LEN;
2018-06-21 20:33:47 +00:00
while (--n >= 0)
*pb++ = *ps++;
1989-06-12 15:22:14 +00:00
pb++;
pb = two_digits(
2018-06-21 20:33:47 +00:00
two_digits(
two_digits(two_digits(pb, timeptr->tm_mday, 0), timeptr->tm_hour, 1), timeptr->tm_min, 1),
timeptr->tm_sec, 1);
1989-06-12 15:22:14 +00:00
four_digits(pb, timeptr->tm_year + 1900);
return buf;
}