Friday, April 20, 2007

Understanding the TimeZone setting with the GetUserAvailability Operation in Exchange Web Services in Exchange 2007

Updated 12/7/2007 with the proper method of using the registry thanks to Benjamin Spain from MSFT for pointing this out

Using TimeZones is very different in EWS then it is in other Exchange API’s like CDOEX where the time zone configuration itself is stored in the DLL (hence causing the need to patch the dll every time the government decides to change the date for Daylight savings). I’ve been working on some GetUserAvailibility code and it’s been a bit of a struggle to get my head around the new format and how to make this flexible (eg no hard coding Bias values or Daylight Saving settings) in the code I’m using so I thought I’d put together a post to share what I’ve learned.

To populate the TimeZone information in a GetUserAvailability Operation you need to set the properties in a GetUserAvailabilityRequestType object so the following nodes will be populated in the SOAP request.


Theses nodes should reflect the setting from the registry timezone keys under SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\ for whatever timezone you want to make the request from. Not all timezones use DST but you still have to fill out both standardTime and daylightTime nodes in the case where you have no Daylight setting.

To read the timezone setting from the registry you need to deal with two different data structures which are documented on MSDN the first is the


Make sure you read the StandardDate section of this doco this is very important in relation to the rest of the code.



To find the correct node in the registry the method that I've used was to get the StandardName from TimeZone.CurrentTimeZone.StandardName which i found usually corresponds to the Key name under SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\. Under that Key you need to get the TZI value which is a binary registry key which stores information in the TIME_ZONE_INFORMATION structure.

To help parse this information i used a few structs as the format defines the first 4 bytes relate to the default Bias the next 4 bytes are the standardTime Bias (usually always 0) and the next four bytes are the daylightTime Bias. The remaining bytes are comprised of 16bit words that define 2 SYSTIME structures one for standard time and one for daylight time. These structures basically tell you when DST and StandardTime starts and Ends. To use these in a GetUserAvailibility operation you need to translate them into a SerializableTimeZoneTime object.

So to put this together is some code it would look like the following there a downloadable version here

class Program
private struct SYSTEMTIME
public Int16 wYear;
public Int16 wMonth;
public Int16 wDayOfWeek;
public Int16 wDay;
public Int16 wHour;
public Int16 wMinute;
public Int16 wSecond;
public Int16 wMilliseconds;
public void getSysTime(byte[] Tzival,int offset) {
wYear = BitConverter.ToInt16(Tzival, offset);
wMonth = BitConverter.ToInt16(Tzival, offset + 2);
wDayOfWeek = BitConverter.ToInt16(Tzival, offset + 4);
wDay = BitConverter.ToInt16(Tzival, offset + 6);
wHour = BitConverter.ToInt16(Tzival, offset + 8);
wMinute = BitConverter.ToInt16(Tzival, offset + 10);
wSecond = BitConverter.ToInt16(Tzival, offset + 12);
wMilliseconds = BitConverter.ToInt16(Tzival, offset + 14);
private struct REG_TZI_FORMAT
public Int32 Bias;
public Int32 StandardBias;
public Int32 DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
public void regget(byte[] Tzival) {
Bias = BitConverter.ToInt32(Tzival, 0);
StandardBias = BitConverter.ToInt32(Tzival, 4);
DaylightBias = BitConverter.ToInt32(Tzival, 8);
StandardDate = new SYSTEMTIME();
StandardDate.getSysTime(Tzival, 12);
DaylightDate = new SYSTEMTIME();
DaylightDate.getSysTime(Tzival, 28);


static void Main(string[] args)
String tzString = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\" + TimeZone.CurrentTimeZone.StandardName;
RegistryKey TziRegKey = Registry.LocalMachine;
TziRegKey = TziRegKey.OpenSubKey(tzString);
byte[] Tzival = (byte[])TziRegKey.GetValue("TZI");
GetUserAvailabilityRequestType fbRequest = new GetUserAvailabilityRequestType();
fbRequest.TimeZone = new SerializableTimeZone();
fbRequest.TimeZone.DaylightTime = new SerializableTimeZoneTime();
fbRequest.TimeZone.StandardTime = new SerializableTimeZoneTime();
fbRequest.TimeZone.Bias = rtRegTimeZone.Bias;
fbRequest.TimeZone.StandardTime.Bias = rtRegTimeZone.StandardBias;
fbRequest.TimeZone.DaylightTime.Bias = rtRegTimeZone.DaylightBias;
if (rtRegTimeZone.StandardDate.wMonth != 0)
fbRequest.TimeZone.StandardTime.DayOfWeek = ((DayOfWeek)rtRegTimeZone.StandardDate.wDayOfWeek).ToString();
fbRequest.TimeZone.StandardTime.DayOrder = (short)rtRegTimeZone.StandardDate.wDay;
fbRequest.TimeZone.StandardTime.Month = rtRegTimeZone.StandardDate.wMonth;
fbRequest.TimeZone.StandardTime.Time = String.Format("{0:0#}:{1:0#}:{2:0#}", rtRegTimeZone.StandardDate.wHour, rtRegTimeZone.StandardDate.wMinute, rtRegTimeZone.StandardDate.wSecond);
else {
fbRequest.TimeZone.StandardTime.DayOfWeek = "Sunday";
fbRequest.TimeZone.StandardTime.DayOrder = 1;
fbRequest.TimeZone.StandardTime.Month = 1;
fbRequest.TimeZone.StandardTime.Time = "00:00:00";

if (rtRegTimeZone.DaylightDate.wMonth != 0)
fbRequest.TimeZone.DaylightTime.DayOfWeek = ((DayOfWeek)rtRegTimeZone.DaylightDate.wDayOfWeek).ToString();
fbRequest.TimeZone.DaylightTime.DayOrder = (short)rtRegTimeZone.DaylightDate.wDay;
fbRequest.TimeZone.DaylightTime.Month = rtRegTimeZone.DaylightDate.wMonth;
fbRequest.TimeZone.DaylightTime.Time = "00:00:00";
else {
fbRequest.TimeZone.DaylightTime.DayOfWeek = "Sunday";
fbRequest.TimeZone.DaylightTime.DayOrder = 5;
fbRequest.TimeZone.DaylightTime.Month = 12;
fbRequest.TimeZone.DaylightTime.Time = "23:59:59";




1 comment:

Shafki said...

Glen...You are the man :)....

Thanks a lot. You have saved my life :)