2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
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>
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 /*****************************************************************************
31 #include <proto/locale.h>
33 AROS_LH4(VOID
, FormatDate
,
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
),
42 struct LocaleBase
*, LocaleBase
, 10, Locale
)
46 Generate a date string based on a template. The bytes generated are sent
47 to a user specified callback function.
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
58 %b -- abbreviated 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
69 %m -- month number with leading zeros
70 %M -- the number of minutes with leading zeros
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
79 %T -- the same as "%H:%M:%S"
80 %U -- the week number, taking Sunday as the first day
82 %w -- the weekday number
83 %W -- the week number, taking Monday as the first day
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
111 ParseDate(), <libraries/locale.h>
115 *****************************************************************************/
119 struct ClockData cData
;
120 ULONG week
, days
, tmp
;
122 if ( /* locale == NULL || */ hook
== NULL
)
125 if (formatString
== NULL
)
127 _WriteChar(0, hook
, locale
);
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
))
145 _WriteString(GetLocaleStr(locale
, ABDAY_1
+ cData
.wday
),
150 _WriteString(GetLocaleStr(locale
, DAY_1
+ cData
.wday
), hook
,
155 _WriteString(GetLocaleStr(locale
,
156 ABMON_1
+ cData
.month
- 1), hook
, locale
);
160 _WriteString(GetLocaleStr(locale
, MON_1
+ cData
.month
- 1),
165 FormatDate(locale
, "%a %b %d %H:%M:%S %Y", date
, hook
);
169 FormatDate(locale
, "%a %b %e %T %Z %Y", date
, hook
);
173 PrintDigits(cData
.mday
, '0', 2, hook
, locale
);
178 FormatDate(locale
, "%m/%d/%y", date
, hook
);
182 PrintDigits(cData
.mday
, ' ', 2, hook
, locale
);
186 _WriteString(GetLocaleStr(locale
,
187 ABMON_1
+ cData
.month
- 1), hook
, locale
);
191 PrintDigits(cData
.hour
, '0', 2, hook
, locale
);
195 PrintDigits(cData
.hour
% 12, '0', 2, hook
, locale
);
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
);
206 PrintDigits(cData
.month
, '0', 2, hook
, locale
);
210 PrintDigits(cData
.min
, '0', 2, hook
, locale
);
214 _WriteChar('\n', hook
, locale
);
218 _WriteString(GetLocaleStr(locale
,
219 cData
.hour
< 12 ? AM_STR
: PM_STR
), hook
, locale
);
223 PrintDigits(cData
.hour
, -1, 2, hook
, locale
);
227 PrintDigits(cData
.hour
% 12, -1, 2, hook
, locale
);
231 FormatDate(locale
, "%I:%M:%S %p", date
, hook
);
235 FormatDate(locale
, "%H:%M", date
, hook
);
239 PrintDigits(cData
.sec
, '0', 2, hook
, locale
);
243 _WriteChar('\t', hook
, locale
);
248 FormatDate(locale
, "%H:%M:%S", date
, hook
);
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
];
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))
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
);
287 /* Monday is first day of week */
289 tmp
= days
+ (7 - cData
.wday
);
298 /* cut off the few days that belong to week 0 */
300 /* Calculate the full amount of weeks */
304 PrintDigits(week
, '0', 2, hook
, locale
);
308 PrintDigits(cData
.wday
, -1, 1, hook
, locale
);
312 PrintDigits(cData
.year
% 100, '0', 2, hook
, locale
);
316 PrintDigits(cData
.year
, '0', 4, hook
, locale
);
320 /* cuurent time zone Unimplemented in 3.1 */
327 _WriteChar(*formatString
, hook
, locale
);
333 _WriteChar(*formatString
, hook
, locale
);
339 _WriteChar(0, hook
, locale
); /* Write null terminator */
345 VOID
_WriteString(CONST_STRPTR string
, const struct Hook
*hook
,
346 const struct Locale
*locale
)
350 _WriteChar(*string
++, hook
, locale
);
355 VOID
_WriteChar(char token
, const struct Hook
*hook
,
356 const struct Locale
*locale
)
358 AROS_UFC3NR(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
)
374 while ((number
|| !i
) && i
< len
)
376 *--ptr
= number
% 10 + '0';
381 while (len
- i
> 0 && (char)-1 != fill
)
384 _WriteChar(fill
, hook
, locale
);
387 _WriteString((char *)ptr
, hook
, locale
);