source: git/src/strftime.c @ 0ed32e5

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 0ed32e5 was 3d658d0, checked in by Olly Betts <olly@…>, 19 years ago

Fixed compiler warnings.

git-svn-id: file:///home/survex-svn/survex/trunk@2788 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/*
2 * strftime.c
3 *
4 * Public-domain relatively quick-and-dirty implemenation of
5 * ANSI library routine for System V Unix systems.
6 *
7 * It's written in old-style C for maximal portability.
8 * However, since I'm used to prototypes, I've included them too.
9 *
10 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
11 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
12 * For complete POSIX semantics, add POSIX_SEMANTICS.
13 *
14 * The code for %c, %x, and %X is my best guess as to what's "appropriate".
15 * This version ignores LOCALE information.
16 * It also doesn't worry about multi-byte characters.
17 * So there.
18 *
19 * Arnold Robbins
20 * January, February, March, 1991
21 * Updated March 1992
22 *
23 * Fixes from ado@elsie.nci.nih.gov
24 * February 1991
25 *
26 * Fixed compiler warnings from GCC
27 * Olly Betts 2005-01-12
28 */
29
30#include <stdio.h>
31#include <ctype.h>
32#include <string.h>
33#include <time.h>
34#include <sys/types.h>
35
36#ifndef __STDC__
37#define const   /**/
38
39extern void *malloc();
40extern void *realloc();
41extern void tzset();
42extern char *strchr();
43extern char *getenv();
44static int weeknumber();
45#else
46extern void *malloc(unsigned count);
47extern void *realloc(void *ptr, unsigned count);
48extern void tzset(void);
49extern char *strchr(const char *str, int ch);
50extern char *getenv(const char *v);
51static int weeknumber(const struct tm *timeptr, int firstweekday);
52#endif
53
54#ifdef __GNUC__
55#define inline  __inline__
56#else
57#define inline  /**/
58#endif
59
60#define range(low, item, hi)    max(low, min(item, hi))
61
62extern char *tzname[2];
63extern int daylight;
64
65#define SYSV_EXT        1       /* stuff in System V ascftime routine */
66#define POSIX2_DATE     1       /* stuff in Posix 1003.2 date command */
67#define VMS_EXT         1       /* include %V for VMS date format */
68#define POSIX_SEMANTICS 1       /* call tzset() if TZ changes */
69
70#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
71#define SYSV_EXT        1
72#endif
73
74/* min --- return minimum of two numbers */
75
76#ifndef __STDC__
77static inline int
78min(a, b)
79int a, b;
80#else
81static inline int
82min(int a, int b)
83#endif
84{
85        return (a < b ? a : b);
86}
87
88/* max --- return maximum of two numbers */
89
90#ifndef __STDC__
91static inline int
92max(a, b)
93int a, b;
94#else
95static inline int
96max(int a, int b)
97#endif
98{
99        return (a > b ? a : b);
100}
101
102/* strftime --- produce formatted time */
103
104#ifndef __STDC__
105size_t
106strftime(s, maxsize, format, timeptr)
107char *s;
108size_t maxsize;
109const char *format;
110const struct tm *timeptr;
111#else
112size_t
113strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
114#endif
115{
116        char *endp = s + maxsize;
117        char *start = s;
118        char tbuf[100];
119        int i;
120        static short first = 1;
121        static char *savetz = NULL;
122        static int savetzlen = 0;
123        char *tz;
124        int tzlen;
125
126        /* various tables, useful in North America */
127        static const char *days_a[] = {
128                "Sun", "Mon", "Tue", "Wed",
129                "Thu", "Fri", "Sat",
130        };
131        static const char *days_l[] = {
132                "Sunday", "Monday", "Tuesday", "Wednesday",
133                "Thursday", "Friday", "Saturday",
134        };
135        static const char *months_a[] = {
136                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
137                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
138        };
139        static const char *months_l[] = {
140                "January", "February", "March", "April",
141                "May", "June", "July", "August", "September",
142                "October", "November", "December",
143        };
144        static const char *ampm[] = { "AM", "PM", };
145
146        if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
147                return 0;
148
149        if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
150                return 0;
151
152#ifndef POSIX_SEMANTICS
153        if (first) {
154                tzset();
155                first = 0;
156        }
157#else   /* POSIX_SEMANTICS */
158        tz = getenv("TZ");
159        tzlen = strlen(tz);
160        if (first) {
161                if (tz != NULL) {
162                        savetz = (char *) malloc(tzlen + 1);
163                        if (savetz != NULL) {
164                                savetzlen = tzlen + 1;
165                                strcpy(savetz, tz);
166                        }
167                }
168                tzset();
169                first = 0;
170        }
171        /* if we have a saved TZ, and it is different, recapture and reset */
172        if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
173                i = strlen(tz) + 1;
174                if (i > savetzlen) {
175                        savetz = (char *) realloc(savetz, i);
176                        if (savetz) {
177                                savetzlen = i;
178                                strcpy(savetz, tz);
179                        }
180                } else
181                        strcpy(savetz, tz);
182                tzset();
183        }
184#endif /* POSIX_SEMANTICS */
185
186        for (; *format && s < endp - 1; format++) {
187                tbuf[0] = '\0';
188                if (*format != '%') {
189                        *s++ = *format;
190                        continue;
191                }
192        again:
193                switch (*++format) {
194                case '\0':
195                        *s++ = '%';
196                        goto out;
197
198                case '%':
199                        *s++ = '%';
200                        continue;
201
202                case 'a':       /* abbreviated weekday name */
203                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
204                                strcpy(tbuf, "?");
205                        else
206                                strcpy(tbuf, days_a[timeptr->tm_wday]);
207                        break;
208
209                case 'A':       /* full weekday name */
210                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
211                                strcpy(tbuf, "?");
212                        else
213                                strcpy(tbuf, days_l[timeptr->tm_wday]);
214                        break;
215
216#ifdef SYSV_EXT
217                case 'h':       /* abbreviated month name */
218#endif
219                case 'b':       /* abbreviated month name */
220                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
221                                strcpy(tbuf, "?");
222                        else
223                                strcpy(tbuf, months_a[timeptr->tm_mon]);
224                        break;
225
226                case 'B':       /* full month name */
227                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
228                                strcpy(tbuf, "?");
229                        else
230                                strcpy(tbuf, months_l[timeptr->tm_mon]);
231                        break;
232
233                case 'c':       /* appropriate date and time representation */
234                        sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
235                                days_a[range(0, timeptr->tm_wday, 6)],
236                                months_a[range(0, timeptr->tm_mon, 11)],
237                                range(1, timeptr->tm_mday, 31),
238                                range(0, timeptr->tm_hour, 23),
239                                range(0, timeptr->tm_min, 59),
240                                range(0, timeptr->tm_sec, 61),
241                                timeptr->tm_year + 1900);
242                        break;
243
244                case 'd':       /* day of the month, 01 - 31 */
245                        i = range(1, timeptr->tm_mday, 31);
246                        sprintf(tbuf, "%02d", i);
247                        break;
248
249                case 'H':       /* hour, 24-hour clock, 00 - 23 */
250                        i = range(0, timeptr->tm_hour, 23);
251                        sprintf(tbuf, "%02d", i);
252                        break;
253
254                case 'I':       /* hour, 12-hour clock, 01 - 12 */
255                        i = range(0, timeptr->tm_hour, 23);
256                        if (i == 0)
257                                i = 12;
258                        else if (i > 12)
259                                i -= 12;
260                        sprintf(tbuf, "%02d", i);
261                        break;
262
263                case 'j':       /* day of the year, 001 - 366 */
264                        sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
265                        break;
266
267                case 'm':       /* month, 01 - 12 */
268                        i = range(0, timeptr->tm_mon, 11);
269                        sprintf(tbuf, "%02d", i + 1);
270                        break;
271
272                case 'M':       /* minute, 00 - 59 */
273                        i = range(0, timeptr->tm_min, 59);
274                        sprintf(tbuf, "%02d", i);
275                        break;
276
277                case 'p':       /* am or pm based on 12-hour clock */
278                        i = range(0, timeptr->tm_hour, 23);
279                        if (i < 12)
280                                strcpy(tbuf, ampm[0]);
281                        else
282                                strcpy(tbuf, ampm[1]);
283                        break;
284
285                case 'S':       /* second, 00 - 61 */
286                        i = range(0, timeptr->tm_sec, 61);
287                        sprintf(tbuf, "%02d", i);
288                        break;
289
290                case 'U':       /* week of year, Sunday is first day of week */
291                        sprintf(tbuf, "%d", weeknumber(timeptr, 0));
292                        break;
293
294                case 'w':       /* weekday, Sunday == 0, 0 - 6 */
295                        i = range(0, timeptr->tm_wday, 6);
296                        sprintf(tbuf, "%d", i);
297                        break;
298
299                case 'W':       /* week of year, Monday is first day of week */
300                        sprintf(tbuf, "%d", weeknumber(timeptr, 1));
301                        break;
302
303                case 'x':       /* appropriate date representation */
304                        sprintf(tbuf, "%s %s %2d %d",
305                                days_a[range(0, timeptr->tm_wday, 6)],
306                                months_a[range(0, timeptr->tm_mon, 11)],
307                                range(1, timeptr->tm_mday, 31),
308                                timeptr->tm_year + 1900);
309                        break;
310
311                case 'X':       /* appropriate time representation */
312                        sprintf(tbuf, "%02d:%02d:%02d",
313                                range(0, timeptr->tm_hour, 23),
314                                range(0, timeptr->tm_min, 59),
315                                range(0, timeptr->tm_sec, 61));
316                        break;
317
318                case 'y':       /* year without a century, 00 - 99 */
319                        i = timeptr->tm_year % 100;
320                        sprintf(tbuf, "%d", i);
321                        break;
322
323                case 'Y':       /* year with century */
324                        sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
325                        break;
326
327                case 'Z':       /* time zone name or abbrevation */
328                        i = 0;
329                        if (daylight && timeptr->tm_isdst)
330                                i = 1;
331                        strcpy(tbuf, tzname[i]);
332                        break;
333
334#ifdef SYSV_EXT
335                case 'n':       /* same as \n */
336                        tbuf[0] = '\n';
337                        tbuf[1] = '\0';
338                        break;
339
340                case 't':       /* same as \t */
341                        tbuf[0] = '\t';
342                        tbuf[1] = '\0';
343                        break;
344
345                case 'D':       /* date as %m/%d/%y */
346                        strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
347                        break;
348
349                case 'e':       /* day of month, blank padded */
350                        sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
351                        break;
352
353                case 'r':       /* time as %I:%M:%S %p */
354                        strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
355                        break;
356
357                case 'R':       /* time as %H:%M */
358                        strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
359                        break;
360
361                case 'T':       /* time as %H:%M:%S */
362                        strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
363                        break;
364#endif
365
366
367#ifdef VMS_EXT
368                case 'V':       /* date as dd-bbb-YYYY */
369                        sprintf(tbuf, "%2d-%3.3s-%4d",
370                                range(1, timeptr->tm_mday, 31),
371                                months_a[range(0, timeptr->tm_mon, 11)],
372                                timeptr->tm_year + 1900);
373                        for (i = 3; i < 6; i++)
374                                if (islower(tbuf[i]))
375                                        tbuf[i] = toupper(tbuf[i]);
376                        break;
377#endif
378
379
380#ifdef POSIX2_DATE
381                case 'C':
382                        sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
383                        break;
384
385
386                case 'E':
387                case 'O':
388                        /* POSIX locale extensions, ignored for now */
389                        goto again;
390#endif
391                default:
392                        tbuf[0] = '%';
393                        tbuf[1] = *format;
394                        tbuf[2] = '\0';
395                        break;
396                }
397                i = strlen(tbuf);
398                if (i) {
399                        if (s + i < endp - 1) {
400                                strcpy(s, tbuf);
401                                s += i;
402                        } else
403                                return 0;
404                }
405        }
406out:
407        if (s < endp && *format == '\0') {
408                *s = '\0';
409                return (s - start);
410        } else
411                return 0;
412}
413
414/* weeknumber --- figure how many weeks into the year */
415
416/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
417/* Modified byMichal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK> */
418
419#ifndef __STDC__
420static int
421weeknumber(timeptr, firstweekday)
422const struct tm *timeptr;
423int firstweekday;
424#else
425static int
426weeknumber(const struct tm *timeptr, int firstweekday)
427#endif
428/*
429 * firstweekday is 0 if starting in Sunday, non-zero if in Monday
430 */
431{
432    return (timeptr->tm_yday - timeptr->tm_wday +
433            (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
434}
Note: See TracBrowser for help on using the repository browser.