Delphi: SetFileDate creates wrong LastWriteTime (summer / winter)

I am downloading a file from my server (I only get bytes and DateTime for the lastwritetime attribute) and after downloading the data, I create a new file on my local machine and want to set the lastwritetime attribute. For this I use the following method:

procedure SetFileDate(const FileName: string; NewDate: TDateTime);
var
    FileDate, FileHandle: Integer;
begin
    try
        FileDate := DateTimeToFileDate(NewDate);

        FileHandle := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
        if FileHandle > 0 then
            begin
                FileSetDate(FileHandle, FileDate);
                FileClose(FileHandle);
            end;
    except
        begin
            // ERROR Log
            err.Msg('FileReqThrd.SetFileDate');
        end;
    end;
end;

      

For the "NewDate" parameter, I use the DateTime that I get from my server. I tried to convert DateTime from server like this to get valid lastwritetime (I am requesting data from WCF, so I am converting it to UTCDateTime, untouched data from WCF service is TXSDateTime):

TDateTime cloudFileDateTime := StrToDateTime(DateTimeToStr(cloudDownloadResult.FileCloudData.Lastwritetime.AsUTCDateTime));

      

But at the end, my lastwritetime attribute from files having last time in winter is wrong with -1h.

Hope you can understand my problem and can give me an idea on how to solve it.

Regards

+3


source to share


1 answer


The easiest way to do this is to call TFile.SetLastWriteTimeUtc

from a block System.IOUtils

.

TFile.SetLastWriteTimeUtc(FileName, 
    DateTimeUtc);

      



If this function is not available, use the Win32 API function SetFileTime

.

You will also need DateTimeToSystemTime

and then SystemTimeToFileTime

in this scenario.

+3


source







All Articles