strftime.c revision 1.18.14.2 1 1.18.14.2 ginsbach /* $NetBSD: strftime.c,v 1.18.14.2 2007/07/31 20:19:17 ginsbach Exp $ */
2 1.18.14.2 ginsbach
3 1.18.14.2 ginsbach #include <sys/cdefs.h>
4 1.18.14.2 ginsbach #if defined(LIBC_SCCS) && !defined(lint)
5 1.18.14.2 ginsbach #if 0
6 1.18.14.2 ginsbach static char elsieid[] = "@(#)strftime.c 7.64";
7 1.18.14.2 ginsbach #else
8 1.18.14.2 ginsbach __RCSID("$NetBSD: strftime.c,v 1.18.14.2 2007/07/31 20:19:17 ginsbach Exp $");
9 1.18.14.2 ginsbach #endif
10 1.18.14.2 ginsbach #endif /* LIBC_SCCS and not lint */
11 1.18.14.2 ginsbach
12 1.18.14.2 ginsbach #include "namespace.h"
13 1.18.14.2 ginsbach
14 1.18.14.2 ginsbach /*
15 1.18.14.2 ginsbach ** Based on the UCB version with the ID appearing below.
16 1.18.14.2 ginsbach ** This is ANSIish only when "multibyte character == plain character".
17 1.18.14.2 ginsbach */
18 1.18.14.2 ginsbach
19 1.18.14.2 ginsbach #include "private.h"
20 1.18.14.2 ginsbach
21 1.18.14.2 ginsbach /*
22 1.18.14.2 ginsbach ** We don't use these extensions in strftime operation even when
23 1.18.14.2 ginsbach ** supported by the local tzcode configuration. A strictly
24 1.18.14.2 ginsbach ** conforming C application may leave them in undefined state.
25 1.18.14.2 ginsbach */
26 1.18.14.2 ginsbach
27 1.18.14.2 ginsbach #ifdef _LIBC
28 1.18.14.2 ginsbach #undef TM_ZONE
29 1.18.14.2 ginsbach #undef TM_GMTOFF
30 1.18.14.2 ginsbach #endif
31 1.18.14.2 ginsbach
32 1.18.14.2 ginsbach /*
33 1.18.14.2 ginsbach ** Copyright (c) 1989, 1993
34 1.18.14.2 ginsbach ** The Regents of the University of California. All rights reserved.
35 1.18.14.2 ginsbach **
36 1.18.14.2 ginsbach ** Redistribution and use in source and binary forms, with or without
37 1.18.14.2 ginsbach ** modification, are permitted provided that the following conditions
38 1.18.14.2 ginsbach ** are met:
39 1.18.14.2 ginsbach ** 1. Redistributions of source code must retain the above copyright
40 1.18.14.2 ginsbach ** notice, this list of conditions and the following disclaimer.
41 1.18.14.2 ginsbach ** 2. Redistributions in binary form must reproduce the above copyright
42 1.18.14.2 ginsbach ** notice, this list of conditions and the following disclaimer in the
43 1.18.14.2 ginsbach ** documentation and/or other materials provided with the distribution.
44 1.18.14.2 ginsbach ** 3. All advertising materials mentioning features or use of this software
45 1.18.14.2 ginsbach ** must display the following acknowledgement:
46 1.18.14.2 ginsbach ** This product includes software developed by the University of
47 1.18.14.2 ginsbach ** California, Berkeley and its contributors.
48 1.18.14.2 ginsbach ** 4. Neither the name of the University nor the names of its contributors
49 1.18.14.2 ginsbach ** may be used to endorse or promote products derived from this software
50 1.18.14.2 ginsbach ** without specific prior written permission.
51 1.18.14.2 ginsbach **
52 1.18.14.2 ginsbach ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.18.14.2 ginsbach ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.18.14.2 ginsbach ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.18.14.2 ginsbach ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.18.14.2 ginsbach ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.18.14.2 ginsbach ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.18.14.2 ginsbach ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.18.14.2 ginsbach ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.18.14.2 ginsbach ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.18.14.2 ginsbach ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.18.14.2 ginsbach ** SUCH DAMAGE.
63 1.18.14.2 ginsbach */
64 1.18.14.2 ginsbach
65 1.18.14.2 ginsbach #ifndef LIBC_SCCS
66 1.18.14.2 ginsbach #ifndef lint
67 1.18.14.2 ginsbach static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
68 1.18.14.2 ginsbach #endif /* !defined lint */
69 1.18.14.2 ginsbach #endif /* !defined LIBC_SCCS */
70 1.18.14.2 ginsbach
71 1.18.14.2 ginsbach #include "tzfile.h"
72 1.18.14.2 ginsbach #include "fcntl.h"
73 1.18.14.2 ginsbach #include "locale.h"
74 1.18.14.2 ginsbach
75 1.18.14.2 ginsbach #include "sys/localedef.h"
76 1.18.14.2 ginsbach #define Locale _CurrentTimeLocale
77 1.18.14.2 ginsbach
78 1.18.14.2 ginsbach static char * _add P((const char *, char *, const char *));
79 1.18.14.2 ginsbach static char * _conv P((int, const char *, char *, const char *));
80 1.18.14.2 ginsbach static char * _fmt P((const char *, const struct tm *, char *, const char *, int *));
81 1.18.14.2 ginsbach
82 1.18.14.2 ginsbach #define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
83 1.18.14.2 ginsbach
84 1.18.14.2 ginsbach #ifndef YEAR_2000_NAME
85 1.18.14.2 ginsbach #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
86 1.18.14.2 ginsbach #endif /* !defined YEAR_2000_NAME */
87 1.18.14.2 ginsbach
88 1.18.14.2 ginsbach
89 1.18.14.2 ginsbach #define IN_NONE 0
90 1.18.14.2 ginsbach #define IN_SOME 1
91 1.18.14.2 ginsbach #define IN_THIS 2
92 1.18.14.2 ginsbach #define IN_ALL 3
93 1.18.14.2 ginsbach
94 1.18.14.2 ginsbach size_t
95 1.18.14.2 ginsbach strftime(s, maxsize, format, t)
96 1.18.14.2 ginsbach char * const s;
97 1.18.14.2 ginsbach const size_t maxsize;
98 1.18.14.2 ginsbach const char * const format;
99 1.18.14.2 ginsbach const struct tm * const t;
100 1.18.14.2 ginsbach {
101 1.18.14.2 ginsbach char * p;
102 1.18.14.2 ginsbach int warn;
103 1.18.14.2 ginsbach
104 1.18.14.2 ginsbach tzset();
105 1.18.14.2 ginsbach warn = IN_NONE;
106 1.18.14.2 ginsbach p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
107 1.18.14.2 ginsbach #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
108 1.18.14.2 ginsbach if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
109 1.18.14.2 ginsbach (void) fprintf(stderr, "\n");
110 1.18.14.2 ginsbach if (format == NULL)
111 1.18.14.2 ginsbach (void) fprintf(stderr, "NULL strftime format ");
112 1.18.14.2 ginsbach else (void) fprintf(stderr, "strftime format \"%s\" ",
113 1.18.14.2 ginsbach format);
114 1.18.14.2 ginsbach (void) fprintf(stderr, "yields only two digits of years in ");
115 1.18.14.2 ginsbach if (warn == IN_SOME)
116 1.18.14.2 ginsbach (void) fprintf(stderr, "some locales");
117 1.18.14.2 ginsbach else if (warn == IN_THIS)
118 1.18.14.2 ginsbach (void) fprintf(stderr, "the current locale");
119 1.18.14.2 ginsbach else (void) fprintf(stderr, "all locales");
120 1.18.14.2 ginsbach (void) fprintf(stderr, "\n");
121 1.18.14.2 ginsbach }
122 1.18.14.2 ginsbach #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
123 1.18.14.2 ginsbach if (p == s + maxsize)
124 1.18.14.2 ginsbach return 0;
125 1.18.14.2 ginsbach *p = '\0';
126 1.18.14.2 ginsbach return p - s;
127 1.18.14.2 ginsbach }
128 1.18.14.2 ginsbach
129 1.18.14.2 ginsbach static char *
130 1.18.14.2 ginsbach _fmt(format, t, pt, ptlim, warnp)
131 1.18.14.2 ginsbach const char * format;
132 1.18.14.2 ginsbach const struct tm * const t;
133 1.18.14.2 ginsbach char * pt;
134 1.18.14.2 ginsbach const char * const ptlim;
135 1.18.14.2 ginsbach int * warnp;
136 1.18.14.2 ginsbach {
137 1.18.14.2 ginsbach for ( ; *format; ++format) {
138 1.18.14.2 ginsbach if (*format == '%') {
139 1.18.14.2 ginsbach label:
140 1.18.14.2 ginsbach switch (*++format) {
141 1.18.14.2 ginsbach case '\0':
142 1.18.14.2 ginsbach --format;
143 1.18.14.2 ginsbach break;
144 1.18.14.2 ginsbach case 'A':
145 1.18.14.2 ginsbach pt = _add((t->tm_wday < 0 ||
146 1.18.14.2 ginsbach t->tm_wday >= DAYSPERWEEK) ?
147 1.18.14.2 ginsbach "?" : Locale->day[t->tm_wday],
148 1.18.14.2 ginsbach pt, ptlim);
149 1.18.14.2 ginsbach continue;
150 1.18.14.2 ginsbach case 'a':
151 1.18.14.2 ginsbach pt = _add((t->tm_wday < 0 ||
152 1.18.14.2 ginsbach t->tm_wday >= DAYSPERWEEK) ?
153 1.18.14.2 ginsbach "?" : Locale->abday[t->tm_wday],
154 1.18.14.2 ginsbach pt, ptlim);
155 1.18.14.2 ginsbach continue;
156 1.18.14.2 ginsbach case 'B':
157 1.18.14.2 ginsbach pt = _add((t->tm_mon < 0 ||
158 1.18.14.2 ginsbach t->tm_mon >= MONSPERYEAR) ?
159 1.18.14.2 ginsbach "?" : Locale->mon[t->tm_mon],
160 1.18.14.2 ginsbach pt, ptlim);
161 1.18.14.2 ginsbach continue;
162 1.18.14.2 ginsbach case 'b':
163 1.18.14.2 ginsbach case 'h':
164 1.18.14.2 ginsbach pt = _add((t->tm_mon < 0 ||
165 1.18.14.2 ginsbach t->tm_mon >= MONSPERYEAR) ?
166 1.18.14.2 ginsbach "?" : Locale->abmon[t->tm_mon],
167 1.18.14.2 ginsbach pt, ptlim);
168 1.18.14.2 ginsbach continue;
169 1.18.14.2 ginsbach case 'C':
170 1.18.14.2 ginsbach /*
171 1.18.14.2 ginsbach ** %C used to do a...
172 1.18.14.2 ginsbach ** _fmt("%a %b %e %X %Y", t);
173 1.18.14.2 ginsbach ** ...whereas now POSIX 1003.2 calls for
174 1.18.14.2 ginsbach ** something completely different.
175 1.18.14.2 ginsbach ** (ado, 1993-05-24)
176 1.18.14.2 ginsbach */
177 1.18.14.2 ginsbach pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
178 1.18.14.2 ginsbach "%02d", pt, ptlim);
179 1.18.14.2 ginsbach continue;
180 1.18.14.2 ginsbach case 'c':
181 1.18.14.2 ginsbach {
182 1.18.14.2 ginsbach int warn2 = IN_SOME;
183 1.18.14.2 ginsbach
184 1.18.14.2 ginsbach pt = _fmt(Locale->d_t_fmt, t, pt, ptlim, &warn2);
185 1.18.14.2 ginsbach if (warn2 == IN_ALL)
186 1.18.14.2 ginsbach warn2 = IN_THIS;
187 1.18.14.2 ginsbach if (warn2 > *warnp)
188 1.18.14.2 ginsbach *warnp = warn2;
189 1.18.14.2 ginsbach }
190 1.18.14.2 ginsbach continue;
191 1.18.14.2 ginsbach case 'D':
192 1.18.14.2 ginsbach pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
193 1.18.14.2 ginsbach continue;
194 1.18.14.2 ginsbach case 'd':
195 1.18.14.2 ginsbach pt = _conv(t->tm_mday, "%02d", pt, ptlim);
196 1.18.14.2 ginsbach continue;
197 1.18.14.2 ginsbach case 'E':
198 1.18.14.2 ginsbach case 'O':
199 1.18.14.2 ginsbach /*
200 1.18.14.2 ginsbach ** C99 locale modifiers.
201 1.18.14.2 ginsbach ** The sequences
202 1.18.14.2 ginsbach ** %Ec %EC %Ex %EX %Ey %EY
203 1.18.14.2 ginsbach ** %Od %oe %OH %OI %Om %OM
204 1.18.14.2 ginsbach ** %OS %Ou %OU %OV %Ow %OW %Oy
205 1.18.14.2 ginsbach ** are supposed to provide alternate
206 1.18.14.2 ginsbach ** representations.
207 1.18.14.2 ginsbach */
208 1.18.14.2 ginsbach goto label;
209 1.18.14.2 ginsbach case 'e':
210 1.18.14.2 ginsbach pt = _conv(t->tm_mday, "%2d", pt, ptlim);
211 1.18.14.2 ginsbach continue;
212 1.18.14.2 ginsbach case 'F':
213 1.18.14.2 ginsbach pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
214 1.18.14.2 ginsbach continue;
215 1.18.14.2 ginsbach case 'H':
216 1.18.14.2 ginsbach pt = _conv(t->tm_hour, "%02d", pt, ptlim);
217 1.18.14.2 ginsbach continue;
218 1.18.14.2 ginsbach case 'I':
219 1.18.14.2 ginsbach pt = _conv((t->tm_hour % 12) ?
220 1.18.14.2 ginsbach (t->tm_hour % 12) : 12,
221 1.18.14.2 ginsbach "%02d", pt, ptlim);
222 1.18.14.2 ginsbach continue;
223 1.18.14.2 ginsbach case 'j':
224 1.18.14.2 ginsbach pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
225 1.18.14.2 ginsbach continue;
226 1.18.14.2 ginsbach case 'k':
227 1.18.14.2 ginsbach /*
228 1.18.14.2 ginsbach ** This used to be...
229 1.18.14.2 ginsbach ** _conv(t->tm_hour % 12 ?
230 1.18.14.2 ginsbach ** t->tm_hour % 12 : 12, 2, ' ');
231 1.18.14.2 ginsbach ** ...and has been changed to the below to
232 1.18.14.2 ginsbach ** match SunOS 4.1.1 and Arnold Robbins'
233 1.18.14.2 ginsbach ** strftime version 3.0. That is, "%k" and
234 1.18.14.2 ginsbach ** "%l" have been swapped.
235 1.18.14.2 ginsbach ** (ado, 1993-05-24)
236 1.18.14.2 ginsbach */
237 1.18.14.2 ginsbach pt = _conv(t->tm_hour, "%2d", pt, ptlim);
238 1.18.14.2 ginsbach continue;
239 1.18.14.2 ginsbach #ifdef KITCHEN_SINK
240 1.18.14.2 ginsbach case 'K':
241 1.18.14.2 ginsbach /*
242 1.18.14.2 ginsbach ** After all this time, still unclaimed!
243 1.18.14.2 ginsbach */
244 1.18.14.2 ginsbach pt = _add("kitchen sink", pt, ptlim);
245 1.18.14.2 ginsbach continue;
246 1.18.14.2 ginsbach #endif /* defined KITCHEN_SINK */
247 1.18.14.2 ginsbach case 'l':
248 1.18.14.2 ginsbach /*
249 1.18.14.2 ginsbach ** This used to be...
250 1.18.14.2 ginsbach ** _conv(t->tm_hour, 2, ' ');
251 1.18.14.2 ginsbach ** ...and has been changed to the below to
252 1.18.14.2 ginsbach ** match SunOS 4.1.1 and Arnold Robbin's
253 1.18.14.2 ginsbach ** strftime version 3.0. That is, "%k" and
254 1.18.14.2 ginsbach ** "%l" have been swapped.
255 1.18.14.2 ginsbach ** (ado, 1993-05-24)
256 1.18.14.2 ginsbach */
257 1.18.14.2 ginsbach pt = _conv((t->tm_hour % 12) ?
258 1.18.14.2 ginsbach (t->tm_hour % 12) : 12,
259 1.18.14.2 ginsbach "%2d", pt, ptlim);
260 1.18.14.2 ginsbach continue;
261 1.18.14.2 ginsbach case 'M':
262 1.18.14.2 ginsbach pt = _conv(t->tm_min, "%02d", pt, ptlim);
263 1.18.14.2 ginsbach continue;
264 1.18.14.2 ginsbach case 'm':
265 1.18.14.2 ginsbach pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
266 1.18.14.2 ginsbach continue;
267 1.18.14.2 ginsbach case 'n':
268 1.18.14.2 ginsbach pt = _add("\n", pt, ptlim);
269 1.18.14.2 ginsbach continue;
270 1.18.14.2 ginsbach case 'p':
271 1.18.14.2 ginsbach pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
272 1.18.14.2 ginsbach Locale->am_pm[1] :
273 1.18.14.2 ginsbach Locale->am_pm[0],
274 1.18.14.2 ginsbach pt, ptlim);
275 1.18.14.2 ginsbach continue;
276 1.18.14.2 ginsbach case 'R':
277 1.18.14.2 ginsbach pt = _fmt("%H:%M", t, pt, ptlim, warnp);
278 1.18.14.2 ginsbach continue;
279 1.18.14.2 ginsbach case 'r':
280 1.18.14.2 ginsbach pt = _fmt(Locale->t_fmt_ampm, t, pt, ptlim,
281 1.18.14.2 ginsbach warnp);
282 1.18.14.2 ginsbach continue;
283 1.18.14.2 ginsbach case 'S':
284 1.18.14.2 ginsbach pt = _conv(t->tm_sec, "%02d", pt, ptlim);
285 1.18.14.2 ginsbach continue;
286 1.18.14.2 ginsbach case 's':
287 1.18.14.2 ginsbach {
288 1.18.14.2 ginsbach struct tm tm;
289 1.18.14.2 ginsbach char buf[INT_STRLEN_MAXIMUM(
290 1.18.14.2 ginsbach time_t) + 1];
291 1.18.14.2 ginsbach time_t mkt;
292 1.18.14.2 ginsbach
293 1.18.14.2 ginsbach tm = *t;
294 1.18.14.2 ginsbach mkt = mktime(&tm);
295 1.18.14.2 ginsbach /* CONSTCOND */
296 1.18.14.2 ginsbach if (TYPE_SIGNED(time_t))
297 1.18.14.2 ginsbach (void) sprintf(buf, "%ld",
298 1.18.14.2 ginsbach (long) mkt);
299 1.18.14.2 ginsbach else (void) sprintf(buf, "%lu",
300 1.18.14.2 ginsbach (unsigned long) mkt);
301 1.18.14.2 ginsbach pt = _add(buf, pt, ptlim);
302 1.18.14.2 ginsbach }
303 1.18.14.2 ginsbach continue;
304 1.18.14.2 ginsbach case 'T':
305 1.18.14.2 ginsbach pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
306 1.18.14.2 ginsbach continue;
307 1.18.14.2 ginsbach case 't':
308 1.18.14.2 ginsbach pt = _add("\t", pt, ptlim);
309 1.18.14.2 ginsbach continue;
310 1.18.14.2 ginsbach case 'U':
311 1.18.14.2 ginsbach pt = _conv((t->tm_yday + DAYSPERWEEK -
312 1.18.14.2 ginsbach t->tm_wday) / DAYSPERWEEK,
313 1.18.14.2 ginsbach "%02d", pt, ptlim);
314 1.18.14.2 ginsbach continue;
315 1.18.14.2 ginsbach case 'u':
316 1.18.14.2 ginsbach /*
317 1.18.14.2 ginsbach ** From Arnold Robbins' strftime version 3.0:
318 1.18.14.2 ginsbach ** "ISO 8601: Weekday as a decimal number
319 1.18.14.2 ginsbach ** [1 (Monday) - 7]"
320 1.18.14.2 ginsbach ** (ado, 1993-05-24)
321 1.18.14.2 ginsbach */
322 1.18.14.2 ginsbach pt = _conv((t->tm_wday == 0) ?
323 1.18.14.2 ginsbach DAYSPERWEEK : t->tm_wday,
324 1.18.14.2 ginsbach "%d", pt, ptlim);
325 1.18.14.2 ginsbach continue;
326 1.18.14.2 ginsbach case 'V': /* ISO 8601 week number */
327 1.18.14.2 ginsbach case 'G': /* ISO 8601 year (four digits) */
328 1.18.14.2 ginsbach case 'g': /* ISO 8601 year (two digits) */
329 1.18.14.2 ginsbach /*
330 1.18.14.2 ginsbach ** From Arnold Robbins' strftime version 3.0: "the week number of the
331 1.18.14.2 ginsbach ** year (the first Monday as the first day of week 1) as a decimal number
332 1.18.14.2 ginsbach ** (01-53)."
333 1.18.14.2 ginsbach ** (ado, 1993-05-24)
334 1.18.14.2 ginsbach **
335 1.18.14.2 ginsbach ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
336 1.18.14.2 ginsbach ** "Week 01 of a year is per definition the first week which has the
337 1.18.14.2 ginsbach ** Thursday in this year, which is equivalent to the week which contains
338 1.18.14.2 ginsbach ** the fourth day of January. In other words, the first week of a new year
339 1.18.14.2 ginsbach ** is the week which has the majority of its days in the new year. Week 01
340 1.18.14.2 ginsbach ** might also contain days from the previous year and the week before week
341 1.18.14.2 ginsbach ** 01 of a year is the last week (52 or 53) of the previous year even if
342 1.18.14.2 ginsbach ** it contains days from the new year. A week starts with Monday (day 1)
343 1.18.14.2 ginsbach ** and ends with Sunday (day 7). For example, the first week of the year
344 1.18.14.2 ginsbach ** 1997 lasts from 1996-12-30 to 1997-01-05..."
345 1.18.14.2 ginsbach ** (ado, 1996-01-02)
346 1.18.14.2 ginsbach */
347 1.18.14.2 ginsbach {
348 1.18.14.2 ginsbach int year;
349 1.18.14.2 ginsbach int yday;
350 1.18.14.2 ginsbach int wday;
351 1.18.14.2 ginsbach int w;
352 1.18.14.2 ginsbach
353 1.18.14.2 ginsbach year = t->tm_year + TM_YEAR_BASE;
354 1.18.14.2 ginsbach yday = t->tm_yday;
355 1.18.14.2 ginsbach wday = t->tm_wday;
356 1.18.14.2 ginsbach for ( ; ; ) {
357 1.18.14.2 ginsbach int len;
358 1.18.14.2 ginsbach int bot;
359 1.18.14.2 ginsbach int top;
360 1.18.14.2 ginsbach
361 1.18.14.2 ginsbach len = isleap(year) ?
362 1.18.14.2 ginsbach DAYSPERLYEAR :
363 1.18.14.2 ginsbach DAYSPERNYEAR;
364 1.18.14.2 ginsbach /*
365 1.18.14.2 ginsbach ** What yday (-3 ... 3) does
366 1.18.14.2 ginsbach ** the ISO year begin on?
367 1.18.14.2 ginsbach */
368 1.18.14.2 ginsbach bot = ((yday + 11 - wday) %
369 1.18.14.2 ginsbach DAYSPERWEEK) - 3;
370 1.18.14.2 ginsbach /*
371 1.18.14.2 ginsbach ** What yday does the NEXT
372 1.18.14.2 ginsbach ** ISO year begin on?
373 1.18.14.2 ginsbach */
374 1.18.14.2 ginsbach top = bot -
375 1.18.14.2 ginsbach (len % DAYSPERWEEK);
376 1.18.14.2 ginsbach if (top < -3)
377 1.18.14.2 ginsbach top += DAYSPERWEEK;
378 1.18.14.2 ginsbach top += len;
379 1.18.14.2 ginsbach if (yday >= top) {
380 1.18.14.2 ginsbach ++year;
381 1.18.14.2 ginsbach w = 1;
382 1.18.14.2 ginsbach break;
383 1.18.14.2 ginsbach }
384 1.18.14.2 ginsbach if (yday >= bot) {
385 1.18.14.2 ginsbach w = 1 + ((yday - bot) /
386 1.18.14.2 ginsbach DAYSPERWEEK);
387 1.18.14.2 ginsbach break;
388 1.18.14.2 ginsbach }
389 1.18.14.2 ginsbach --year;
390 1.18.14.2 ginsbach yday += isleap(year) ?
391 1.18.14.2 ginsbach DAYSPERLYEAR :
392 1.18.14.2 ginsbach DAYSPERNYEAR;
393 1.18.14.2 ginsbach }
394 1.18.14.2 ginsbach #ifdef XPG4_1994_04_09
395 1.18.14.2 ginsbach if ((w == 52
396 1.18.14.2 ginsbach && t->tm_mon == TM_JANUARY)
397 1.18.14.2 ginsbach || (w == 1
398 1.18.14.2 ginsbach && t->tm_mon == TM_DECEMBER))
399 1.18.14.2 ginsbach w = 53;
400 1.18.14.2 ginsbach #endif /* defined XPG4_1994_04_09 */
401 1.18.14.2 ginsbach if (*format == 'V')
402 1.18.14.2 ginsbach pt = _conv(w, "%02d",
403 1.18.14.2 ginsbach pt, ptlim);
404 1.18.14.2 ginsbach else if (*format == 'g') {
405 1.18.14.2 ginsbach *warnp = IN_ALL;
406 1.18.14.2 ginsbach pt = _conv(year % 100, "%02d",
407 1.18.14.2 ginsbach pt, ptlim);
408 1.18.14.2 ginsbach } else pt = _conv(year, "%04d",
409 1.18.14.2 ginsbach pt, ptlim);
410 1.18.14.2 ginsbach }
411 1.18.14.2 ginsbach continue;
412 1.18.14.2 ginsbach case 'v':
413 1.18.14.2 ginsbach /*
414 1.18.14.2 ginsbach ** From Arnold Robbins' strftime version 3.0:
415 1.18.14.2 ginsbach ** "date as dd-bbb-YYYY"
416 1.18.14.2 ginsbach ** (ado, 1993-05-24)
417 1.18.14.2 ginsbach */
418 1.18.14.2 ginsbach pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
419 1.18.14.2 ginsbach continue;
420 1.18.14.2 ginsbach case 'W':
421 1.18.14.2 ginsbach pt = _conv((t->tm_yday + DAYSPERWEEK -
422 1.18.14.2 ginsbach (t->tm_wday ?
423 1.18.14.2 ginsbach (t->tm_wday - 1) :
424 1.18.14.2 ginsbach (DAYSPERWEEK - 1))) / DAYSPERWEEK,
425 1.18.14.2 ginsbach "%02d", pt, ptlim);
426 1.18.14.2 ginsbach continue;
427 1.18.14.2 ginsbach case 'w':
428 1.18.14.2 ginsbach pt = _conv(t->tm_wday, "%d", pt, ptlim);
429 1.18.14.2 ginsbach continue;
430 1.18.14.2 ginsbach case 'X':
431 1.18.14.2 ginsbach pt = _fmt(Locale->t_fmt, t, pt, ptlim, warnp);
432 1.18.14.2 ginsbach continue;
433 1.18.14.2 ginsbach case 'x':
434 1.18.14.2 ginsbach {
435 1.18.14.2 ginsbach int warn2 = IN_SOME;
436 1.18.14.2 ginsbach
437 1.18.14.2 ginsbach pt = _fmt(Locale->d_fmt, t, pt, ptlim, &warn2);
438 1.18.14.2 ginsbach if (warn2 == IN_ALL)
439 1.18.14.2 ginsbach warn2 = IN_THIS;
440 1.18.14.2 ginsbach if (warn2 > *warnp)
441 1.18.14.2 ginsbach *warnp = warn2;
442 1.18.14.2 ginsbach }
443 1.18.14.2 ginsbach continue;
444 1.18.14.2 ginsbach case 'y':
445 1.18.14.2 ginsbach *warnp = IN_ALL;
446 1.18.14.2 ginsbach pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
447 1.18.14.2 ginsbach "%02d", pt, ptlim);
448 1.18.14.2 ginsbach continue;
449 1.18.14.2 ginsbach case 'Y':
450 1.18.14.2 ginsbach pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
451 1.18.14.2 ginsbach pt, ptlim);
452 1.18.14.2 ginsbach continue;
453 1.18.14.2 ginsbach case 'Z':
454 1.18.14.2 ginsbach #ifdef TM_ZONE
455 1.18.14.2 ginsbach if (t->TM_ZONE != NULL)
456 1.18.14.2 ginsbach pt = _add(t->TM_ZONE, pt, ptlim);
457 1.18.14.2 ginsbach else
458 1.18.14.2 ginsbach #endif /* defined TM_ZONE */
459 1.18.14.2 ginsbach if (t->tm_isdst >= 0)
460 1.18.14.2 ginsbach pt = _add(tzname[t->tm_isdst != 0],
461 1.18.14.2 ginsbach pt, ptlim);
462 1.18.14.2 ginsbach /*
463 1.18.14.2 ginsbach ** C99 says that %Z must be replaced by the
464 1.18.14.2 ginsbach ** empty string if the time zone is not
465 1.18.14.2 ginsbach ** determinable.
466 1.18.14.2 ginsbach */
467 1.18.14.2 ginsbach continue;
468 1.18.14.2 ginsbach case 'z':
469 1.18.14.2 ginsbach {
470 1.18.14.2 ginsbach int diff;
471 1.18.14.2 ginsbach char const * sign;
472 1.18.14.2 ginsbach
473 1.18.14.2 ginsbach if (t->tm_isdst < 0)
474 1.18.14.2 ginsbach continue;
475 1.18.14.2 ginsbach #ifdef TM_GMTOFF
476 1.18.14.2 ginsbach diff = (int)t->TM_GMTOFF;
477 1.18.14.2 ginsbach #else /* !defined TM_GMTOFF */
478 1.18.14.2 ginsbach /*
479 1.18.14.2 ginsbach ** C99 says that the UTC offset must
480 1.18.14.2 ginsbach ** be computed by looking only at
481 1.18.14.2 ginsbach ** tm_isdst. This requirement is
482 1.18.14.2 ginsbach ** incorrect, since it means the code
483 1.18.14.2 ginsbach ** must rely on magic (in this case
484 1.18.14.2 ginsbach ** altzone and timezone), and the
485 1.18.14.2 ginsbach ** magic might not have the correct
486 1.18.14.2 ginsbach ** offset. Doing things correctly is
487 1.18.14.2 ginsbach ** tricky and requires disobeying C99;
488 1.18.14.2 ginsbach ** see GNU C strftime for details.
489 1.18.14.2 ginsbach ** For now, punt and conform to the
490 1.18.14.2 ginsbach ** standard, even though it's incorrect.
491 1.18.14.2 ginsbach **
492 1.18.14.2 ginsbach ** C99 says that %z must be replaced by the
493 1.18.14.2 ginsbach ** empty string if the time zone is not
494 1.18.14.2 ginsbach ** determinable, so output nothing if the
495 1.18.14.2 ginsbach ** appropriate variables are not available.
496 1.18.14.2 ginsbach */
497 1.18.14.2 ginsbach #ifndef STD_INSPIRED
498 1.18.14.2 ginsbach if (t->tm_isdst == 0)
499 1.18.14.2 ginsbach #ifdef USG_COMPAT
500 1.18.14.2 ginsbach diff = -timezone;
501 1.18.14.2 ginsbach #else /* !defined USG_COMPAT */
502 1.18.14.2 ginsbach continue;
503 1.18.14.2 ginsbach #endif /* !defined USG_COMPAT */
504 1.18.14.2 ginsbach else
505 1.18.14.2 ginsbach #ifdef ALTZONE
506 1.18.14.2 ginsbach diff = -altzone;
507 1.18.14.2 ginsbach #else /* !defined ALTZONE */
508 1.18.14.2 ginsbach continue;
509 1.18.14.2 ginsbach #endif /* !defined ALTZONE */
510 1.18.14.2 ginsbach #else /* defined STD_INSPIRED */
511 1.18.14.2 ginsbach {
512 1.18.14.2 ginsbach struct tm tmp;
513 1.18.14.2 ginsbach time_t lct, gct;
514 1.18.14.2 ginsbach
515 1.18.14.2 ginsbach /*
516 1.18.14.2 ginsbach ** Get calendar time from t
517 1.18.14.2 ginsbach ** being treated as local.
518 1.18.14.2 ginsbach */
519 1.18.14.2 ginsbach tmp = *t; /* mktime discards const */
520 1.18.14.2 ginsbach lct = mktime(&tmp);
521 1.18.14.2 ginsbach
522 1.18.14.2 ginsbach if (lct == (time_t)-1)
523 1.18.14.2 ginsbach continue;
524 1.18.14.2 ginsbach
525 1.18.14.2 ginsbach /*
526 1.18.14.2 ginsbach ** Get calendar time from t
527 1.18.14.2 ginsbach ** being treated as GMT.
528 1.18.14.2 ginsbach **/
529 1.18.14.2 ginsbach tmp = *t; /* mktime discards const */
530 1.18.14.2 ginsbach gct = timegm(&tmp);
531 1.18.14.2 ginsbach
532 1.18.14.2 ginsbach if (gct == (time_t)-1)
533 1.18.14.2 ginsbach continue;
534 1.18.14.2 ginsbach
535 1.18.14.2 ginsbach /* LINTED difference will fit int */
536 1.18.14.2 ginsbach diff = (intmax_t)gct - (intmax_t)lct;
537 1.18.14.2 ginsbach }
538 1.18.14.2 ginsbach #endif /* defined STD_INSPIRED */
539 1.18.14.2 ginsbach #endif /* !defined TM_GMTOFF */
540 1.18.14.2 ginsbach if (diff < 0) {
541 1.18.14.2 ginsbach sign = "-";
542 1.18.14.2 ginsbach diff = -diff;
543 1.18.14.2 ginsbach } else sign = "+";
544 1.18.14.2 ginsbach pt = _add(sign, pt, ptlim);
545 1.18.14.2 ginsbach diff /= 60;
546 1.18.14.2 ginsbach pt = _conv((diff/60)*100 + diff%60,
547 1.18.14.2 ginsbach "%04d", pt, ptlim);
548 1.18.14.2 ginsbach }
549 1.18.14.2 ginsbach continue;
550 1.18.14.2 ginsbach #if 0
551 1.18.14.2 ginsbach case '+':
552 1.18.14.2 ginsbach pt = _fmt(Locale->date_fmt, t, pt, ptlim,
553 1.18.14.2 ginsbach warnp);
554 1.18.14.2 ginsbach continue;
555 1.18.14.2 ginsbach #endif
556 1.18.14.2 ginsbach case '%':
557 1.18.14.2 ginsbach /*
558 1.18.14.2 ginsbach ** X311J/88-090 (4.12.3.5): if conversion char is
559 1.18.14.2 ginsbach ** undefined, behavior is undefined. Print out the
560 1.18.14.2 ginsbach ** character itself as printf(3) also does.
561 1.18.14.2 ginsbach */
562 1.18.14.2 ginsbach default:
563 1.18.14.2 ginsbach break;
564 1.18.14.2 ginsbach }
565 1.18.14.2 ginsbach }
566 1.18.14.2 ginsbach if (pt == ptlim)
567 1.18.14.2 ginsbach break;
568 1.18.14.2 ginsbach *pt++ = *format;
569 1.18.14.2 ginsbach }
570 1.18.14.2 ginsbach return pt;
571 1.18.14.2 ginsbach }
572 1.18.14.2 ginsbach
573 1.18.14.2 ginsbach static char *
574 1.18.14.2 ginsbach _conv(n, format, pt, ptlim)
575 1.18.14.2 ginsbach const int n;
576 1.18.14.2 ginsbach const char * const format;
577 1.18.14.2 ginsbach char * const pt;
578 1.18.14.2 ginsbach const char * const ptlim;
579 1.18.14.2 ginsbach {
580 1.18.14.2 ginsbach char buf[INT_STRLEN_MAXIMUM(int) + 1];
581 1.18.14.2 ginsbach
582 1.18.14.2 ginsbach (void) sprintf(buf, format, n);
583 1.18.14.2 ginsbach return _add(buf, pt, ptlim);
584 1.18.14.2 ginsbach }
585 1.18.14.2 ginsbach
586 1.18.14.2 ginsbach static char *
587 1.18.14.2 ginsbach _add(str, pt, ptlim)
588 1.18.14.2 ginsbach const char * str;
589 1.18.14.2 ginsbach char * pt;
590 1.18.14.2 ginsbach const char * const ptlim;
591 1.18.14.2 ginsbach {
592 1.18.14.2 ginsbach while (pt < ptlim && (*pt = *str++) != '\0')
593 1.18.14.2 ginsbach ++pt;
594 1.18.14.2 ginsbach return pt;
595 1.18.14.2 ginsbach }
596