为什么 XMLTimeToDateTime 忽略毫秒?
Test := XMLTimeToDateTime('2011-11-11T12:41:36.696+01:00', TRUE);
T2 := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Test);
之后 T2 = '2011-11-11T11:41:36.000'
我使用的是 Delphi 2007。
最佳答案
XSBuiltIns中的代码确实解析了毫秒部分,但是编码函数中从来没有使用过这部分。
function TXSBaseTime.GetAsTime: TDateTime;
begin
Result := EncodeTime(Hour, Minute, Second, 0);
end;
和
function TXSBaseCustomDateTime.GetAsDateTime: TDateTime;
var
BiasDT: TDateTime;
BiasTime, BiasLocal: Integer;
BiasHour, BiasMins: Word;
begin
{ NOTE: In XML Years can exceed 9999 - that's not the case for TDateTime.
So here, there would be a problem with the conversion }
Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, 0);
和
function TXSBaseCustomDateTime.GetAsUTCDateTime: TDateTime;
var
AdjustDT: TDateTime;
begin
Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, 0);
由于最后一个是从 XMLTimeToDateTime 调用的,因此毫秒部分始终为 0 是可以理解的。
所有解析和数据存储都在内部(实现部分)类中完成,除非通过(损坏的)包装器才能直接访问这些类。 IOW,您应该编写自己的日期/时间解析器。
除了在 XSBuiltIns 中发现的所有丑陋之处之外,XMLTimeToDateTime 实际上会解析日期两次。首先调用 TXSDateTime.XSToNative 解析日期/时间,丢弃结果并仅存储原始字符串,然后 TXSCustomDateTime.GetAsUTCDateTime 再次解析该字符串。哎呀!
关于XMLTimeToDateTime 忽略毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8093733/