private void ShowOffset(DateTime time, TimeZoneInfo timeZone) { DateTime convertedTime = time; TimeSpan offset; if (time.Kind == DateTimeKind.Local && !timeZone.Equals(TimeZoneInfo.Local)) { convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone); } else if (time.Kind == DateTimeKind.Utc && !timeZone.Equals(TimeZoneInfo.Utc)) { convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone); } offset = timeZone.GetUtcOffset(time); if (time == convertedTime) { Console.WriteLine("{0} {1} ", time, timeZone.IsDaylightSavingTime(time) ? timeZone.DaylightName : timeZone.StandardName); Console.WriteLine(" It differs from UTC by {0} hours, {1} minutes.", offset.Hours, offset.Minutes); } else { Console.WriteLine("{0} {1} ", time, time.Kind == DateTimeKind.Utc ? "UTC" : TimeZoneInfo.Local.Id); Console.WriteLine(" converts to {0} {1}.", convertedTime, timeZone.Id); Console.WriteLine(" It differs from UTC by {0} hours, {1} minutes.", offset.Hours, offset.Minutes); } Console.WriteLine(); }
/// <summary> /// Creates a new time record. /// </summary> public static TimeRecord Create(DateTime dateTime, TimeZoneInfo timeZone) { DateTime utcTime = dateTime.Kind == DateTimeKind.Utc ? dateTime : TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone); DateTime localTime; if (dateTime.Kind == DateTimeKind.Utc && !timeZone.Equals(TimeZoneInfo.Utc)) { localTime = TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Utc, timeZone); } else if (dateTime.Kind == DateTimeKind.Local && !timeZone.Equals(TimeZoneInfo.Local)) { localTime = TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, timeZone); } else { localTime = dateTime; } DateTimeOffset timeOffset = new(localTime, timeZone.GetUtcOffset(dateTime)); return(new TimeRecord { Ms = timeOffset.ToUnixTimeMilliseconds(), Ut = utcTime.ToString(WebUtils.JsDateTimeFormat), Lt = timeOffset.ToString(WebUtils.JsDateTimeFormat) }); }
// <Snippet1> private void ShowPossibleUtcTimes(DateTime ambiguousTime, TimeZoneInfo timeZone) { // Determine if time is ambiguous in target time zone if (!timeZone.IsAmbiguousTime(ambiguousTime)) { Console.WriteLine("{0} is not ambiguous in time zone {1}.", ambiguousTime, timeZone.DisplayName); } else { // Display time and its time zone (local, UTC, or indicated by timeZone argument) string originalTimeZoneName; if (ambiguousTime.Kind == DateTimeKind.Utc) { originalTimeZoneName = "UTC"; } else if (ambiguousTime.Kind == DateTimeKind.Local) { originalTimeZoneName = "local time"; } else { originalTimeZoneName = timeZone.DisplayName; } Console.WriteLine("{0} {1} maps to the following possible times:", ambiguousTime, originalTimeZoneName); // Get ambiguous offsets TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(ambiguousTime); // Handle times not in time zone of timeZone argument // Local time where timeZone is not local zone if ((ambiguousTime.Kind == DateTimeKind.Local) && !timeZone.Equals(TimeZoneInfo.Local)) { ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Local, timeZone); } // UTC time where timeZone is not UTC zone else if ((ambiguousTime.Kind == DateTimeKind.Utc) && !timeZone.Equals(TimeZoneInfo.Utc)) { ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Utc, timeZone); } // Display each offset and its mapping to UTC foreach (TimeSpan offset in offsets) { if (offset.Equals(timeZone.BaseUtcOffset)) { Console.WriteLine("If {0} is {1}, {2} UTC", ambiguousTime, timeZone.StandardName, ambiguousTime - offset); } else { Console.WriteLine("If {0} is {1}, {2} UTC", ambiguousTime, timeZone.DaylightName, ambiguousTime - offset); } } } }
/// <summary> /// Maps a date time in terms of Colombia time zone. /// </summary> /// <param name="dt"></param> /// <returns></returns> public static DateTime MapToLocalTimeColombia(this DateTime dt) { TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time"); DateTime time = DateTime.UtcNow; DateTime convertedTime = time; if (time.Kind == DateTimeKind.Local && !timeZone.Equals(TimeZoneInfo.Local)) { convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone); } else if (time.Kind == DateTimeKind.Utc && !timeZone.Equals(TimeZoneInfo.Utc)) { convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone); } return(convertedTime); }
public bool IsOpenAt(TimeSpan time) { TimeZoneInfo local = TimeZoneInfo.Local; TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset; // Is the store in the same time zone? if (tz.Equals(local)) { return(time >= open & time <= close); } else { TimeSpan delta = TimeSpan.Zero; TimeSpan storeDelta = TimeSpan.Zero; // Is it daylight saving time in either time zone? if (local.IsDaylightSavingTime(DateTime.Now.Date + time)) { delta = local.GetAdjustmentRules()[local.GetAdjustmentRules().Length - 1].DaylightDelta; } if (tz.IsDaylightSavingTime(TimeZoneInfo.ConvertTime(DateTime.Now.Date + time, local, tz))) { storeDelta = tz.GetAdjustmentRules()[tz.GetAdjustmentRules().Length - 1].DaylightDelta; } TimeSpan comparisonTime = time + (offset - tz.BaseUtcOffset).Negate() + (delta - storeDelta).Negate(); return(comparisonTime >= open & comparisonTime <= close); } }
public void Freeze(DateTime timeToFreeze) { var frozenTime = !timeZoneInfo.Equals(TimeZoneInfo.Local) ? timeToFreeze.ToLocalTime() : timeToFreeze; ClockManager.Freeze(frozenTime); }
/// <summary> /// Returns the coordinated universal time (UTC) that corresponds to a specified local time. /// </summary> /// <param name="time">The local date and time.</param> /// <returns> /// A <see cref="T:System.DateTime"></see> instance whose value is the UTC time that corresponds to time. /// </returns> public DateTime ToUniversalTime(DateTime time) { if (time.Kind == DateTimeKind.Local && !_timeZoneInfo.Equals(TimeZoneInfo.Local)) { // if the time is local, and we can only recalculate from local throw new ArgumentException( "When specified the Local time (DateTime.Kind = Local), I can convert only from Local timezone!"); } return(time.Kind == DateTimeKind.Utc ? time : ConvertTimeToUtc(time)); }
public void Serialization_Deserialization() { TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 1, 0, 0), 3, 5, DayOfWeek.Sunday); TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 10, 5, DayOfWeek.Sunday); TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan(1, 0, 0), start, end); TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone("Europe/London", new TimeSpan(0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] { rule }); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, london); stream.Position = 0; TimeZoneInfo deserialized = (TimeZoneInfo)formatter.Deserialize(stream); stream.Close(); stream.Dispose(); Assert.IsTrue(london.Equals(deserialized)); }
public static string GetPossibleUtcTimes(TimeZoneInfo timeZone, DateTime ambiguousTime) { StringBuilder oSB = new StringBuilder(); oSB.AppendLine(" Possible UTC Times for timezone:"); // Determine if time is ambiguous in target time zone if (!timeZone.IsAmbiguousTime(ambiguousTime)) { oSB.AppendFormat(" {0} is not ambiguous in time zone {1}.", ambiguousTime, timeZone.DisplayName); } else { // Display time and its time zone (local, UTC, or indicated by timeZone argument) string originalTimeZoneName = string.Empty;; if (ambiguousTime.Kind == DateTimeKind.Utc) { originalTimeZoneName = "UTC"; } else if (ambiguousTime.Kind == DateTimeKind.Local) { originalTimeZoneName = "local time"; } else { originalTimeZoneName = timeZone.DisplayName; } oSB.AppendFormat(" {0} {1} maps to the following possible times:", ambiguousTime, originalTimeZoneName); // Get ambiguous offsets TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(ambiguousTime); // Handle times not in time zone of timeZone argument // Local time where timeZone is not local zone if ((ambiguousTime.Kind == DateTimeKind.Local) && !timeZone.Equals(TimeZoneInfo.Local)) { ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Local, timeZone); } // UTC time where timeZone is not UTC zone else if ((ambiguousTime.Kind == DateTimeKind.Utc) && !timeZone.Equals(TimeZoneInfo.Utc)) { ambiguousTime = TimeZoneInfo.ConvertTime(ambiguousTime, TimeZoneInfo.Utc, timeZone); } // Display each offset and its mapping to UTC foreach (TimeSpan offset in offsets) { if (offset.Equals(timeZone.BaseUtcOffset)) { oSB.AppendFormat(" If {0} is {1}, {2} UTC", ambiguousTime, timeZone.StandardName, ambiguousTime - offset); } else { oSB.AppendFormat(" If {0} is {1}, {2} UTC", ambiguousTime, timeZone.DaylightName, ambiguousTime - offset); } } } return(oSB.ToString()); }