System.TimeZoneInfo.GetApplicableRule C# (CSharp) Method

GetApplicableRule() private method

private GetApplicableRule ( System.DateTime dateTime ) : AdjustmentRule
dateTime System.DateTime
return AdjustmentRule
		private AdjustmentRule GetApplicableRule (DateTime dateTime)
		{
			//Applicable rules are in standard time
			DateTime date = dateTime;

			if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local) {
				if (!TryAddTicks (date.ToUniversalTime (), BaseUtcOffset.Ticks, out date))
					return null;
			} else if (dateTime.Kind == DateTimeKind.Utc && this != TimeZoneInfo.Utc) {
				if (!TryAddTicks (date, BaseUtcOffset.Ticks, out date))
					return null;
			}

			// get the date component of the datetime
			date = date.Date;

			if (adjustmentRules != null) {
				foreach (AdjustmentRule rule in adjustmentRules) {
					if (rule.DateStart > date)
						return null;
					if (rule.DateEnd < date)
						continue;
					return rule;
				}
			}
			return null;
		}

Usage Example

		// This is an helper method used by the method above, do not use this on its own.
		private static TimeSpan GetUtcOffsetHelper (DateTime dateTime, TimeZoneInfo tz, out bool isDST)
		{
			if (dateTime.Kind == DateTimeKind.Local && tz != TimeZoneInfo.Local)
				throw new Exception ();

			isDST = false;

			if (tz == TimeZoneInfo.Utc)
				return TimeSpan.Zero;

			TimeSpan offset;
			if (tz.TryGetTransitionOffset(dateTime, out offset, out isDST))
				return offset;

			if (dateTime.Kind == DateTimeKind.Utc) {
				var utcRule = tz.GetApplicableRule (dateTime);
				if (utcRule != null && tz.IsInDST (utcRule, dateTime)) {
					isDST = true;
					return tz.BaseUtcOffset + utcRule.DaylightDelta;
				}

				return tz.BaseUtcOffset;
			}

			DateTime stdUtcDateTime;
			if (!TryAddTicks (dateTime, -tz.BaseUtcOffset.Ticks, out stdUtcDateTime, DateTimeKind.Utc))
				return tz.BaseUtcOffset;

			var tzRule = tz.GetApplicableRule (stdUtcDateTime);

			DateTime dstUtcDateTime = DateTime.MinValue;
			if (tzRule != null) {
				if (!TryAddTicks (stdUtcDateTime, -tzRule.DaylightDelta.Ticks, out dstUtcDateTime, DateTimeKind.Utc))
					return tz.BaseUtcOffset;
			}

			if (tzRule != null && tz.IsInDST (tzRule, stdUtcDateTime) && tz.IsInDST (tzRule, dstUtcDateTime)) {
				isDST = true;
				return tz.BaseUtcOffset + tzRule.DaylightDelta;
			}

			return tz.BaseUtcOffset;
		}
All Usage Examples Of System.TimeZoneInfo::GetApplicableRule