필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
FM
FM 2023년 12월 23일
편집: FM 2024년 1월 2일
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
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.
FM
FM 2023년 12월 24일
I'll respond to this in a comment to Star Strider's answer. Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
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
James Tursa
James Tursa 2024년 1월 1일
@FM Thanks for digging into this, btw. I would give you more votes if I could!
FM
FM 2024년 1월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by