sfxge: suppress set but not used warnings
[unleashed.git] / contrib / tzcode / asctime.c
blob85ebf8820adf83f84796b58f2ce73899cd80f081
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
4 */
6 /*
7 ** Avoid the temptation to punt entirely to strftime;
8 ** the output of strftime is supposed to be locale specific
9 ** whereas the output of asctime is supposed to be constant.
12 /*LINTLIBRARY*/
14 #include "private.h"
17 ** Some systems only handle "%.2d"; others only handle "%02d";
18 ** "%02.2d" makes (most) everybody happy.
19 ** At least some versions of gcc warn about the %02.2d;
20 ** we conditionalize below to avoid the warning.
23 ** All years associated with 32-bit time_t values are exactly four digits long;
24 ** some years associated with 64-bit time_t values are not.
25 ** Vintage programs are coded for years that are always four digits long
26 ** and may assume that the newline always lands in the same place.
27 ** For years that are less than four digits, we pad the output with
28 ** leading zeroes to get the newline in the traditional place.
29 ** The -4 ensures that we get four characters of output even if
30 ** we call a strftime variant that produces fewer characters for some years.
31 ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
32 ** but many implementations pad anyway; most likely the standards are buggy.
34 #ifdef __GNUC__
35 #define ASCTIME_FMT "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
36 #else /* !defined __GNUC__ */
37 #define ASCTIME_FMT "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
38 #endif /* !defined __GNUC__ */
40 ** For years that are more than four digits we put extra spaces before the year
41 ** so that code trying to overwrite the newline won't end up overwriting
42 ** a digit within a year and truncating the year (operating on the assumption
43 ** that no output is better than wrong output).
45 #ifdef __GNUC__
46 #define ASCTIME_FMT_B "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %s\n"
47 #else /* !defined __GNUC__ */
48 #define ASCTIME_FMT_B "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %s\n"
49 #endif /* !defined __GNUC__ */
51 #define STD_ASCTIME_BUF_SIZE 26
53 ** Big enough for something such as
54 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
55 ** (two three-character abbreviations, five strings denoting integers,
56 ** seven explicit spaces, two explicit colons, a newline,
57 ** and a trailing NUL byte).
58 ** The values above are for systems where an int is 32 bits and are provided
59 ** as an example; the define below calculates the maximum for the system at
60 ** hand.
62 #define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
64 static char buf_asctime[MAX_ASCTIME_BUF_SIZE];
67 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
70 char *
71 asctime_r(register const struct tm *timeptr, char *buf)
73 static const char wday_name[][3] = {
74 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
76 static const char mon_name[][3] = {
77 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
80 register const char * wn;
81 register const char * mn;
82 char year[INT_STRLEN_MAXIMUM(int) + 2];
83 char result[MAX_ASCTIME_BUF_SIZE];
85 if (timeptr == NULL) {
86 errno = EINVAL;
87 return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
89 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
90 wn = "???";
91 else wn = wday_name[timeptr->tm_wday];
92 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
93 mn = "???";
94 else mn = mon_name[timeptr->tm_mon];
96 ** Use strftime's %Y to generate the year, to avoid overflow problems
97 ** when computing timeptr->tm_year + TM_YEAR_BASE.
98 ** Assume that strftime is unaffected by other out-of-range members
99 ** (e.g., timeptr->tm_mday) when processing "%Y".
101 strftime(year, sizeof year, "%Y", timeptr);
103 ** We avoid using snprintf since it's not available on all systems.
105 sprintf(result,
106 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
107 wn, mn,
108 timeptr->tm_mday, timeptr->tm_hour,
109 timeptr->tm_min, timeptr->tm_sec,
110 year);
111 if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
112 return strcpy(buf, result);
113 else {
114 errno = EOVERFLOW;
115 return NULL;
120 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
123 char *
124 asctime(register const struct tm *timeptr)
126 return asctime_r(timeptr, buf_asctime);