mirror of
https://frontier.innolan.net/rainlance/amiga-tz.git
synced 2025-11-23 13:48:52 +00:00
SCCS-file: asctime.c
SCCS-SID: 7.16
This commit is contained in:
committed by
Paul Eggert
parent
f9a007fe6e
commit
c3aa8c70e0
78
asctime.c
78
asctime.c
@ -14,7 +14,45 @@ static char elsieid[] = "%W%";
|
||||
#include "private.h"
|
||||
#include "tzfile.h"
|
||||
|
||||
#define STANDARD_BUFFER_SIZE 26
|
||||
#if STRICTLY_STANDARD_ASCTIME
|
||||
#define ASCTIME_FMT "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
|
||||
#define ASCTIME_FMT_B ASCTIME_FMT
|
||||
#else /* !STRICTLY_STANDARD_ASCTIME */
|
||||
/*
|
||||
** Some systems only handle "%.2d"; others only handle "%02d";
|
||||
** "%02.2d" makes (most) everybody happy.
|
||||
*/
|
||||
/*
|
||||
** All years associated with 32-bit time_t values are exactly four digits long;
|
||||
** some years associated with 64-bit time_t values are not.
|
||||
** Vintage programs are coded for years that are always four digits long
|
||||
** and may assume that the newline always lands in the same place.
|
||||
** For years that are less than four digits, we pad the output with
|
||||
** spaces before the newline to get the newline in the traditional place.
|
||||
*/
|
||||
#define ASCTIME_FMT "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4ld\n"
|
||||
/*
|
||||
** For years that are more than four digits we put extra spaces before the year
|
||||
** so that code trying to overwrite the newline won't end up overwriting
|
||||
** a digit within a year and truncating the year (operating on the assumption
|
||||
** that no output is better than wrong output).
|
||||
*/
|
||||
#define ASCTIME_FMT_B "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %ld\n"
|
||||
#endif /* !STRICTLY_STANDARD_ASCTIME */
|
||||
|
||||
#define STD_ASCTIME_BUF_SIZE 26
|
||||
/*
|
||||
** Big enough for something such as
|
||||
** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
|
||||
** (two three-character abbreviations, five strings denoting integers,
|
||||
** seven explicit spaces, two explicit colons, a newline,
|
||||
** and a trailing ASCII nul).
|
||||
** The values above are for systems where an int is 32 bits and are provided
|
||||
** as an example; the define below calculates the maximum for system at hand.
|
||||
*/
|
||||
#define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
|
||||
|
||||
static char buf_asctime[MAX_ASCTIME_BUF_SIZE];
|
||||
|
||||
/*
|
||||
** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
|
||||
@ -34,15 +72,8 @@ char * buf;
|
||||
};
|
||||
register const char * wn;
|
||||
register const char * mn;
|
||||
/*
|
||||
** Big enough for something such as
|
||||
** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
|
||||
** (two three-character abbreviations, five strings denoting integers,
|
||||
** three explicit spaces, two explicit colons, a newline,
|
||||
** and a trailing ASCII nul).
|
||||
*/
|
||||
char result[2 * 3 + 5 * INT_STRLEN_MAXIMUM(int) +
|
||||
3 + 2 + 1 + 1];
|
||||
long year;
|
||||
char result[MAX_ASCTIME_BUF_SIZE];
|
||||
|
||||
if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
|
||||
wn = "???";
|
||||
@ -50,29 +81,22 @@ char * buf;
|
||||
if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
|
||||
mn = "???";
|
||||
else mn = mon_name[timeptr->tm_mon];
|
||||
/*
|
||||
** The format used in the (2004) standard is
|
||||
** "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
|
||||
** Some systems only handle "%.2d"; others only handle "%02d";
|
||||
** "%02.2d" makes (most) everybody happy.
|
||||
** All years associated with 32-bit time_t values are exactly
|
||||
** four digits long; some years associated with 64-bit time_t
|
||||
** values are not four digits long so we throw in the 4 below.
|
||||
*/
|
||||
year = timeptr->tm_year + (long) TM_YEAR_BASE;
|
||||
/*
|
||||
** We avoid using snprintf since it's not available on all systems.
|
||||
*/
|
||||
(void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %4ld\n",
|
||||
(void) sprintf(result,
|
||||
((year >= -999 && year <= 9999) ? ASCTIME_FMT : ASCTIME_FMT_B),
|
||||
wn, mn,
|
||||
timeptr->tm_mday, timeptr->tm_hour,
|
||||
timeptr->tm_min, timeptr->tm_sec,
|
||||
timeptr->tm_year + (long) TM_YEAR_BASE);
|
||||
if (strlen(result) >= STANDARD_BUFFER_SIZE) {
|
||||
errno = EOVERFLOW;
|
||||
return NULL;
|
||||
} else {
|
||||
year);
|
||||
if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) {
|
||||
(void) strcpy(buf, result);
|
||||
return buf;
|
||||
} else {
|
||||
errno = EOVERFLOW;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +108,5 @@ char *
|
||||
asctime(timeptr)
|
||||
register const struct tm * timeptr;
|
||||
{
|
||||
static char result[STANDARD_BUFFER_SIZE];
|
||||
|
||||
return asctime_r(timeptr, result);
|
||||
return asctime_r(timeptr, buf_asctime);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user