Changeset View
Changeset View
Standalone View
Standalone View
contrib/tzcode/asctime.c
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | |||||
** seven explicit spaces, two explicit colons, a newline, | ** seven explicit spaces, two explicit colons, a newline, | ||||
** and a trailing NUL byte). | ** and a trailing NUL byte). | ||||
** The values above are for systems where an int is 32 bits and are provided | ** The values above are for systems where an int is 32 bits and are provided | ||||
** as an example; the size expression below is a bound for the system at | ** as an example; the size expression below is a bound for the system at | ||||
** hand. | ** hand. | ||||
*/ | */ | ||||
static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1]; | static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1]; | ||||
/* A similar buffer for ctime. | |||||
C89 requires that they be the same buffer. | |||||
This requirement was removed in C99, so support it only if requested, | |||||
as support is more likely to lead to bugs in badly written programs. */ | |||||
#if SUPPORT_C89 | |||||
# define buf_ctime buf_asctime | |||||
#else | |||||
static char buf_ctime[sizeof buf_asctime]; | |||||
#endif | |||||
char * | char * | ||||
asctime_r(register const struct tm *timeptr, char *buf) | asctime_r(struct tm const *restrict timeptr, char *restrict buf) | ||||
{ | { | ||||
static const char wday_name[][4] = { | static const char wday_name[][4] = { | ||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | ||||
}; | }; | ||||
static const char mon_name[][4] = { | static const char mon_name[][4] = { | ||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | ||||
}; | }; | ||||
Show All 23 Lines | asctime_r(struct tm const *restrict timeptr, char *restrict buf) | ||||
** We avoid using snprintf since it's not available on all systems. | ** We avoid using snprintf since it's not available on all systems. | ||||
*/ | */ | ||||
sprintf(result, | sprintf(result, | ||||
((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), | ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), | ||||
wn, mn, | wn, mn, | ||||
timeptr->tm_mday, timeptr->tm_hour, | timeptr->tm_mday, timeptr->tm_hour, | ||||
timeptr->tm_min, timeptr->tm_sec, | timeptr->tm_min, timeptr->tm_sec, | ||||
year); | year); | ||||
if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) | if (strlen(result) < STD_ASCTIME_BUF_SIZE | ||||
|| buf == buf_ctime || buf == buf_asctime) | |||||
return strcpy(buf, result); | return strcpy(buf, result); | ||||
else { | else { | ||||
errno = EOVERFLOW; | errno = EOVERFLOW; | ||||
return NULL; | return NULL; | ||||
} | } | ||||
} | } | ||||
char * | char * | ||||
asctime(register const struct tm *timeptr) | asctime(register const struct tm *timeptr) | ||||
{ | { | ||||
return asctime_r(timeptr, buf_asctime); | return asctime_r(timeptr, buf_asctime); | ||||
} | |||||
char * | |||||
ctime_r(const time_t *timep, char *buf) | |||||
{ | |||||
struct tm mytm; | |||||
struct tm *tmp = localtime_r(timep, &mytm); | |||||
return tmp ? asctime_r(tmp, buf) : NULL; | |||||
} | |||||
char * | |||||
ctime(const time_t *timep) | |||||
{ | |||||
return ctime_r(timep, buf_ctime); | |||||
} | } |