Inconsistent date/time stamps, *possibly* due to time zone
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have the following functions and constants within (say) FOO.m to generate date/time stamps for *.mat dump file names and diagnostic messages:
% Fine grain date/time
FineDT = @() string(datetime,"yyyyMMdd.HHmmssSS");
% Coarser date/time, but human readable
NiceDTspc = @() string(datetime,"eee yyyy-MM-dd HH:mm:ss ");
% The m-file function name (say) FOO
mfn = string(mfilename);
My FOO.m m-file function uses the above to dump the workspace to a file:
% Build file name with date/time suffix
MatFN = mfn + "b4ilp" + FineDT() + ".mat";
save(MatFN);
disp( NiceDTspc() + mfn + ... Message prefix: Time & name of m-file
" : Saved " + MatFN + ". Calling intlinprog...");
The date/time suffix in the filename is 5 hours later than actual time of the file as revealed by the operating system (OS):
2023-12-22 19:37 FOOb4ilp20231223.00373136.mat
2023-12-22 19:37 FOOb4ilp20231223.00373172.mat
I thought that the OS's date/time might simply be 5 hours ahead because my time zone is GMT - 5 hours. The "disp" command above also issues messages with a date/time prefix that is 5 hours later:
Sat 2023-12-23 00:37:32 FOO : Saved FOOb4ilp20231223.00373172.mat. Calling intlinprog...
Sat 2023-12-23 00:37:33 FOO : Saved FOOb4ilp20231223.00373136.mat. Calling intlinprog...
However, when I define the above anonymous date/time functions FineDT and NiceDTspc at the command line, then invoke them, they return the correct local time.
Why would string(datetime,....) above yield the correct local time at the command line, but not from within an m-file function?
I am using Matlab 2023a on Windows 10.
2 Kommentare
James Tursa
am 23 Dez. 2023
Please post a small, complete example that reproduces the problem on your machine, isolating only the datetime issue. Also show us how you are getting the OS file time stamp.
Akzeptierte Antwort
Star Strider
am 23 Dez. 2023
I can’t determine what the exact problem is (I don’t have access to your computer, and I am not certain that I completely understand the problem), however in the function or script, it could be possible that the time zone is somehow set incorrectly to UCT. Use the datetime TimeZone property to set it correctly in the datetime calls, and if necessary consult the timezones function for a list of them.
9 Kommentare
James Tursa
am 1 Jan. 2024
@FM Thanks for digging into this, btw. I would give you more votes if I could!
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!