2014-03-07 136 views
0

我有一個問題,一個字符串轉換爲日期和時間。我知道我需要截斷字符串,但似乎無法找到任何有關如何操作的信息。我想其他人也會有這個問題,所以我想我會把它放在這裏用於研究目的。下面是代碼:德爾福日期時間轉換

uses 
    SysUtils, 
    ActiveX, 
    ComObj, 
    Variants; 


procedure GetLogEvents; 
const 
    wbemFlagForwardOnly = $00000020; 
var 
    FSWbemLocator : OLEVariant; 
    FWMIService : OLEVariant; 
    FWbemObjectSet: OLEVariant; 
    FWbemObject : OLEVariant; 
    oEnum   : IEnumvariant; 
    iValue  : LongWord; 
    EventDate  : TDateTime; 
begin; 
    FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); 
    FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); 
    FWbemObjectSet:= FWMIService.ExecQuery(
        'SELECT SourceName,TimeGenerated,ComputerName,EventCode,Message,RecordNumber ' + 
        ' FROM Win32_NTLogEvent ' + 
        'WHERE Logfile="Application"','WQL',wbemFlagForwardOnly); 
    oEnum   := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; 
    while oEnum.Next(1, FWbemObject, iValue) = 0 do 
    begin 
    if not VarIsNull(FWbemObject.SourceName) 
      then Writeln(Format('SourceName   %s',[String(FWbemObject.SourceName)])); 

    if not VarIsNull(FWbemObject.TimeGenerated) 
      then Writeln(StrToDateTime(FWbemObject.TimeGenerated)); 

而我得到的錯誤:

EConvertError: '20140307135950.000000-000' 不是一個有效的日期和時間。

誰能告訴我如何解決這個問題,好嗎?

回答

1

使用WbemScripting.SWbemDateTime對象來處理的WMI日期時間(公共信息模型日期時間值)的正確方法。

試試這個樣本

uses 
    SysUtils, 
    ActiveX, 
    ComObj, 
    Variants; 

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

procedure GetLogEvents; 
const 
    wbemFlagForwardOnly = $00000020; 
var 
    FSWbemLocator : OLEVariant; 
    FWMIService : OLEVariant; 
    FWbemObjectSet: OLEVariant; 
    FWbemObject : OLEVariant; 
    oEnum   : IEnumvariant; 
    iValue  : LongWord; 
    EventDate  : TDateTime; 
begin; 
    FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); 
    FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); 
    FWbemObjectSet:= FWMIService.ExecQuery(
        'SELECT SourceName,TimeGenerated,ComputerName,EventCode,Message,RecordNumber ' + 
        ' FROM Win32_NTLogEvent ' + 
        'WHERE Logfile="Application"','WQL',wbemFlagForwardOnly); 
    oEnum   := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; 
    while oEnum.Next(1, FWbemObject, iValue) = 0 do 
    begin 
    if not VarIsNull(FWbemObject.SourceName) 
      then Writeln(Format('SourceName   %s',[String(FWbemObject.SourceName)])); 

    if not VarIsNull(FWbemObject.TimeGenerated) 
      then Writeln(DateTimeToStr(WmiDateToTDatetime(FWbemObject.TimeGenerated))); 
    FWbemObject:=Unassigned; 
    end; 
end; 


begin 
try 
    CoInitialize(nil); 
    try 
     GetLogEvents; 
    finally 
     CoUninitialize; 
    end; 
except 
    on E:EOleException do 
     Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); 
    on E:Exception do 
     Writeln(E.Classname, ':', E.Message); 
end; 
Writeln('Press Enter to exit'); 
Readln; 
end. 
+0

啊,那優美的工作,謝謝! – user3393429