yongsing
asked on
Get Time at specific Timezone
In my win form application, can I get a list of timezones, as well as get the time for a specific timezone?
In Window's Control Panel's Date/Time properties window, there is a list of timezones such as GMT -09:00 Alaska. I would like to retrieve this list of timezones in my application. Then it would be nice to get the time for a specific timezone, so that, for example, I know what's the time now in Alaska.
In Window's Control Panel's Date/Time properties window, there is a list of timezones such as GMT -09:00 Alaska. I would like to retrieve this list of timezones in my application. Then it would be nice to get the time for a specific timezone, so that, for example, I know what's the time now in Alaska.
ASKER
The article mentioned about the registry. How is that going to help me get the time?
time zones!
and you can use timezone class to get time!
and you can use timezone class to get time!
well registry is a system database which stores necessary info abt the machine and other stuff... u can use the info in the registry about the time zones in your combo box at run time ....
later u can get the time for a specific timezone on the COMBO_SelectedIndexChanged event you just have to parse the beginning of the timezone string.... (after the (GMT XXXX) )... think it should help... and give points to uncle ghost :) ...
later u can get the time for a specific timezone on the COMBO_SelectedIndexChanged
<snip from USENET NETMaster (Thomas Scheidegger)>
using System.Runtime.InteropServ ices;
using Microsoft.Win32;
...
[StructLayout(LayoutKind.S equential, Pack=2)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
// MS KB Q115231
[StructLayout(LayoutKind.S equential, Pack=2)]
public struct TZI
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}
.....
RegistryKey rootKey = Registry.LocalMachine;
// only on NT+!
rootKey = rootKey.OpenSubKey( @"SOFTWARE\Microsoft\Windo ws NT\CurrentVersion\Time Zones\" );
string[] zones = rootKey.GetSubKeyNames();
int year = DateTime.Now.Year;
Type tziType = typeof( TZI );
int tziSize = Marshal.SizeOf( tziType );
foreach( string zone in zones )
{
RegistryKey zoneKey = rootKey.OpenSubKey( zone );
Console.WriteLine( zone );
byte[] bytes = zoneKey.GetValue("TZI") as byte[];
zoneKey.Close();
if( bytes == null )
continue;
if( bytes.Length < tziSize )
continue;
GCHandle handle = GCHandle.Alloc( bytes, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject( );
TZI tzi = (TZI) Marshal.PtrToStructure( buffer, tziType );
handle.Free();
Console.WriteLine( " Bias : {0}", tzi.Bias.ToString() );
Console.WriteLine( " DaylightBias : {0}", tzi.DaylightBias.ToString( ) );
if( tzi.DaylightDate.wMonth > 0 )
{
DateTime tzSD = new DateTime( year, tzi.DaylightDate.wMonth, tzi.DaylightDate.wDay,
tzi.DaylightDate.wHour, tzi.DaylightDate.wMinute, tzi.DaylightDate.wSecond,
tzi.DaylightDate.wMillisec onds );
Console.WriteLine( " DaylightDate : {0}", tzSD.ToString("s") );
}
}
//
</snip>
using System.Runtime.InteropServ
using Microsoft.Win32;
...
[StructLayout(LayoutKind.S
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
// MS KB Q115231
[StructLayout(LayoutKind.S
public struct TZI
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}
.....
RegistryKey rootKey = Registry.LocalMachine;
// only on NT+!
rootKey = rootKey.OpenSubKey( @"SOFTWARE\Microsoft\Windo
string[] zones = rootKey.GetSubKeyNames();
int year = DateTime.Now.Year;
Type tziType = typeof( TZI );
int tziSize = Marshal.SizeOf( tziType );
foreach( string zone in zones )
{
RegistryKey zoneKey = rootKey.OpenSubKey( zone );
Console.WriteLine( zone );
byte[] bytes = zoneKey.GetValue("TZI") as byte[];
zoneKey.Close();
if( bytes == null )
continue;
if( bytes.Length < tziSize )
continue;
GCHandle handle = GCHandle.Alloc( bytes, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject(
TZI tzi = (TZI) Marshal.PtrToStructure( buffer, tziType );
handle.Free();
Console.WriteLine( " Bias : {0}", tzi.Bias.ToString() );
Console.WriteLine( " DaylightBias : {0}", tzi.DaylightBias.ToString(
if( tzi.DaylightDate.wMonth > 0 )
{
DateTime tzSD = new DateTime( year, tzi.DaylightDate.wMonth, tzi.DaylightDate.wDay,
tzi.DaylightDate.wHour, tzi.DaylightDate.wMinute, tzi.DaylightDate.wSecond,
tzi.DaylightDate.wMillisec
Console.WriteLine( " DaylightDate : {0}", tzSD.ToString("s") );
}
}
//
</snip>
ASKER
testn, what is "Bias" and "DayLightBias"?
I want the time in another time zone, but the code you gave doesn't show the time.
I want the time in another time zone, but the code you gave doesn't show the time.
ASKER
I realized that the Bias is the number of minutes offset from GMT.
One question: how come the time zones printed by the program is not the same as the list shown on the Windows Control Panel's Date/Time Properties Window?
One question: how come the time zones printed by the program is not the same as the list shown on the Windows Control Panel's Date/Time Properties Window?
The problem is about DayLight saving.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is ok to give me more points hehe......
ASKER
Thanks. I increased the points to 150. That was quite complicated.
http://support.microsoft.com/default.aspx?scid=KB;en-us;q115231