Skip to content

Commit d16d7c3

Browse files
committed
set_modtime more robust
1 parent c0baf41 commit d16d7c3

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

+stdlib/+java/set_modtime.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
utc = convertTo(datetime(t, 'TimeZone', "UTC"), "posixtime");
1010

11-
ok = java.io.File(file).setLastModified(int64(utc) * 1000);
11+
try
12+
ok = java.io.File(file).setLastModified(int64(utc) * 1000);
13+
catch e
14+
javaException(e)
15+
ok = logical.empty;
16+
end
1217

1318
end

+stdlib/+python/set_modtime.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
dt (1,1) datetime
55
end
66

7-
ok = false;
8-
if ~isfile(file), return, end
7+
if ~isfile(file)
8+
ok = false;
9+
return
10+
end
911

1012
utc = convertTo(datetime(dt, 'TimeZone', "UTC"), "posixtime");
1113

@@ -14,8 +16,7 @@
1416
py.os.utime(file, py.tuple([s.st_atime, utc]));
1517
ok = true;
1618
catch e
17-
warning(e.identifier, "set_modtime(%s, %s) failed: %s", file, utc, e.message);
18-
ok = false;
19+
ok = logical.empty;
1920
end
2021

2122
end

+stdlib/set_modtime.m

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,34 @@
55
% * t: new modification time
66
% * backend: backend to use
77
%%% Outputs
8-
% * ok: true if successful
8+
% * i: true if successful
99
% * b: backend used
1010

11-
function [ok, b] = set_modtime(file, t, backend)
11+
function [i, b] = set_modtime(file, t, backend)
1212
arguments
1313
file
1414
t (1,1) datetime
1515
backend (1,:) string = ["java", "python", "sys"]
1616
end
1717

18-
o = stdlib.Backend(mfilename(), backend);
19-
ok = o.func(file, t);
20-
b = o.backend;
18+
i = logical.empty;
19+
20+
for b = backend
21+
switch b
22+
case "java"
23+
i = stdlib.java.set_modtime(file, t);
24+
case "python"
25+
if stdlib.matlabOlderThan('R2022a'), continue, end
26+
i = stdlib.python.set_modtime(file, t);
27+
case "sys"
28+
i = stdlib.sys.set_modtime(file, t);
29+
otherwise
30+
error("stdlib:set_modtime:ValueError", "Unknown backend: %s", b)
31+
end
32+
33+
if ~isempty(i)
34+
return
35+
end
36+
end
2137

2238
end

0 commit comments

Comments
 (0)