Wednesday, April 13, 2011

Displaying ActiveDirectory's "whenChanged" attribute in C#

"whenChanged" comes through without the time offset. Not a big deal to fix it, but a quick Google search yielded no results on how to deal with it. Here's my solution to properly display Eastern Daylight/Standard time:
TimeZone localZone = TimeZone.CurrentTimeZone;
DateTime generalizedDate = DateTime.Parse(result.Properties["whenChanged"][0].ToString());
double hourDelta = localZone.IsDaylightSavingTime(generalizedDate) ? -4D : -5D;
DateTime lastUpdated = generalizedDate.AddHours(hourDelta);
BTW, Sysinternals' AD Explorer is great for finding out meta information (i.e. data type, property name) about AD attributes.