Imported Upstream version 1.15.2
[platform/upstream/krb5.git] / src / lib / krb5 / krb / strftime.c
1 /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
2 /*      $NetBSD: strftime.c,v 1.8 1999/02/07 17:33:30 augustss Exp $    */
3
4 /*
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char *sccsid = "@(#)strftime.c   5.11 (Berkeley) 2/24/91";
40 #else
41 __RCSID("$NetBSD: strftime.c,v 1.8 1999/02/07 17:33:30 augustss Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <string.h>
46 #include <time.h>
47
48 static  int _add __P((const char *, char **, const char *));
49 static  int _conv __P((int, int, int, char **, const char *));
50 static  int _secs __P((const struct tm *, char **, const char *));
51 static  size_t _fmt __P((const char *, const struct tm *, char **,
52             const char *));
53
54 static size_t
55 strftime(s, maxsize, format, t)
56         char *s;
57         size_t maxsize;
58         const char *format;
59         const struct tm *t;
60 {
61         char *pt;
62
63         tzset();
64         if (maxsize < 1)
65                 return (0);
66
67         pt = s;
68         if (_fmt(format, t, &pt, s + maxsize)) {
69                 *pt = '\0';
70                 return (pt - s);
71         } else
72                 return (0);
73 }
74
75 #define SUN_WEEK(t)     (((t)->tm_yday + 7 - \
76                                 ((t)->tm_wday)) / 7)
77 #define MON_WEEK(t)     (((t)->tm_yday + 7 - \
78                                 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
79
80 static size_t
81 _fmt(format, t, pt, ptlim)
82         const char *format;
83         const struct tm *t;
84         char **pt;
85         const char * const ptlim;
86 {
87         for (; *format; ++format) {
88                 if (*format == '%') {
89                         ++format;
90                         if (*format == 'E') {
91                                 /* Alternate Era */
92                                 ++format;
93                         } else if (*format == 'O') {
94                                 /* Alternate numeric symbols */
95                                 ++format;
96                         }
97                         switch (*format) {
98                         case '\0':
99                                 --format;
100                                 break;
101                         case 'A':
102                                 if (t->tm_wday < 0 || t->tm_wday > 6)
103                                         return (0);
104                                 if (!_add(_CurrentTimeLocale->day[t->tm_wday],
105                                     pt, ptlim))
106                                         return (0);
107                                 continue;
108
109                         case 'a':
110                                 if (t->tm_wday < 0 || t->tm_wday > 6)
111                                         return (0);
112                                 if (!_add(_CurrentTimeLocale->abday[t->tm_wday],
113                                     pt, ptlim))
114                                         return (0);
115                                 continue;
116                         case 'B':
117                                 if (t->tm_mon < 0 || t->tm_mon > 11)
118                                         return (0);
119                                 if (!_add(_CurrentTimeLocale->mon[t->tm_mon],
120                                     pt, ptlim))
121                                         return (0);
122                                 continue;
123                         case 'b':
124                         case 'h':
125                                 if (t->tm_mon < 0 || t->tm_mon > 11)
126                                         return (0);
127                                 if (!_add(_CurrentTimeLocale->abmon[t->tm_mon],
128                                     pt, ptlim))
129                                         return (0);
130                                 continue;
131                         case 'C':
132                                 if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
133                                     2, '0', pt, ptlim))
134                                         return (0);
135                                 continue;
136                         case 'c':
137                                 if (!_fmt(_CurrentTimeLocale->d_t_fmt, t, pt,
138                                     ptlim))
139                                         return (0);
140                                 continue;
141                         case 'D':
142                                 if (!_fmt("%m/%d/%y", t, pt, ptlim))
143                                         return (0);
144                                 continue;
145                         case 'd':
146                                 if (!_conv(t->tm_mday, 2, '0', pt, ptlim))
147                                         return (0);
148                                 continue;
149                         case 'e':
150                                 if (!_conv(t->tm_mday, 2, ' ', pt, ptlim))
151                                         return (0);
152                                 continue;
153                         case 'H':
154                                 if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
155                                         return (0);
156                                 continue;
157                         case 'I':
158                                 if (!_conv(t->tm_hour % 12 ?
159                                     t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
160                                         return (0);
161                                 continue;
162                         case 'j':
163                                 if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
164                                         return (0);
165                                 continue;
166                         case 'k':
167                                 if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
168                                         return (0);
169                                 continue;
170                         case 'l':
171                                 if (!_conv(t->tm_hour % 12 ?
172                                     t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
173                                         return (0);
174                                 continue;
175                         case 'M':
176                                 if (!_conv(t->tm_min, 2, '0', pt, ptlim))
177                                         return (0);
178                                 continue;
179                         case 'm':
180                                 if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
181                                         return (0);
182                                 continue;
183                         case 'n':
184                                 if (!_add("\n", pt, ptlim))
185                                         return (0);
186                                 continue;
187                         case 'p':
188                                 if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
189                                     >= 12], pt, ptlim))
190                                         return (0);
191                                 continue;
192                         case 'R':
193                                 if (!_fmt("%H:%M", t, pt, ptlim))
194                                         return (0);
195                                 continue;
196                         case 'r':
197                                 if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
198                                     ptlim))
199                                         return (0);
200                                 continue;
201                         case 'S':
202                                 if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
203                                         return (0);
204                                 continue;
205                         case 's':
206                                 if (!_secs(t, pt, ptlim))
207                                         return (0);
208                                 continue;
209                         case 'T':
210                                 if (!_fmt("%H:%M:%S", t, pt, ptlim))
211                                         return (0);
212                                 continue;
213                         case 't':
214                                 if (!_add("\t", pt, ptlim))
215                                         return (0);
216                                 continue;
217                         case 'U':
218                                 if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
219                                         return (0);
220                                 continue;
221                         case 'u':
222                                 if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
223                                     pt, ptlim))
224                                         return (0);
225                                 continue;
226                         case 'V':       /* ISO 8601 week number */
227                         case 'G':       /* ISO 8601 year (four digits) */
228                         case 'g':       /* ISO 8601 year (two digits) */
229 /*
230 ** From Arnold Robbins' strftime version 3.0:  "the week number of the
231 ** year (the first Monday as the first day of week 1) as a decimal number
232 ** (01-53)."
233 ** (ado, 1993-05-24)
234 **
235 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
236 ** "Week 01 of a year is per definition the first week which has the
237 ** Thursday in this year, which is equivalent to the week which contains
238 ** the fourth day of January. In other words, the first week of a new year
239 ** is the week which has the majority of its days in the new year. Week 01
240 ** might also contain days from the previous year and the week before week
241 ** 01 of a year is the last week (52 or 53) of the previous year even if
242 ** it contains days from the new year. A week starts with Monday (day 1)
243 ** and ends with Sunday (day 7).  For example, the first week of the year
244 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
245 ** (ado, 1996-01-02)
246 */
247                                 {
248                                         int     year;
249                                         int     yday;
250                                         int     wday;
251                                         int     w;
252
253                                         year = t->tm_year + TM_YEAR_BASE;
254                                         yday = t->tm_yday;
255                                         wday = t->tm_wday;
256                                         for ( ; ; ) {
257                                                 int     len;
258                                                 int     bot;
259                                                 int     top;
260
261                                                 len = isleap(year) ?
262                                                         DAYSPERLYEAR :
263                                                         DAYSPERNYEAR;
264                                                 /*
265                                                 ** What yday (-3 ... 3) does
266                                                 ** the ISO year begin on?
267                                                 */
268                                                 bot = ((yday + 11 - wday) %
269                                                         DAYSPERWEEK) - 3;
270                                                 /*
271                                                 ** What yday does the NEXT
272                                                 ** ISO year begin on?
273                                                 */
274                                                 top = bot -
275                                                         (len % DAYSPERWEEK);
276                                                 if (top < -3)
277                                                         top += DAYSPERWEEK;
278                                                 top += len;
279                                                 if (yday >= top) {
280                                                         ++year;
281                                                         w = 1;
282                                                         break;
283                                                 }
284                                                 if (yday >= bot) {
285                                                         w = 1 + ((yday - bot) /
286                                                                 DAYSPERWEEK);
287                                                         break;
288                                                 }
289                                                 --year;
290                                                 yday += isleap(year) ?
291                                                         DAYSPERLYEAR :
292                                                         DAYSPERNYEAR;
293                                         }
294 #ifdef XPG4_1994_04_09
295                                         if ((w == 52
296                                              && t->tm_mon == TM_JANUARY)
297                                             || (w == 1
298                                                 && t->tm_mon == TM_DECEMBER))
299                                                 w = 53;
300 #endif /* defined XPG4_1994_04_09 */
301                                         if (*format == 'V') {
302                                                 if (!_conv(w, 2, '0',
303                                                         pt, ptlim))
304                                                         return (0);
305                                         } else if (*format == 'g') {
306                                                 if (!_conv(year % 100, 2, '0',
307                                                         pt, ptlim))
308                                                         return (0);
309                                         } else  if (!_conv(year, 4, '0',
310                                                         pt, ptlim))
311                                                         return (0);
312                                 }
313                                 continue;
314                         case 'W':
315                                 if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
316                                         return (0);
317                                 continue;
318                         case 'w':
319                                 if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
320                                         return (0);
321                                 continue;
322                         case 'x':
323                                 if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
324                                     ptlim))
325                                         return (0);
326                                 continue;
327                         case 'X':
328                                 if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
329                                     ptlim))
330                                         return (0);
331                                 continue;
332                         case 'y':
333                                 if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
334                                     2, '0', pt, ptlim))
335                                         return (0);
336                                 continue;
337                         case 'Y':
338                                 if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
339                                     pt, ptlim))
340                                         return (0);
341                                 continue;
342                         case 'Z':
343                                 if (tzname[t->tm_isdst ? 1 : 0] &&
344                                     !_add(tzname[t->tm_isdst ? 1 : 0], pt,
345                                     ptlim))
346                                         return (0);
347                                 continue;
348                         case '%':
349                         /*
350                          * X311J/88-090 (4.12.3.5): if conversion char is
351                          * undefined, behavior is undefined.  Print out the
352                          * character itself as printf(3) does.
353                          */
354                         default:
355                                 break;
356                         }
357                 }
358                 if (*pt == ptlim)
359                         return (0);
360                 *(*pt)++ = *format;
361         }
362         return (ptlim - *pt);
363 }
364
365 static int
366 _secs(t, pt, ptlim)
367         const struct tm *t;
368         char **pt;
369         const char * const ptlim;
370 {
371         char buf[15];
372         time_t s;
373         char *p;
374         struct tm tmp;
375
376         buf[sizeof (buf) - 1] = '\0';
377         /* Make a copy, mktime(3) modifies the tm struct. */
378         tmp = *t;
379         s = mktime(&tmp);
380         for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
381                 *p-- = (char)(s % 10 + '0');
382         return (_add(++p, pt, ptlim));
383 }
384
385 static int
386 _conv(n, digits, pad, pt, ptlim)
387         int n, digits;
388         int pad;
389         char **pt;
390         const char * const ptlim;
391 {
392         char buf[10];
393         char *p;
394
395         buf[sizeof (buf) - 1] = '\0';
396         for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
397                 *p-- = n % 10 + '0';
398         while (p > buf && digits-- > 0)
399                 *p-- = pad;
400         return (_add(++p, pt, ptlim));
401 }
402
403 static int
404 _add(str, pt, ptlim)
405         const char *str;
406         char **pt;
407         const char * const ptlim;
408 {
409
410         for (;; ++(*pt)) {
411                 if (*pt == ptlim)
412                         return (0);
413                 if ((**pt = *str++) == '\0')
414                         return (1);
415         }
416 }