comparison dwtx/dwtxhelper/mangoicu/UTimeZone.d @ 89:040da1cb0d76

Add a local copy of the mango ICU binding to work out the utf8 usability. Will hopefully go back into mango.
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Jun 2008 22:57:31 +0200
parents
children 11e8159caf7a
comparison
equal deleted inserted replaced
88:cd18fa3b71f1 89:040da1cb0d76
1 /*******************************************************************************
2
3 @file UTimeZone.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 dwtx.dwthelper.mangoicu.UTimeZone;
86
87 private import dwtx.dwthelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.UString,
89 dwtx.dwthelper.mangoicu.UEnumeration;
90
91 /*******************************************************************************
92
93 A representation of a TimeZone. Unfortunately, ICU does not expose
94 this as a seperate entity from the C-API, so we have to make do
95 with an approximation instead.
96
97 *******************************************************************************/
98
99 struct UTimeZone
100 {
101 public wchar[] name;
102
103 public static UTimeZone Default = {null};
104 public static UTimeZone Gmt = {"Etc/GMT"};
105 public static UTimeZone Greenwich = {"Etc/Greenwich"};
106 public static UTimeZone Uct = {"Etc/UCT"};
107 public static UTimeZone Utc = {"Etc/UTC"};
108 public static UTimeZone Universal = {"Etc/Universal"};
109
110 public static UTimeZone GmtPlus0 = {"Etc/GMT+0"};
111 public static UTimeZone GmtPlus1 = {"Etc/GMT+1"};
112 public static UTimeZone GmtPlus2 = {"Etc/GMT+2"};
113 public static UTimeZone GmtPlus3 = {"Etc/GMT+3"};
114 public static UTimeZone GmtPlus4 = {"Etc/GMT+4"};
115 public static UTimeZone GmtPlus5 = {"Etc/GMT+5"};
116 public static UTimeZone GmtPlus6 = {"Etc/GMT+6"};
117 public static UTimeZone GmtPlus7 = {"Etc/GMT+7"};
118 public static UTimeZone GmtPlus8 = {"Etc/GMT+8"};
119 public static UTimeZone GmtPlus9 = {"Etc/GMT+9"};
120 public static UTimeZone GmtPlus10 = {"Etc/GMT+10"};
121 public static UTimeZone GmtPlus11 = {"Etc/GMT+11"};
122 public static UTimeZone GmtPlus12 = {"Etc/GMT+12"};
123
124 public static UTimeZone GmtMinus0 = {"Etc/GMT-0"};
125 public static UTimeZone GmtMinus1 = {"Etc/GMT-1"};
126 public static UTimeZone GmtMinus2 = {"Etc/GMT-2"};
127 public static UTimeZone GmtMinus3 = {"Etc/GMT-3"};
128 public static UTimeZone GmtMinus4 = {"Etc/GMT-4"};
129 public static UTimeZone GmtMinus5 = {"Etc/GMT-5"};
130 public static UTimeZone GmtMinus6 = {"Etc/GMT-6"};
131 public static UTimeZone GmtMinus7 = {"Etc/GMT-7"};
132 public static UTimeZone GmtMinus8 = {"Etc/GMT-8"};
133 public static UTimeZone GmtMinus9 = {"Etc/GMT-9"};
134 public static UTimeZone GmtMinus10 = {"Etc/GMT-10"};
135 public static UTimeZone GmtMinus11 = {"Etc/GMT-11"};
136 public static UTimeZone GmtMinus12 = {"Etc/GMT-12"};
137
138 /***********************************************************************
139
140 Get the default time zone.
141
142 ***********************************************************************/
143
144 static void getDefault (inout UTimeZone zone)
145 {
146 uint format (wchar* dst, uint length, inout ICU.Error e)
147 {
148 return ucal_getDefaultTimeZone (dst, length, e);
149 }
150
151 UString s = new UString(64);
152 s.format (&format, "failed to get default time zone");
153 zone.name = s.get();
154 }
155
156 /***********************************************************************
157
158 Set the default time zone.
159
160 ***********************************************************************/
161
162 static void setDefault (inout UTimeZone zone)
163 {
164 ICU.Error e;
165
166 ucal_setDefaultTimeZone (ICU.toString (zone.name), e);
167 ICU.testError (e, "failed to set default time zone");
168 }
169
170 /***********************************************************************
171
172 Return the amount of time in milliseconds that the clock
173 is advanced during daylight savings time for the given
174 time zone, or zero if the time zone does not observe daylight
175 savings time
176
177 ***********************************************************************/
178
179 static uint getDSTSavings (inout UTimeZone zone)
180 {
181 ICU.Error e;
182
183 uint x = ucal_getDSTSavings (ICU.toString (zone.name), e);
184 ICU.testError (e, "failed to get DST savings");
185 return x;
186 }
187
188
189 /**********************************************************************
190
191 Iterate over the available timezone names
192
193 **********************************************************************/
194
195 static int opApply (int delegate(inout wchar[] element) dg)
196 {
197 ICU.Error e;
198 wchar[] name;
199 int result;
200
201 void* h = ucal_openTimeZones (e);
202 ICU.testError (e, "failed to open timeszone iterator");
203
204 UEnumeration zones = new UEnumeration (cast(UEnumeration.Handle) h);
205 while (zones.next(name) && (result = dg(name)) != 0) {}
206 delete zones;
207 return result;
208 }
209
210
211
212 /***********************************************************************
213
214 Bind the ICU functions from a shared library. This is
215 complicated by the issues regarding D and DLLs on the
216 Windows platform
217
218 ***********************************************************************/
219
220 private static void* library;
221
222 /***********************************************************************
223
224 ***********************************************************************/
225
226 private static extern (C)
227 {
228 void* function (inout ICU.Error) ucal_openTimeZones;
229 uint function (wchar*, uint, inout ICU.Error) ucal_getDefaultTimeZone;
230 void function (wchar*, inout ICU.Error) ucal_setDefaultTimeZone;
231 uint function (wchar*, inout ICU.Error) ucal_getDSTSavings;
232 }
233
234 /***********************************************************************
235
236 ***********************************************************************/
237
238 static FunctionLoader.Bind[] targets =
239 [
240 {cast(void**) &ucal_openTimeZones, "ucal_openTimeZones"},
241 {cast(void**) &ucal_getDefaultTimeZone, "ucal_getDefaultTimeZone"},
242 {cast(void**) &ucal_setDefaultTimeZone, "ucal_setDefaultTimeZone"},
243 {cast(void**) &ucal_getDSTSavings, "ucal_getDSTSavings"},
244 ];
245
246 /***********************************************************************
247
248 ***********************************************************************/
249
250 static this ()
251 {
252 library = FunctionLoader.bind (ICU.icuin, targets);
253 }
254
255 /***********************************************************************
256
257 ***********************************************************************/
258
259 static ~this ()
260 {
261 FunctionLoader.unbind (library);
262 }
263 }