strftime.c revision 1.1.1.1 1 1.1.1.1 kleink #ifndef lint
2 1.1.1.1 kleink #ifndef NOID
3 1.1.1.1 kleink static char elsieid[] = "@(#)strftime.c 7.62";
4 1.1 mrg /*
5 1.1.1.1 kleink ** Based on the UCB version with the ID appearing below.
6 1.1.1.1 kleink ** This is ANSIish only when "multibyte character == plain character".
7 1.1.1.1 kleink */
8 1.1.1.1 kleink #endif /* !defined NOID */
9 1.1.1.1 kleink #endif /* !defined lint */
10 1.1.1.1 kleink
11 1.1.1.1 kleink #include "private.h"
12 1.1.1.1 kleink
13 1.1.1.1 kleink /*
14 1.1.1.1 kleink ** Copyright (c) 1989 The Regents of the University of California.
15 1.1.1.1 kleink ** All rights reserved.
16 1.1.1.1 kleink **
17 1.1.1.1 kleink ** Redistribution and use in source and binary forms are permitted
18 1.1.1.1 kleink ** provided that the above copyright notice and this paragraph are
19 1.1.1.1 kleink ** duplicated in all such forms and that any documentation,
20 1.1.1.1 kleink ** advertising materials, and other materials related to such
21 1.1.1.1 kleink ** distribution and use acknowledge that the software was developed
22 1.1.1.1 kleink ** by the University of California, Berkeley. The name of the
23 1.1.1.1 kleink ** University may not be used to endorse or promote products derived
24 1.1.1.1 kleink ** from this software without specific prior written permission.
25 1.1.1.1 kleink ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26 1.1.1.1 kleink ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27 1.1.1.1 kleink ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 1.1.1.1 kleink */
29 1.1.1.1 kleink
30 1.1.1.1 kleink #ifndef LIBC_SCCS
31 1.1.1.1 kleink #ifndef lint
32 1.1.1.1 kleink static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
33 1.1.1.1 kleink #endif /* !defined lint */
34 1.1.1.1 kleink #endif /* !defined LIBC_SCCS */
35 1.1.1.1 kleink
36 1.1.1.1 kleink #include "tzfile.h"
37 1.1.1.1 kleink #include "fcntl.h"
38 1.1.1.1 kleink #include "locale.h"
39 1.1.1.1 kleink
40 1.1.1.1 kleink struct lc_time_T {
41 1.1.1.1 kleink const char * mon[MONSPERYEAR];
42 1.1.1.1 kleink const char * month[MONSPERYEAR];
43 1.1.1.1 kleink const char * wday[DAYSPERWEEK];
44 1.1.1.1 kleink const char * weekday[DAYSPERWEEK];
45 1.1.1.1 kleink const char * X_fmt;
46 1.1.1.1 kleink const char * x_fmt;
47 1.1.1.1 kleink const char * c_fmt;
48 1.1.1.1 kleink const char * am;
49 1.1.1.1 kleink const char * pm;
50 1.1.1.1 kleink const char * date_fmt;
51 1.1.1.1 kleink };
52 1.1.1.1 kleink
53 1.1.1.1 kleink #ifdef LOCALE_HOME
54 1.1.1.1 kleink #include "sys/stat.h"
55 1.1.1.1 kleink static struct lc_time_T localebuf;
56 1.1.1.1 kleink static struct lc_time_T * _loc P((void));
57 1.1.1.1 kleink #define Locale _loc()
58 1.1.1.1 kleink #endif /* defined LOCALE_HOME */
59 1.1.1.1 kleink #ifndef LOCALE_HOME
60 1.1.1.1 kleink #define Locale (&C_time_locale)
61 1.1.1.1 kleink #endif /* !defined LOCALE_HOME */
62 1.1.1.1 kleink
63 1.1.1.1 kleink static const struct lc_time_T C_time_locale = {
64 1.1.1.1 kleink {
65 1.1.1.1 kleink "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 1.1.1.1 kleink "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
67 1.1.1.1 kleink }, {
68 1.1.1.1 kleink "January", "February", "March", "April", "May", "June",
69 1.1.1.1 kleink "July", "August", "September", "October", "November", "December"
70 1.1.1.1 kleink }, {
71 1.1.1.1 kleink "Sun", "Mon", "Tue", "Wed",
72 1.1.1.1 kleink "Thu", "Fri", "Sat"
73 1.1.1.1 kleink }, {
74 1.1.1.1 kleink "Sunday", "Monday", "Tuesday", "Wednesday",
75 1.1.1.1 kleink "Thursday", "Friday", "Saturday"
76 1.1.1.1 kleink },
77 1.1.1.1 kleink
78 1.1.1.1 kleink /* X_fmt */
79 1.1.1.1 kleink "%H:%M:%S",
80 1.1.1.1 kleink
81 1.1.1.1 kleink /*
82 1.1.1.1 kleink ** x_fmt
83 1.1.1.1 kleink ** C99 requires this format.
84 1.1.1.1 kleink ** Using just numbers (as here) makes Quakers happier;
85 1.1.1.1 kleink ** it's also compatible with SVR4.
86 1.1.1.1 kleink */
87 1.1.1.1 kleink "%m/%d/%y",
88 1.1.1.1 kleink
89 1.1.1.1 kleink /*
90 1.1.1.1 kleink ** c_fmt
91 1.1.1.1 kleink ** C99 requires this format.
92 1.1.1.1 kleink ** Previously this code used "%D %X", but we now conform to C99.
93 1.1.1.1 kleink ** Note that
94 1.1.1.1 kleink ** "%a %b %d %H:%M:%S %Y"
95 1.1.1.1 kleink ** is used by Solaris 2.3.
96 1.1.1.1 kleink */
97 1.1.1.1 kleink "%a %b %e %T %Y",
98 1.1.1.1 kleink
99 1.1.1.1 kleink /* am */
100 1.1.1.1 kleink "AM",
101 1.1.1.1 kleink
102 1.1.1.1 kleink /* pm */
103 1.1.1.1 kleink "PM",
104 1.1.1.1 kleink
105 1.1.1.1 kleink /* date_fmt */
106 1.1.1.1 kleink "%a %b %e %H:%M:%S %Z %Y"
107 1.1.1.1 kleink };
108 1.1.1.1 kleink
109 1.1.1.1 kleink static char * _add P((const char *, char *, const char *));
110 1.1.1.1 kleink static char * _conv P((int, const char *, char *, const char *));
111 1.1.1.1 kleink static char * _fmt P((const char *, const struct tm *, char *, const char *, int *));
112 1.1.1.1 kleink
113 1.1.1.1 kleink size_t strftime P((char *, size_t, const char *, const struct tm *));
114 1.1.1.1 kleink
115 1.1.1.1 kleink extern char * tzname[];
116 1.1.1.1 kleink
117 1.1.1.1 kleink #ifndef YEAR_2000_NAME
118 1.1.1.1 kleink #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
119 1.1.1.1 kleink #endif /* !defined YEAR_2000_NAME */
120 1.1.1.1 kleink
121 1.1.1.1 kleink
122 1.1.1.1 kleink #define IN_NONE 0
123 1.1.1.1 kleink #define IN_SOME 1
124 1.1.1.1 kleink #define IN_THIS 2
125 1.1.1.1 kleink #define IN_ALL 3
126 1.1 mrg
127 1.1 mrg size_t
128 1.1 mrg strftime(s, maxsize, format, t)
129 1.1.1.1 kleink char * const s;
130 1.1.1.1 kleink const size_t maxsize;
131 1.1.1.1 kleink const char * const format;
132 1.1.1.1 kleink const struct tm * const t;
133 1.1 mrg {
134 1.1.1.1 kleink char * p;
135 1.1.1.1 kleink int warn;
136 1.1 mrg
137 1.1.1.1 kleink tzset();
138 1.1.1.1 kleink #ifdef LOCALE_HOME
139 1.1.1.1 kleink localebuf.mon[0] = 0;
140 1.1.1.1 kleink #endif /* defined LOCALE_HOME */
141 1.1.1.1 kleink warn = IN_NONE;
142 1.1.1.1 kleink p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
143 1.1.1.1 kleink #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
144 1.1.1.1 kleink if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
145 1.1.1.1 kleink (void) fprintf(stderr, "\n");
146 1.1.1.1 kleink if (format == NULL)
147 1.1.1.1 kleink (void) fprintf(stderr, "NULL strftime format ");
148 1.1.1.1 kleink else (void) fprintf(stderr, "strftime format \"%s\" ",
149 1.1.1.1 kleink format);
150 1.1.1.1 kleink (void) fprintf(stderr, "yields only two digits of years in ");
151 1.1.1.1 kleink if (warn == IN_SOME)
152 1.1.1.1 kleink (void) fprintf(stderr, "some locales");
153 1.1.1.1 kleink else if (warn == IN_THIS)
154 1.1.1.1 kleink (void) fprintf(stderr, "the current locale");
155 1.1.1.1 kleink else (void) fprintf(stderr, "all locales");
156 1.1.1.1 kleink (void) fprintf(stderr, "\n");
157 1.1 mrg }
158 1.1.1.1 kleink #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
159 1.1.1.1 kleink if (p == s + maxsize)
160 1.1.1.1 kleink return 0;
161 1.1.1.1 kleink *p = '\0';
162 1.1.1.1 kleink return p - s;
163 1.1 mrg }
164 1.1 mrg
165 1.1.1.1 kleink static char *
166 1.1.1.1 kleink _fmt(format, t, pt, ptlim, warnp)
167 1.1.1.1 kleink const char * format;
168 1.1.1.1 kleink const struct tm * const t;
169 1.1.1.1 kleink char * pt;
170 1.1.1.1 kleink const char * const ptlim;
171 1.1.1.1 kleink int * warnp;
172 1.1 mrg {
173 1.1.1.1 kleink for ( ; *format; ++format) {
174 1.1 mrg if (*format == '%') {
175 1.1.1.1 kleink label:
176 1.1.1.1 kleink switch (*++format) {
177 1.1 mrg case '\0':
178 1.1 mrg --format;
179 1.1 mrg break;
180 1.1 mrg case 'A':
181 1.1.1.1 kleink pt = _add((t->tm_wday < 0 ||
182 1.1.1.1 kleink t->tm_wday >= DAYSPERWEEK) ?
183 1.1.1.1 kleink "?" : Locale->weekday[t->tm_wday],
184 1.1.1.1 kleink pt, ptlim);
185 1.1 mrg continue;
186 1.1 mrg case 'a':
187 1.1.1.1 kleink pt = _add((t->tm_wday < 0 ||
188 1.1.1.1 kleink t->tm_wday >= DAYSPERWEEK) ?
189 1.1.1.1 kleink "?" : Locale->wday[t->tm_wday],
190 1.1.1.1 kleink pt, ptlim);
191 1.1 mrg continue;
192 1.1 mrg case 'B':
193 1.1.1.1 kleink pt = _add((t->tm_mon < 0 ||
194 1.1.1.1 kleink t->tm_mon >= MONSPERYEAR) ?
195 1.1.1.1 kleink "?" : Locale->month[t->tm_mon],
196 1.1.1.1 kleink pt, ptlim);
197 1.1 mrg continue;
198 1.1 mrg case 'b':
199 1.1 mrg case 'h':
200 1.1.1.1 kleink pt = _add((t->tm_mon < 0 ||
201 1.1.1.1 kleink t->tm_mon >= MONSPERYEAR) ?
202 1.1.1.1 kleink "?" : Locale->mon[t->tm_mon],
203 1.1.1.1 kleink pt, ptlim);
204 1.1 mrg continue;
205 1.1 mrg case 'C':
206 1.1.1.1 kleink /*
207 1.1.1.1 kleink ** %C used to do a...
208 1.1.1.1 kleink ** _fmt("%a %b %e %X %Y", t);
209 1.1.1.1 kleink ** ...whereas now POSIX 1003.2 calls for
210 1.1.1.1 kleink ** something completely different.
211 1.1.1.1 kleink ** (ado, 1993-05-24)
212 1.1.1.1 kleink */
213 1.1.1.1 kleink pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
214 1.1.1.1 kleink "%02d", pt, ptlim);
215 1.1 mrg continue;
216 1.1 mrg case 'c':
217 1.1.1.1 kleink {
218 1.1.1.1 kleink int warn2 = IN_SOME;
219 1.1.1.1 kleink
220 1.1.1.1 kleink pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
221 1.1.1.1 kleink if (warn2 == IN_ALL)
222 1.1.1.1 kleink warn2 = IN_THIS;
223 1.1.1.1 kleink if (warn2 > *warnp)
224 1.1.1.1 kleink *warnp = warn2;
225 1.1.1.1 kleink }
226 1.1 mrg continue;
227 1.1 mrg case 'D':
228 1.1.1.1 kleink pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
229 1.1 mrg continue;
230 1.1 mrg case 'd':
231 1.1.1.1 kleink pt = _conv(t->tm_mday, "%02d", pt, ptlim);
232 1.1 mrg continue;
233 1.1.1.1 kleink case 'E':
234 1.1.1.1 kleink case 'O':
235 1.1.1.1 kleink /*
236 1.1.1.1 kleink ** C99 locale modifiers.
237 1.1.1.1 kleink ** The sequences
238 1.1.1.1 kleink ** %Ec %EC %Ex %EX %Ey %EY
239 1.1.1.1 kleink ** %Od %oe %OH %OI %Om %OM
240 1.1.1.1 kleink ** %OS %Ou %OU %OV %Ow %OW %Oy
241 1.1.1.1 kleink ** are supposed to provide alternate
242 1.1.1.1 kleink ** representations.
243 1.1.1.1 kleink */
244 1.1.1.1 kleink goto label;
245 1.1 mrg case 'e':
246 1.1.1.1 kleink pt = _conv(t->tm_mday, "%2d", pt, ptlim);
247 1.1.1.1 kleink continue;
248 1.1.1.1 kleink case 'F':
249 1.1.1.1 kleink pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
250 1.1 mrg continue;
251 1.1 mrg case 'H':
252 1.1.1.1 kleink pt = _conv(t->tm_hour, "%02d", pt, ptlim);
253 1.1 mrg continue;
254 1.1 mrg case 'I':
255 1.1.1.1 kleink pt = _conv((t->tm_hour % 12) ?
256 1.1.1.1 kleink (t->tm_hour % 12) : 12,
257 1.1.1.1 kleink "%02d", pt, ptlim);
258 1.1 mrg continue;
259 1.1 mrg case 'j':
260 1.1.1.1 kleink pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
261 1.1 mrg continue;
262 1.1 mrg case 'k':
263 1.1.1.1 kleink /*
264 1.1.1.1 kleink ** This used to be...
265 1.1.1.1 kleink ** _conv(t->tm_hour % 12 ?
266 1.1.1.1 kleink ** t->tm_hour % 12 : 12, 2, ' ');
267 1.1.1.1 kleink ** ...and has been changed to the below to
268 1.1.1.1 kleink ** match SunOS 4.1.1 and Arnold Robbins'
269 1.1.1.1 kleink ** strftime version 3.0. That is, "%k" and
270 1.1.1.1 kleink ** "%l" have been swapped.
271 1.1.1.1 kleink ** (ado, 1993-05-24)
272 1.1.1.1 kleink */
273 1.1.1.1 kleink pt = _conv(t->tm_hour, "%2d", pt, ptlim);
274 1.1.1.1 kleink continue;
275 1.1.1.1 kleink #ifdef KITCHEN_SINK
276 1.1.1.1 kleink case 'K':
277 1.1.1.1 kleink /*
278 1.1.1.1 kleink ** After all this time, still unclaimed!
279 1.1.1.1 kleink */
280 1.1.1.1 kleink pt = _add("kitchen sink", pt, ptlim);
281 1.1 mrg continue;
282 1.1.1.1 kleink #endif /* defined KITCHEN_SINK */
283 1.1 mrg case 'l':
284 1.1.1.1 kleink /*
285 1.1.1.1 kleink ** This used to be...
286 1.1.1.1 kleink ** _conv(t->tm_hour, 2, ' ');
287 1.1.1.1 kleink ** ...and has been changed to the below to
288 1.1.1.1 kleink ** match SunOS 4.1.1 and Arnold Robbin's
289 1.1.1.1 kleink ** strftime version 3.0. That is, "%k" and
290 1.1.1.1 kleink ** "%l" have been swapped.
291 1.1.1.1 kleink ** (ado, 1993-05-24)
292 1.1.1.1 kleink */
293 1.1.1.1 kleink pt = _conv((t->tm_hour % 12) ?
294 1.1.1.1 kleink (t->tm_hour % 12) : 12,
295 1.1.1.1 kleink "%2d", pt, ptlim);
296 1.1 mrg continue;
297 1.1 mrg case 'M':
298 1.1.1.1 kleink pt = _conv(t->tm_min, "%02d", pt, ptlim);
299 1.1 mrg continue;
300 1.1 mrg case 'm':
301 1.1.1.1 kleink pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
302 1.1 mrg continue;
303 1.1 mrg case 'n':
304 1.1.1.1 kleink pt = _add("\n", pt, ptlim);
305 1.1 mrg continue;
306 1.1 mrg case 'p':
307 1.1.1.1 kleink pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
308 1.1.1.1 kleink Locale->pm :
309 1.1.1.1 kleink Locale->am,
310 1.1.1.1 kleink pt, ptlim);
311 1.1 mrg continue;
312 1.1 mrg case 'R':
313 1.1.1.1 kleink pt = _fmt("%H:%M", t, pt, ptlim, warnp);
314 1.1 mrg continue;
315 1.1 mrg case 'r':
316 1.1.1.1 kleink pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
317 1.1 mrg continue;
318 1.1 mrg case 'S':
319 1.1.1.1 kleink pt = _conv(t->tm_sec, "%02d", pt, ptlim);
320 1.1 mrg continue;
321 1.1 mrg case 's':
322 1.1.1.1 kleink {
323 1.1.1.1 kleink struct tm tm;
324 1.1.1.1 kleink char buf[INT_STRLEN_MAXIMUM(
325 1.1.1.1 kleink time_t) + 1];
326 1.1.1.1 kleink time_t mkt;
327 1.1.1.1 kleink
328 1.1.1.1 kleink tm = *t;
329 1.1.1.1 kleink mkt = mktime(&tm);
330 1.1.1.1 kleink if (TYPE_SIGNED(time_t))
331 1.1.1.1 kleink (void) sprintf(buf, "%ld",
332 1.1.1.1 kleink (long) mkt);
333 1.1.1.1 kleink else (void) sprintf(buf, "%lu",
334 1.1.1.1 kleink (unsigned long) mkt);
335 1.1.1.1 kleink pt = _add(buf, pt, ptlim);
336 1.1.1.1 kleink }
337 1.1 mrg continue;
338 1.1 mrg case 'T':
339 1.1.1.1 kleink pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
340 1.1 mrg continue;
341 1.1 mrg case 't':
342 1.1.1.1 kleink pt = _add("\t", pt, ptlim);
343 1.1 mrg continue;
344 1.1 mrg case 'U':
345 1.1.1.1 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
346 1.1.1.1 kleink t->tm_wday) / DAYSPERWEEK,
347 1.1.1.1 kleink "%02d", pt, ptlim);
348 1.1 mrg continue;
349 1.1 mrg case 'u':
350 1.1.1.1 kleink /*
351 1.1.1.1 kleink ** From Arnold Robbins' strftime version 3.0:
352 1.1.1.1 kleink ** "ISO 8601: Weekday as a decimal number
353 1.1.1.1 kleink ** [1 (Monday) - 7]"
354 1.1.1.1 kleink ** (ado, 1993-05-24)
355 1.1.1.1 kleink */
356 1.1.1.1 kleink pt = _conv((t->tm_wday == 0) ?
357 1.1.1.1 kleink DAYSPERWEEK : t->tm_wday,
358 1.1.1.1 kleink "%d", pt, ptlim);
359 1.1.1.1 kleink continue;
360 1.1.1.1 kleink case 'V': /* ISO 8601 week number */
361 1.1.1.1 kleink case 'G': /* ISO 8601 year (four digits) */
362 1.1.1.1 kleink case 'g': /* ISO 8601 year (two digits) */
363 1.1.1.1 kleink /*
364 1.1.1.1 kleink ** From Arnold Robbins' strftime version 3.0: "the week number of the
365 1.1.1.1 kleink ** year (the first Monday as the first day of week 1) as a decimal number
366 1.1.1.1 kleink ** (01-53)."
367 1.1.1.1 kleink ** (ado, 1993-05-24)
368 1.1.1.1 kleink **
369 1.1.1.1 kleink ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
370 1.1.1.1 kleink ** "Week 01 of a year is per definition the first week which has the
371 1.1.1.1 kleink ** Thursday in this year, which is equivalent to the week which contains
372 1.1.1.1 kleink ** the fourth day of January. In other words, the first week of a new year
373 1.1.1.1 kleink ** is the week which has the majority of its days in the new year. Week 01
374 1.1.1.1 kleink ** might also contain days from the previous year and the week before week
375 1.1.1.1 kleink ** 01 of a year is the last week (52 or 53) of the previous year even if
376 1.1.1.1 kleink ** it contains days from the new year. A week starts with Monday (day 1)
377 1.1.1.1 kleink ** and ends with Sunday (day 7). For example, the first week of the year
378 1.1.1.1 kleink ** 1997 lasts from 1996-12-30 to 1997-01-05..."
379 1.1.1.1 kleink ** (ado, 1996-01-02)
380 1.1.1.1 kleink */
381 1.1 mrg {
382 1.1.1.1 kleink int year;
383 1.1.1.1 kleink int yday;
384 1.1.1.1 kleink int wday;
385 1.1.1.1 kleink int w;
386 1.1.1.1 kleink
387 1.1.1.1 kleink year = t->tm_year + TM_YEAR_BASE;
388 1.1.1.1 kleink yday = t->tm_yday;
389 1.1.1.1 kleink wday = t->tm_wday;
390 1.1.1.1 kleink for ( ; ; ) {
391 1.1.1.1 kleink int len;
392 1.1.1.1 kleink int bot;
393 1.1.1.1 kleink int top;
394 1.1.1.1 kleink
395 1.1.1.1 kleink len = isleap(year) ?
396 1.1.1.1 kleink DAYSPERLYEAR :
397 1.1.1.1 kleink DAYSPERNYEAR;
398 1.1.1.1 kleink /*
399 1.1.1.1 kleink ** What yday (-3 ... 3) does
400 1.1.1.1 kleink ** the ISO year begin on?
401 1.1.1.1 kleink */
402 1.1.1.1 kleink bot = ((yday + 11 - wday) %
403 1.1.1.1 kleink DAYSPERWEEK) - 3;
404 1.1.1.1 kleink /*
405 1.1.1.1 kleink ** What yday does the NEXT
406 1.1.1.1 kleink ** ISO year begin on?
407 1.1.1.1 kleink */
408 1.1.1.1 kleink top = bot -
409 1.1.1.1 kleink (len % DAYSPERWEEK);
410 1.1.1.1 kleink if (top < -3)
411 1.1.1.1 kleink top += DAYSPERWEEK;
412 1.1.1.1 kleink top += len;
413 1.1.1.1 kleink if (yday >= top) {
414 1.1.1.1 kleink ++year;
415 1.1.1.1 kleink w = 1;
416 1.1.1.1 kleink break;
417 1.1.1.1 kleink }
418 1.1.1.1 kleink if (yday >= bot) {
419 1.1.1.1 kleink w = 1 + ((yday - bot) /
420 1.1.1.1 kleink DAYSPERWEEK);
421 1.1.1.1 kleink break;
422 1.1.1.1 kleink }
423 1.1.1.1 kleink --year;
424 1.1.1.1 kleink yday += isleap(year) ?
425 1.1.1.1 kleink DAYSPERLYEAR :
426 1.1.1.1 kleink DAYSPERNYEAR;
427 1.1.1.1 kleink }
428 1.1.1.1 kleink #ifdef XPG4_1994_04_09
429 1.1.1.1 kleink if ((w == 52
430 1.1.1.1 kleink && t->tm_mon == TM_JANUARY)
431 1.1.1.1 kleink || (w == 1
432 1.1.1.1 kleink && t->tm_mon == TM_DECEMBER))
433 1.1.1.1 kleink w = 53;
434 1.1.1.1 kleink #endif /* defined XPG4_1994_04_09 */
435 1.1.1.1 kleink if (*format == 'V')
436 1.1.1.1 kleink pt = _conv(w, "%02d",
437 1.1.1.1 kleink pt, ptlim);
438 1.1.1.1 kleink else if (*format == 'g') {
439 1.1.1.1 kleink *warnp = IN_ALL;
440 1.1.1.1 kleink pt = _conv(year % 100, "%02d",
441 1.1.1.1 kleink pt, ptlim);
442 1.1.1.1 kleink } else pt = _conv(year, "%04d",
443 1.1.1.1 kleink pt, ptlim);
444 1.1 mrg }
445 1.1 mrg continue;
446 1.1.1.1 kleink case 'v':
447 1.1.1.1 kleink /*
448 1.1.1.1 kleink ** From Arnold Robbins' strftime version 3.0:
449 1.1.1.1 kleink ** "date as dd-bbb-YYYY"
450 1.1.1.1 kleink ** (ado, 1993-05-24)
451 1.1.1.1 kleink */
452 1.1.1.1 kleink pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
453 1.1.1.1 kleink continue;
454 1.1 mrg case 'W':
455 1.1.1.1 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
456 1.1.1.1 kleink (t->tm_wday ?
457 1.1.1.1 kleink (t->tm_wday - 1) :
458 1.1.1.1 kleink (DAYSPERWEEK - 1))) / DAYSPERWEEK,
459 1.1.1.1 kleink "%02d", pt, ptlim);
460 1.1 mrg continue;
461 1.1 mrg case 'w':
462 1.1.1.1 kleink pt = _conv(t->tm_wday, "%d", pt, ptlim);
463 1.1 mrg continue;
464 1.1 mrg case 'X':
465 1.1.1.1 kleink pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
466 1.1.1.1 kleink continue;
467 1.1.1.1 kleink case 'x':
468 1.1.1.1 kleink {
469 1.1.1.1 kleink int warn2 = IN_SOME;
470 1.1.1.1 kleink
471 1.1.1.1 kleink pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
472 1.1.1.1 kleink if (warn2 == IN_ALL)
473 1.1.1.1 kleink warn2 = IN_THIS;
474 1.1.1.1 kleink if (warn2 > *warnp)
475 1.1.1.1 kleink *warnp = warn2;
476 1.1.1.1 kleink }
477 1.1 mrg continue;
478 1.1 mrg case 'y':
479 1.1.1.1 kleink *warnp = IN_ALL;
480 1.1.1.1 kleink pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
481 1.1.1.1 kleink "%02d", pt, ptlim);
482 1.1 mrg continue;
483 1.1 mrg case 'Y':
484 1.1.1.1 kleink pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
485 1.1.1.1 kleink pt, ptlim);
486 1.1 mrg continue;
487 1.1 mrg case 'Z':
488 1.1.1.1 kleink #ifdef TM_ZONE
489 1.1.1.1 kleink if (t->TM_ZONE != NULL)
490 1.1.1.1 kleink pt = _add(t->TM_ZONE, pt, ptlim);
491 1.1.1.1 kleink else
492 1.1.1.1 kleink #endif /* defined TM_ZONE */
493 1.1.1.1 kleink if (t->tm_isdst >= 0)
494 1.1.1.1 kleink pt = _add(tzname[t->tm_isdst != 0],
495 1.1.1.1 kleink pt, ptlim);
496 1.1.1.1 kleink /*
497 1.1.1.1 kleink ** C99 says that %Z must be replaced by the
498 1.1.1.1 kleink ** empty string if the time zone is not
499 1.1.1.1 kleink ** determinable.
500 1.1.1.1 kleink */
501 1.1.1.1 kleink continue;
502 1.1.1.1 kleink case 'z':
503 1.1.1.1 kleink {
504 1.1.1.1 kleink int diff;
505 1.1.1.1 kleink char const * sign;
506 1.1.1.1 kleink
507 1.1.1.1 kleink if (t->tm_isdst < 0)
508 1.1.1.1 kleink continue;
509 1.1.1.1 kleink #ifdef TM_GMTOFF
510 1.1.1.1 kleink diff = t->TM_GMTOFF;
511 1.1.1.1 kleink #else /* !defined TM_GMTOFF */
512 1.1.1.1 kleink /*
513 1.1.1.1 kleink ** C99 says that the UTC offset must
514 1.1.1.1 kleink ** be computed by looking only at
515 1.1.1.1 kleink ** tm_isdst. This requirement is
516 1.1.1.1 kleink ** incorrect, since it means the code
517 1.1.1.1 kleink ** must rely on magic (in this case
518 1.1.1.1 kleink ** altzone and timezone), and the
519 1.1.1.1 kleink ** magic might not have the correct
520 1.1.1.1 kleink ** offset. Doing things correctly is
521 1.1.1.1 kleink ** tricky and requires disobeying C99;
522 1.1.1.1 kleink ** see GNU C strftime for details.
523 1.1.1.1 kleink ** For now, punt and conform to the
524 1.1.1.1 kleink ** standard, even though it's incorrect.
525 1.1.1.1 kleink **
526 1.1.1.1 kleink ** C99 says that %z must be replaced by the
527 1.1.1.1 kleink ** empty string if the time zone is not
528 1.1.1.1 kleink ** determinable, so output nothing if the
529 1.1.1.1 kleink ** appropriate variables are not available.
530 1.1.1.1 kleink */
531 1.1.1.1 kleink if (t->tm_isdst == 0)
532 1.1.1.1 kleink #ifdef USG_COMPAT
533 1.1.1.1 kleink diff = -timezone;
534 1.1.1.1 kleink #else /* defined USG_COMPAT */
535 1.1.1.1 kleink continue;
536 1.1.1.1 kleink #endif /* !defined USG_COMPAT */
537 1.1.1.1 kleink else
538 1.1.1.1 kleink #ifdef ALTZONE
539 1.1.1.1 kleink diff = -altzone;
540 1.1.1.1 kleink #else /* !defined ALTZONE */
541 1.1.1.1 kleink continue;
542 1.1.1.1 kleink #endif /* !defined ALTZONE */
543 1.1.1.1 kleink #endif /* !defined TM_GMTOFF */
544 1.1.1.1 kleink if (diff < 0) {
545 1.1.1.1 kleink sign = "-";
546 1.1.1.1 kleink diff = -diff;
547 1.1.1.1 kleink } else sign = "+";
548 1.1.1.1 kleink pt = _add(sign, pt, ptlim);
549 1.1.1.1 kleink diff /= 60;
550 1.1.1.1 kleink pt = _conv((diff/60)*100 + diff%60,
551 1.1.1.1 kleink "%04d", pt, ptlim);
552 1.1.1.1 kleink }
553 1.1.1.1 kleink continue;
554 1.1.1.1 kleink case '+':
555 1.1.1.1 kleink pt = _fmt(Locale->date_fmt, t, pt, ptlim,
556 1.1.1.1 kleink warnp);
557 1.1 mrg continue;
558 1.1 mrg case '%':
559 1.1 mrg /*
560 1.1.1.1 kleink ** X311J/88-090 (4.12.3.5): if conversion char is
561 1.1.1.1 kleink ** undefined, behavior is undefined. Print out the
562 1.1.1.1 kleink ** character itself as printf(3) also does.
563 1.1.1.1 kleink */
564 1.1 mrg default:
565 1.1 mrg break;
566 1.1 mrg }
567 1.1 mrg }
568 1.1.1.1 kleink if (pt == ptlim)
569 1.1.1.1 kleink break;
570 1.1 mrg *pt++ = *format;
571 1.1 mrg }
572 1.1.1.1 kleink return pt;
573 1.1 mrg }
574 1.1 mrg
575 1.1.1.1 kleink static char *
576 1.1.1.1 kleink _conv(n, format, pt, ptlim)
577 1.1.1.1 kleink const int n;
578 1.1.1.1 kleink const char * const format;
579 1.1.1.1 kleink char * const pt;
580 1.1.1.1 kleink const char * const ptlim;
581 1.1 mrg {
582 1.1.1.1 kleink char buf[INT_STRLEN_MAXIMUM(int) + 1];
583 1.1.1.1 kleink
584 1.1.1.1 kleink (void) sprintf(buf, format, n);
585 1.1.1.1 kleink return _add(buf, pt, ptlim);
586 1.1 mrg }
587 1.1 mrg
588 1.1.1.1 kleink static char *
589 1.1.1.1 kleink _add(str, pt, ptlim)
590 1.1.1.1 kleink const char * str;
591 1.1.1.1 kleink char * pt;
592 1.1.1.1 kleink const char * const ptlim;
593 1.1 mrg {
594 1.1.1.1 kleink while (pt < ptlim && (*pt = *str++) != '\0')
595 1.1.1.1 kleink ++pt;
596 1.1.1.1 kleink return pt;
597 1.1 mrg }
598 1.1 mrg
599 1.1.1.1 kleink #ifdef LOCALE_HOME
600 1.1.1.1 kleink static struct lc_time_T *
601 1.1.1.1 kleink _loc P((void))
602 1.1 mrg {
603 1.1.1.1 kleink static const char locale_home[] = LOCALE_HOME;
604 1.1.1.1 kleink static const char lc_time[] = "LC_TIME";
605 1.1.1.1 kleink static char * locale_buf;
606 1.1.1.1 kleink static char locale_buf_C[] = "C";
607 1.1.1.1 kleink
608 1.1.1.1 kleink int fd;
609 1.1.1.1 kleink int oldsun; /* "...ain't got nothin' to do..." */
610 1.1.1.1 kleink char * lbuf;
611 1.1.1.1 kleink char * name;
612 1.1.1.1 kleink char * p;
613 1.1.1.1 kleink const char ** ap;
614 1.1.1.1 kleink const char * plim;
615 1.1.1.1 kleink char filename[FILENAME_MAX];
616 1.1.1.1 kleink struct stat st;
617 1.1.1.1 kleink size_t namesize;
618 1.1.1.1 kleink size_t bufsize;
619 1.1.1.1 kleink
620 1.1.1.1 kleink /*
621 1.1.1.1 kleink ** Use localebuf.mon[0] to signal whether locale is already set up.
622 1.1.1.1 kleink */
623 1.1.1.1 kleink if (localebuf.mon[0])
624 1.1.1.1 kleink return &localebuf;
625 1.1.1.1 kleink name = setlocale(LC_TIME, (char *) NULL);
626 1.1.1.1 kleink if (name == NULL || *name == '\0')
627 1.1.1.1 kleink goto no_locale;
628 1.1.1.1 kleink /*
629 1.1.1.1 kleink ** If the locale name is the same as our cache, use the cache.
630 1.1.1.1 kleink */
631 1.1.1.1 kleink lbuf = locale_buf;
632 1.1.1.1 kleink if (lbuf != NULL && strcmp(name, lbuf) == 0) {
633 1.1.1.1 kleink p = lbuf;
634 1.1.1.1 kleink for (ap = (const char **) &localebuf;
635 1.1.1.1 kleink ap < (const char **) (&localebuf + 1);
636 1.1.1.1 kleink ++ap)
637 1.1.1.1 kleink *ap = p += strlen(p) + 1;
638 1.1.1.1 kleink return &localebuf;
639 1.1.1.1 kleink }
640 1.1.1.1 kleink /*
641 1.1.1.1 kleink ** Slurp the locale file into the cache.
642 1.1.1.1 kleink */
643 1.1.1.1 kleink namesize = strlen(name) + 1;
644 1.1.1.1 kleink if (sizeof(filename) <
645 1.1.1.1 kleink sizeof(locale_home) + namesize + sizeof(lc_time))
646 1.1.1.1 kleink goto no_locale;
647 1.1.1.1 kleink oldsun = 0;
648 1.1.1.1 kleink (void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
649 1.1.1.1 kleink fd = open(filename, O_RDONLY);
650 1.1.1.1 kleink if (fd < 0) {
651 1.1.1.1 kleink /*
652 1.1.1.1 kleink ** Old Sun systems have a different naming and data convention.
653 1.1.1.1 kleink */
654 1.1.1.1 kleink oldsun = 1;
655 1.1.1.1 kleink (void) sprintf(filename, "%s/%s/%s", locale_home,
656 1.1.1.1 kleink lc_time, name);
657 1.1.1.1 kleink fd = open(filename, O_RDONLY);
658 1.1.1.1 kleink if (fd < 0)
659 1.1.1.1 kleink goto no_locale;
660 1.1.1.1 kleink }
661 1.1.1.1 kleink if (fstat(fd, &st) != 0)
662 1.1.1.1 kleink goto bad_locale;
663 1.1.1.1 kleink if (st.st_size <= 0)
664 1.1.1.1 kleink goto bad_locale;
665 1.1.1.1 kleink bufsize = namesize + st.st_size;
666 1.1.1.1 kleink locale_buf = NULL;
667 1.1.1.1 kleink lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
668 1.1.1.1 kleink malloc(bufsize) : realloc(lbuf, bufsize);
669 1.1.1.1 kleink if (lbuf == NULL)
670 1.1.1.1 kleink goto bad_locale;
671 1.1.1.1 kleink (void) strcpy(lbuf, name);
672 1.1.1.1 kleink p = lbuf + namesize;
673 1.1.1.1 kleink plim = p + st.st_size;
674 1.1.1.1 kleink if (read(fd, p, (size_t) st.st_size) != st.st_size)
675 1.1.1.1 kleink goto bad_lbuf;
676 1.1.1.1 kleink if (close(fd) != 0)
677 1.1.1.1 kleink goto bad_lbuf;
678 1.1.1.1 kleink /*
679 1.1.1.1 kleink ** Parse the locale file into localebuf.
680 1.1.1.1 kleink */
681 1.1.1.1 kleink if (plim[-1] != '\n')
682 1.1.1.1 kleink goto bad_lbuf;
683 1.1.1.1 kleink for (ap = (const char **) &localebuf;
684 1.1.1.1 kleink ap < (const char **) (&localebuf + 1);
685 1.1.1.1 kleink ++ap) {
686 1.1.1.1 kleink if (p == plim)
687 1.1.1.1 kleink goto bad_lbuf;
688 1.1.1.1 kleink *ap = p;
689 1.1.1.1 kleink while (*p != '\n')
690 1.1.1.1 kleink ++p;
691 1.1.1.1 kleink *p++ = '\0';
692 1.1.1.1 kleink }
693 1.1.1.1 kleink if (oldsun) {
694 1.1.1.1 kleink /*
695 1.1.1.1 kleink ** SunOS 4 used an obsolescent format; see localdtconv(3).
696 1.1.1.1 kleink ** c_fmt had the ``short format for dates and times together''
697 1.1.1.1 kleink ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
698 1.1.1.1 kleink ** date_fmt had the ``long format for dates''
699 1.1.1.1 kleink ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
700 1.1.1.1 kleink ** Discard the latter in favor of the former.
701 1.1.1.1 kleink */
702 1.1.1.1 kleink localebuf.date_fmt = localebuf.c_fmt;
703 1.1 mrg }
704 1.1.1.1 kleink /*
705 1.1.1.1 kleink ** Record the successful parse in the cache.
706 1.1.1.1 kleink */
707 1.1.1.1 kleink locale_buf = lbuf;
708 1.1.1.1 kleink
709 1.1.1.1 kleink return &localebuf;
710 1.1.1.1 kleink
711 1.1.1.1 kleink bad_lbuf:
712 1.1.1.1 kleink free(lbuf);
713 1.1.1.1 kleink bad_locale:
714 1.1.1.1 kleink (void) close(fd);
715 1.1.1.1 kleink no_locale:
716 1.1.1.1 kleink localebuf = C_time_locale;
717 1.1.1.1 kleink locale_buf = locale_buf_C;
718 1.1.1.1 kleink return &localebuf;
719 1.1 mrg }
720 1.1.1.1 kleink #endif /* defined LOCALE_HOME */
721