Ejemplo n.º 1
0
        public static DateTime SetValidTimeOfDay(DateTime dateTime, TimeSpan timeOfDay, TimeZoneInfo timezone)
        {
            dateTime = DateTimeUtils.SetTimeOfDay(dateTime, timeOfDay);
            // time can be invalid when setting the clock one hour forward
            if (timezone.IsInvalidTime(dateTime))
            {
                TimeZoneInfo.AdjustmentRule rule = AdjustmentRuleUtils.GetAdjustmentRuleForTime(timezone.GetAdjustmentRules(), dateTime.Date);
                // rule cannot be null for invalid time
                dateTime = dateTime.Add(rule.DaylightDelta);
            }

            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
            return(dateTime);
        }
Ejemplo n.º 2
0
 public static DateTime SafeConvertToUtc(DateTime dateTime, TimeZoneInfo sourceTimeZone)
 {
     // time can be invalid when setting the clock one hour forward
     if (sourceTimeZone.IsInvalidTime(dateTime))
     {
         TimeZoneInfo.AdjustmentRule rule = AdjustmentRuleUtils.GetAdjustmentRuleForTime(sourceTimeZone.GetAdjustmentRules(), dateTime.Date);
         // rule cannot be null for invalid time
         return(TimeZoneInfo.ConvertTimeToUtc(dateTime.Add(rule.DaylightDelta), sourceTimeZone));
     }
     else
     {
         return(TimeZoneInfo.ConvertTimeToUtc(dateTime, sourceTimeZone));
     }
 }
Ejemplo n.º 3
0
        public static DateTime AdvancedConvertToUtc(DateTime dateTime, TimeZoneInfo sourceTimeZone, bool assumeDaylightTimeForAmbiguousTime)
        {
            DateTime result = SafeConvertToUtc(dateTime, sourceTimeZone);

            // time can be ambiguous when setting the clock one hour backward
            if (sourceTimeZone.IsAmbiguousTime(dateTime) && assumeDaylightTimeForAmbiguousTime)
            {
                TimeZoneInfo.AdjustmentRule rule = AdjustmentRuleUtils.GetAdjustmentRuleForTime(sourceTimeZone.GetAdjustmentRules(), dateTime.Date);
                // ConvertTimeToUtc will assume standard time, we want it to assume daylight time
                // rule cannot be null for ambiguous time
                result = result.Add(-rule.DaylightDelta);
            }

            return(result);
        }
        public static TimeZoneInfo GetSystemStaticTimeZone(string keyName)
        {
            RegistryTimeZoneInformation information = RegistryTimeZoneUtils.GetStaticTimeZoneInformation(keyName);

            TimeZoneInfo.AdjustmentRule rule = AdjustmentRuleUtils.GetStaticAdjustmentRule(information);
            string displayName;
            string standardDisplayName;
            string daylightDisplayName;

            displayName = RegistryTimeZoneUtils.GetDisplayName(keyName, out standardDisplayName, out daylightDisplayName);

            TimeSpan baseUtcOffset = new TimeSpan(0, -(information.Bias + information.StandardBias), 0);

            if (rule == null)
            {
                // null will only be returned if there is no daylight saving
                return(TimeZoneInfo.CreateCustomTimeZone(keyName, baseUtcOffset, displayName, standardDisplayName));
            }
            else
            {
                return(TimeZoneInfo.CreateCustomTimeZone(keyName, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, new TimeZoneInfo.AdjustmentRule[] { rule }));
            }
        }