(dateTime + BaseUtcOffset).Date :
dateTime.Date;
- for (int i = 0; i < _adjustmentRules.Length; i++)
+ int low = 0;
+ int high = _adjustmentRules.Length - 1;
+
+ while (low <= high)
{
- AdjustmentRule rule = _adjustmentRules[i];
- AdjustmentRule previousRule = i > 0 ? _adjustmentRules[i - 1] : rule;
- if (IsAdjustmentRuleValid(rule, previousRule, dateTime, date, dateTimeisUtc))
+ int median = low + ((high - low) >> 1);
+
+ AdjustmentRule rule = _adjustmentRules[median];
+ AdjustmentRule previousRule = median > 0 ? _adjustmentRules[median - 1] : rule;
+
+ int compareResult = CompareAdjustmentRuleToDateTime(rule, previousRule, dateTime, date, dateTimeisUtc);
+ if (compareResult == 0)
{
return rule;
}
+ else if (compareResult < 0)
+ {
+ low = median + 1;
+ }
+ else
+ {
+ high = median - 1;
+ }
}
return null;
/// <summary>
/// Determines if 'rule' is the correct AdjustmentRule for the given dateTime.
/// </summary>
- private bool IsAdjustmentRuleValid(AdjustmentRule rule, AdjustmentRule previousRule,
+ /// <returns>
+ /// A value less than zero if rule is for times before dateTime.
+ /// Zero if rule is correct for dateTime.
+ /// A value greater than zero if rule is for times after dateTime.
+ /// </returns>
+ private int CompareAdjustmentRuleToDateTime(AdjustmentRule rule, AdjustmentRule previousRule,
DateTime dateTime, DateTime dateOnly, bool dateTimeisUtc)
{
bool isAfterStart;
if (!isAfterStart)
{
- return false;
+ return 1;
}
bool isBeforeEnd;
isBeforeEnd = dateOnly <= rule.DateEnd;
}
- return isBeforeEnd;
+ return isBeforeEnd ? 0 : -1;
}
/// <summary>