home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 4
/
DATAFILE_PDCD4.iso
/
unix
/
unixlib36d
/
src
/
c
/
ctime
< prev
next >
Wrap
Text File
|
1994-03-08
|
4KB
|
225 lines
#ifdef __STDC__
static char sccs_id[] = "@(#) ctime.c 2.0 " __DATE__ " HJR";
#else
static char sccs_id[] = "@(#) ctime.c 2.0 26/9/90 HJR";
#endif
/* ctime.c (c) Copyright 1990 H.Rogers */
#include <time.h>
#include <stdio.h>
#include <ctype.h>
static char *__tdays[] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char *__tdayl[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static char *__tmonths[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char *__tmonthl[] =
{"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
/* standard representations (take care to avoid making
* strftime() call itself recursively ad infinitum) */
static char *__dtrep = "%a %b %d %H:%M:%S %Y";
static char *__drep = "%a %b %d %Y";
static char *__trep = "%H:%M:%S";
#ifdef __STDC__
char *
asctime (register const struct tm *t)
#else
char *
asctime (t)
register const struct tm *t;
#endif
{
static char _buf[26];
register char *buf = _buf;
buf += strftime (buf, 24, __dtrep, t);
*buf++ = '\n';
*buf++ = 0;
return (_buf);
}
#ifdef __STDC__
char *
ctime (register const time_t * tp)
#else
char *
ctime (tp)
register const time_t *tp;
#endif
{
return (asctime (localtime (tp)));
}
#ifdef __STDC__
size_t
strftime (register char *buf, register size_t max,
register const char *fmt, register const struct tm * t)
#else
size_t
strftime (buf, max, fmt, t)
register char *buf;
register size_t max;
register const char *fmt;
register const struct tm *t;
#endif
{
register int i = max;
while (*fmt && i)
{
if (*fmt != '%' || *++fmt == '%') /* left to right evaluation */
{
*buf++ = *fmt++, i--;
continue;
}
{
register char *s;
register int j;
switch (*fmt)
{
case 'a':
s = __tdays[t->tm_wday];
scp:while (i && (*buf = *s))
buf++, s++, i--;
break;
case 'A':
s = __tdayl[t->tm_wday];
goto scp;
case 'b':
s = __tmonths[t->tm_mon];
goto scp;
case 'B':
s = __tmonthl[t->tm_mon];
goto scp;
case 'c':
j = strftime (buf, i, __dtrep, t);
buf += j, i -= j;
break;
case 'd':
if (i >= 2)
{
sprintf (buf, "%2d", t->tm_mday);
buf += 2, i -= 2;
}
break;
case 'H':
if (i >= 2)
{
sprintf (buf, "%.2d", t->tm_hour);
buf += 2, i -= 2;
}
break;
case 'I':
j = t->tm_hour;
if (j > 12)
j -= 12;
if (i >= 2)
{
sprintf (buf, "%2d", j);
buf += 2, i -= 2;
}
break;
case 'j':
if (i >= 3)
{
sprintf (buf, "%3d", t->tm_yday);
buf += 3, i -= 3;
}
break;
case 'm':
if (i >= 2)
{
sprintf (buf, "%2d", t->tm_mon);
buf += 2, i -= 2;
}
break;
case 'M':
if (i >= 2)
{
sprintf (buf, "%.2d", t->tm_min);
buf += 2, i -= 2;
}
break;
case 'p':
s = (t->tm_hour > 12) ? "PM" : "AM";
goto scp;
case 'S':
if (i >= 2)
{
sprintf (buf, "%.2d", t->tm_sec);
buf += 2, i -= 2;
}
break;
case 'U':
j = t->tm_yday;
if (j > 2)
j += (4 - t->tm_wday);
if (i >= 2)
{
sprintf (buf, "%2d", j / 7);
buf += 2, i -= 2;
}
break;
case 'w':
*buf++ = t->tm_wday + '0';
i--;
break;
case 'W':
j = t->tm_yday;
if (j > 2)
j += (5 - ((j = t->tm_wday) ? j : 7));
if (i >= 2)
{
sprintf (buf, "%2d", j / 7);
buf += 2, i -= 2;
}
break;
case 'x':
j = strftime (buf, i, __drep, t);
buf += j, i -= j;
break;
case 'X':
j = strftime (buf, i, __trep, t);
buf += j, i -= j;
break;
case 'y':
if (i >= 2)
{
sprintf (buf, "%2d", t->tm_year);
buf += 2, i -= 2;
}
break;
case 'Y':
if (i >= 4)
{
sprintf (buf, "%4d", t->tm_year + 1900);
buf += 4, i -= 4;
}
break;
case 'Z':
s = (char *) t->tm_zone;
goto scp;
break;
default:
*buf++ = *fmt, i--;
break;
}
fmt++;
}
}
*buf = 0;
return ((*fmt) ? 0 : max - i);
}