- Tabs to spaces.
[AROS.git] / workbench / libs / locale / formatdate.c
blob8db1b5782f4a46dd0647993da1cabab937058d63
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 #include "locale_intern.h"
10 #include <exec/types.h>
11 #include <utility/hooks.h>
12 #include <utility/date.h>
13 #include <proto/utility.h>
14 #include <clib/alib_protos.h>
16 #include <stdio.h>
18 VOID PrintDigits(UWORD number, char fill, UWORD len, const struct Hook *hook,
19 const struct Locale *locale);
20 VOID _WriteChar(char token, const struct Hook *hook,
21 const struct Locale *locale);
22 VOID _WriteString(CONST_STRPTR string, const struct Hook *hook,
23 const struct Locale *locale);
25 static const ULONG dayspermonth[13] =
26 {0 /* not used */,0,31,59,90,120,151,181,212,243,273,304,334};
28 /*****************************************************************************
30 NAME */
31 #include <proto/locale.h>
33 AROS_LH4(VOID, FormatDate,
35 /* SYNOPSIS */
36 AROS_LHA(const struct Locale *, locale, A0),
37 AROS_LHA(CONST_STRPTR, formatString, A1),
38 AROS_LHA(const struct DateStamp *, date, A2),
39 AROS_LHA(const struct Hook *, hook, A3),
41 /* LOCATION */
42 struct LocaleBase *, LocaleBase, 10, Locale)
44 /* FUNCTION
46 Generate a date string based on a template. The bytes generated are sent
47 to a user specified callback function.
49 INPUTS
51 locale -- the locale to use when formatting the string
52 formatString -- the formatting template string; this is much like the
53 printf() formatting style, i.e. a % followed by a
54 formatting command. The following commands exist:
56 %a -- abbreviated weekday name
57 %A -- weekday name
58 %b -- abbreviated month name
59 %B -- month name
60 %c -- the same as "%a %b %d %H:%M:%S %Y"
61 %C -- the same as "%a %b %e %T %Z %Y"
62 %d -- day number with leading zeros
63 %D -- the same as "%m/%d/%y"
64 %e -- day number with leading spaces
65 %h -- abbreviated month name
66 %H -- hour using 24 hour style with leading zeros
67 %I -- hour using 12 hour style with leading zeros
68 %j -- julian date
69 %m -- month number with leading zeros
70 %M -- the number of minutes with leading zeros
71 %n -- linefeed
72 %p -- AM or PM string
73 %q -- hour using 24 hour style
74 %Q -- hour using 12 hour style
75 %r -- the same as "%I:%M:%S %p"
76 %R -- the same as "%H:%M"
77 %S -- the number of seconds with leading zeros
78 %t -- tab
79 %T -- the same as "%H:%M:%S"
80 %U -- the week number, taking Sunday as the first day
81 of the week
82 %w -- the weekday number
83 %W -- the week number, taking Monday as the first day
84 of the week
85 %x -- the same as "%m/%d/%y"
86 %X -- the same as "%H:%M:%S"
87 %y -- the year using two digits with leading zeros
88 %Y -- the year using four digits with leading zeros
90 If the template parameter is NULL, a single null byte
91 is sent to the callback function.
93 date -- the current date
94 hook -- callback function; this is called for every character
95 generated with the following arguments:
97 * pointer to hook structure
98 * character
99 * pointer to locale
101 RESULT
103 NOTES
105 EXAMPLE
107 BUGS
109 SEE ALSO
111 ParseDate(), <libraries/locale.h>
113 INTERNALS
115 *****************************************************************************/
117 AROS_LIBFUNC_INIT
119 struct ClockData cData;
120 ULONG week, days, tmp;
122 if ( /* locale == NULL || */ hook == NULL)
123 return;
125 if (formatString == NULL)
127 _WriteChar(0, hook, locale);
128 return;
131 /* TODO: Amiga2Date will fail around year 2114, because then the
132 * number of seconds since 1978 won't fit in a 32 bit variable anymore!
135 Amiga2Date(date->ds_Days * 86400 + date->ds_Minute * 60 +
136 date->ds_Tick / 50, &cData);
138 while (*formatString != 0)
140 if (*formatString == '%')
142 switch (*(++formatString))
144 case 'a':
145 _WriteString(GetLocaleStr(locale, ABDAY_1 + cData.wday),
146 hook, locale);
147 break;
149 case 'A':
150 _WriteString(GetLocaleStr(locale, DAY_1 + cData.wday), hook,
151 locale);
152 break;
154 case 'b':
155 _WriteString(GetLocaleStr(locale,
156 ABMON_1 + cData.month - 1), hook, locale);
157 break;
159 case 'B':
160 _WriteString(GetLocaleStr(locale, MON_1 + cData.month - 1),
161 hook, locale);
162 break;
164 case 'c':
165 FormatDate(locale, "%a %b %d %H:%M:%S %Y", date, hook);
166 break;
168 case 'C':
169 FormatDate(locale, "%a %b %e %T %Z %Y", date, hook);
170 break;
172 case 'd':
173 PrintDigits(cData.mday, '0', 2, hook, locale);
174 break;
176 case 'x':
177 case 'D':
178 FormatDate(locale, "%m/%d/%y", date, hook);
179 break;
181 case 'e':
182 PrintDigits(cData.mday, ' ', 2, hook, locale);
183 break;
185 case 'h':
186 _WriteString(GetLocaleStr(locale,
187 ABMON_1 + cData.month - 1), hook, locale);
188 break;
190 case 'H':
191 PrintDigits(cData.hour, '0', 2, hook, locale);
192 break;
194 case 'I':
195 PrintDigits(cData.hour % 12, '0', 2, hook, locale);
196 break;
198 case 'j':
199 /* TODO: Julian date not tested. */
200 /* Julian date is DDD (1 - 366) */
201 PrintDigits(cData.mday + dayspermonth[cData.month],
202 '0', 3, hook, locale);
203 break;
205 case 'm':
206 PrintDigits(cData.month, '0', 2, hook, locale);
207 break;
209 case 'M':
210 PrintDigits(cData.min, '0', 2, hook, locale);
211 break;
213 case 'n':
214 _WriteChar('\n', hook, locale);
215 break;
217 case 'p':
218 _WriteString(GetLocaleStr(locale,
219 cData.hour < 12 ? AM_STR : PM_STR), hook, locale);
220 break;
222 case 'q':
223 PrintDigits(cData.hour, -1, 2, hook, locale);
224 break;
226 case 'Q':
227 PrintDigits(cData.hour % 12, -1, 2, hook, locale);
228 break;
230 case 'r':
231 FormatDate(locale, "%I:%M:%S %p", date, hook);
232 break;
234 case 'R':
235 FormatDate(locale, "%H:%M", date, hook);
236 break;
238 case 'S':
239 PrintDigits(cData.sec, '0', 2, hook, locale);
240 break;
242 case 't':
243 _WriteChar('\t', hook, locale);
244 break;
246 case 'X':
247 case 'T':
248 FormatDate(locale, "%H:%M:%S", date, hook);
249 break;
251 case 'W': /* week number, Monday first day of week */
252 case 'U': /* week number, Sunday first day of week */
253 days = cData.mday + dayspermonth[cData.month];
255 /* leap year ? */
256 if (0 == (cData.year % 4) && cData.month > 2)
259 ** 1700, 1800, 1900, 2100, 2200 re not leap years.
260 ** 2000 is a leap year.
261 ** -> if a year is divisible by 100 but not by 400 then
262 ** it is not a leap year!
264 if (0 == (cData.year % 100) && 0 != (cData.year % 400))
266 else
267 days++;
271 ** If January 1st is a Monday then the first week
272 ** will start with a Sunday January 7th if Sunday is the first day of the week
273 ** but if Monday is the first day of the week then Jan 1st will also be the
274 ** first day of the first week.
277 ** Go to Saturday = last day of week if Sunday is first day of week
278 ** Go to Sunday = last day of week if Monday is first day of week
280 if ('U' == *formatString)
282 /* Sunday is first day of the week */
283 tmp = days + (6 - cData.wday);
285 else
287 /* Monday is first day of week */
288 if (0 != cData.wday)
289 tmp = days + (7 - cData.wday);
290 else
291 tmp = days;
294 if (tmp < 7)
295 week = 0;
296 else
298 /* cut off the few days that belong to week 0 */
299 tmp -= (tmp % 7);
300 /* Calculate the full amount of weeks */
301 week = tmp / 7;
304 PrintDigits(week, '0', 2, hook, locale);
305 break;
307 case 'w':
308 PrintDigits(cData.wday, -1, 1, hook, locale);
309 break;
311 case 'y':
312 PrintDigits(cData.year % 100, '0', 2, hook, locale);
313 break;
315 case 'Y':
316 PrintDigits(cData.year, '0', 4, hook, locale);
317 break;
319 case 'Z':
320 /* cuurent time zone Unimplemented in 3.1 */
321 break;
323 case 0:
324 break;
326 default:
327 _WriteChar(*formatString, hook, locale);
328 break;
331 else
333 _WriteChar(*formatString, hook, locale);
336 formatString++;
339 _WriteChar(0, hook, locale); /* Write null terminator */
341 AROS_LIBFUNC_EXIT
345 VOID _WriteString(CONST_STRPTR string, const struct Hook *hook,
346 const struct Locale *locale)
348 while (*string != 0)
350 _WriteChar(*string++, hook, locale);
355 VOID _WriteChar(char token, const struct Hook *hook,
356 const struct Locale *locale)
358 AROS_UFC3(VOID, hook->h_Entry,
359 AROS_UFCA(const struct Hook *, hook, A0),
360 AROS_UFCA(const struct Locale *, locale, A2),
361 AROS_UFCA(char, token, A1));
365 VOID PrintDigits(UWORD number, char fill, UWORD len,
366 const struct Hook *hook, const struct Locale *locale)
368 char buf[7];
369 char *ptr = &buf[6];
370 int i = 0;
372 buf[6] = 0;
374 while ((number || !i) && i < len)
376 *--ptr = number % 10 + '0';
377 number /= 10;
378 i++;
381 while (len - i > 0 && (char)-1 != fill)
383 len--;
384 _WriteChar(fill, hook, locale);
387 _WriteString((char *)ptr, hook, locale);