设置文件时间函数的奇怪毛病(高手请看过来吧) (100分)

  • 主题发起人 主题发起人 DZHZH2000
  • 开始时间 开始时间
D

DZHZH2000

Unregistered / Unconfirmed
GUEST, unregistred user!
从以前的帖子里翻来的。我发现当时间的毫秒是奇数是总是设成小一毫秒的偶数。<br>如:<br>SetFileDateTime('aaa.txt',fttLastWrite,StrToDateTime('2003-1-1 13:32:21:13'))<br>结果是2003-1-1 13:32:21:12<br>而毫秒是偶数则结果正确。<br><br>function SetFileDateTime(const FileName: string; FileTimeType: TFileTimeType; DateTime: TDateTime): Integer;<br>var<br>&nbsp; Handle: THandle;<br>&nbsp; LocalFileTime, FileTime: TFileTime;<br>&nbsp; DosDateTime: Integer;<br>&nbsp; I: TFileTimeType;<br>&nbsp; FileTimes: array[TFileTimeType] of Pointer;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; DosDateTime := DateTimeToFileDate(DateTime);<br>&nbsp; Handle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);<br>&nbsp; if Handle &lt;&gt; INVALID_HANDLE_VALUE then<br>&nbsp; try<br>&nbsp; &nbsp; for I := fttCreation to fttLastWrite do FileTimes := nil;<br>&nbsp; &nbsp; DosDateTimeToFileTime(LongRec(DosDateTime).Hi, LongRec(DosDateTime).Lo, LocalFileTime);<br>&nbsp; &nbsp; LocalFileTimeToFileTime(LocalFileTime, FileTime);<br>&nbsp; &nbsp; FileTimes[FileTimeType] := @FileTime;<br>&nbsp; &nbsp; if SetFileTime(Handle, FileTimes[fttCreation], FileTimes[fttLastAccess],<br>&nbsp; &nbsp; &nbsp; FileTimes[fttLastWrite]) then Exit;<br>&nbsp; finally<br>&nbsp; &nbsp; FileClose(Handle);<br>&nbsp; end;<br>&nbsp; Result := GetLastError;<br>end;<br>
 
利害, 這樣的小節都讓你找出來!<br>會不會是小數進位的問題???我們知道, 日期其實也只是一個帶小數的表示而已, 所以, 在保存, 取位可能出現你說的問題!
 
文件时间还有这样的格式: 13:32:21:13 ?
 
这个有什么影响吗?如果不编写实时控件类的程序?
 
文件的日期时间TimeStamp 是用32位的表示的,而TDateTime是64位<br>因此TimeStamp表示的范围和精度都比TDateTime少,转换时有误差
 
The DosDateTimeToFileTime function converts MS-DOS date and time values to a 64-bit file time. <br><br>BOOL DosDateTimeToFileTime(<br><br>&nbsp; &nbsp; WORD wFatDate, // 16-bit MS-DOS date <br>&nbsp; &nbsp; WORD wFatTime, // 16-bit MS-DOS time <br>&nbsp; &nbsp; LPFILETIME lpFileTime // pointer to buffer for 64-bit file time<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>wFatDate<br><br>Specifies the MS-DOS date. The date is a packed 16-bit value with the following format: <br><br>Bits Contents<br>0-4 Day of the month (1-31)<br>5-8 Month (1 = January, 2 = February, and so on)<br>9-15 Year offset from 1980 (add 1980 to get actual year)<br>&nbsp;<br><br>wFatTime<br><br>Specifies the MS-DOS time. The time is a packed 16-bit value with the following format: <br><br>Bits Contents<br>0-4 Second divided by 2<br>5-10 Minute (0-59)<br>11-15 Hour (0-23 on a 24-hour clock)<br><br>有点奇怪啊, Bit0到Bit4只能精确到2秒,转换之后怎么得到的100ns单位的时间?
 
弟兄们,怎么改造一下这个函数才能使结果正确?
 
没法改,当初Bill Gates没钱,一个Byte分开几瓣用(那象现在财大气粗?),<br>60秒他用5个bit(最大数是31)所以只能用1表示2秒,2表示4秒...<br>
 
这样看看行不行?<br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>&nbsp; function FSetDate(Handle: Integer; Age: Integer): Integer;<br>&nbsp; var<br>&nbsp; &nbsp; LocalFileTime, FileTime: TFileTime;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := 0;<br>&nbsp; &nbsp; if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime)<br>&nbsp; &nbsp; &nbsp; and<br>&nbsp; &nbsp; &nbsp; LocalFileTimeToFileTime(LocalFileTime, FileTime) and<br>&nbsp; &nbsp; &nbsp; SetFileTime(Handle, @FileTime, @FileTime, @FileTime) then Exit;<br>&nbsp; &nbsp; Result := GetLastError;<br>&nbsp; end;<br>var<br>&nbsp; ddd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Tdatetime;<br>&nbsp; dese &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TFileStream;<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; dese := TFileStream.Create(OpenDialog1.FileName, fmOpenWrite);<br>&nbsp; ddd := EncodeDateTime(2003, 1, 1, 13, 32, 21, 13);//'2003-1-1 13:32:21:13'<br>&nbsp; FSetDate(dese.handle, DateTimeToFileDate(ddd));<br>&nbsp; dese.Free;<br>end;<br>
 
jackchin的回答正确,我死心了
 
后退
顶部