"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Tue, 12 Feb 2008 11:23:05 -0800, je**********@gm ail.com
<je**********@g mail.comwrote:
>[...]
That stinks. I was reading a post somewhere else where they were using
the registry to get all available Timezones. However, I am not sure
how they figured out UTC offsets from that, nor how they handled Day
Light Savings Time. I will just ignore it.
I don't recall the format off the top of my head, but the timezone
database in the registry is not all that complicated. Microsoft has a KB
article on their support web site...
You'll find that KB article here:
http://support.microsoft.com/kb/115231
Once you're grabbed all the timezones from the registry you can use
SystemTimeToTzS pecificLocalTim e and
TzSpecificLocal TimeToSystemTim e to convert betwen UTC and timezone specific
local time
The following code should help:
[DllImport("kern el32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int SystemTimeToTzS pecificLocalTim e(ref
TIME_ZONE_INFOR MATION lpTimeZone, ref SYSTEMTIME lpUniversalTIme , out
SYSTEMTIME lpLocalTime);
[DllImport("kern el32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int TzSpecificLocal TimeToSystemTim e(ref
TIME_ZONE_INFOR MATION lpTimeZone, ref SYSTEMTIME lpLocalTime, out SYSTEMTIME
lpUniversalTIme );
[StructLayout(La youtKind.Sequen tial)]
private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
//Registry time zone format. See KB article Q115231
[StructLayout(La youtKind.Sequen tial)]
private struct REG_TIME_ZONE_I NFORMATION
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
private struct TIME_ZONE_INFOR MATION
{
public int Bias;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 32)]
public string StandardName;
public SYSTEMTIME StandardDate;
public int StandardBias;
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 32)]
public string DaylightName;
public SYSTEMTIME DaylightDate;
public int DaylightBias;
}
private static List<TIME_ZONE_ INFORMATIONGetT imeZones()
{
List<TIME_ZONE_ INFORMATIONlist = new List<TIME_ZONE_ INFORMATION>();
RegistryKey key =
Registry.LocalM achine.OpenSubK ey(@"SOFTWARE\M icrosoft\Window s
NT\CurrentVersi on\Time Zones");
if (key == null)
return list;
string[] subKeyNames = key.GetSubKeyNa mes();
foreach (string subKeyName in subKeyNames)
{
RegistryKey subKey = key.OpenSubKey( subKeyName);
if (subKey != null)
{
object value = subKey.GetValue ("TZI");
if (value != null)
{
int length =
Marshal.SizeOf( typeof(REG_TIME _ZONE_INFORMATI ON));
IntPtr p = Marshal.AllocHG lobal(length);
Marshal.Copy((b yte[])value, 0, p, length);
REG_TIME_ZONE_I NFORMATION rtzi =
(REG_TIME_ZONE_ INFORMATION)Mar shal.PtrToStruc ture(p,
typeof(REG_TIME _ZONE_INFORMATI ON));
Marshal.FreeHGl obal(p);
TIME_ZONE_INFOR MATION tzi = new TIME_ZONE_INFOR MATION();
tzi.Bias = rtzi.Bias;
tzi.DaylightBia s = rtzi.DaylightBi as;
tzi.StandardBia s = rtzi.StandardBi as;
tzi.DaylightDat e = rtzi.DaylightDa te;
tzi.StandardDat e = rtzi.StandardDa te;
tzi.DaylightNam e = (string)subKey. GetValue("Dlt", "");
tzi.StandardNam e = (string)subKey. GetValue("Std", "");
list.Add(tzi);
}
subKey.Close();
}
}
key.Close();
return list;
}
/claes