Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// Returns the abbreviated name of the specified time zone if it has one,
        /// otherwise the full standard name. This method can be used to get a
        /// string representation of System.TimeZone consistent with that of Type.TimeZone.
        /// </summary>
        public static string ToString(System.TimeZone timeZone)
        {
            if (timeZone is TimeZone)
            {
                return(timeZone.ToString());
            }

            string abbrev = TimeZoneInfo.FullNameToAbbreviation(timeZone.StandardName);

            return(abbrev == null ? timeZone.StandardName : abbrev);
        }
Пример #3
0
        private void m_settings_SettingChanged(object sender, SettingChangedEventArgs e)
        {
            if (e.SettingName == EventViewerSettings.SettingDisplayTimeZone)
            {
                m_timeZone = m_settings.DisplayTimeZone;

                if (m_timePropertyWrapper != null)
                {
                    m_timePropertyWrapper.SetValue(LogForm.GetEventMessageTime(m_eventMessage, m_timeZone));
                    panes.gridDetails.Invalidate(true);                     // Must be called to refresh the grid immediately.
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Returns a DateTime equivalent to the specified System.DateTime. You must also
        /// specify a time zone, because System.DateTime may represent times in the local
        /// time zone as well as in UTC.
        /// </summary>
        public static DateTime FromSystemDateTime(System.DateTime dateTime,
                                                  System.TimeZone timeZone)
        {
            System.DateTime baseValue = new System.DateTime(dateTime.Year,
                                                            dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute,
                                                            dateTime.Second, dateTime.Millisecond);

            int diffTicks    = (int)(dateTime.Ticks - baseValue.Ticks);
            int microseconds = diffTicks / 10;
            int nanoseconds  = (diffTicks % 10) * 100;

            return(new DateTime(baseValue, microseconds, nanoseconds, timeZone, false));
        }
Пример #5
0
 public DateTime ToTimeZone(System.TimeZone timeZone)
 {
     try
     {
         return(new DateTime(Universal, m_nanosecond, timeZone, true));
     }
     catch (System.ArgumentOutOfRangeException ex)
     {
         throw new System.InvalidOperationException("The DateTime '" + ToString() + "' cannot be converted to time zone '"
                                                    + Type.TimeZone.ToString(timeZone) + "', because the result would be"
                                                    + " less than DateTime.MinValue or greater than DateTime.MaxValue.", ex);
     }
 }
Пример #6
0
        public void Initialise(LogForm parentForm)
        {
            // Apply settings.

            m_settings = ((MainForm)MdiParent).Settings;

            m_settings.DetailsWindow.ApplyToWindow(this);
            panes.SplitRatioTop    = m_settings.DetailsSplitterRatioTop;
            panes.SplitRatioBottom = m_settings.DetailsSplitterRatioBottom;
            panes.gridDetails.EditorDialogSettings    = m_settings.GenericEditorWindow;
            panes.gridParameters.EditorDialogSettings = m_settings.GenericEditorWindow;
            m_timeZone = m_settings.DisplayTimeZone;

            m_parentForm = parentForm;

            m_settings.SettingChanged += m_settings_SettingChanged;

            m_initialised = true;
        }
Пример #7
0
        /// <summary>
        /// Constructor for recreating object during de-serialization
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        private DateTime(SerializationInfo info, StreamingContext context)
        {
            m_universal  = info.GetDateTime("m_universal");
            m_nanosecond = info.GetInt32("m_nanosecond");

            bool isCurrentTimeZone = info.GetBoolean("isCurrentTimeZone");

            if (isCurrentTimeZone)
            {
                m_timeZone = System.TimeZone.CurrentTimeZone;
            }
            else
            {
                m_timeZone = (System.TimeZone)info.GetValue("m_timeZone", typeof(System.TimeZone));
            }
            Debug.Assert(m_timeZone != null, "m_timeZone != null");

            m_base = m_timeZone.ToLocalTime(m_universal);
        }
Пример #8
0
        // This constructor is for cloning. The nanosecond parameter is the total of microseconds and
        // nanoseconds (ie. 0 to 1000000).
        private DateTime(System.DateTime baseValue, int nanosecond, System.TimeZone timeZone,
                         bool isUniversalTime)
        {
            Debug.Assert(nanosecond >= 0 && nanosecond < 1000000, "The nanosecond value is invalid -"
                         + " an exception should be thrown immediately after this.");

            if (isUniversalTime)
            {
                m_universal = baseValue;
                m_base      = timeZone.ToLocalTime(baseValue);
            }
            else
            {
                m_base      = baseValue;
                m_universal = timeZone.ToUniversalTime(baseValue);
            }

            m_nanosecond = nanosecond;
            m_timeZone   = timeZone;
        }
Пример #9
0
        /// <summary>
        /// If <c>isUniversalTime</c> is true then <c>baseValue</c> is taken to represent universal time.
        /// Otherwise it's taken to represent time in the time zone specified by <c>timeZone</c>.
        /// Universal time should be passed in whenever possible to avoid ambiguities
        /// during the hour that occurs twice when changing from daylight time to standard
        /// time.
        /// </summary>
        private DateTime(System.DateTime baseValue, int microsecond, int nanosecond,
                         System.TimeZone timeZone, bool isUniversalTime)
            : this(baseValue, microsecond *DateTimeHelper.Microseconds2Nanoseconds + nanosecond,
                   timeZone, isUniversalTime)
        {
            const string method = ".ctor";

            // Check that microsecond and nanosecond are valid values - the rest have
            // already been checked by System.DateTime.

            if (microsecond < DateTimeHelper.MinMicrosecond || microsecond > DateTimeHelper.MaxMicrosecond)
            {
                throw new ParameterOutOfRangeException(typeof(DateTime), method,
                                                       "microsecond", microsecond, DateTimeHelper.MinMicrosecond, DateTimeHelper.MaxMicrosecond);
            }
            if (nanosecond < DateTimeHelper.MinNanosecond || nanosecond > DateTimeHelper.MaxNanosecond)
            {
                throw new ParameterOutOfRangeException(typeof(DateTime), method,
                                                       "nanosecond", nanosecond, DateTimeHelper.MinNanosecond, DateTimeHelper.MaxNanosecond);
            }
        }
Пример #10
0
        /// <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);
        }
Пример #11
0
 public void setTimeZone(System.TimeZone tz)
 {
     this.m_tz = tz;
 }
Пример #12
0
 public DateTime(int year, int month, int day, System.TimeZone timeZone)
     : this(new System.DateTime(year, month, day), 0, 0, timeZone, false)
 {
 }
Пример #13
0
 public DateTime(int year, int month, int day, int hour, int minute, int second,
                 System.TimeZone timeZone)
     : this(new System.DateTime(year, month, day, hour, minute, second), 0, 0,
            timeZone, false)
 {
 }
Пример #14
0
 public DateTime(Date date, TimeOfDay time, System.TimeZone timeZone)
     : this(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second,
            time.Millisecond, time.Microsecond, time.Nanosecond, timeZone)
 {
 }