amiga-tz/library/time_asctime.c

71 lines
1.7 KiB
C

#include "time_header.h"
#include "time_local.h"
#define STD_ASCTIME_BUF_SIZE 26
#define MAX_ASCTIME_BUF_SIZE 64
static char buf_asctime[MAX_ASCTIME_BUF_SIZE];
static const unsigned long _fmtcallback[] = { 0x16c04e75 };
struct out {
const char* wn;
const char* mn;
int16_t day;
int16_t hour;
int16_t min;
int16_t sec;
const char* year;
APTR end;
};
char* asctime_r(const struct tm *timeptr, char *buf)
{
static const char *fmt1 = "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n";
static const char *fmt2 = "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %s\n";
struct out out;
char year[6];
char result[MAX_ASCTIME_BUF_SIZE];
if (timeptr == NULL) {
errno = EINVAL;
return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
}
if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
out.wn = "???";
else
out.wn = C_time_locale.wday[timeptr->tm_wday];
if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
out.mn = "???";
else
out.mn = C_time_locale.mon[timeptr->tm_mon];
strftime(year, 6, "%Y", timeptr);
out.day = (int16_t)timeptr->tm_mday;
out.hour = (int16_t)timeptr->tm_hour;
out.min = (int16_t)timeptr->tm_min;
out.sec = (int16_t)timeptr->tm_sec;
out.year = year;
out.end = NULL;
RawDoFmt(
(APTR)((strlen(year) <= 4) ? fmt1 : fmt2),
(APTR)&out, (void (*)(void))&_fmtcallback, result);
if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) {
return strcpy(buf, result);
} else {
errno = EOVERFLOW;
return NULL;
}
}
char* asctime(const struct tm *timeptr)
{
return asctime_r(timeptr, buf_asctime);
}