DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") results in a string one hour later on some machines

Viewed 96

I'm modding a C# Unity game (2020.3.26f, Mono 4.6.57.0) with BepInEx 5.4.19. In my mod, I create a file with a filename containing the current time formatted:

string filename = DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") + ".dat";
File.WriteAllText(Application.persistentDataPath + "\\" + filename, "blah,blah");

On my machine, this results in a filename that aligns with the last modification date of the file created when looking at it from Explorer:

20220524_132000_123.dat <last modified: 2022.05.24 13:20:00>

(My timezone is CEST)

However, one of the users of this mod reports that they get a filename that is one hour later than what the filesystem shows, for example:

20220524_143000_456.dat <last modified: 2022.05.24 13:30:00>

The time for when the user did the save was at 13:30 so the filename is off by one hour for some reason. (Their timezone is GMT+3)

Why is there such a one hour difference on their machine but not on mine?

Edit:

After a bit back-and-forth with the user, it turns out the problem is with the user's Windows' timezone registry keys that are not up-to-date for some reason.

As for the duplicate pointing to DateTimeOffset.UtcNow, it is no good because the users want to see the same time in the filename itself and in the filesystem. In other words, if I make a save at 15:50 local time, the filename should have 1550 in it, not 1350 or 1650.

0 Answers
Related