public abstract GetUtcOffset ( System.DateTime time ) : System.TimeSpan | ||
time | System.DateTime | |
Résultat | System.TimeSpan |
private void CET (TimeZone t1) { Assert.AreEqual("CET", t1.StandardName, "A01"); Assert.AreEqual("CEST", t1.DaylightName, "A02"); DaylightTime d1 = t1.GetDaylightChanges (2002); Assert.AreEqual("03/31/2002 02:00:00", d1.Start.ToString ("G", CultureInfo.InvariantCulture), "A03"); Assert.AreEqual("10/27/2002 03:00:00", d1.End.ToString ("G", CultureInfo.InvariantCulture), "A04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "A05"); DaylightTime d2 = t1.GetDaylightChanges (1996); Assert.AreEqual("03/31/1996 02:00:00", d2.Start.ToString ("G", CultureInfo.InvariantCulture), "A06"); Assert.AreEqual("10/27/1996 03:00:00", d2.End.ToString ("G", CultureInfo.InvariantCulture), "A07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "A08"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "A09"); DateTime d4 = new DateTime (2002,4,2); Assert.AreEqual(true, t1.IsDaylightSavingTime (d4), "A10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "A11"); Assert.AreEqual(36000000000L, t1.GetUtcOffset (d3).Ticks, "A12"); Assert.AreEqual(72000000000L, t1.GetUtcOffset (d4).Ticks, "A13"); Assert.AreEqual(36000000000L, t1.GetUtcOffset (d5).Ticks, "A14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d4.ToUniversalTime (); Assert.AreEqual(false, t1.IsDaylightSavingTime (d6), "A15"); Assert.AreEqual(0, t1.GetUtcOffset (d6).Ticks, "A16"); }
private void EST (TimeZone t1) { Assert.IsTrue("EST" == t1.StandardName || "Eastern Standard Time" == t1.StandardName, "B01"); Assert.IsTrue("EDT" == t1.DaylightName || "Eastern Daylight Time" == t1.DaylightName, "B02"); DaylightTime d1 = t1.GetDaylightChanges (2002); Assert.AreEqual("04/07/2002 02:00:00", d1.Start.ToString ("G", CultureInfo.InvariantCulture), "B03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString ("G", CultureInfo.InvariantCulture), "B04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "B05"); DaylightTime d2 = t1.GetDaylightChanges (1996); Assert.AreEqual("04/07/1996 02:00:00", d2.Start.ToString ("G", CultureInfo.InvariantCulture), "B06"); Assert.AreEqual("10/27/1996 02:00:00", d2.End.ToString ("G", CultureInfo.InvariantCulture), "B07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "B08"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "B09"); DateTime d4 = new DateTime (2002,4,8); Assert.AreEqual(true, t1.IsDaylightSavingTime (d4), "B10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "B11"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset (d3).Ticks, "B12"); Assert.AreEqual(-144000000000L, t1.GetUtcOffset (d4).Ticks, "B13"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset (d5).Ticks, "B14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d4.ToUniversalTime (); Assert.AreEqual(false, t1.IsDaylightSavingTime (d6), "B15"); Assert.AreEqual(0, t1.GetUtcOffset (d6).Ticks, "B16"); }
private void CET (TimeZone t1) { Assert.AreEqual("CET", t1.StandardName, "A01"); Assert.AreEqual("CEST", t1.DaylightName, "A02"); DaylightTime d1 = t1.GetDaylightChanges (2002); Assert.AreEqual("03/31/2002 02:00:00", d1.Start.ToString ("G"), "A03"); Assert.AreEqual("10/27/2002 03:00:00", d1.End.ToString ("G"), "A04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "A05"); DaylightTime d2 = t1.GetDaylightChanges (1996); Assert.AreEqual("03/31/1996 02:00:00", d2.Start.ToString ("G"), "A06"); Assert.AreEqual("10/27/1996 03:00:00", d2.End.ToString ("G"), "A07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "A08"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "A09"); DateTime d4 = new DateTime (2002,4,2); Assert.AreEqual(true, t1.IsDaylightSavingTime (d4), "A10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "A11"); Assert.AreEqual(36000000000L, t1.GetUtcOffset (d3).Ticks, "A12"); Assert.AreEqual(72000000000L, t1.GetUtcOffset (d4).Ticks, "A13"); Assert.AreEqual(36000000000L, t1.GetUtcOffset (d5).Ticks, "A14"); }
private void EST (TimeZone t1) { // It could be EST though... //Assert.AreEqual("Eastern Standard Time", t1.StandardName, "B01"); //Assert.AreEqual("Eastern Daylight Time", t1.DaylightName, "B02"); DaylightTime d1 = t1.GetDaylightChanges (2002); Assert.AreEqual("04/07/2002 02:00:00", d1.Start.ToString ("G"), "B03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString ("G"), "B04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "B05"); DaylightTime d2 = t1.GetDaylightChanges (1996); Assert.AreEqual("04/07/1996 02:00:00", d2.Start.ToString ("G"), "B06"); Assert.AreEqual("10/27/1996 02:00:00", d2.End.ToString ("G"), "B07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "B08"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "B09"); DateTime d4 = new DateTime (2002,4,8); Assert.AreEqual(true, t1.IsDaylightSavingTime (d4), "B10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "B11"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset (d3).Ticks, "B12"); Assert.AreEqual(-144000000000L, t1.GetUtcOffset (d4).Ticks, "B13"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset (d5).Ticks, "B14"); }
private void CET (TimeZone t1) { AssertEquals("A01", "CET", t1.StandardName); AssertEquals("A02", "CEST", t1.DaylightName); DaylightTime d1 = t1.GetDaylightChanges (2002); AssertEquals("A03", "03/31/2002 01:00:00", d1.Start.ToString ("G")); AssertEquals("A04", "10/27/2002 01:00:00", d1.End.ToString ("G")); AssertEquals("A05", 36000000000L, d1.Delta.Ticks); DaylightTime d2 = t1.GetDaylightChanges (1996); AssertEquals("A06", "03/31/1996 01:00:00", d2.Start.ToString ("G")); AssertEquals("A07", "10/27/1996 01:00:00", d2.End.ToString ("G")); AssertEquals("A08", 36000000000L, d2.Delta.Ticks); DateTime d3 = new DateTime (2002,2,25); AssertEquals("A09", false, t1.IsDaylightSavingTime (d3)); DateTime d4 = new DateTime (2002,4,2); AssertEquals("A10", true, t1.IsDaylightSavingTime (d4)); DateTime d5 = new DateTime (2002,11,4); AssertEquals("A11", false, t1.IsDaylightSavingTime (d5)); AssertEquals("A12", 36000000000L, t1.GetUtcOffset (d3).Ticks); AssertEquals("A13", 72000000000L, t1.GetUtcOffset (d4).Ticks); AssertEquals("A14", 36000000000L, t1.GetUtcOffset (d5).Ticks); }
int IComparable.CompareTo(object o) { if (o == null) { return(1); } Server server = o as Server; if (server == null) { throw new ArgumentException(); } System.TimeZone currentTimeZone = System.TimeZone.CurrentTimeZone; DateTime now = DateTime.Now; int hours = currentTimeZone.GetUtcOffset(now).Hours; int num2 = -this.m_TimeZone; int num3 = -server.m_TimeZone; int num4 = Math.Abs((int)(hours - num2)); int num5 = Math.Abs((int)(hours - num3)); int num6 = num4.CompareTo(num5); if (num6 == 0) { num6 = server.m_PercentFull.CompareTo(this.m_PercentFull); if (num6 == 0) { num6 = this.m_Name.CompareTo(server.m_Name); if (num6 == 0) { num6 = this.m_ServerID.CompareTo(server.m_ServerID); } } } return(num6); }
public override string ToString() { string result = null; switch (SelectedIndex) { case 1: string dateFormat = string.Empty; string timeFormat = string.Empty; Regex r1 = new Regex(@"(y*[-\/]*M*[-\/]*[Dd]*)(M*[-\/]*[Dd]*[-\/]*y*)([Dd]*[-\/]*M*[-\/]*y*)([Hh]*[:]*m*[:]*s*[ ]*t*)"); MatchCollection mc = r1.Matches(DateTimeFormat); foreach (Match match in mc) { if (match.Value.Trim().Length > 0 && match.Value.Contains('d')) { dateFormat = match.Value; } if (match.Value.Trim().Length > 0 && match.Value.Contains('m')) { timeFormat = match.Value; } } System.TimeZone zone = System.TimeZone.CurrentTimeZone; TimeSpan span = zone.GetUtcOffset(DateTime.Now); DateTime dtLocal = DateTime.Now.Add(TimeZone.UtcOffset - span); // if GAT is behind Now, return +1 int addDays = ExactTimeValidDays; if (ExactTimeValidDays == 0 && DateTime.Parse(dtLocal.ToShortTimeString()) >= DateTime.Parse(ExactTime.ToShortTimeString())) { addDays = 1; } result = DateTimeFormat.Replace(dateFormat, dtLocal.AddDays(addDays).ToString(dateFormat)); result = result.Replace(timeFormat, ExactTime.ToString(timeFormat)); result += TimeZone != null ? " " + TimeZone.Id : ""; //System.Diagnostics.Debug.WriteLine("GTD: " + result); break; case 2: //result = OrderTime.AddSeconds(Seconds).ToString(DateTimeFormat); // assume using current timezone setting result = DateTime.Now.AddSeconds(Seconds).ToString(DateTimeFormat); break; case 3: int div = (int)Math.Ceiling((float)(OrderTime.Minute / (int)BarInterval)); // in case of the beginning of bar if (OrderTime.Minute % (int)BarInterval == 0) { div++; } result = DateTime.Now.AddMinutes((int)BarInterval * div).ToString(DateTimeFormat); break; } return(result); }
/// <summary> /// <para>Converts a given <see cref='System.DateTime'/> object to DMTF format.</para> /// /// </summary> /// <param name='date'>A <see cref='System.DateTime'/> object representing the datetime to be converted to DMTF datetime.</param> /// <returns> /// <para>A string that represents the DMTF datetime for the given DateTime object.</para> /// </returns> /// <remarks> /// <para> Date and time in WMI is represented in DMTF datetime format. This format is explained in WMI SDK documentation. /// The DMTF datetime string represented will be with respect to the UTC offset of the /// current timezone. The lowest precision in DMTF is microseconds and /// in <see cref='System.DateTime'/> is Ticks , which is equivalent to 100 of nanoseconds. /// During conversion these Ticks are converted to microseconds and rounded /// off to the the nearest microsecond. /// </para> /// </remarks> /// <example> /// <code lang='C#'> /// // Convert the current time in System.DateTime to DMTF format /// string dmtfDateTime = ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now); /// </code> /// <code lang='VB'> /// ' Convert the current time in System.DateTime to DMTF format /// Dim dmtfDateTime as String = ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now) /// </code> /// </example> public static string ToDmtfDateTime(DateTime date) { string UtcString = String.Empty; // Fill up the UTC field in the DMTF date with the current // zones UTC value System.TimeZone curZone = System.TimeZone.CurrentTimeZone; System.TimeSpan tickOffset = curZone.GetUtcOffset(date); long OffsetMins = (tickOffset.Ticks / System.TimeSpan.TicksPerMinute); IFormatProvider frmInt32 = (IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int32)); // If the offset is more than that what can be specified in DMTF format, then // convert the date to UniversalTime if (Math.Abs(OffsetMins) > MAXSIZE_UTC_DMTF) { date = date.ToUniversalTime(); UtcString = "+000"; } else if ((tickOffset.Ticks >= 0)) { UtcString = "+" + ((tickOffset.Ticks / System.TimeSpan.TicksPerMinute)).ToString(frmInt32).PadLeft(3, '0'); } else { string strTemp = OffsetMins.ToString(frmInt32); UtcString = "-" + strTemp.Substring(1, strTemp.Length - 1).PadLeft(3, '0'); } string dmtfDateTime = date.Year.ToString(frmInt32).PadLeft(4, '0'); dmtfDateTime = (dmtfDateTime + date.Month.ToString(frmInt32).PadLeft(2, '0')); dmtfDateTime = (dmtfDateTime + date.Day.ToString(frmInt32).PadLeft(2, '0')); dmtfDateTime = (dmtfDateTime + date.Hour.ToString(frmInt32).PadLeft(2, '0')); dmtfDateTime = (dmtfDateTime + date.Minute.ToString(frmInt32).PadLeft(2, '0')); dmtfDateTime = (dmtfDateTime + date.Second.ToString(frmInt32).PadLeft(2, '0')); dmtfDateTime = (dmtfDateTime + "."); // Construct a DateTime with with the precision to Second as same as the passed DateTime and so get // the ticks difference so that the microseconds can be calculated DateTime dtTemp = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, 0); System.Int64 microsec = ((date.Ticks - dtTemp.Ticks) * 1000) / System.TimeSpan.TicksPerMillisecond; // fill the microseconds field String strMicrosec = microsec.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int64))); if (strMicrosec.Length > 6) { strMicrosec = strMicrosec.Substring(0, 6); } dmtfDateTime = dmtfDateTime + strMicrosec.PadLeft(6, '0'); // adding the UTC offset dmtfDateTime = dmtfDateTime + UtcString; return(dmtfDateTime); }
static public int GetUtcOffset(IntPtr l) { try { System.TimeZone self = (System.TimeZone)checkSelf(l); System.DateTime a1; checkValueType(l, 2, out a1); var ret = self.GetUtcOffset(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
static int GetUtcOffset(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); System.TimeZone obj = (System.TimeZone)ToLua.CheckObject <System.TimeZone>(L, 1); System.DateTime arg0 = StackTraits <System.DateTime> .Check(L, 2); System.TimeSpan o = obj.GetUtcOffset(arg0); ToLua.PushValue(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private static string GetTimezone() { System.TimeZone tz = System.TimeZone.CurrentTimeZone; System.TimeSpan tspan = tz.GetUtcOffset(System.DateTime.Now); // TimeZone. Format should be '+|-HH:MM' (e.g., -07:00). String tzformat; if (tspan.Hours >= 0) { tzformat = String.Format("+{0}:{1}", tspan.Hours.ToString("D2"), tspan.Minutes.ToString("D2")); } else { tzformat = String.Format("{0}:{1}", tspan.Hours.ToString("D2"), tspan.Minutes.ToString("D2")); } return(tzformat); }
static StackObject *GetUtcOffset_3(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(__esp, 2); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.DateTime @time = (System.DateTime) typeof(System.DateTime).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); ptr_of_this_method = ILIntepreter.Minus(__esp, 2); System.TimeZone instance_of_this_method = (System.TimeZone) typeof(System.TimeZone).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); var result_of_this_method = instance_of_this_method.GetUtcOffset(@time); return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method)); }
public ServerInfo( string name, int fullPercent, TimeZone tz, IPEndPoint address ) { m_Name = name; m_FullPercent = fullPercent; m_TimeZone = tz.GetUtcOffset( Core.Now ).Hours; m_Address = address; }
private void GMT (TimeZone t1) { // Probably wont work on MS.NET, but is better than nothing. Where do // we change our implementation to match theirs? Assert.AreEqual("GMT", t1.StandardName, "D01"); Assert.IsTrue("BST" == t1.DaylightName || "IST" == t1.DaylightName, "D02"); DaylightTime d1 = t1.GetDaylightChanges (2002); Assert.AreEqual("03/31/2002 01:00:00", d1.Start.ToString ("G"), "D03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString ("G"), "D04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "D05"); DaylightTime d2 = t1.GetDaylightChanges (1996); Assert.AreEqual("03/31/1996 01:00:00", d2.Start.ToString ("G"), "D06"); Assert.AreEqual("10/27/1996 02:00:00", d2.End.ToString ("G"), "D07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "D08"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "D09"); DateTime d4 = new DateTime (2002,4,2); Assert.AreEqual(true, t1.IsDaylightSavingTime (d4), "D10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "D11"); Assert.AreEqual(0L, t1.GetUtcOffset (d3).Ticks, "D12"); Assert.AreEqual(36000000000L, t1.GetUtcOffset (d4).Ticks, "D13"); Assert.AreEqual(0L, t1.GetUtcOffset (d5).Ticks, "D14"); }
private void TST (TimeZone t1) { Assert.AreEqual("Tokyo Standard Time", t1.StandardName, "C01"); Assert.AreEqual("Tokyo Standard Time", t1.DaylightName, "C02"); DateTime d3 = new DateTime (2002,2,25); Assert.AreEqual(false, t1.IsDaylightSavingTime (d3), "C09"); DateTime d4 = new DateTime (2002,4,8); Assert.AreEqual(false, t1.IsDaylightSavingTime (d4), "C10"); DateTime d5 = new DateTime (2002,11,4); Assert.AreEqual(false, t1.IsDaylightSavingTime (d5), "C11"); Assert.AreEqual(324000000000L, t1.GetUtcOffset (d3).Ticks, "C12"); Assert.AreEqual(324000000000L, t1.GetUtcOffset (d4).Ticks, "C13"); Assert.AreEqual(324000000000L, t1.GetUtcOffset (d5).Ticks, "C14"); }
private void TST (TimeZone t1) { AssertEquals("C01", "Tokyo Standard Time", t1.StandardName); AssertEquals("C02", "Tokyo Standard Time", t1.DaylightName); DateTime d3 = new DateTime (2002,2,25); AssertEquals("C09", false, t1.IsDaylightSavingTime (d3)); DateTime d4 = new DateTime (2002,4,8); AssertEquals("C10", false, t1.IsDaylightSavingTime (d4)); DateTime d5 = new DateTime (2002,11,4); AssertEquals("C11", false, t1.IsDaylightSavingTime (d5)); AssertEquals("C12", 324000000000L, t1.GetUtcOffset (d3).Ticks); AssertEquals("C13", 324000000000L, t1.GetUtcOffset (d4).Ticks); AssertEquals("C14", 324000000000L, t1.GetUtcOffset (d5).Ticks); }
private void NZST(TimeZone t1) { Assert.AreEqual("NZST", t1.StandardName, "E01"); Assert.AreEqual("NZDT", t1.DaylightName, "E02"); DaylightTime d1 = t1.GetDaylightChanges (2013); Assert.AreEqual("09/29/2013 02:00:00", d1.Start.ToString ("G"), "E03"); Assert.AreEqual("04/07/2013 03:00:00", d1.End.ToString ("G"), "E04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "E05"); DaylightTime d2 = t1.GetDaylightChanges (2001); Assert.AreEqual("10/07/2001 02:00:00", d2.Start.ToString ("G"), "E06"); Assert.AreEqual("03/18/2001 03:00:00", d2.End.ToString ("G"), "E07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "E08"); DateTime d3 = new DateTime(2013,02,15); Assert.AreEqual(true, t1.IsDaylightSavingTime (d3), "E09"); DateTime d4 = new DateTime(2013,04,30); Assert.AreEqual(false, t1.IsDaylightSavingTime (d4), "E10"); DateTime d5 = new DateTime(2013,11,03); Assert.AreEqual(true, t1.IsDaylightSavingTime (d5), "E11"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d3).Ticks, "E12"); Assert.AreEqual(36000000000L /*hour*/ * 12L, t1.GetUtcOffset (d4).Ticks, "E13"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d5).Ticks, "E14"); }
/// <summary> /// Returns true if the specified DayEntry is within the same month as <c>month</c>; /// </summary> /// <param name="dayEntry"></param> /// <param name="timeZone"></param> /// <param name="month"></param> /// <returns></returns> public static bool OccursInMonth(DayEntry dayEntry, TimeZone timeZone, DateTime month) { DateTime startOfMonth = new DateTime(month.Year, month.Month, 1, 0, 0, 0); DateTime endOfMonth = new DateTime(month.Year, month.Month, 1, 0, 0, 0); endOfMonth = endOfMonth.AddMonths(1); endOfMonth = endOfMonth.AddSeconds(-1); TimeSpan offset = timeZone.GetUtcOffset(endOfMonth); endOfMonth = endOfMonth.AddHours(offset.Negate().Hours); return ( OccursBetween(dayEntry, timeZone, startOfMonth, endOfMonth) ); }
public ServerInfo(string name, int fullPercent, TimeZone tz, IPEndPoint address) { Name = name; FullPercent = fullPercent; TimeZone = tz.GetUtcOffset(DateTime.Now).Hours; Address = address; }
/// <summary> /// <para>Converts a given DMTF datetime to <see cref='System.DateTime'/> object. The returned DateTime will be in the /// current TimeZone of the system.</para> /// </summary> /// <param name='dmtfDate'>A string representing the datetime in DMTF format.</param> /// <returns> /// <para>A <see cref='System.DateTime'/> object that represents the given DMTF datetime.</para> /// </returns> /// <remarks> /// <para> Date and time in WMI is represented in DMTF datetime format. This format is explained in WMI SDK documentation. /// DMTF datetime string has an UTC offset which this datetime string represents. /// During conversion to <see cref='System.DateTime'/>, UTC offset is used to convert the date to the /// current timezone. According to DMTF format a particular field can be represented by the character /// '*'. This will be converted to the MinValue of this field that can be represented in <see cref='System.DateTime'/>. /// </para> /// </remarks> /// <example> /// <code lang='C#'> /// // Convert a DMTF datetime to System.DateTime /// DateTime date = ManagementDateTimeConverter.ToDateTime("20020408141835.999999-420"); /// </code> /// <code lang='VB'> /// ' Convert a DMTF datetime to System.DateTime /// Dim date as DateTime = ManagementDateTimeConverter.ToDateTime("20020408141835.999999-420") /// </code> /// </example> public static DateTime ToDateTime(string dmtfDate) { int year = DateTime.MinValue.Year; int month = DateTime.MinValue.Month; int day = DateTime.MinValue.Day; int hour = DateTime.MinValue.Hour; int minute = DateTime.MinValue.Minute; int second = DateTime.MinValue.Second; int millisec = 0; string dmtf = dmtfDate; DateTime datetime = DateTime.MinValue; // If the string passed is empty or null then throw // an exception if (dmtf == null) { throw new System.ArgumentOutOfRangeException(); } if (dmtf.Length == 0) { throw new System.ArgumentOutOfRangeException(); } // if the length of the string is not equal to the // standard length of the DMTF datetime then throw an exception if (dmtf.Length != SIZEOFDMTFDATETIME) { throw new System.ArgumentOutOfRangeException(); } System.Int64 ticks = 0; try { string tempString = System.String.Empty; tempString = dmtf.Substring(0, 4); if (("****" != tempString)) { year = System.Int32.Parse(tempString); } tempString = dmtf.Substring(4, 2); if (("**" != tempString)) { month = System.Int32.Parse(tempString); } tempString = dmtf.Substring(6, 2); if (("**" != tempString)) { day = System.Int32.Parse(tempString); } tempString = dmtf.Substring(8, 2); if (("**" != tempString)) { hour = System.Int32.Parse(tempString); } tempString = dmtf.Substring(10, 2); if (("**" != tempString)) { minute = System.Int32.Parse(tempString); } tempString = dmtf.Substring(12, 2); if (("**" != tempString)) { second = System.Int32.Parse(tempString); } tempString = dmtf.Substring(15, 6); if (("******" != tempString)) { ticks = (System.Int64.Parse(tempString)) * (System.TimeSpan.TicksPerMillisecond / 1000); } if (year < 0 || month < 0 || day < 0 || hour < 0 || minute < 0 || second < 0 || ticks < 0) { throw new System.ArgumentOutOfRangeException(); } } catch { throw new System.ArgumentOutOfRangeException(); } // Construct a new System.DateTime object datetime = new System.DateTime(year, month, day, hour, minute, second, millisec); // Then add the ticks calculated from the microseconds datetime = datetime.AddTicks(ticks); // Adjust the UTC time to reflect the current zone time System.TimeZone curZone = System.TimeZone.CurrentTimeZone; System.TimeSpan tickOffset = curZone.GetUtcOffset(datetime); long OffsetMins = tickOffset.Ticks / System.TimeSpan.TicksPerMinute; // Adjusting the DateTime for the current UTC int UTCOffset = 0; string tempString1 = dmtf.Substring(22, 3); long OffsetToBeAdjusted = 0; if (("***" != tempString1)) { tempString1 = dmtf.Substring(21, 4); try { UTCOffset = System.Int32.Parse(tempString1); } catch { throw new System.ArgumentOutOfRangeException(); } OffsetToBeAdjusted = UTCOffset - OffsetMins; // We have to substract the minutes from the time datetime = datetime.AddMinutes(OffsetToBeAdjusted * -1); } return(datetime); }
private void NZST(TimeZone t1) { Assert.AreEqual("NZST", t1.StandardName, "E01"); Assert.AreEqual("NZDT", t1.DaylightName, "E02"); DaylightTime d1 = t1.GetDaylightChanges (2013); Assert.AreEqual("09/29/2013 02:00:00", d1.Start.ToString ("G", CultureInfo.InvariantCulture), "E03"); Assert.AreEqual("04/07/2013 03:00:00", d1.End.ToString ("G", CultureInfo.InvariantCulture), "E04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "E05"); DaylightTime d2 = t1.GetDaylightChanges (2001); Assert.AreEqual("10/07/2001 02:00:00", d2.Start.ToString ("G", CultureInfo.InvariantCulture), "E06"); Assert.AreEqual("03/18/2001 03:00:00", d2.End.ToString ("G", CultureInfo.InvariantCulture), "E07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "E08"); DateTime d3 = new DateTime(2013,02,15); Assert.AreEqual(true, t1.IsDaylightSavingTime (d3), "E09"); DateTime d4 = new DateTime(2013,04,30); Assert.AreEqual(false, t1.IsDaylightSavingTime (d4), "E10"); DateTime d5 = new DateTime(2013,11,03); Assert.AreEqual(true, t1.IsDaylightSavingTime (d5), "E11"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d3).Ticks, "E12"); Assert.AreEqual(36000000000L /*hour*/ * 12L, t1.GetUtcOffset (d4).Ticks, "E13"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d5).Ticks, "E14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d5.ToUniversalTime (); Assert.AreEqual(false, t1.IsDaylightSavingTime (d6), "E15"); Assert.AreEqual(0, t1.GetUtcOffset (d6).Ticks, "E16"); }