File modification time: Difference between revisions

Content added Content deleted
(Added Rust example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 380: Line 380:
return r;
return r;
}</lang>
}</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++}}==
=={{header|C++}}==
Line 408: Line 416:
}
}
}</lang>
}</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}}==
=={{header|Clojure}}==
Line 503: Line 503:
test("file", <file:output.txt>)
test("file", <file:output.txt>)
test("directory", <file:docs>)</lang>
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}}==
=={{header|Elixir}}==
Line 533: Line 521:
mtime: {{2016, 3, 7}, {23, 12, 35}}, size: 45, type: :regular, uid: 0}</lang>
mtime: {{2016, 3, 7}, {23, 12, 35}}, size: 45, type: :regular, uid: 0}</lang>
</pre>
</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}}==
=={{header|Erlang}}==
Line 807: Line 807:
print(path .. " does not exist.")
print(path .. " does not exist.")
end</lang>
end</lang>

=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
Most of the file statements/functions use current directory as path.
Most of the file statements/functions use current directory as path.
Line 1,054: Line 1,055:
# keep atime unchanged
# keep atime unchanged
# set mtime to current time</lang>
# 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}}==
=={{header|Phix}}==
Line 1,173: Line 1,148:
(file-or-directory-modify-seconds "foo.rkt")
(file-or-directory-modify-seconds "foo.rkt")
</lang>
</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}}==
=={{header|RapidQ}}==
Line 1,279: Line 1,281:
}
}
</lang>
</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}}==
=={{header|Scala}}==
Line 1,321: Line 1,302:
List(new File("output.txt"), new File("docs")).foreach(test)
List(new File("output.txt"), new File("docs")).foreach(test)
}</lang>
}</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}}==
=={{header|Slate}}==