Mercurial > projects > dwt2
comparison base/src/java/mangoicu/UDateFormat.d @ 27:1bf55a6eb092
Renamed java tree to base
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 21 Mar 2009 11:33:57 +0100 |
parents | java/src/java/mangoicu/UDateFormat.d@9b96950f2c3c |
children |
comparison
equal
deleted
inserted
replaced
26:f589fc20a5f9 | 27:1bf55a6eb092 |
---|---|
1 /******************************************************************************* | |
2 | |
3 @file UDateFormat.d | |
4 | |
5 Copyright (c) 2004 Kris Bell | |
6 | |
7 This software is provided 'as-is', without any express or implied | |
8 warranty. In no event will the authors be held liable for damages | |
9 of any kind arising from the use of this software. | |
10 | |
11 Permission is hereby granted to anyone to use this software for any | |
12 purpose, including commercial applications, and to alter it and/or | |
13 redistribute it freely, subject to the following restrictions: | |
14 | |
15 1. The origin of this software must not be misrepresented; you must | |
16 not claim that you wrote the original software. If you use this | |
17 software in a product, an acknowledgment within documentation of | |
18 said product would be appreciated but is not required. | |
19 | |
20 2. Altered source versions must be plainly marked as such, and must | |
21 not be misrepresented as being the original software. | |
22 | |
23 3. This notice may not be removed or altered from any distribution | |
24 of the source. | |
25 | |
26 4. Derivative works are permitted, but they must carry this notice | |
27 in full and credit the original source. | |
28 | |
29 | |
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
31 | |
32 | |
33 @version Initial version, November 2004 | |
34 @author Kris | |
35 | |
36 Note that this package and documentation is built around the ICU | |
37 project (http://oss.software.ibm.com/icu/). Below is the license | |
38 statement as specified by that software: | |
39 | |
40 | |
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
42 | |
43 | |
44 ICU License - ICU 1.8.1 and later | |
45 | |
46 COPYRIGHT AND PERMISSION NOTICE | |
47 | |
48 Copyright (c) 1995-2003 International Business Machines Corporation and | |
49 others. | |
50 | |
51 All rights reserved. | |
52 | |
53 Permission is hereby granted, free of charge, to any person obtaining a | |
54 copy of this software and associated documentation files (the | |
55 "Software"), to deal in the Software without restriction, including | |
56 without limitation the rights to use, copy, modify, merge, publish, | |
57 distribute, and/or sell copies of the Software, and to permit persons | |
58 to whom the Software is furnished to do so, provided that the above | |
59 copyright notice(s) and this permission notice appear in all copies of | |
60 the Software and that both the above copyright notice(s) and this | |
61 permission notice appear in supporting documentation. | |
62 | |
63 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
64 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
65 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT | |
66 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
67 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL | |
68 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING | |
69 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | |
70 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | |
71 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
72 | |
73 Except as contained in this notice, the name of a copyright holder | |
74 shall not be used in advertising or otherwise to promote the sale, use | |
75 or other dealings in this Software without prior written authorization | |
76 of the copyright holder. | |
77 | |
78 ---------------------------------------------------------------------- | |
79 | |
80 All trademarks and registered trademarks mentioned herein are the | |
81 property of their respective owners. | |
82 | |
83 *******************************************************************************/ | |
84 | |
85 module java.mangoicu.UDateFormat; | |
86 | |
87 private import java.mangoicu.ICU, | |
88 java.mangoicu.UString, | |
89 java.mangoicu.UCalendar, | |
90 java.mangoicu.UNumberFormat; | |
91 | |
92 /******************************************************************************* | |
93 | |
94 UDateFormat consists of functions that convert dates and | |
95 times from their internal representations to textual form and back | |
96 again in a language-independent manner. Converting from the internal | |
97 representation (milliseconds since midnight, January 1, 1970) to text | |
98 is known as "formatting," and converting from text to millis is known | |
99 as "parsing." We currently define one concrete structure UDateFormat, | |
100 which can handle pretty much all normal date formatting and parsing | |
101 actions. | |
102 | |
103 UDateFormat helps you to format and parse dates for any locale. | |
104 Your code can be completely independent of the locale conventions | |
105 for months, days of the week, or even the calendar format: lunar | |
106 vs. solar. | |
107 | |
108 See <A HREF="http://oss.software.ibm.com/icu/apiref/udat_8h.html"> | |
109 this page</A> for full details. | |
110 | |
111 *******************************************************************************/ | |
112 | |
113 private class UDateFormat : ICU | |
114 { | |
115 private Handle handle; | |
116 | |
117 alias UCalendar.UDate UDate; | |
118 | |
119 typedef void* UFieldPos; | |
120 | |
121 public enum Style | |
122 { | |
123 Full, | |
124 Long, | |
125 Medium, | |
126 Short, | |
127 Default = Medium, | |
128 None = -1, | |
129 Ignore = -2 | |
130 }; | |
131 | |
132 public enum Field | |
133 { | |
134 EraField = 0, | |
135 YearField = 1, | |
136 MonthField = 2, | |
137 DateField = 3, | |
138 HourOfDay1Field = 4, | |
139 HourOfDay0Field = 5, | |
140 MinuteField = 6, | |
141 SecondField = 7, | |
142 FractionalSecondField = 8, | |
143 DayOfWeekField = 9, | |
144 DayOfYearField = 10, | |
145 DayOfWeekInMonthField = 11, | |
146 WeekOfYearField = 12, | |
147 WeekOfMonthField = 13, | |
148 AmPmField = 14, | |
149 Hour1Field = 15, | |
150 Hour0Field = 16, | |
151 TimezoneField = 17, | |
152 YearWoyField = 18, | |
153 DowLocalField = 19, | |
154 ExtendedYearField = 20, | |
155 JulianDayField = 21, | |
156 MillisecondsInDayField = 22, | |
157 TimezoneRfcField = 23, | |
158 FieldCount = 24 | |
159 }; | |
160 | |
161 private enum Symbol | |
162 { | |
163 Eras, | |
164 Months, | |
165 ShortMonths, | |
166 Weekdays, | |
167 ShortWeekdays, | |
168 AmPms, | |
169 LocalizedChars | |
170 }; | |
171 | |
172 | |
173 /*********************************************************************** | |
174 | |
175 Open a new UDateFormat for formatting and parsing dates | |
176 and time. If a pattern is not specified, an appropriate | |
177 one for the given locale will be used. | |
178 | |
179 ***********************************************************************/ | |
180 | |
181 this (Style time, Style date, inout ULocale locale, inout UTimeZone tz, UStringView pattern=null) | |
182 { | |
183 UErrorCode e; | |
184 wchar* p; | |
185 uint c; | |
186 | |
187 if (pattern) | |
188 p = pattern.get.ptr, c = pattern.length; | |
189 handle = udat_open (time, date, ICU.toString(locale.name), cast(wchar*)tz.name.ptr, tz.name.length, p, c, e); | |
190 testError (e, "failed to create DateFormat"); | |
191 } | |
192 | |
193 /*********************************************************************** | |
194 | |
195 Close a UDateFormat | |
196 | |
197 ***********************************************************************/ | |
198 | |
199 ~this () | |
200 { | |
201 udat_close (handle); | |
202 } | |
203 | |
204 /*********************************************************************** | |
205 | |
206 Format a date using an UDateFormat | |
207 | |
208 ***********************************************************************/ | |
209 | |
210 void format (UString dst, UDate date, UFieldPos p = null) | |
211 { | |
212 uint fmat (wchar* result, uint len, inout UErrorCode e) | |
213 { | |
214 return udat_format (handle, date, result, len, p, e); | |
215 } | |
216 | |
217 dst.format (&fmat, "date format failed"); | |
218 } | |
219 | |
220 /*********************************************************************** | |
221 | |
222 Parse a string into an date/time using a UDateFormat | |
223 | |
224 ***********************************************************************/ | |
225 | |
226 UDate parse (UStringView src, uint* index=null) | |
227 { | |
228 UErrorCode e; | |
229 | |
230 UDate x = udat_parse (handle, src.content.ptr, src.len, index, e); | |
231 testError (e, "failed to parse date"); | |
232 return x; | |
233 } | |
234 | |
235 /*********************************************************************** | |
236 | |
237 Set the UCalendar associated with an UDateFormat. A | |
238 UDateFormat uses a UCalendar to convert a raw value | |
239 to, for example, the day of the week. | |
240 | |
241 ***********************************************************************/ | |
242 | |
243 void setCalendar (UCalendar c) | |
244 { | |
245 udat_setCalendar (handle, c.handle); | |
246 } | |
247 | |
248 /*********************************************************************** | |
249 | |
250 Get the UCalendar associated with this UDateFormat | |
251 | |
252 ***********************************************************************/ | |
253 | |
254 UCalendar getCalendar () | |
255 { | |
256 Handle h = udat_getCalendar (handle); | |
257 return new UCalendar (h); | |
258 } | |
259 | |
260 /*********************************************************************** | |
261 | |
262 Set the UNumberFormat associated with an UDateFormat.A | |
263 UDateFormat uses a UNumberFormat to format numbers within | |
264 a date, for example the day number. | |
265 | |
266 ***********************************************************************/ | |
267 | |
268 void setNumberFormat (UNumberFormat n) | |
269 { | |
270 udat_setCalendar (handle, n.handle); | |
271 } | |
272 | |
273 /*********************************************************************** | |
274 | |
275 Get the year relative to which all 2-digit years are | |
276 interpreted | |
277 | |
278 ***********************************************************************/ | |
279 | |
280 UDate getTwoDigitYearStart () | |
281 { | |
282 UErrorCode e; | |
283 | |
284 UDate x = udat_get2DigitYearStart (handle, e); | |
285 testError (e, "failed to get two digit year start"); | |
286 return x; | |
287 } | |
288 | |
289 /*********************************************************************** | |
290 | |
291 Set the year relative to which all 2-digit years are | |
292 interpreted | |
293 | |
294 ***********************************************************************/ | |
295 | |
296 void setTwoDigitYearStart (UDate start) | |
297 { | |
298 UErrorCode e; | |
299 | |
300 udat_set2DigitYearStart (handle, start, e); | |
301 testError (e, "failed to set two digit year start"); | |
302 } | |
303 | |
304 /*********************************************************************** | |
305 | |
306 Extract the pattern from a UDateFormat | |
307 | |
308 ***********************************************************************/ | |
309 | |
310 void getPattern (UString dst, bool localize) | |
311 { | |
312 uint fmat (wchar* result, uint len, inout UErrorCode e) | |
313 { | |
314 return udat_toPattern (handle, localize, result, len, e); | |
315 } | |
316 | |
317 dst.format (&fmat, "failed to retrieve date format pattern"); | |
318 } | |
319 | |
320 /*********************************************************************** | |
321 | |
322 Set the pattern for a UDateFormat | |
323 | |
324 ***********************************************************************/ | |
325 | |
326 void setPattern (UStringView pattern, bool localized) | |
327 { | |
328 udat_applyPattern (handle, localized, pattern.get.ptr, pattern.length); | |
329 } | |
330 | |
331 /*********************************************************************** | |
332 | |
333 Specify whether an UDateFormat will perform lenient parsing. | |
334 | |
335 ***********************************************************************/ | |
336 | |
337 void setLenient (bool yes) | |
338 { | |
339 udat_setLenient (handle, yes); | |
340 } | |
341 | |
342 /*********************************************************************** | |
343 | |
344 Determine if an UDateFormat will perform lenient parsing. | |
345 | |
346 ***********************************************************************/ | |
347 | |
348 bool isLenient () | |
349 { | |
350 return udat_isLenient (handle) != 0; | |
351 } | |
352 | |
353 | |
354 /*********************************************************************** | |
355 | |
356 Bind the ICU functions from a shared library. This is | |
357 complicated by the issues regarding D and DLLs on the | |
358 Windows platform | |
359 | |
360 ***********************************************************************/ | |
361 | |
362 private static void* library; | |
363 | |
364 /*********************************************************************** | |
365 | |
366 ***********************************************************************/ | |
367 | |
368 private static extern (C) | |
369 { | |
370 Handle function (uint, uint, char*, wchar*, uint, wchar*, uint, inout UErrorCode) udat_open; | |
371 void function (Handle) udat_close; | |
372 uint function (Handle, UDate, wchar*, uint, UFieldPos, inout UErrorCode) udat_format; | |
373 UDate function (Handle, wchar*, uint, uint*, inout UErrorCode) udat_parse; | |
374 void function (Handle, Handle) udat_setCalendar; | |
375 void function (Handle, Handle) udat_setNumberFormat; | |
376 UDate function (Handle, inout UErrorCode) udat_get2DigitYearStart; | |
377 void function (Handle, UDate, inout UErrorCode) udat_set2DigitYearStart; | |
378 uint function (Handle, byte, wchar*, uint, inout UErrorCode) udat_toPattern; | |
379 void function (Handle, byte, wchar*, uint) udat_applyPattern; | |
380 void function (Handle, byte) udat_setLenient; | |
381 byte function (Handle) udat_isLenient; | |
382 Handle function (Handle) udat_getCalendar; | |
383 } | |
384 | |
385 /*********************************************************************** | |
386 | |
387 ***********************************************************************/ | |
388 | |
389 static FunctionLoader.Bind[] targets = | |
390 [ | |
391 {cast(void**) &udat_open, "udat_open"}, | |
392 {cast(void**) &udat_close, "udat_close"}, | |
393 {cast(void**) &udat_format, "udat_format"}, | |
394 {cast(void**) &udat_parse, "udat_parse"}, | |
395 {cast(void**) &udat_setCalendar, "udat_setCalendar"}, | |
396 {cast(void**) &udat_setNumberFormat, "udat_setNumberFormat"}, | |
397 {cast(void**) &udat_get2DigitYearStart, "udat_get2DigitYearStart"}, | |
398 {cast(void**) &udat_set2DigitYearStart, "udat_set2DigitYearStart"}, | |
399 {cast(void**) &udat_toPattern, "udat_toPattern"}, | |
400 {cast(void**) &udat_applyPattern, "udat_applyPattern"}, | |
401 {cast(void**) &udat_setLenient, "udat_setLenient"}, | |
402 {cast(void**) &udat_isLenient, "udat_isLenient"}, | |
403 {cast(void**) &udat_getCalendar, "udat_getCalendar"}, | |
404 ]; | |
405 | |
406 /*********************************************************************** | |
407 | |
408 ***********************************************************************/ | |
409 | |
410 static this () | |
411 { | |
412 library = FunctionLoader.bind (icuin, targets); | |
413 } | |
414 | |
415 /*********************************************************************** | |
416 | |
417 ***********************************************************************/ | |
418 | |
419 static ~this () | |
420 { | |
421 FunctionLoader.unbind (library); | |
422 } | |
423 } | |
424 | |
425 | |
426 |