2 Copyright © 1995-2004, 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
, struct Hook
*hook
,
19 struct Locale
*locale
);
20 VOID
_WriteChar(char token
, struct Hook
*hook
, struct Locale
*locale
);
21 VOID
_WriteString(STRPTR string
, struct Hook
*hook
, struct Locale
*locale
);
23 static const ULONG dayspermonth
[13] = {0 /* not used */,0,31,59,90,120,151,181,212,243,273,304,334};
25 /*****************************************************************************
28 #include <proto/locale.h>
30 AROS_LH4(VOID
, FormatDate
,
33 AROS_LHA(struct Locale
*, locale
, A0
),
34 AROS_LHA(STRPTR
, formatString
, A1
),
35 AROS_LHA(struct DateStamp
*, date
, A2
),
36 AROS_LHA(struct Hook
*, hook
, A3
),
39 struct LocaleBase
*, LocaleBase
, 10, Locale
)
43 Generate a date string based on a template. The bytes generated are sent
44 to a user specified callback function.
48 locale -- the locale to use when formatting the string
49 formatString -- the formatting template string; this is much like the
50 printf() formatting style, i.e. a % followed by a
51 formatting command. The following commands exist:
53 %a -- abbreviated weekday name
55 %b -- abbreviated month name
57 %c -- the same as "%a %b %d %H:%M:%S %Y"
58 %C -- the same as "%a %b %e %T %Z %Y"
59 %d -- day number with leading zeros
60 %D -- the same as "%m/%d/%y"
61 %e -- day number with leading spaces
62 %h -- abbreviated month name
63 %H -- hour using 24 hour style with leading zeros
64 %I -- hour using 12 hour style with leading zeros
66 %m -- month number with leading zeros
67 %M -- the number of minutes with leading zeros
70 %q -- hour using 24 hour style
71 %Q -- hour using 12 hour style
72 %r -- the same as "%I:%M:%S %p"
73 %R -- the same as "%H:%M"
74 %S -- the number of seconds with leading zeros
76 %T -- the same as "%H:%M:%S"
77 %U -- the week number, taking Sunday as the first day
79 %w -- the weekday number
80 %W -- the week number, taking Monday as the first day
82 %x -- the same as "%m/%d/%y"
83 %X -- the same as "%H:%M:%S"
84 %y -- the year using two digits with leading zeros
85 %Y -- the year using four digits with leading zeros
87 If the template parameter is NULL, a single null byte
88 is sent to the callback function.
90 date -- the current date
91 hook -- callback function; this is called for every character
92 generated with the following arguments:
94 * pointer to hook structure
108 ParseDate(), <libraries/locale.h>
114 17.01.2000 bergers implemented U & W, j is still missing
115 07.07.1999 SDuvan implemented (U, W, j not done yet)
117 *****************************************************************************/
121 struct ClockData cData
;
122 ULONG week
, days
, tmp
;
124 if(/* locale == NULL || */ hook
== NULL
)
127 if(formatString
== NULL
)
129 _WriteChar(0, hook
, locale
);
133 #warning Amiga2Date will fail around year 2114, because then the numer of seconds since 1978 dont fit in a 32 bit variable anymore!
135 Amiga2Date(date
->ds_Days
*86400 + date
->ds_Minute
*60 + date
->ds_Tick
/ 50,
138 while(*formatString
!= 0)
140 if(*formatString
== '%')
142 switch(*(++formatString
))
145 _WriteString(GetLocaleStr(locale
, ABDAY_1
+ cData
.wday
), hook
,
150 _WriteString(GetLocaleStr(locale
, DAY_1
+ cData
.wday
), hook
,
155 _WriteString(GetLocaleStr(locale
, ABMON_1
+ cData
.month
- 1),
160 _WriteString(GetLocaleStr(locale
, MON_1
+ cData
.month
- 1), hook
,
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
, ABMON_1
+ cData
.month
- 1),
191 PrintDigits(cData
.hour
, '0', 2, hook
, locale
);
195 PrintDigits(cData
.hour
% 12, '0', 2, hook
, locale
);
200 #warning Julian date not tested.
201 /* Julian date is DDD (1 - 366)*/
203 cData
.mday
+ dayspermonth
[cData
.month
],
212 PrintDigits(cData
.month
, '0', 2, hook
, locale
);
216 PrintDigits(cData
.min
, '0', 2, hook
, locale
);
220 _WriteChar('\n', hook
, locale
);
224 _WriteString(GetLocaleStr(locale
,
225 cData
.hour
< 12 ? AM_STR
: PM_STR
),
230 PrintDigits(cData
.hour
, -1, 2, hook
, locale
);
234 PrintDigits(cData
.hour
% 12, -1, 2, hook
, locale
);
238 FormatDate(locale
, "%I:%M:%S %p", date
, hook
);
242 FormatDate(locale
, "%H:%M", date
, hook
);
246 PrintDigits(cData
.sec
, '0', 2, hook
, locale
);
250 _WriteChar('\t', hook
, locale
);
255 FormatDate(locale
, "%H:%M:%S", date
, hook
);
258 case 'W': /* week number, Monday first day of week */
259 case 'U': /* week number, Sunday first day of week */
260 days
= cData
.mday
+ dayspermonth
[cData
.month
];
263 if (0 == (cData
.year
% 4) && cData
.month
> 2)
266 ** 1700, 1800, 1900, 2100, 2200 re not leap years.
267 ** 2000 is a leap year.
268 ** -> if a year is divisible by 100 but not by 400 then
269 ** it is not a leap year!
271 if (0 == (cData
.year
% 100) && 0 != (cData
.year
% 400))
278 ** If January 1st is a Monday then the first week
279 ** will start with a Sunday January 7th if Sunday is the first day of the week
280 ** but if Monday is the first day of the week then Jan 1st will also be the
281 ** first day of the first week.
284 ** Go to Saturday = last day of week if Sunday is first day of week
285 ** Go to Sunday = last day of week if Monday is first day of week
287 if ('U' == *formatString
)
289 /* Sunday is first day of the week */
290 tmp
= days
+ (6 - cData
.wday
);
294 /* Monday is first day of week */
296 tmp
= days
+ (7 - cData
.wday
);
305 /* cut off the few days that belong to week 0 */
307 /* Calculate the full amount of weeks */
311 PrintDigits(week
, '0', 2, hook
, locale
);
315 PrintDigits(cData
.wday
, -1, 1, hook
, locale
);
319 PrintDigits(cData
.year
% 100, '0', 2, hook
, locale
);
323 PrintDigits(cData
.year
, '0', 4, hook
, locale
);
327 /* cuurent time zone Unimplemented in 3.1 */
334 _WriteChar(*formatString
, hook
, locale
);
340 _WriteChar(*formatString
, hook
, locale
);
346 _WriteChar(0, hook
, locale
); /* Write null terminator */
352 VOID
_WriteString(STRPTR string
, struct Hook
*hook
, struct Locale
*locale
)
356 _WriteChar(*string
++, hook
, locale
);
361 VOID
_WriteChar(char token
, struct Hook
*hook
, struct Locale
*locale
)
363 AROS_UFC3(VOID
, hook
->h_Entry
,
364 AROS_UFCA(struct Hook
*, hook
, A0
),
365 AROS_UFCA(struct Locale
*, locale
, A2
),
366 AROS_UFCA(char, token
, A1
)
371 VOID
PrintDigits(UWORD number
, char fill
, UWORD len
, struct Hook
*hook
,
372 struct Locale
*locale
)
380 while((number
|| !i
) && i
< len
)
382 *--ptr
= number
% 10 + '0';
387 while(len
- i
> 0 && (char)-1 != fill
)
390 _WriteChar(fill
, hook
, locale
);
393 _WriteString((char *)ptr
, hook
, locale
);