Skip to main content

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.

<TimeZone>
<Bias>...</Bias>
<StandardTime>...</StandardTime>
<DaylightTime>...</DaylightTime>
</TimeZone>


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

TIME_ZONE_INFORMATION http://msdn2.microsoft.com/en-us/library/ms725481.aspx

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

and

SYSTIME http://msdn2.microsoft.com/en-us/library/ms724950.aspx

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");
REG_TZI_FORMAT rtRegTimeZone = new REG_TZI_FORMAT();
rtRegTimeZone.regget(Tzival);
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";

}
Console.WriteLine("end");

}

}





Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.