Skip to content

Commit a0b56a4

Browse files
committed
set_modtime: java or python
1 parent 9fc8b8d commit a0b56a4

File tree

5 files changed

+68
-48
lines changed

5 files changed

+68
-48
lines changed

+stdlib/private/py_set_modtime.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function ok = py_set_modtime(p)
2+
3+
try
4+
s = py.os.stat(p);
5+
py.os.utime(p, py.tuple(s.st_atime, utc));
6+
ok = true;
7+
catch
8+
ok = false;
9+
end
10+
11+
end

+stdlib/set_modtime.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
try
1111
utc = convertTo(datetime(t, 'TimeZone', "UTC"), "posixtime");
1212
catch e
13-
switch e.identifier
14-
case "Octave:undefined-function", utc = t;
15-
otherwise, rethrow(e);
13+
if strcmp(e.identifier, "Octave:undefined-function")
14+
utc = t;
15+
else
16+
rethrow(e);
1617
end
1718
end
1819

19-
ok = javaObject("java.io.File", p).setLastModified(int64(utc) * 1000);
20+
% Java or Python assume POSIX epoch time (seconds since Jan 1, 1970)
21+
if stdlib.has_java()
22+
ok = javaObject("java.io.File", p).setLastModified(int64(utc) * 1000);
23+
elseif stdlib.has_python()
24+
ok = py_set_modtime(p);
25+
end
2026

2127
end
2228

test/TestJava.m

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
classdef TestJava < matlab.unittest.TestCase
22

3-
properties
4-
td
5-
end
6-
7-
methods(TestClassSetup)
8-
function set_temp_wd(tc)
9-
if isMATLABReleaseOlderThan('R2022a')
10-
tc.td = tempname();
11-
mkdir(tc.td);
12-
else
13-
tc.td = tc.createTemporaryFolder();
14-
end
15-
end
16-
end
17-
18-
methods(TestClassTeardown)
19-
function remove_temp_wd(tc)
20-
if isMATLABReleaseOlderThan('R2022a')
21-
[s, m, i] = rmdir(tc.td, 's');
22-
if ~s, warning(i, "Failed to remove temporary directory %s: %s", tc.td, m); end
23-
end
24-
end
25-
end
263

274
methods(Test, TestTags="java")
285

@@ -54,23 +31,6 @@ function test_is_regular_file(tc)
5431
end
5532

5633

57-
function test_touch_modtime(tc)
58-
59-
fn = fullfile(tc.td, "modtime.txt");
60-
61-
tc.verifyTrue(stdlib.touch(fn, datetime("yesterday")))
62-
t0 = stdlib.get_modtime(fn);
63-
64-
tc.verifyTrue(stdlib.set_modtime(fn, datetime("now")))
65-
t1 = stdlib.get_modtime(fn);
66-
67-
tc.verifyGreaterThanOrEqual(t1, t0)
68-
end
69-
70-
function test_set_modtime(tc)
71-
tc.verifyEqual(stdlib.set_modtime("", datetime("now")), false)
72-
end
73-
7434
end
7535

7636
end

test/TestSys.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function test_is_parallel(tc)
5252
end
5353

5454
function test_hostname(tc)
55-
tc.assumeTrue(stdlib.has_dotnet() || stdlib.has_java())
55+
tc.assumeTrue(stdlib.has_dotnet() || stdlib.has_java() || stdlib.has_python())
5656

5757
h = stdlib.hostname();
5858
tc.verifyGreaterThan(strlength(h), 0)
@@ -66,14 +66,12 @@ function test_username(tc)
6666
end
6767

6868
function test_cpu_arch(tc)
69-
tc.assumeTrue(stdlib.has_dotnet() || stdlib.has_java())
7069

7170
arch = stdlib.cpu_arch();
7271
tc.verifyGreaterThan(strlength(arch), 0, "CPU architecture should not be empty")
7372
end
7473

7574
function test_ram_total(tc)
76-
tc.assumeTrue(stdlib.dotnet_api() >= 6 || stdlib.has_java())
7775

7876
t = stdlib.ram_total();
7977
tc.verifyGreaterThan(t, 0)
@@ -82,7 +80,6 @@ function test_ram_total(tc)
8280

8381

8482
function test_ram_free(tc)
85-
tc.assumeTrue(ispc() || stdlib.has_java())
8683

8784
f = stdlib.ram_free();
8885
tc.verifyGreaterThan(f, 0)

test/TestTime.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
11
classdef TestTime < matlab.unittest.TestCase
22

3+
properties
4+
td
5+
end
6+
7+
methods(TestClassSetup)
8+
function set_temp_wd(tc)
9+
if isMATLABReleaseOlderThan('R2022a')
10+
tc.td = tempname();
11+
mkdir(tc.td);
12+
else
13+
tc.td = tc.createTemporaryFolder();
14+
end
15+
end
16+
end
17+
18+
methods(TestClassTeardown)
19+
function remove_temp_wd(tc)
20+
if isMATLABReleaseOlderThan('R2022a')
21+
[s, m, i] = rmdir(tc.td, 's');
22+
if ~s, warning(i, "Failed to remove temporary directory %s: %s", tc.td, m); end
23+
end
24+
end
25+
end
26+
327
methods (Test)
428

529
function test_get_modtime(tc)
630
tc.verifyEmpty(stdlib.get_modtime(""))
731
end
832

933

34+
function test_touch_modtime(tc)
35+
tc.assumeTrue(stdlib.has_java() || stdlib.has_python())
36+
37+
fn = fullfile(tc.td, "modtime.txt");
38+
39+
tc.verifyTrue(stdlib.touch(fn, datetime("yesterday")))
40+
t0 = stdlib.get_modtime(fn);
41+
42+
tc.verifyTrue(stdlib.set_modtime(fn, datetime("now")))
43+
t1 = stdlib.get_modtime(fn);
44+
45+
tc.verifyGreaterThanOrEqual(t1, t0)
46+
end
47+
48+
49+
function test_set_modtime(tc)
50+
tc.assumeTrue(stdlib.has_java() || stdlib.has_python())
51+
52+
tc.verifyEqual(stdlib.set_modtime("", datetime("now")), false)
53+
end
54+
55+
1056
end
1157

1258
end

0 commit comments

Comments
 (0)