Mercurial > projects > dwt2
comparison com.ibm.icu/src/com/ibm/icu/mangoicu/UCalendar.d @ 92:ebefa5c2eab4
moving ICU bindings to com.ibm.icu
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 13:49:38 +0200 |
parents | base/src/java/mangoicu/UCalendar.d@1bf55a6eb092 |
children | 536e43f63c81 |
comparison
equal
deleted
inserted
replaced
91:2755ef2c8ef8 | 92:ebefa5c2eab4 |
---|---|
1 /******************************************************************************* | |
2 | |
3 @file UCalendar.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 com.ibm.icu.mangoicu.UCalendar; | |
86 | |
87 private import com.ibm.icu.mangoicu.ICU, | |
88 com.ibm.icu.mangoicu.UString; | |
89 | |
90 public import com.ibm.icu.mangoicu.ULocale, | |
91 com.ibm.icu.mangoicu.UTimeZone; | |
92 | |
93 /******************************************************************************* | |
94 | |
95 UCalendar is used for converting between a UDate object and | |
96 a set of integer fields such as Year, Month, Day, | |
97 Hour, and so on. (A UDate object represents a specific instant | |
98 in time with millisecond precision. See UDate for information about | |
99 the UDate) | |
100 | |
101 Types of UCalendar interpret a UDate according to the rules of a | |
102 specific calendar system. UCalendar supports Traditional & Gregorian. | |
103 | |
104 A UCalendar object can produce all the time field values needed to | |
105 implement the date-time formatting for a particular language and | |
106 calendar style (for example, Japanese-Gregorian, Japanese-Traditional). | |
107 | |
108 When computing a UDate from time fields, two special circumstances | |
109 may arise: there may be insufficient information to compute the UDate | |
110 (such as only year and month but no day in the month), or there may be | |
111 inconsistent information (such as "Tuesday, July 15, 1996" -- July 15, | |
112 1996 is actually a Monday). | |
113 | |
114 Insufficient information. The calendar will use default information | |
115 to specify the missing fields. This may vary by calendar; for the | |
116 Gregorian calendar, the default for a field is the same as that of | |
117 the start of the epoch: i.e., Year = 1970, Month = January, | |
118 Date = 1, etc. | |
119 | |
120 Inconsistent information. If fields conflict, the calendar will give | |
121 preference to fields set more recently. For example, when determining | |
122 the day, the calendar will look for one of the following combinations | |
123 of fields. The most recent combination, as determined by the most | |
124 recently set single field, will be used. | |
125 | |
126 See http://oss.software.ibm.com/icu/apiref/udat_8h.html for full | |
127 details. | |
128 | |
129 *******************************************************************************/ | |
130 | |
131 class UCalendar : ICU | |
132 { | |
133 package Handle handle; | |
134 | |
135 typedef double UDate; | |
136 | |
137 //Possible types of UCalendars | |
138 public enum Type | |
139 { | |
140 Traditional, | |
141 Gregorian | |
142 } | |
143 | |
144 // Possible fields in a UCalendar | |
145 public enum DateFields | |
146 { | |
147 Era, | |
148 Year, | |
149 Month, | |
150 WeekOfYear, | |
151 WeekOfMonth, | |
152 Date, | |
153 DayOfYear, | |
154 DayOfWeek, | |
155 DayOfWeekInMonth, | |
156 AmPm, | |
157 Hour, | |
158 HourOfDay, | |
159 Minute, | |
160 Second, | |
161 Millisecond, | |
162 ZoneOffset, | |
163 DstOffset, | |
164 YearWoy, | |
165 DowLocal, | |
166 ExtendedYear, | |
167 JulianDay, | |
168 MillisecondsInDay, | |
169 FieldCount, | |
170 DayOfMonth = Date | |
171 } | |
172 | |
173 // Possible days of the week in a UCalendar | |
174 public enum DaysOfWeek | |
175 { | |
176 Sunday = 1, | |
177 Monday, | |
178 Tuesday, | |
179 Wednesday, | |
180 Thursday, | |
181 Friday, | |
182 Saturday | |
183 } | |
184 | |
185 // Possible months in a UCalendar | |
186 public enum Months | |
187 { | |
188 January, | |
189 February, | |
190 March, | |
191 April, | |
192 May, | |
193 June, | |
194 July, | |
195 August, | |
196 September, | |
197 October, | |
198 November, | |
199 December, | |
200 UnDecimber | |
201 } | |
202 | |
203 // Possible AM/PM values in a UCalendar | |
204 public enum AMPMs | |
205 { | |
206 AM, | |
207 PM | |
208 } | |
209 | |
210 // Possible formats for a UCalendar's display name | |
211 public enum DisplayNameType | |
212 { | |
213 Standard, | |
214 ShortStandard, | |
215 DST, | |
216 ShortDST | |
217 } | |
218 | |
219 // Possible limit values for a UCalendar | |
220 public enum Limit | |
221 { | |
222 Minimum, | |
223 Maximum, | |
224 GreatestMinimum, | |
225 LeastMaximum, | |
226 ActualMinimum, | |
227 ActualMaximum | |
228 } | |
229 | |
230 // Types of UCalendar attributes | |
231 private enum Attribute | |
232 { | |
233 Lenient, // unused: set from UDateFormat instead | |
234 FirstDayOfWeek, | |
235 MinimalDaysInFirstWeek | |
236 } | |
237 | |
238 /*********************************************************************** | |
239 | |
240 Open a UCalendar. A UCalendar may be used to convert a | |
241 millisecond value to a year, month, and day | |
242 | |
243 ***********************************************************************/ | |
244 | |
245 this (inout UTimeZone zone, inout ULocale locale, Type type = Type.Traditional) | |
246 { | |
247 UErrorCode e; | |
248 | |
249 handle = ucal_open (cast(wchar*)zone.name.ptr, zone.name.length, ICU.toString(locale.name), type, e); | |
250 testError (e, "failed to open calendar"); | |
251 } | |
252 | |
253 /*********************************************************************** | |
254 | |
255 Internal only: Open a UCalendar with the given handle | |
256 | |
257 ***********************************************************************/ | |
258 | |
259 package this (Handle handle) | |
260 { | |
261 this.handle = handle; | |
262 } | |
263 | |
264 /*********************************************************************** | |
265 | |
266 Close this UCalendar | |
267 | |
268 ***********************************************************************/ | |
269 | |
270 ~this () | |
271 { | |
272 ucal_close (handle); | |
273 } | |
274 | |
275 /*********************************************************************** | |
276 | |
277 Set the TimeZone used by a UCalendar | |
278 | |
279 ***********************************************************************/ | |
280 | |
281 void setTimeZone (inout UTimeZone zone) | |
282 { | |
283 UErrorCode e; | |
284 | |
285 ucal_setTimeZone (handle, cast(wchar*)zone.name.ptr, zone.name.length, e); | |
286 testError (e, "failed to set calendar time zone"); | |
287 } | |
288 | |
289 /*********************************************************************** | |
290 | |
291 Get display name of the TimeZone used by this UCalendar | |
292 | |
293 ***********************************************************************/ | |
294 | |
295 void getTimeZoneName (UString s, inout ULocale locale, DisplayNameType type=DisplayNameType.Standard) | |
296 { | |
297 uint format (wchar* dst, uint length, inout ICU.UErrorCode e) | |
298 { | |
299 return ucal_getTimeZoneDisplayName (handle, type, toString(locale.name), dst, length, e); | |
300 } | |
301 | |
302 s.format (&format, "failed to get time zone name"); | |
303 } | |
304 | |
305 /*********************************************************************** | |
306 | |
307 Determine if a UCalendar is currently in daylight savings | |
308 time | |
309 | |
310 ***********************************************************************/ | |
311 | |
312 bool inDaylightTime () | |
313 { | |
314 UErrorCode e; | |
315 | |
316 auto x = ucal_inDaylightTime (handle, e); | |
317 testError (e, "failed to test calendar daylight time"); | |
318 return x != 0; | |
319 } | |
320 | |
321 /*********************************************************************** | |
322 | |
323 Get the current date and time | |
324 | |
325 ***********************************************************************/ | |
326 | |
327 UDate getNow () | |
328 { | |
329 return ucal_getNow (); | |
330 } | |
331 | |
332 /*********************************************************************** | |
333 | |
334 Get a UCalendar's current time in millis. The time is | |
335 represented as milliseconds from the epoch | |
336 | |
337 ***********************************************************************/ | |
338 | |
339 UDate getMillis () | |
340 { | |
341 UErrorCode e; | |
342 | |
343 auto x = ucal_getMillis (handle, e); | |
344 testError (e, "failed to get time"); | |
345 return x; | |
346 } | |
347 | |
348 /*********************************************************************** | |
349 | |
350 Set a UCalendar's current time in millis. The time is | |
351 represented as milliseconds from the epoch | |
352 | |
353 ***********************************************************************/ | |
354 | |
355 void setMillis (UDate date) | |
356 { | |
357 UErrorCode e; | |
358 | |
359 ucal_setMillis (handle, date, e); | |
360 testError (e, "failed to set time"); | |
361 } | |
362 | |
363 /*********************************************************************** | |
364 | |
365 Set a UCalendar's current date | |
366 | |
367 ***********************************************************************/ | |
368 | |
369 void setDate (uint year, Months month, uint date) | |
370 { | |
371 UErrorCode e; | |
372 | |
373 ucal_setDate (handle, year, month, date, e); | |
374 testError (e, "failed to set date"); | |
375 } | |
376 | |
377 /*********************************************************************** | |
378 | |
379 Set a UCalendar's current date | |
380 | |
381 ***********************************************************************/ | |
382 | |
383 void setDateTime (uint year, Months month, uint date, uint hour, uint minute, uint second) | |
384 { | |
385 UErrorCode e; | |
386 | |
387 ucal_setDateTime (handle, year, month, date, hour, minute, second, e); | |
388 testError (e, "failed to set date/time"); | |
389 } | |
390 | |
391 /*********************************************************************** | |
392 | |
393 Returns TRUE if the given Calendar object is equivalent | |
394 to this one | |
395 | |
396 ***********************************************************************/ | |
397 | |
398 bool isEquivalent (UCalendar when) | |
399 { | |
400 return ucal_equivalentTo (handle, when.handle) != 0; | |
401 } | |
402 | |
403 /*********************************************************************** | |
404 | |
405 Compares the Calendar time | |
406 | |
407 ***********************************************************************/ | |
408 | |
409 bool isEqual (UCalendar when) | |
410 { | |
411 return (this is when || getMillis == when.getMillis); | |
412 } | |
413 | |
414 /*********************************************************************** | |
415 | |
416 Returns true if this Calendar's current time is before | |
417 "when"'s current time | |
418 | |
419 ***********************************************************************/ | |
420 | |
421 bool isBefore (UCalendar when) | |
422 { | |
423 return (this !is when || getMillis < when.getMillis); | |
424 } | |
425 | |
426 /*********************************************************************** | |
427 | |
428 Returns true if this Calendar's current time is after | |
429 "when"'s current time | |
430 | |
431 ***********************************************************************/ | |
432 | |
433 bool isAfter (UCalendar when) | |
434 { | |
435 return (this !is when || getMillis > when.getMillis); | |
436 } | |
437 | |
438 /*********************************************************************** | |
439 | |
440 Add a specified signed amount to a particular field in a | |
441 UCalendar | |
442 | |
443 ***********************************************************************/ | |
444 | |
445 void add (DateFields field, uint amount) | |
446 { | |
447 UErrorCode e; | |
448 | |
449 ucal_add (handle, field, amount, e); | |
450 testError (e, "failed to add to calendar"); | |
451 } | |
452 | |
453 /*********************************************************************** | |
454 | |
455 Add a specified signed amount to a particular field in a | |
456 UCalendar | |
457 | |
458 ***********************************************************************/ | |
459 | |
460 void roll (DateFields field, uint amount) | |
461 { | |
462 UErrorCode e; | |
463 | |
464 ucal_roll (handle, field, amount, e); | |
465 testError (e, "failed to roll calendar"); | |
466 } | |
467 | |
468 /*********************************************************************** | |
469 | |
470 Get the current value of a field from a UCalendar | |
471 | |
472 ***********************************************************************/ | |
473 | |
474 uint get (DateFields field) | |
475 { | |
476 UErrorCode e; | |
477 | |
478 auto x = ucal_get (handle, field, e); | |
479 testError (e, "failed to get calendar field"); | |
480 return x; | |
481 } | |
482 | |
483 /*********************************************************************** | |
484 | |
485 Set the value of a field in a UCalendar | |
486 | |
487 ***********************************************************************/ | |
488 | |
489 void set (DateFields field, uint value) | |
490 { | |
491 ucal_set (handle, field, value); | |
492 } | |
493 | |
494 /*********************************************************************** | |
495 | |
496 Determine if a field in a UCalendar is set | |
497 | |
498 ***********************************************************************/ | |
499 | |
500 bool isSet (DateFields field) | |
501 { | |
502 return ucal_isSet (handle, field) != 0; | |
503 } | |
504 | |
505 /*********************************************************************** | |
506 | |
507 Clear a field in a UCalendar | |
508 | |
509 ***********************************************************************/ | |
510 | |
511 void clearField (DateFields field) | |
512 { | |
513 ucal_clearField (handle, field); | |
514 } | |
515 | |
516 /*********************************************************************** | |
517 | |
518 Clear all fields in a UCalendar | |
519 | |
520 ***********************************************************************/ | |
521 | |
522 void clear () | |
523 { | |
524 ucal_clear (handle); | |
525 } | |
526 | |
527 /*********************************************************************** | |
528 | |
529 Determine a limit for a field in a UCalendar. A limit is a | |
530 maximum or minimum value for a field | |
531 | |
532 ***********************************************************************/ | |
533 | |
534 uint getLimit (DateFields field, Limit type) | |
535 { | |
536 UErrorCode e; | |
537 | |
538 auto x = ucal_getLimit (handle, field, type, e); | |
539 testError (e, "failed to get calendar limit"); | |
540 return x; | |
541 } | |
542 | |
543 /*********************************************************************** | |
544 | |
545 ***********************************************************************/ | |
546 | |
547 uint getDaysInFirstWeek () | |
548 { | |
549 return ucal_getAttribute (handle, Attribute.MinimalDaysInFirstWeek); | |
550 } | |
551 | |
552 /*********************************************************************** | |
553 | |
554 ***********************************************************************/ | |
555 | |
556 uint getFirstDayOfWeek () | |
557 { | |
558 return ucal_getAttribute (handle, Attribute.FirstDayOfWeek); | |
559 } | |
560 | |
561 /*********************************************************************** | |
562 | |
563 ***********************************************************************/ | |
564 | |
565 void setDaysInFirstWeek (uint value) | |
566 { | |
567 ucal_setAttribute (handle, Attribute.MinimalDaysInFirstWeek, value); | |
568 } | |
569 | |
570 /*********************************************************************** | |
571 | |
572 ***********************************************************************/ | |
573 | |
574 void setFirstDayOfWeek (uint value) | |
575 { | |
576 ucal_setAttribute (handle, Attribute.FirstDayOfWeek, value); | |
577 } | |
578 | |
579 | |
580 /*********************************************************************** | |
581 | |
582 Bind the ICU functions from a shared library. This is | |
583 complicated by the issues regarding D and DLLs on the | |
584 Windows platform | |
585 | |
586 ***********************************************************************/ | |
587 | |
588 private static void* library; | |
589 | |
590 /*********************************************************************** | |
591 | |
592 ***********************************************************************/ | |
593 | |
594 private static extern (C) | |
595 { | |
596 Handle function (wchar*, uint, char*, Type, inout UErrorCode) ucal_open; | |
597 void function (Handle) ucal_close; | |
598 UDate function () ucal_getNow; | |
599 UDate function (Handle, inout UErrorCode) ucal_getMillis; | |
600 void function (Handle, UDate, inout UErrorCode) ucal_setMillis; | |
601 void function (Handle, uint, uint, uint, inout UErrorCode) ucal_setDate; | |
602 void function (Handle, uint, uint, uint, uint, uint, uint, inout UErrorCode) ucal_setDateTime; | |
603 byte function (Handle, Handle) ucal_equivalentTo; | |
604 void function (Handle, uint, uint, inout UErrorCode) ucal_add; | |
605 void function (Handle, uint, uint, inout UErrorCode) ucal_roll; | |
606 uint function (Handle, uint, inout UErrorCode) ucal_get; | |
607 void function (Handle, uint, uint) ucal_set; | |
608 byte function (Handle, uint) ucal_isSet; | |
609 void function (Handle, uint) ucal_clearField; | |
610 void function (Handle) ucal_clear; | |
611 uint function (Handle, uint, uint, inout UErrorCode) ucal_getLimit; | |
612 void function (Handle, wchar*, uint, inout UErrorCode) ucal_setTimeZone; | |
613 byte function (Handle, uint) ucal_inDaylightTime; | |
614 uint function (Handle, uint) ucal_getAttribute; | |
615 void function (Handle, uint, uint) ucal_setAttribute; | |
616 uint function (Handle, uint, char*, wchar*, uint, inout UErrorCode) ucal_getTimeZoneDisplayName; | |
617 } | |
618 | |
619 /*********************************************************************** | |
620 | |
621 ***********************************************************************/ | |
622 | |
623 static FunctionLoader.Bind[] targets = | |
624 [ | |
625 {cast(void**) &ucal_open, "ucal_open"}, | |
626 {cast(void**) &ucal_close, "ucal_close"}, | |
627 {cast(void**) &ucal_getNow, "ucal_getNow"}, | |
628 {cast(void**) &ucal_getMillis, "ucal_getMillis"}, | |
629 {cast(void**) &ucal_setMillis, "ucal_setMillis"}, | |
630 {cast(void**) &ucal_setDate, "ucal_setDate"}, | |
631 {cast(void**) &ucal_setDateTime, "ucal_setDateTime"}, | |
632 {cast(void**) &ucal_equivalentTo, "ucal_equivalentTo"}, | |
633 {cast(void**) &ucal_add, "ucal_add"}, | |
634 {cast(void**) &ucal_roll, "ucal_roll"}, | |
635 {cast(void**) &ucal_get, "ucal_get"}, | |
636 {cast(void**) &ucal_set, "ucal_set"}, | |
637 {cast(void**) &ucal_clearField, "ucal_clearField"}, | |
638 {cast(void**) &ucal_clear, "ucal_clear"}, | |
639 {cast(void**) &ucal_getLimit, "ucal_getLimit"}, | |
640 {cast(void**) &ucal_setTimeZone, "ucal_setTimeZone"}, | |
641 {cast(void**) &ucal_inDaylightTime, "ucal_inDaylightTime"}, | |
642 {cast(void**) &ucal_getAttribute, "ucal_getAttribute"}, | |
643 {cast(void**) &ucal_setAttribute, "ucal_setAttribute"}, | |
644 {cast(void**) &ucal_isSet, "ucal_isSet"}, | |
645 {cast(void**) &ucal_getTimeZoneDisplayName, "ucal_getTimeZoneDisplayName"}, | |
646 ]; | |
647 | |
648 /*********************************************************************** | |
649 | |
650 ***********************************************************************/ | |
651 | |
652 static this () | |
653 { | |
654 library = FunctionLoader.bind (icuin, targets); | |
655 } | |
656 | |
657 /*********************************************************************** | |
658 | |
659 ***********************************************************************/ | |
660 | |
661 static ~this () | |
662 { | |
663 FunctionLoader.unbind (library); | |
664 } | |
665 | |
666 } |