Commit | Line | Data |
---|---|---|
e9fe19df KB |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | */ | |
17 | ||
18 | #if defined(LIBC_SCCS) && !defined(lint) | |
e2abfca3 | 19 | static char sccsid[] = "@(#)strftime.c 5.3 (Berkeley) %G%"; |
e9fe19df KB |
20 | #endif /* LIBC_SCCS and not lint */ |
21 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/time.h> | |
24 | #include <tzfile.h> | |
25 | ||
26 | static char *afmt[] = { | |
27 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", | |
28 | }; | |
29 | static char *Afmt[] = { | |
30 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | |
31 | "Saturday", | |
32 | }; | |
33 | static char *bfmt[] = { | |
34 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", | |
35 | "Oct", "Nov", "Dec", | |
36 | }; | |
37 | static char *Bfmt[] = { | |
38 | "January", "February", "March", "April", "May", "June", "July", | |
39 | "August", "September", "October", "November", "December", | |
40 | }; | |
41 | ||
42 | static size_t gsize; | |
43 | static char *pt; | |
44 | ||
45 | size_t | |
46 | strftime(s, maxsize, format, t) | |
47 | char *s; | |
48 | char *format; | |
49 | size_t maxsize; | |
50 | struct tm *t; | |
51 | { | |
52 | size_t _fmt(); | |
53 | ||
54 | pt = s; | |
55 | if ((gsize = maxsize) < 1) | |
56 | return(0); | |
57 | if (_fmt(format, t)) { | |
58 | *pt = '\0'; | |
59 | return(maxsize - gsize); | |
60 | } | |
61 | return(0); | |
62 | } | |
63 | ||
64 | static size_t | |
65 | _fmt(format, t) | |
66 | register char *format; | |
67 | struct tm *t; | |
68 | { | |
e9fe19df KB |
69 | for (; *format; ++format) { |
70 | if (*format == '%') | |
71 | switch(*++format) { | |
e2abfca3 KB |
72 | case '\0': |
73 | --format; | |
74 | break; | |
e9fe19df | 75 | case 'A': |
e2abfca3 KB |
76 | if (t->tm_wday < 0 || t->tm_wday > 6) |
77 | return(0); | |
78 | if (!_add(Afmt[t->tm_wday])) | |
e9fe19df KB |
79 | return(0); |
80 | continue; | |
81 | case 'a': | |
e2abfca3 KB |
82 | if (t->tm_wday < 0 || t->tm_wday > 6) |
83 | return(0); | |
84 | if (!_add(afmt[t->tm_wday])) | |
e9fe19df KB |
85 | return(0); |
86 | continue; | |
87 | case 'B': | |
e2abfca3 KB |
88 | if (t->tm_mon < 0 || t->tm_mon > 11) |
89 | return(0); | |
e9fe19df KB |
90 | if (!_add(Bfmt[t->tm_mon])) |
91 | return(0); | |
92 | continue; | |
93 | case 'b': | |
e386ba1e | 94 | case 'h': |
e2abfca3 KB |
95 | if (t->tm_mon < 0 || t->tm_mon > 11) |
96 | return(0); | |
e9fe19df KB |
97 | if (!_add(bfmt[t->tm_mon])) |
98 | return(0); | |
99 | continue; | |
100 | case 'c': | |
101 | if (!_fmt("%x %X %Z %Y", t)) | |
102 | return(0); | |
103 | continue; | |
e386ba1e KB |
104 | case 'D': |
105 | if (!_fmt("%m/%d/%y", t)) | |
106 | return(0); | |
107 | continue; | |
e9fe19df KB |
108 | case 'd': |
109 | if (!_conv(t->tm_mday, 2)) | |
110 | return(0); | |
111 | continue; | |
112 | case 'H': | |
113 | if (!_conv(t->tm_hour, 2)) | |
114 | return(0); | |
115 | continue; | |
116 | case 'I': | |
e2abfca3 KB |
117 | if (!_conv(t->tm_hour % 12 ? |
118 | t->tm_hour % 12 : 12, 2)) | |
e9fe19df KB |
119 | return(0); |
120 | continue; | |
121 | case 'j': | |
122 | if (!_conv(t->tm_yday + 1, 3)) | |
123 | return(0); | |
124 | continue; | |
125 | case 'M': | |
126 | if (!_conv(t->tm_min, 2)) | |
127 | return(0); | |
128 | continue; | |
129 | case 'm': | |
130 | if (!_conv(t->tm_mon + 1, 2)) | |
131 | return(0); | |
132 | continue; | |
e386ba1e KB |
133 | case 'n': |
134 | if (!_add("\n")) | |
135 | return(0); | |
136 | continue; | |
e9fe19df | 137 | case 'p': |
e2abfca3 | 138 | if (!_add(t->tm_hour >= 12 ? "PM" : "AM")) |
e9fe19df KB |
139 | return(0); |
140 | continue; | |
e386ba1e KB |
141 | case 'R': |
142 | if (!_fmt("%H:%M", t)) | |
143 | return(0); | |
144 | continue; | |
145 | case 'r': | |
146 | if (!_fmt("%I:%M:%S %p", t)) | |
147 | return(0); | |
148 | continue; | |
e9fe19df KB |
149 | case 'S': |
150 | if (!_conv(t->tm_sec, 2)) | |
151 | return(0); | |
152 | continue; | |
e386ba1e KB |
153 | case 'T': |
154 | case 'X': | |
155 | if (!_fmt("%H:%M:%S", t)) | |
156 | return(0); | |
157 | continue; | |
158 | case 't': | |
159 | if (!_add("\t")) | |
160 | return(0); | |
161 | continue; | |
e9fe19df KB |
162 | case 'U': |
163 | if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, | |
164 | 2)) | |
165 | return(0); | |
166 | continue; | |
167 | case 'W': | |
168 | if (!_conv((t->tm_yday + 7 - | |
e2abfca3 KB |
169 | (t->tm_wday ? (t->tm_wday - 1) : 6)) |
170 | / 7, 2)) | |
e9fe19df KB |
171 | return(0); |
172 | continue; | |
173 | case 'w': | |
174 | if (!_conv(t->tm_wday, 1)) | |
175 | return(0); | |
e9fe19df KB |
176 | continue; |
177 | case 'x': | |
e2abfca3 | 178 | if (!_fmt("%a %b %d %Y", t)) |
e9fe19df KB |
179 | return(0); |
180 | continue; | |
181 | case 'y': | |
182 | if (!_conv((t->tm_year + TM_YEAR_BASE) | |
183 | % 100, 2)) | |
184 | return(0); | |
185 | continue; | |
186 | case 'Y': | |
187 | if (!_conv(t->tm_year + TM_YEAR_BASE, 4)) | |
188 | return(0); | |
189 | continue; | |
190 | case 'Z': | |
e2abfca3 | 191 | if (!t->tm_zone || !_add(t->tm_zone)) |
e9fe19df KB |
192 | return(0); |
193 | continue; | |
194 | case '%': | |
195 | /* | |
196 | * X311J/88-090 (4.12.3.5): if conversion char is | |
197 | * undefined, behavior is undefined. Print out the | |
198 | * character itself as printf(3) also does. | |
199 | */ | |
200 | default: | |
201 | break; | |
202 | } | |
203 | if (!gsize--) | |
204 | return(0); | |
205 | *pt++ = *format; | |
206 | } | |
207 | return(gsize); | |
208 | } | |
209 | ||
210 | static | |
211 | _conv(n, digits) | |
212 | int n, digits; | |
213 | { | |
214 | static char buf[10]; | |
215 | register char *p; | |
216 | ||
217 | for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits) | |
218 | *p-- = n % 10 + '0'; | |
219 | while (p > buf && digits-- > 0) | |
220 | *p-- = '0'; | |
221 | return(_add(++p)); | |
222 | } | |
223 | ||
224 | static | |
225 | _add(str) | |
226 | register char *str; | |
227 | { | |
228 | for (;; ++pt, --gsize) { | |
229 | if (!gsize) | |
230 | return(0); | |
231 | if (!(*pt = *str++)) | |
232 | return(1); | |
233 | } | |
234 | } |