WMI7: Working with WMI Dates and Times

The information provided below was obtained from the following link:  http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_fvwp.mspx?mfr=true

However, I had difficulty finding this link when I needed it, so I'm re-posting the essential information here for posterity.

WMI displays dates in the Universal Time Coordinate (UTC) format.  In the UTC format, dates are displayed as yyyymmddHHMMSS.xxxxxx±UUU, where:

  • yyyy represents the year.
  • mm represents the month.
  • dd represents the day.
  • HH represents the hour (in 24-hour format).
  • MM represents the minutes.
  • SS represents the seconds.
  • xxxxxx represents the milliseconds.
  • UUU represents the difference, in minutes, between the local time zone and Greenwich Mean Time (GMT).

Thus, '20011224113047.000000-480' is really December 24, 2001 at 11:30:47.  The '000000' are the milli-seconds and the '480' is the number of minutes different from Greenwich Mean Time.

The bottom line is that you'll need to remember to convert to and from this date format when working with dates in WMI.

Date's are actually not used in the config system for IIS7, however, timespan's are.  In C-Sharp, they refer to WMI's date format as DMTF.  Below are some common methods I used while testing our IIS7 WMI provider to convert back and forth between the two date formats:

    public static string ConvertTimeSpanToDMTFTimeInterval(string timespan)
    {
        TimeSpan myTimeSpan = TimeSpan.Parse(timespan);
        return (System.Management.ManagementDateTimeConverter.ToDmtfTimeInterval(myTimeSpan));
    }

    public static string ConvertTimeSpanToDMTFTimeInterval(string timespan, string timeSpanFormat)
    {
        TimeSpan ts;
        timeSpanFormat = (timeSpanFormat == null) ? (string.Empty) : (timeSpanFormat);

        if (timespan == "Infinite")
        {
            return "10675199024805.000000:000";
        }

        switch (timeSpanFormat)
        {
            case "seconds":
                ts = new TimeSpan(0, 0, int.Parse(timespan));
                timespan = ts.ToString();
                break;
            case "minutes":
                ts = new TimeSpan(0, int.Parse(timespan), 0);
                timespan = ts.ToString();
                break;
        }

        return ConvertTimeSpanToDMTFTimeInterval(timespan);
    }

    public static string ConvertStringToTimeSpan(string timespan)
    {
        TimeSpan myTimeSpan = System.Management.ManagementDateTimeConverter.ToTimeSpan(timespan);
        return myTimeSpan.ToString();
    }

    public static string ConvertStringToTimeSpan(string timespan, string timeSpanFormat)
    {
        timespan = ConvertStringToTimeSpan(timespan);
        timeSpanFormat = (timeSpanFormat == null) ? (string.Empty) : (timeSpanFormat);

        string[] ti = timespan.Split(new char[] { ':' });
        TimeSpan ts = new TimeSpan(int.Parse(ti[0]), int.Parse(ti[1]), int.Parse(ti[2]));

        switch (timeSpanFormat)
        {
            case "seconds":
                timespan = ts.TotalSeconds.ToString();
                break;
            case "minutes":
                timespan = ts.TotalMinutes.ToString();
                break;
        }

        return timespan;
    }

 

 

No Comments