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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, November 2004 00031 @author Kris 00032 00033 Note that this package and documentation is built around the ICU 00034 project (http://oss.software.ibm.com/icu/). Below is the license 00035 statement as specified by that software: 00036 00037 00038 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00039 00040 00041 ICU License - ICU 1.8.1 and later 00042 00043 COPYRIGHT AND PERMISSION NOTICE 00044 00045 Copyright (c) 1995-2003 International Business Machines Corporation and 00046 others. 00047 00048 All rights reserved. 00049 00050 Permission is hereby granted, free of charge, to any person obtaining a 00051 copy of this software and associated documentation files (the 00052 "Software"), to deal in the Software without restriction, including 00053 without limitation the rights to use, copy, modify, merge, publish, 00054 distribute, and/or sell copies of the Software, and to permit persons 00055 to whom the Software is furnished to do so, provided that the above 00056 copyright notice(s) and this permission notice appear in all copies of 00057 the Software and that both the above copyright notice(s) and this 00058 permission notice appear in supporting documentation. 00059 00060 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00061 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00062 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00063 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00064 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00065 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00066 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00067 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00068 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00069 00070 Except as contained in this notice, the name of a copyright holder 00071 shall not be used in advertising or otherwise to promote the sale, use 00072 or other dealings in this Software without prior written authorization 00073 of the copyright holder. 00074 00075 ---------------------------------------------------------------------- 00076 00077 All trademarks and registered trademarks mentioned herein are the 00078 property of their respective owners. 00079 00080 *******************************************************************************/ 00081 00082 module mango.icu.UTimeZone; 00083 00084 private import mango.icu.ICU, 00085 mango.icu.UString; 00086 00087 /******************************************************************************* 00088 00089 A representation of a TimeZone. Unfortunately, ICU does not expose 00090 this as a seperate entity from the C-API, so we have to make do 00091 with an approximation instead. 00092 00093 *******************************************************************************/ 00094 00095 struct UTimeZone 00096 { 00097 public wchar[] name; 00098 00099 public static UTimeZone Default = {null}; 00100 00101 /*********************************************************************** 00102 00103 Get the default time zone. 00104 00105 ***********************************************************************/ 00106 00107 static void getDefault (inout UTimeZone zone) 00108 { 00109 uint format (wchar* dst, uint length, inout ICU.Error e) 00110 { 00111 return ucal_getDefaultTimeZone (dst, length, e); 00112 } 00113 00114 UString s = new UString(64); 00115 s.format (&format, "failed to get default time zone"); 00116 zone.name = s.get(); 00117 } 00118 00119 /*********************************************************************** 00120 00121 Set the default time zone. 00122 00123 ***********************************************************************/ 00124 00125 static void setDefault (inout UTimeZone zone) 00126 { 00127 ICU.Error e; 00128 00129 ucal_setDefaultTimeZone (ICU.toString (zone.name), e); 00130 ICU.testError (e, "failed to set default time zone"); 00131 } 00132 00133 /*********************************************************************** 00134 00135 Return the amount of time in milliseconds that the clock 00136 is advanced during daylight savings time for the given 00137 time zone, or zero if the time zone does not observe daylight 00138 savings time 00139 00140 ***********************************************************************/ 00141 00142 static uint getDSTSavings (inout UTimeZone zone) 00143 { 00144 ICU.Error e; 00145 00146 uint x = ucal_getDSTSavings (ICU.toString (zone.name), e); 00147 ICU.testError (e, "failed to get DST savings"); 00148 return x; 00149 } 00150 00151 00152 /*********************************************************************** 00153 00154 Bind the ICU functions from a shared library. This is 00155 complicated by the issues regarding D and DLLs on the 00156 Windows platform 00157 00158 ***********************************************************************/ 00159 00160 version (Win32) 00161 { 00162 private static void* library; 00163 private static char[] libraryName = "icuin30.dll"; 00164 00165 /*************************************************************** 00166 00167 ***************************************************************/ 00168 00169 private static extern (C) 00170 { 00171 uint function (wchar*, uint, inout ICU.Error) ucal_getDefaultTimeZone; 00172 void function (wchar*, inout ICU.Error) ucal_setDefaultTimeZone; 00173 uint function (wchar*, inout ICU.Error) ucal_getDSTSavings; 00174 } 00175 00176 /*************************************************************** 00177 00178 ***************************************************************/ 00179 00180 static FunctionLoader.Bind[] targets = 00181 [ 00182 {cast(void**) &ucal_getDefaultTimeZone, "ucal_getDefaultTimeZone"}, 00183 {cast(void**) &ucal_setDefaultTimeZone, "ucal_setDefaultTimeZone"}, 00184 {cast(void**) &ucal_getDSTSavings, "ucal_getDSTSavings"}, 00185 ]; 00186 00187 /*************************************************************** 00188 00189 ***************************************************************/ 00190 00191 static this () 00192 { 00193 library = FunctionLoader.bind (libraryName, targets); 00194 } 00195 00196 /*************************************************************** 00197 00198 ***************************************************************/ 00199 00200 static ~this () 00201 { 00202 FunctionLoader.unbind (library); 00203 } 00204 } 00205 }