Inconsistent date/time stamps, *possibly* due to time zone

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
James Tursa
James Tursa il 23 Dic 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.
FM
FM il 24 Dic 2023
I'll respond to this in a comment to Star Strider's answer. Thanks.

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 23 Dic 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 Commenti
James Tursa
James Tursa il 1 Gen 2024
@FM Thanks for digging into this, btw. I would give you more votes if I could!
FM
FM il 1 Gen 2024
No worries about the votes. I do hope that TMW sees it as worthwhile to track down and fix. It's disconcerting to know that deployed products may not behave as expected. I would do further troubleshooting if I could, but this was a discovered and unexpected issue, and my main task is something else.

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by