File modification time: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added Rust example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 380:
return r;
}</lang>
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.IO;
 
Console.WriteLine(File.GetLastWriteTime("file.txt"));
File.SetLastWriteTime("file.txt", DateTime.Now);</lang>
 
=={{header|C++}}==
Line 408 ⟶ 416:
}
}</lang>
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.IO;
 
Console.WriteLine(File.GetLastWriteTime("file.txt"));
File.SetLastWriteTime("file.txt", DateTime.Now);</lang>
 
=={{header|Clojure}}==
Line 503:
test("file", <file:output.txt>)
test("directory", <file:docs>)</lang>
 
=={{header|Emacs Lisp}}==
<lang Lisp>(nth 5 (file-attributes "input.txt")) ;; mod date+time
 
(set-file-times "input.txt") ;; to current-time
(set-file-times "input.txt"
(encode-time 0 0 0 1 1 2014)) ;; to given date+time</lang>
 
<code>set-file-times</code> sets both the access time and modification time of the file. Time values are in the usual Emacs list form.
Emacs file name handler magic applies to both <code>file-attributes</code> and <code>set-file-times</code> so they can act on "remote" files too.
 
File visiting buffers record the modification time of the file so as to guard against changes by another program. <code>set-file-times</code> from within Emacs doesn't update those buffer times and so looks like an external change.
 
=={{header|Elixir}}==
Line 533 ⟶ 521:
mtime: {{2016, 3, 7}, {23, 12, 35}}, size: 45, type: :regular, uid: 0}</lang>
</pre>
 
=={{header|Emacs Lisp}}==
<lang Lisp>(nth 5 (file-attributes "input.txt")) ;; mod date+time
 
(set-file-times "input.txt") ;; to current-time
(set-file-times "input.txt"
(encode-time 0 0 0 1 1 2014)) ;; to given date+time</lang>
 
<code>set-file-times</code> sets both the access time and modification time of the file. Time values are in the usual Emacs list form.
Emacs file name handler magic applies to both <code>file-attributes</code> and <code>set-file-times</code> so they can act on "remote" files too.
 
File visiting buffers record the modification time of the file so as to guard against changes by another program. <code>set-file-times</code> from within Emacs doesn't update those buffer times and so looks like an external change.
 
=={{header|Erlang}}==
Line 807:
print(path .. " does not exist.")
end</lang>
 
=={{header|M2000 Interpreter}}==
Most of the file statements/functions use current directory as path.
Line 1,054 ⟶ 1,055:
# keep atime unchanged
# set mtime to current time</lang>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.03}}
<lang perl6>use NativeCall;
 
class utimbuf is repr('CStruct') {
has int $.actime;
has int $.modtime;
 
submethod BUILD(:$atime, :$mtime) {
$!actime = $atime;
$!modtime = $mtime.to-posix[0].round;
}
}
 
sub sysutime(Str, utimbuf --> int32) is native is symbol('utime') {*}
 
sub MAIN (Str $file) {
my $mtime = $file.IO.modified orelse .die;
 
my $ubuff = utimbuf.new(:atime(time),:mtime($mtime));
 
sysutime($file, $ubuff);
}</lang>
Sets the last access time to now,
while restoring the modification time to what it was before.
 
=={{header|Phix}}==
Line 1,173 ⟶ 1,148:
(file-or-directory-modify-seconds "foo.rkt")
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
<lang perl6>use NativeCall;
 
class utimbuf is repr('CStruct') {
has int $.actime;
has int $.modtime;
 
submethod BUILD(:$atime, :$mtime) {
$!actime = $atime;
$!modtime = $mtime.to-posix[0].round;
}
}
 
sub sysutime(Str, utimbuf --> int32) is native is symbol('utime') {*}
 
sub MAIN (Str $file) {
my $mtime = $file.IO.modified orelse .die;
 
my $ubuff = utimbuf.new(:atime(time),:mtime($mtime));
 
sysutime($file, $ubuff);
}</lang>
Sets the last access time to now,
while restoring the modification time to what it was before.
 
=={{header|RapidQ}}==
Line 1,279 ⟶ 1,281:
}
</lang>
 
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
include "osfiles.s7i";
include "time.s7i";
 
const proc: main is func
local
var time: modificationTime is time.value;
begin
modificationTime := getMTime("data.txt");
setMTime("data.txt", modificationTime);
end func;</lang>
 
=={{header|Sidef}}==
<lang ruby>var file = File.new(__FILE__);
say file.stat.mtime; # seconds since the epoch
 
# keep atime unchanged
# set mtime to current time
file.utime(file.stat.atime, Time.now);</lang>
 
=={{header|Scala}}==
Line 1,321 ⟶ 1,302:
List(new File("output.txt"), new File("docs")).foreach(test)
}</lang>
 
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
include "osfiles.s7i";
include "time.s7i";
 
const proc: main is func
local
var time: modificationTime is time.value;
begin
modificationTime := getMTime("data.txt");
setMTime("data.txt", modificationTime);
end func;</lang>
 
=={{header|Sidef}}==
<lang ruby>var file = File.new(__FILE__);
say file.stat.mtime; # seconds since the epoch
 
# keep atime unchanged
# set mtime to current time
file.utime(file.stat.atime, Time.now);</lang>
 
=={{header|Slate}}==
10,333

edits