4

Hey, I've a TimeZoneInfo object and from this one I want to create a TIME_ZONE_INFO struct.

Bias, StandardDate and Daylightdate are pretty easy to obtain. However, I'm having problems getting standardbias and daylightbias. So the question is, How can I get the standardbias from a TimeZOneInfo object and how can I get the same thing for daylightbias (there is a AdjustmentRule.DaylightDelta, but as you can see I need the offset not the delta).

Thanks.

2 Answers 2

3

I compared this code to the results of using CrankedUp (which reads TIME_ZONE_INFORMATION) and the results are identical on my Windows XP sp3 machine. Your results may vary.

TimeZoneInfo.AdjustmentRule[] adjustmentRules = timeZoneInfo.GetAdjustmentRules();
TimeZoneInfo.AdjustmentRule adjustmentRule = null;
if (adjustmentRules.Length > 0)
{
    // Find the single record that encompasses today's date. If none exists, sets adjustmentRule to null.
    adjustmentRule = adjustmentRules.SingleOrDefault(ar => ar.DateStart <= DateTime.Now && DateTime.Now <= ar.DateEnd);
}

double bias = -timeZoneInfo.BaseUtcOffset.TotalMinutes; // I'm not sure why this number needs to be negated, but it does.
string daylightName = timeZoneInfo.DaylightName;
string standardName = timeZoneInfo.StandardName;
double daylightBias = adjustmentRule == null ? -60 : -adjustmentRule.DaylightDelta.TotalMinutes; // Not sure why default is -60, or why this number needs to be negated, but it does.
int daylightDay = 0;
int daylightDayOfWeek = 0;
int daylightHour = 0;
int daylightMonth = 0;
int standardDay = 0;
int standardDayOfWeek = 0;
int standardHour = 0;
int standardMonth = 0;

if (adjustmentRule != null)
{
    TimeZoneInfo.TransitionTime daylightTime = adjustmentRule.DaylightTransitionStart;
    TimeZoneInfo.TransitionTime standardTime = adjustmentRule.DaylightTransitionEnd;

    // Valid values depend on IsFixedDateRule: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule.
    daylightDay = daylightTime.IsFixedDateRule ? daylightTime.Day : daylightTime.Week;
    daylightDayOfWeek = daylightTime.IsFixedDateRule ? -1 : (int)daylightTime.DayOfWeek;
    daylightHour = daylightTime.TimeOfDay.Hour;
    daylightMonth = daylightTime.Month;

    standardDay = standardTime.IsFixedDateRule ? standardTime.Day : standardTime.Week;
    standardDayOfWeek = standardTime.IsFixedDateRule ? -1 : (int)standardTime.DayOfWeek;
    standardHour = standardTime.TimeOfDay.Hour;
    standardMonth = standardTime.Month;
}
1
  • How to get the standardBias? Sep 22, 2020 at 13:42
1

The TIME_ZONE_INFORMATION help is pretty useful. It says that the standardbias is 0 for most time zones. It doesn't make much sense, to me, to have a Time Zone where standardbias is non-zero. Isn't that what "standard" means?

The DaylightDelta is the difference between the Standard UTC Offset and the Daylight UTC Offset. DaylightBias is defined the same way, so your DaylightDelta is your DaylightBias.

I can't hack this right now, but I suggest playing around with the data in your Time Zone. Alternatively, is there a way you can use Win32 objects to get the TIME_ZONE_INFORMATION struct for the appropriate TimeZoneInfo instead of creating the object? Something like GetTimeZoneInformationForYear by specifying the TimeZoneInfo.StandardName in DYNAMIC_TIME_ZONE_INFORMATION.StandardName ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.