/// <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); }
public static int TimestampToURI(char[] buf, int pos, DateTime value) { if (value.Kind == DateTimeKind.Utc) { value = value.ToLocalTime(); } var offset = CurrentZone.GetUtcOffset(value); if (offset.Minutes != 0) { return(TimestampConverter.Serialize(value.AddMinutes(offset.Minutes), buf, pos, offset.Hours)); } return(TimestampConverter.Serialize(value, buf, pos, offset.Hours)); }
/// <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("dmtfDate"); } if (dmtf.Length == 0) { throw new System.ArgumentOutOfRangeException("dmtfDate"); } // 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("dmtfDate"); } IFormatProvider frmInt32 = (IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int32)); System.Int64 ticks = 0; try { string tempString = System.String.Empty; tempString = dmtf.Substring(0, 4); if (("****" != tempString)) { year = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(4, 2); if (("**" != tempString)) { month = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(6, 2); if (("**" != tempString)) { day = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(8, 2); if (("**" != tempString)) { hour = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(10, 2); if (("**" != tempString)) { minute = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(12, 2); if (("**" != tempString)) { second = System.Int32.Parse(tempString, frmInt32); } tempString = dmtf.Substring(15, 6); if (("******" != tempString)) { ticks = (System.Int64.Parse(tempString, (IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int64)))) * (System.TimeSpan.TicksPerMillisecond / 1000); } if (year < 0 || month < 0 || day < 0 || hour < 0 || minute < 0 || second < 0 || ticks < 0) { throw new System.ArgumentOutOfRangeException("dmtfDate"); } } catch { throw new System.ArgumentOutOfRangeException("dmtfDate"); } // 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, frmInt32); } catch { throw new System.ArgumentOutOfRangeException(); } OffsetToBeAdjusted = UTCOffset - OffsetMins; // We have to substract the minutes from the time datetime = datetime.AddMinutes(OffsetToBeAdjusted * -1); } return(datetime); }