How to convert WMI DateTime to standard DateTime?

前端 未结 5 1118
栀梦
栀梦 2021-01-20 01:14

I\'m trying to read the install date from WMI (Win32_OperatingSystem.InstallDate). The return value looks like this: 20091020221246.000000+180. How can I get a

相关标签:
5条回答
  • 2021-01-20 01:47
    System.Management.ManagementDateTimeConverter.ToDateTime
    
    0 讨论(0)
  • 2021-01-20 01:47

    WbemScripting.SWbemDateTime does not always work. Better way:

    function WmiDate2DT (S: string; var UtcOffset: integer): TDateTime ;
    // yyyymmddhhnnss.zzzzzzsUUU  +60 means 60 mins of UTC time
    // 20030709091030.686000+060
    // 1234567890123456789012345
    var
        yy, mm, dd, hh, nn, ss, zz: integer ;
        timeDT: TDateTime ;
    
        function GetNum (offset, len: integer): integer ;
        var
            E: Integer;
        begin
            Val (copy (S, offset, len), result, E) ;
        end ;
    
    begin
        result := 0 ;
        UtcOffset := 0 ;
        if length (S) <> 25 then exit ;   // fixed length
        yy := GetNum (1, 4) ;
        mm := GetNum (5, 2) ;
        if (mm = 0) or (mm > 12) then exit ;
        dd := GetNum (7, 2) ;
        if (dd = 0) or (dd > 31) then exit ;
        if NOT TryEncodeDate (yy, mm, dd, result) then     // D6 and later
        begin
            result := -1 ;
            exit ;
        end ;
        hh := GetNum (9, 2) ;
        nn := GetNum (11, 2) ;
        ss := GetNum (13, 2) ;
        zz := 0 ;
        if Length (S) >= 18 then zz := GetNum (16, 3) ;
        if NOT TryEncodeTime (hh, nn, ss, zz, timeDT) then exit ;   // D6 and later
        result := result + timeDT ;
        UtcOffset := GetNum (22, 4) ; // including sign
    end ;
    
    function VarDateToDateTime(const V: OleVariant): TDateTime;
    var
        rawdate: string ;
        utcoffset: integer ;
    begin
      Result:=0;
      if VarIsNull(V) then exit;
      Dt.Value := V;
      try
      Result:=Dt.GetVarDate;
      except
        rawdate:=V;
        result := WmiDate2DT (rawdate, utcoffset);
      end;
    end;
    
    0 讨论(0)
  • 2021-01-20 01:55

    MagWMI from Magenta Systems contains MagWmiDate2DT() that does this.

    http://www.magsys.co.uk/delphi/magwmi.asp

    0 讨论(0)
  • 2021-01-20 01:56

    maybe this helps: http://technet.microsoft.com/en-us/library/ee156576.aspx

    0 讨论(0)
  • 2021-01-20 02:02

    Instead of parsing and extracting the values manually (how the accepted answer suggest), you can use the WbemScripting.SWbemDateTime object.

    check this sample

    function  WmiDateToTDatetime(vDate : OleVariant) : TDateTime;
    var
      FWbemDateObj  : OleVariant;
    begin;
      FWbemDateObj  := CreateOleObject('WbemScripting.SWbemDateTime');
      FWbemDateObj.Value:=vDate;
      Result:=FWbemDateObj.GetVarDate;
    end;
    

    For more info about this topic you can read this artile WMI Tasks using Delphi – Dates and Times

    0 讨论(0)
提交回复
热议问题