00001 /******************************************************************************* 00002 00003 @file UTimeZone.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, November 2004 00034 @author Kris 00035 00036 Note that this package and documentation is built around the ICU 00037 project (http://oss.software.ibm.com/icu/). Below is the license 00038 statement as specified by that software: 00039 00040 00041 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00042 00043 00044 ICU License - ICU 1.8.1 and later 00045 00046 COPYRIGHT AND PERMISSION NOTICE 00047 00048 Copyright (c) 1995-2003 International Business Machines Corporation and 00049 others. 00050 00051 All rights reserved. 00052 00053 Permission is hereby granted, free of charge, to any person obtaining a 00054 copy of this software and associated documentation files (the 00055 "Software"), to deal in the Software without restriction, including 00056 without limitation the rights to use, copy, modify, merge, publish, 00057 distribute, and/or sell copies of the Software, and to permit persons 00058 to whom the Software is furnished to do so, provided that the above 00059 copyright notice(s) and this permission notice appear in all copies of 00060 the Software and that both the above copyright notice(s) and this 00061 permission notice appear in supporting documentation. 00062 00063 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00064 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00065 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00066 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00067 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00068 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00069 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00070 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00071 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00072 00073 Except as contained in this notice, the name of a copyright holder 00074 shall not be used in advertising or otherwise to promote the sale, use 00075 or other dealings in this Software without prior written authorization 00076 of the copyright holder. 00077 00078 ---------------------------------------------------------------------- 00079 00080 All trademarks and registered trademarks mentioned herein are the 00081 property of their respective owners. 00082 00083 *******************************************************************************/ 00084 00085 module mango.icu.UTimeZone; 00086 00087 private import mango.icu.ICU, 00088 mango.icu.UString, 00089 mango.icu.UEnumeration; 00090 00091 /******************************************************************************* 00092 00093 A representation of a TimeZone. Unfortunately, ICU does not expose 00094 this as a seperate entity from the C-API, so we have to make do 00095 with an approximation instead. 00096 00097 *******************************************************************************/ 00098 00099 struct UTimeZone 00100 { 00101 public wchar[] name; 00102 00103 public static UTimeZone Default = {null}; 00104 public static UTimeZone Gmt = {"Etc/GMT"}; 00105 public static UTimeZone Greenwich = {"Etc/Greenwich"}; 00106 public static UTimeZone Uct = {"Etc/UCT"}; 00107 public static UTimeZone Utc = {"Etc/UTC"}; 00108 public static UTimeZone Universal = {"Etc/Universal"}; 00109 00110 public static UTimeZone GmtPlus0 = {"Etc/GMT+0"}; 00111 public static UTimeZone GmtPlus1 = {"Etc/GMT+1"}; 00112 public static UTimeZone GmtPlus2 = {"Etc/GMT+2"}; 00113 public static UTimeZone GmtPlus3 = {"Etc/GMT+3"}; 00114 public static UTimeZone GmtPlus4 = {"Etc/GMT+4"}; 00115 public static UTimeZone GmtPlus5 = {"Etc/GMT+5"}; 00116 public static UTimeZone GmtPlus6 = {"Etc/GMT+6"}; 00117 public static UTimeZone GmtPlus7 = {"Etc/GMT+7"}; 00118 public static UTimeZone GmtPlus8 = {"Etc/GMT+8"}; 00119 public static UTimeZone GmtPlus9 = {"Etc/GMT+9"}; 00120 public static UTimeZone GmtPlus10 = {"Etc/GMT+10"}; 00121 public static UTimeZone GmtPlus11 = {"Etc/GMT+11"}; 00122 public static UTimeZone GmtPlus12 = {"Etc/GMT+12"}; 00123 00124 public static UTimeZone GmtMinus0 = {"Etc/GMT-0"}; 00125 public static UTimeZone GmtMinus1 = {"Etc/GMT-1"}; 00126 public static UTimeZone GmtMinus2 = {"Etc/GMT-2"}; 00127 public static UTimeZone GmtMinus3 = {"Etc/GMT-3"}; 00128 public static UTimeZone GmtMinus4 = {"Etc/GMT-4"}; 00129 public static UTimeZone GmtMinus5 = {"Etc/GMT-5"}; 00130 public static UTimeZone GmtMinus6 = {"Etc/GMT-6"}; 00131 public static UTimeZone GmtMinus7 = {"Etc/GMT-7"}; 00132 public static UTimeZone GmtMinus8 = {"Etc/GMT-8"}; 00133 public static UTimeZone GmtMinus9 = {"Etc/GMT-9"}; 00134 public static UTimeZone GmtMinus10 = {"Etc/GMT-10"}; 00135 public static UTimeZone GmtMinus11 = {"Etc/GMT-11"}; 00136 public static UTimeZone GmtMinus12 = {"Etc/GMT-12"}; 00137 00138 /*********************************************************************** 00139 00140 Get the default time zone. 00141 00142 ***********************************************************************/ 00143 00144 static void getDefault (inout UTimeZone zone) 00145 { 00146 uint format (wchar* dst, uint length, inout ICU.Error e) 00147 { 00148 return ucal_getDefaultTimeZone (dst, length, e); 00149 } 00150 00151 UString s = new UString(64); 00152 s.format (&format, "failed to get default time zone"); 00153 zone.name = s.get(); 00154 } 00155 00156 /*********************************************************************** 00157 00158 Set the default time zone. 00159 00160 ***********************************************************************/ 00161 00162 static void setDefault (inout UTimeZone zone) 00163 { 00164 ICU.Error e; 00165 00166 ucal_setDefaultTimeZone (ICU.toString (zone.name), e); 00167 ICU.testError (e, "failed to set default time zone"); 00168 } 00169 00170 /*********************************************************************** 00171 00172 Return the amount of time in milliseconds that the clock 00173 is advanced during daylight savings time for the given 00174 time zone, or zero if the time zone does not observe daylight 00175 savings time 00176 00177 ***********************************************************************/ 00178 00179 static uint getDSTSavings (inout UTimeZone zone) 00180 { 00181 ICU.Error e; 00182 00183 uint x = ucal_getDSTSavings (ICU.toString (zone.name), e); 00184 ICU.testError (e, "failed to get DST savings"); 00185 return x; 00186 } 00187 00188 00189 /********************************************************************** 00190 00191 Iterate over the available timezone names 00192 00193 **********************************************************************/ 00194 00195 static int opApply (int delegate(inout wchar[] element) dg) 00196 { 00197 ICU.Error e; 00198 wchar[] name; 00199 int result; 00200 00201 void* h = ucal_openTimeZones (e); 00202 ICU.testError (e, "failed to open timeszone iterator"); 00203 00204 UEnumeration zones = new UEnumeration (cast(UEnumeration.Handle) h); 00205 while (zones.next(name) && (result = dg(name)) != 0) {} 00206 delete zones; 00207 return result; 00208 } 00209 00210 00211 00212 /*********************************************************************** 00213 00214 Bind the ICU functions from a shared library. This is 00215 complicated by the issues regarding D and DLLs on the 00216 Windows platform 00217 00218 ***********************************************************************/ 00219 00220 private static void* library; 00221 00222 /*********************************************************************** 00223 00224 ***********************************************************************/ 00225 00226 private static extern (C) 00227 { 00228 void* function (inout ICU.Error) ucal_openTimeZones; 00229 uint function (wchar*, uint, inout ICU.Error) ucal_getDefaultTimeZone; 00230 void function (wchar*, inout ICU.Error) ucal_setDefaultTimeZone; 00231 uint function (wchar*, inout ICU.Error) ucal_getDSTSavings; 00232 } 00233 00234 /*********************************************************************** 00235 00236 ***********************************************************************/ 00237 00238 static FunctionLoader.Bind[] targets = 00239 [ 00240 {cast(void**) &ucal_openTimeZones, "ucal_openTimeZones"}, 00241 {cast(void**) &ucal_getDefaultTimeZone, "ucal_getDefaultTimeZone"}, 00242 {cast(void**) &ucal_setDefaultTimeZone, "ucal_setDefaultTimeZone"}, 00243 {cast(void**) &ucal_getDSTSavings, "ucal_getDSTSavings"}, 00244 ]; 00245 00246 /*********************************************************************** 00247 00248 ***********************************************************************/ 00249 00250 static this () 00251 { 00252 library = FunctionLoader.bind (ICU.icuin, targets); 00253 } 00254 00255 /*********************************************************************** 00256 00257 ***********************************************************************/ 00258 00259 static ~this () 00260 { 00261 FunctionLoader.unbind (library); 00262 } 00263 }