Inconsistent date/time stamps, *possibly* due to time zone
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2023-12-23
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.
采纳的回答
Star Strider
2023-12-23
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!