- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题简而言之:TDateTime A
(03.09.2014 13:40) - TDateTime B
(03.09.2014 13:40) = -1
我有两个要比较的 TDateTime 值,首先我使用 =
运算符来检查它们是否相同,但经过几次测试后,我意识到这在我的情况下不起作用。令人困惑的是,它在大多数时候都工作得很好,但有时却不然。
我从现有文件的 LastWriteTime
属性中获取一个值,另一个值来自 MySQL 数据库。
这是一些代码:
TDateTime a := FileList[loop].Lastwritetime.AsUTCDateTime; // TDateTime from MySQL
TDateTime b := GetLastwritetimeUtc(Sourcedirectory); // TDateTime from my local file
if (CompareDateTime(a, b) = 0) then
begin
// do some stuff.
end;
现在,正如之前提到的,这个简单的代码在大多数情况下都可以工作,但是对于某些 TDateTime
值,我得到一个负结果,这应该意味着我来自 MySQL 的 TDateTime
值数据库早于我的本地文件 TDateTime
值。
所以我开始调试:
double aTicks := a; // MySQL TDateTime
double bTicks := b; // Local file TDateTime
这为我提供了自 1899 年 12 月 30 日以来经过的天数以及时间的十进制值。
示例值:
// a = 02.09.2014 11:42:01
// b = 02.09.2014 11:42:01
// aTicks = 41884,4875115741
// bTicks = 41884,4875153356
不相同的小数点应该是毫秒还是不是毫秒(从 xxxx,4875 之后开始)?现在,如果我比较它们(例如 CompareDateTime(a,b)
或 a = b
),我不会得到 0
/true
(我不比较 aTicks
和 bTicks
值)。
我是否必须以我获取本地文件TDateTime
的方式进行更改(目前我正在使用WinAPI,GetLastWriteTimeUTC
没有为我提供正确的世界标准时间)?
我认为这并不是一个真正的难题,但我不知道如何解决这个问题。 `` TDateTime
是否存储隐藏的毫秒数?在 Debug模式下,我看不到任何毫秒,并且我不知道如何从我的 TDateTime
中获取该值(使用 Delphi XE2)。
以下是有关我的项目的一些额外详细信息
我通过这种方式获取TDateTime b
值
function GetLastwritetimeUtc(source: String): TDateTime;
var
fad: TWin32FileAttributeData;
SystemTime: TSystemTime;
lastwritetimeUtc: TDateTime;
begin
GetFileAttributesEx(PWideChar(source),GetFileExInfoStandard,@fad);
FileTimeToSystemTime(fad.ftLastWriteTime, SystemTime);
lastwritetimeUtc := SystemTimeToDateTime(SystemTime);
result := lastwritetimeUtc;
end;
如果 MySQL 数据库中的文件“较新”,我将替换它并从 MySQL TDateTime a
属性中设置 LastWriteTime
,如下所示:SetLastWriteTimeUTC(a )
(并且我的 MySQL (a) 中的 TDateTime
值没有任何毫秒值)。所以这个问题不应该再次发生,但它确实发生了。
我的 MySQL 数据库上的 TDateTime
值来自此
XSDateTime c := DateTimeToXSDateTime(GetLastwritetimeUtc(sourceDirectory));
// i send this via WCF service to the MySQL database and store it in a `TDateTime` column (which does not include milliseconds)
我希望这是足够的信息,而不是太多。
最诚挚的问候,
尼古拉斯
更新:
该代码与我的主程序“相同”,正如我上面所说,错误的日期时间比较不会始终只在某些文件上触发(在我的例子中是 $Default10.dsk)。
uses
SysUtils,
Soap.SoapHttpTrans,
DateUtils,
Windows,
System.IOUtils,
Soap.XSBuiltIns;
var
fad: TWin32FileAttributeData;
SystemTime: TSystemTime;
lastwritetimeUtcA: TDateTime;
lastwritetimeUtcB: TDateTime;
sourceFileA: string;
sourceFileB: string;
lastwritetimeXS: TXSDateTime;
begin
while True do
begin
sourceFileA := 'Path to a file on your computer no matter which';
sourceFileB := 'Path to another file on your computer no matter which';
//GetLastWriteTime from local file
GetFileAttributesEx(PWideChar(sourceFileA),GetFileExInfoStandard,@fad);
FileTimeToSystemTime(fad.ftLastWriteTime, SystemTime);
lastwritetimeUtcA := SystemTimeToDateTime(SystemTime);
//Set the localfile lastwritetime to the theoretical mySQL file
// in my main program there does not exist a mySQL file (only a value of TDateTime in the TDateTime column of my database where i store the lastwritetime from the local files
TFile.SetLastWriteTimeUtc(sourceFileB, lastwritetimeUtcA);
//Get the LastWriteTime from theoretical mySQL file
// in my main program i get the lastwritetime value from the MySQL database via WCF that is the reason for the convertion of XSDateTime.AsUTCDateTime and DateTimeToXSDateTime
GetFileAttributesEx(PWideChar(sourceFileB),GetFileExInfoStandard,@fad);
FileTimeToSystemTime(fad.ftLastWriteTime, SystemTime);
lastwritetimeUtcB := SystemTimeToDateTime(SystemTime);
//Convert lastwritetime to XSDatetime - how i do it in my program
lastwritetimeXS := DateTimeToXSDateTime(lastwritetimeUtcB);
{Convert it back to DateTime}
lastwritetimeUtcB := lastwritetimeXS.AsUTCDateTime;
//Compare them
if lastwritetimeUtcA = lastwritetimeUtcB then
Writeln('Same time')
else
writeln('Not same time');
Sleep(500);
end;
end;
最佳答案
如果您想要比较两个 TDateTime
值并匹配秒数,忽略毫秒差异,请使用 DateUtils
单元中的 SecondsBetween
:
program Project1;
uses
SysUtils, DateUtils;
var
dtOne, dtTwo: TDateTime;
begin
dtOne := 41884.4875115741;
dtTwo := 41884.4875153356;
if SecondsBetween(dtOne, dtTwo) = 0 then
WriteLn('Dates the same without MS')
else
WriteLn('Not the same dates.');
ReadLn;
end.
如果需要匹配其他分辨率,对于其他差异也有类似的函数,例如 DaysBetween
、MinutesBetween
和 MilliSecondsBetween
。这是一个适用于各种分辨率(精确匹配、日、小时、分钟或秒)的实用函数:
type
TDiffResolution = (tdrExact, tdrDay, tdrHour, tdrMin, tdrSec);
function IsSameDateTime(dValOne, dValTwo: TDateTime;
const Resolution: TDiffResolution = tdrSec): Boolean;
begin
case Resolution of
tdrExact: Result := MillisecondsBetween(dValOne, dValTwo) = 0;
tdrDay: Result := IsSameDay(dValOne, dValTwo);
tdrHour: Result := HoursBetween(dValOne, dValTwo) = 0;
tdrMin: Result := MinutesBetween(dValOne, dValTwo) = 0;
tdrSec: Result := SecondsBetween(dValOne, dValTwo) = 0;
else
raise Exception.CreateFmt('Invalid resolution value (%d) provided.',
[Ord(Resolution)]);
end;
end;
使用示例:
dtOne := 41884.4875115741;
dtTwo := 41884.4875153356;
if IsSameDateTime(dtOne, dtTwo, tdrSec) then
WriteLn('Dates are the same.')
else
WriteLn('Dates are different.');
ReadLn;
关于mysql - 忽略 TDateTime 中的毫秒(相同 TDateTime 值相减不为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644507/
这个问题已经有答案了: Diff of two Dataframes (8 个回答) 已关闭 4 年前。 我有 2 个数据框(df_a 和 df_b),有 2 列:“动物”和“名称”。 在更大的数据框
如果我得到以下数字字符串,是否可以使用 LINQ 查询将这些数字一起加/减? string numbers = "1 + 1, 2 - 1, 3 + 3"; 所以我最终会得到这样的东西: 字符串数字
具有以下对象列表: public class Example { public string Local { get; set; } public string Type { get;
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
#include #include float sum (float *A, int len) // sum of table of floats { float ss = 0.0;
我正在阅读这篇博文:https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/ 我很困惑: size_t len = end
我正在阅读这篇博文:https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/ 我很困惑: size_t len = end
这个问题已经有答案了: Pointer Arithmetic In C (2 个回答) Pointer subtraction confusion (8 个回答) 已关闭 4 年前。 int vect
我知道这可能是个愚蠢的问题,但我遇到了一些麻烦,我很惭愧,但我真的不知道如何做到。我想加减两个以整数形式给出的“小时”。 #include #include #include using nam
大家好 StackOverflow.. 这个网站相当新,但我得到了很好的反馈!所以首先要感谢大家! 我想做的是类似于wonga的事情 https://www.wonga.com/ 但是随着值的协同工作
我是一名优秀的程序员,十分优秀!