Skip to content

Commit 070d6eb

Browse files
committed
set_modttime: java or python
1 parent 9fc8b8d commit 070d6eb

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ function test_is_regular_file(tc)
5454
end
5555

5656

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-
7457
end
7558

7659
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ function test_get_modtime(tc)
77
end
88

99

10+
function test_touch_modtime(tc)
11+
tc.assumeTrue(stdlib.has_java() || stdlib.has_python())
12+
13+
fn = fullfile(tc.td, "modtime.txt");
14+
15+
tc.verifyTrue(stdlib.touch(fn, datetime("yesterday")))
16+
t0 = stdlib.get_modtime(fn);
17+
18+
tc.verifyTrue(stdlib.set_modtime(fn, datetime("now")))
19+
t1 = stdlib.get_modtime(fn);
20+
21+
tc.verifyGreaterThanOrEqual(t1, t0)
22+
end
23+
24+
25+
function test_set_modtime(tc)
26+
tc.assumeTrue(stdlib.has_java() || stdlib.has_python())
27+
28+
tc.verifyEqual(stdlib.set_modtime("", datetime("now")), false)
29+
end
30+
31+
1032
end
1133

1234
end

0 commit comments

Comments
 (0)