0

today there is November (DST). I set File date time with :

TFile.SetCreationTime(FilePath, ADateTime);
  1. If I set date time to: '2021-11-11 02:27:00' read in Windows Explorer is as set.

  2. If I set date time to: '2021-05-14 02:27:00' then date is OK but hour read in Windows Explorer is 03:27.

How to set hour to 02:27:00 independently of DST?

m227
  • 51
  • 5
  • Isn't this just how NTFS handles this? I bet you find that there are countless other questions on this very topic. – David Heffernan Nov 23 '21 at 14:26
  • [Changing file timestamps on windows correctly?](https://stackoverflow.com/q/33071733/327083) – J... Nov 23 '21 at 15:40
  • [Daylight saving time and time zone best practices](https://stackoverflow.com/q/2532729/327083) – J... Nov 23 '21 at 15:40
  • What version of Delphi are you using? I can't reproduce this in 10.4. `dt := EncodeDateTime(2021, 05,14,02,27,0,0); TFile.SetCreationTime('C:\test\test.txt', dt);` Result is a file that shows in Windows with a timestamp of May 14, 2:27:00, as expected. – J... Nov 23 '21 at 15:56
  • @J... Did you activate and look at a column "Creation time" – Tom Brunberg Nov 23 '21 at 17:22
  • @TomBrunberg No, I looked at the file's "Properties" dialog. I did just enable the "Date Created" column in Windows Explorer's details view (if that's what you're talking about?) and it shows the same (expected) date as the properties dialog. – J... Nov 23 '21 at 17:30
  • @J: Where in the world are you located? Is DST active at your location on May 14th? – HeartWare Nov 23 '21 at 17:48
  • @J... Yes, I meant the "Date Created" column. Is your computer setup to use local time and is your country using daylight saving time (commonly summer time)? I am in Finland, GMT + 2 and + 1 more at DST. I can reproduce the situation in the question. – Tom Brunberg Nov 23 '21 at 17:57
  • @TomBrunberg Yes, DST is active in my timezone on OP's date and is not currently active now. This isn't my problem - it's OP's problem. I can't reproduce it with the information in their question so if there's something different about their system configuration then I think the right thing to do is to have them [edit] it to include enough information to be able to reproduce the problem. – J... Nov 23 '21 at 18:27
  • Ok @J... , No problem for me neither, I just became curious why we have different results (I was able to reproduce). But never mind. – Tom Brunberg Nov 23 '21 at 19:41
  • @TomBrunberg I'm Win10 21H1, D10.4 (Update1), currently in EST (UTC-5), EDT is UTC-4 and active in May at OP's date. "Set time zone automatically" is turned OFF in Windows for me and Eastern Time is set manually. Windows is configured with "Adjust for daylight savings time automatically" turned ON. I'm not sure what's different with your system, but if you're interested in figuring it out that's my configuration. – J... Nov 23 '21 at 19:48
  • Thanks @J... Basically similar settings here. I tested with D XE7, but hardly relevant for this case. I'll take a closer look tomorrow, just of curiosity. – Tom Brunberg Nov 23 '21 at 20:34
  • @TomBrunberg No, I think XE7 is probably highly relevant - the RTL does timezone management in 10.4's TFile implementation so it's likely that implementation has changed since XE7. I was waiting for OP to tell us which version of Delphi they were using to pick one to go back and check. I'm guessing this is an old RTL bug which has since been patched. – J... Nov 23 '21 at 20:44
  • @TomBrunberg I've confirmed at least in D2010, which is the only convenient old version I have installed at the moment, that the implementation of `TDirectory.ConvertDateTimeToFileTime` calls into the WinAPI `LocalFileTimeToFileTime` whereas in 10.4 it uses `tzSpecificLocalTimeToSystemTime` instead. I presume XE7 also uses this older code. I'm not sure when this changed, but this Q can close as a fixed bug if someone can find the QP or release note that captures when this was fixed. – J... Nov 23 '21 at 20:52
  • @TomBrunberg Actually, I also have 10.0 Seattle and it uses the same implementation as 2010 and XE7, so it's something that was changed quite recently. – J... Nov 23 '21 at 21:03
  • @m227 It appears that this is a bug in the RTL which was patched in some version after 10.0 and before 10.4. Your options are to either forego using the (defective) RTL tools and make the WinAPI calls yourself **or** to upgrade to the current version of Delphi in which this bug is fixed. – J... Nov 23 '21 at 21:16
  • I am sorry for the delay, did not expect such popularity. I use Windows 10 x64 PL. Delphi 10.2. I rather did not change much in date time settings. I live in Poland. I can check it on XE7 soon as I have it installed too. – m227 Nov 24 '21 at 11:33
  • I tested it in XE7 Update 1. The same result. (Like in Delphi 10.2 Update 3). My sample code is: ADateTime := EncodeDateTime(2021, 05, 14, 02, 27, 00, 00); TFile.SetCreationTime(TestFile, ADateTime); TFile.SetLastWriteTime(TestFile, ADateTime); – m227 Nov 24 '21 at 11:47
  • @m227 Well that narrows it down to being fixed in either 10.3 or 10.4. Either way, it's a fixed bug so the options remain the same - upgrade to a newer version of Delphi or write your own implementation. – J... Nov 24 '21 at 12:57
  • @J... Glad to hear that it is (was) a bug. The necessity to deal with time zones when trying to set hour for different date in file is ridiculous. I think it should be easy to track down changes in Delphi sources between SetCreationTime() function and Windows API functions (between 10.2U3 and 10.4). – m227 Nov 24 '21 at 14:12

1 Answers1

0

The documentation on TFile.SetCreationTime() isn't great. However, since also a method TFile.SetCreationTimeUtc() exists the former will most likely interpret the TDateTime as local time (the currently chosen timezone in an operating account's settings), while the latter will interpret it as system time (UTC = GMT = timezone 0000).

Long story short: keep yourself from trouble and operate on UTC times, which also doesn't know daylight saving. Examples:

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
  • I tried once using UTC function version (TFile.SetCreationTimeUtc) but it made 4:27:00 (I set 2:27:00). At least Windows Explorer says that. – m227 Nov 24 '21 at 11:36
  • You still haven't mentioned the timezone set in your operating system account. And if it is correct (i.e. chosen a wrong timezone just to fit a summer/winter offset). – AmigoJack Nov 24 '21 at 13:48
  • My timezone set in Windows 10 is: "(UTC +01:00) Sarajevo... Warsaw..." Don't you think that there should be function to set hour without taking TZ into account? – m227 Nov 24 '21 at 14:04
  • Setting `02:27` as UTC time will be shown as `03:27` with a timezone of +0100, and when DST is in effect (as you described it in your question, although I have my doubts November is DST) then another hour is added, so the time `04:27` will be displayed by Windows. Which is what happens to you. – AmigoJack Nov 24 '21 at 18:20