System.TimeZoneInfo.TryGetTransitionOffset C# (CSharp) Method

TryGetTransitionOffset() private method

private TryGetTransitionOffset ( System.DateTime dateTime, System.TimeSpan &offset, bool &isDst ) : bool
dateTime System.DateTime
offset System.TimeSpan
isDst bool
return bool
		private bool TryGetTransitionOffset (DateTime dateTime, out TimeSpan offset,out bool isDst)
		{
			offset = BaseUtcOffset;
			isDst = false;

			if (transitions == null)
				return false;

			//Transitions are in UTC
			DateTime date = dateTime;

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

			if (dateTime.Kind != DateTimeKind.Utc) {
				if (!TryAddTicks (date, -BaseUtcOffset.Ticks, out date, DateTimeKind.Utc))
					return false;
			}

			var inDelta = false;
			for (var i =  transitions.Count - 1; i >= 0; i--) {
				var pair = transitions [i];
				DateTime ttime = pair.Key;
				TimeType ttype = pair.Value;

				var delta =  new TimeSpan (0, 0, ttype.Offset) - BaseUtcOffset;

				if ((ttime + delta) > date) {
					inDelta = ttime <= date;
					continue;
				}

				offset =  new TimeSpan (0, 0, ttype.Offset);
				if (inDelta) {
					// Replicate what .NET does when given a time which falls into the hour which is lost when
					// DST starts. isDST should be true but the offset should be the non-DST offset.
					isDst = transitions [i - 1].Value.IsDst;
				} else {
					isDst = ttype.IsDst;
				}

				return true;
			}

			return false;
		}

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::TryGetTransitionOffset