问题
When using the EWS GetUserAvailability()
function, I get in the AttendeesAvailability[i]
object a WorkingHours object containing a Start, an End, the WeekDays, and a TimeZoneInfo
object.
For me, the TimeZoneInfo
object always has the name Custom Timezone
and a GUID as ID, so I cannot restore the very same time zone using FindSystemTimeZoneById
.
The time zone that the user can select for his availability, however, is one of the system time zones, so I really expected to find a system time zone returned here. How can I find - from EWS - which system time zone the user has selected in his Outlook or OWA account settings?
If I can't, what is the appropriate way to store a TimeZoneInfo
object in SQL server?
回答1:
I have since found out that the problem is the EWS XML format. As can be seen here, it does not contain the full time zone information (e.g. name), only a subset:
<TimeZone>
<Bias>480</Bias>
<StandardTime>
<Bias>0</Bias>
<Time>02:00:00</Time>
<DayOrder>5</DayOrder>
<Month>10</Month>
<DayOfWeek>Sunday</DayOfWeek>
</StandardTime>
<DaylightTime>
<Bias>-60</Bias>
<Time>02:00:00</Time>
<DayOrder>1</DayOrder>
<Month>4</Month>
<DayOfWeek>Sunday</DayOfWeek>
</DaylightTime>
</TimeZone>
This subset is then parsed back into a time zone in this EWS Managed API code, with this really useful Guid "Id" (the Id is different every time I call GetUserAvailability
, although the timezone is still the same):
return TimeZoneInfo.CreateCustomTimeZone(
Guid.NewGuid().ToString(),
-this.bias,
"Custom time zone",
"Standard time",
"Daylight time",
new TimeZoneInfo.AdjustmentRule[] { adjustmentRule });
So I guess I can expect that in 99% of the cases I will find one or more matching time zones in the list of available system time zones, and I am for now using the extension method:
public static TimeZoneInfo ToSystemTimeZone(this TimeZoneInfo customTimeZone)
{
var tz = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.HasSameRules(customTimeZone));
if (tz != null) return tz;
else return customTimeZone;
}
This provides the first system time zone that has the same rules as the custom time zone. It's not a 100% match, since there may be multiple time zones with the same rules, but at least this should keep all the time zone information that is exposed via EWS.
来源:https://stackoverflow.com/questions/37702790/ews-returns-custom-time-zone-how-to-store-it-in-sql