File modification time: Difference between revisions
(Frink) |
m (→{{header|Wren}}: Minor tidy) |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 2: | Line 2: | ||
[[Category:Date and time]] |
[[Category:Date and time]] |
||
{{omit from|Befunge|No filesystem support}} |
{{omit from|Befunge|No filesystem support}} |
||
{{omit from|EasyLang}} |
|||
{{omit from|HTML}} |
{{omit from|HTML}} |
||
{{omit from|Locomotive Basic|Does not have a real time clock.}} |
{{omit from|Locomotive Basic|Does not have a real time clock.}} |
||
Line 18: | Line 19: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Ada does not allow you to change the date of a file but you can definitely read it: |
Ada does not allow you to change the date of a file but you can definitely read it: |
||
< |
<syntaxhighlight lang="ada">with Ada.Directories; use Ada.Directories; |
||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; |
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; |
||
Line 25: | Line 26: | ||
begin |
begin |
||
Put_Line (Image (Modification_Time ("file_time_test.adb"))); |
Put_Line (Image (Modification_Time ("file_time_test.adb"))); |
||
end File_Time_Test;</ |
end File_Time_Test;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Definition of the PROC is lifted from the Algol68 Genie documentation. |
Definition of the PROC is lifted from the Algol68 Genie documentation. |
||
< |
<syntaxhighlight lang="algol68">PROC get output = (STRING cmd) VOID: |
||
IF STRING sh cmd = " " + cmd + " ; 2>&1"; |
IF STRING sh cmd = " " + cmd + " ; 2>&1"; |
||
STRING output; |
STRING output; |
||
Line 39: | Line 40: | ||
get output ("touch -t 200109111246.40 WTC_1"); CO Change its last modified time CO |
get output ("touch -t 200109111246.40 WTC_1"); CO Change its last modified time CO |
||
get output ("ls -l --time-style=full-iso WTC_1") CO Verify it changed CO |
get output ("ls -l --time-style=full-iso WTC_1") CO Verify it changed CO |
||
</syntaxhighlight> |
|||
⚫ | |||
{{Out}} |
{{Out}} |
||
<pre>-rw-r--r-- 1 pcl pcl 0 2015-08-10 17:57:50.279854695 +0100 WTC_1 |
<pre>-rw-r--r-- 1 pcl pcl 0 2015-08-10 17:57:50.279854695 +0100 WTC_1 |
||
Line 45: | Line 46: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">FileGetTime, OutputVar, output.txt |
||
MsgBox % OutputVar |
MsgBox % OutputVar |
||
FileSetTime, 20080101, output.txt |
FileSetTime, 20080101, output.txt |
||
FileGetTime, OutputVar, output.txt |
FileGetTime, OutputVar, output.txt |
||
MsgBox % OutputVar</ |
MsgBox % OutputVar</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
{{works with|gawk}} |
{{works with|gawk}} |
||
< |
<syntaxhighlight lang="awk">@load "filefuncs" |
||
BEGIN { |
BEGIN { |
||
Line 67: | Line 68: | ||
close(cmd) |
close(cmd) |
||
}</ |
}</syntaxhighlight> |
||
Non-GNU awk's don't have direct access to the filesystem but can execute system-commands. |
Non-GNU awk's don't have direct access to the filesystem but can execute system-commands. |
||
< |
<syntaxhighlight lang="awk">#!/bin/awk -f |
||
BEGIN { # file modification time on Unix, using stat |
BEGIN { # file modification time on Unix, using stat |
||
fn ="input.txt" |
fn ="input.txt" |
||
Line 104: | Line 105: | ||
cmd="stat " fn |
cmd="stat " fn |
||
print "#", cmd; system(cmd) |
print "#", cmd; system(cmd) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 132: | Line 133: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
{{works with|Windows NT|4}} |
{{works with|Windows NT|4}} |
||
< |
<syntaxhighlight lang="dos">for %%f in (file.txt) do echo.%%~tf</syntaxhighlight> |
||
The date/time format is dependent on the user's locale, like the contents of the <code>%DATE%</code> and <code>%TIME%</code> built-ins. |
The date/time format is dependent on the user's locale, like the contents of the <code>%DATE%</code> and <code>%TIME%</code> built-ins. |
||
There is no built-in way of setting a file's modification time. |
There is no built-in way of setting a file's modification time. |
||
Line 138: | Line 139: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM ft{dwLowDateTime%, dwHighDateTime%} |
||
DIM st{wYear{l&,h&}, wMonth{l&,h&}, wDayOfWeek{l&,h&}, \ |
DIM st{wYear{l&,h&}, wMonth{l&,h&}, wDayOfWeek{l&,h&}, \ |
||
\ wDay{l&,h&}, wHour{l&,h&}, wMinute{l&,h&}, \ |
\ wDay{l&,h&}, wHour{l&,h&}, wMinute{l&,h&}, \ |
||
Line 165: | Line 166: | ||
SYS "SetFileTime", @hfile%(file%), 0, 0, ft{} |
SYS "SetFileTime", @hfile%(file%), 0, 0, ft{} |
||
CLOSE #file% |
CLOSE #file% |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 174: | Line 175: | ||
{{libheader|POSIX}} |
{{libheader|POSIX}} |
||
< |
<syntaxhighlight lang="c">#include <sys/stat.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <time.h> |
#include <time.h> |
||
Line 200: | Line 201: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===BSD utimes()=== |
===BSD utimes()=== |
||
Line 207: | Line 208: | ||
{{libheader|BSD libc}} |
{{libheader|BSD libc}} |
||
< |
<syntaxhighlight lang="c">#include <sys/stat.h> |
||
#include <sys/time.h> |
#include <sys/time.h> |
||
#include <err.h> |
#include <err.h> |
||
Line 230: | Line 231: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=== POSIX utimensat() === |
=== POSIX utimensat() === |
||
Line 238: | Line 239: | ||
{{libheader|POSIX}} |
{{libheader|POSIX}} |
||
{{works with|POSIX|-1.2008}} |
{{works with|POSIX|-1.2008}} |
||
< |
<syntaxhighlight lang="c">#include <sys/stat.h> |
||
#include <sys/time.h> |
#include <sys/time.h> |
||
#include <time.h> |
#include <time.h> |
||
Line 267: | Line 268: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=== Windows === |
=== Windows === |
||
Line 273: | Line 274: | ||
{{libheader|Win32}} |
{{libheader|Win32}} |
||
< |
<syntaxhighlight lang="c">#include <windows.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 379: | Line 380: | ||
if (setmodtime(argv[i])) r = 1; |
if (setmodtime(argv[i])) r = 1; |
||
return r; |
return r; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.IO; |
using System.IO; |
||
Console.WriteLine(File.GetLastWriteTime("file.txt")); |
Console.WriteLine(File.GetLastWriteTime("file.txt")); |
||
File.SetLastWriteTime("file.txt", DateTime.Now);</ |
File.SetLastWriteTime("file.txt", DateTime.Now);</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
compiled with g++ -lboost_filesystem <sourcefile> -o <destination file> |
compiled with g++ -lboost_filesystem <sourcefile> -o <destination file> |
||
< |
<syntaxhighlight lang="cpp">#include <boost/filesystem/operations.hpp> |
||
#include <ctime> |
#include <ctime> |
||
#include <iostream> |
#include <iostream> |
||
Line 415: | Line 416: | ||
return 2 ; |
return 2 ; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(import '(java.io File) |
||
'(java.util Date)) |
'(java.util Date)) |
||
Line 425: | Line 426: | ||
(.setLastModified (File. "output.txt") |
(.setLastModified (File. "output.txt") |
||
(.lastModified (File. "docs")))</ |
(.lastModified (File. "docs")))</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 432: | Line 433: | ||
The write date can be accessed, however, using [http://www.lispworks.com/documentation/HyperSpec/Body/f_file_w.htm file-write-date]: |
The write date can be accessed, however, using [http://www.lispworks.com/documentation/HyperSpec/Body/f_file_w.htm file-write-date]: |
||
< |
<syntaxhighlight lang="lisp">(file-write-date "input.txt")</syntaxhighlight> |
||
Implementations may, in addition, provide other ways of accessing these file attributes, e.g., POSIX bindings: |
Implementations may, in addition, provide other ways of accessing these file attributes, e.g., POSIX bindings: |
||
Line 440: | Line 441: | ||
(<code>sb-posix:utime</code> takes an access time and a modification time rather than an array of two times.) |
(<code>sb-posix:utime</code> takes an access time and a modification time rather than an array of two times.) |
||
< |
<syntaxhighlight lang="lisp">(let* ((filename "input.txt") |
||
(stat (sb-posix:stat filename)) |
(stat (sb-posix:stat filename)) |
||
(mtime (sb-posix:stat-mtime stat))) |
(mtime (sb-posix:stat-mtime stat))) |
||
(sb-posix:utime filename |
(sb-posix:utime filename |
||
(sb-posix:stat-atime stat) |
(sb-posix:stat-atime stat) |
||
(sb-posix:time)))</ |
(sb-posix:time)))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
import std.file: getTimes, setTimes, SysTime; |
import std.file: getTimes, setTimes, SysTime; |
||
Line 457: | Line 458: | ||
writeln(fileAccessTime, "\n", fileModificationTime); |
writeln(fileAccessTime, "\n", fileModificationTime); |
||
setTimes(fname, fileAccessTime, fileModificationTime); |
setTimes(fname, fileAccessTime, fileModificationTime); |
||
}</ |
}</syntaxhighlight> |
||
For Windows systems there is also getTimesWin(), |
For Windows systems there is also getTimesWin(), |
||
that gives the file creation time too. |
that gives the file creation time too. |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">function GetModifiedDate(const aFilename: string): TDateTime; |
||
var |
var |
||
hFile: Integer; |
hFile: Integer; |
||
Line 477: | Line 478: | ||
begin |
begin |
||
FileSetDate(aFileName, DateTimeToFileDate(aDateTime)); |
FileSetDate(aFileName, DateTimeToFileDate(aDateTime)); |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
Line 487: | Line 488: | ||
E follows the Java File interface, except that it replaces boolean success/failure returns with an ejector parameter, so that the default behavior if the client does not handle the case is not to continue ignoring the failure. |
E follows the Java File interface, except that it replaces boolean success/failure returns with an ejector parameter, so that the default behavior if the client does not handle the case is not to continue ignoring the failure. |
||
< |
<syntaxhighlight lang="e">def strdate(date) { |
||
return E.toString(<unsafe:java.util.makeDate>(date)) |
return E.toString(<unsafe:java.util.makeDate>(date)) |
||
} |
} |
||
Line 502: | Line 503: | ||
test("file", <file:output.txt>) |
test("file", <file:output.txt>) |
||
test("directory", <file:docs>)</ |
test("directory", <file:docs>)</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
Line 523: | Line 524: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(nth 5 (file-attributes "input.txt")) ;; mod date+time |
||
(set-file-times "input.txt") ;; to current-time |
(set-file-times "input.txt") ;; to current-time |
||
(set-file-times "input.txt" |
(set-file-times "input.txt" |
||
(encode-time 0 0 0 1 1 2014)) ;; to given date+time</ |
(encode-time 0 0 0 1 1 2014)) ;; to given date+time</syntaxhighlight> |
||
<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. |
<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. |
||
Line 536: | Line 537: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( file_modification_time ). |
-module( file_modification_time ). |
||
Line 548: | Line 549: | ||
io:fwrite( "Modification time ~p~n", [File_info#file_info.mtime] ), |
io:fwrite( "Modification time ~p~n", [File_info#file_info.mtime] ), |
||
ok = file:write_file_info( File, File_info#file_info{mtime=calendar:local_time()} ). |
ok = file:write_file_info( File, File_info#file_info{mtime=calendar:local_time()} ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
open System.IO |
open System.IO |
||
Line 558: | Line 559: | ||
Console.WriteLine(File.GetLastWriteTime(args.[0])) |
Console.WriteLine(File.GetLastWriteTime(args.[0])) |
||
File.SetLastWriteTime(args.[0], DateTime.Now) |
File.SetLastWriteTime(args.[0], DateTime.Now) |
||
0</ |
0</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"foo.txt" file-info modified>> .</syntaxhighlight> |
||
Setting the modified time is not cross-platform, so here's a Unix version. |
Setting the modified time is not cross-platform, so here's a Unix version. |
||
< |
<syntaxhighlight lang="factor">USE: io.files.info.unix |
||
"foo.txt" now 2 hours time+ set-file-modified-time</ |
"foo.txt" now 2 hours time+ set-file-modified-time</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 571: | Line 572: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
' This example is taken directly from the FB documentation (see [http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFiledatetime]) |
' This example is taken directly from the FB documentation (see [http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFiledatetime]) |
||
Line 590: | Line 591: | ||
End If |
End If |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
Sample input/output: |
Sample input/output: |
||
Line 603: | Line 604: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Frink runs on a Java Virtual Machine (JVM) and can use any of its methods. The underlying code returns the modification time as the number of milliseconds since 1970-01-01 00:00 UTC. The following converts between Frink's human-friendly time systems and the milliseconds since the epoch. |
Frink runs on a Java Virtual Machine (JVM) and can use any of its methods. The underlying code returns the modification time as the number of milliseconds since 1970-01-01 00:00 UTC. The following converts between Frink's human-friendly time systems and the milliseconds since the epoch. |
||
< |
<syntaxhighlight lang="frink">f = newJava["java.io.File", "FileModificationTime.frink"] |
||
epoch = #1970 UTC# |
|||
f.setLastModified[(#2022-01-01 5:00 AM# - epoch) / ms] |
|||
println[f.lastModified[] ms + |
println[f.lastModified[] ms + epoch]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
AD 2022-01-01 AM 05:00:00.000 (Sat) Mountain Standard Time |
AD 2022-01-01 AM 05:00:00.000 (Sat) Mountain Standard Time |
||
</pre> |
</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask ) |
|||
CFURLRef url = fn URLByAppendingPathComponent( desktopURL, @"file.txt" ) |
|||
CFDictionaryRef dict = fn FileManagerAttributesOfItemAtURL( url ) |
|||
print dict[@NSFileModificationDate] // Read file's current date |
|||
// Reset file date to current |
|||
fn FileManagerSetAttributesOfItemAtURL( CFURLRef url, @{NSFileModificationDate:fn DateNow ) |
|||
</syntaxhighlight> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas">' There is no built in command in Gambas to 'set' the modification time of a file |
||
' A shell call to 'touch' would do it |
' A shell call to 'touch' would do it |
||
Line 620: | Line 635: | ||
Print "Rosetta.txt was last modified " & Format(stInfo.LastModified, "dd/mm/yyy hh:nn:ss") |
Print "Rosetta.txt was last modified " & Format(stInfo.LastModified, "dd/mm/yyy hh:nn:ss") |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 627: | Line 642: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 653: | Line 668: | ||
os.Chtimes(filename, atime, mtime) |
os.Chtimes(filename, atime, mtime) |
||
fmt.Println("mod time now:", mtime) |
fmt.Println("mod time now:", mtime) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|GUISS}}== |
=={{header|GUISS}}== |
||
Line 660: | Line 675: | ||
In this example, we get the date and timestamp for the file Foobar.txt. |
In this example, we get the date and timestamp for the file Foobar.txt. |
||
< |
<syntaxhighlight lang="guiss">Start,My Documents,Rightclick:Icon:Foobar.txt,Properties</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import System.Posix.Files |
||
import System.Posix.Time |
import System.Posix.Time |
||
Line 672: | Line 687: | ||
curTime <- epochTime |
curTime <- epochTime |
||
setFileTimes filename atime curTime -- keep atime unchanged |
setFileTimes filename atime curTime -- keep atime unchanged |
||
-- set mtime to current time</ |
-- set mtime to current time</syntaxhighlight> |
||
Alternative (only gets modification time): |
Alternative (only gets modification time): |
||
< |
<syntaxhighlight lang="haskell">import System.Directory |
||
import System.Time |
import System.Time |
||
do ct <- getModificationTime filename |
do ct <- getModificationTime filename |
||
cal <- toCalendarTime ct |
cal <- toCalendarTime ct |
||
putStrLn (calendarTimeToString cal)</ |
putStrLn (calendarTimeToString cal)</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER timestamp*18 |
||
timestamp = ' ' ! blank timestamp will read: |
timestamp = ' ' ! blank timestamp will read: |
||
Line 689: | Line 704: | ||
timestamp = '19991231235950' ! set timestamp to Millenium - 10 seconds |
timestamp = '19991231235950' ! set timestamp to Millenium - 10 seconds |
||
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp)</ |
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp)</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Icon doesn't support 'stat' or 'utime'; however, |
Icon doesn't support 'stat' or 'utime'; however, |
||
information can be obtained by use of the system function to access command line. |
information can be obtained by use of the system function to access command line. |
||
<syntaxhighlight lang="unicon"> |
|||
<lang Unicon> |
|||
every dir := !["./","/"] do { |
every dir := !["./","/"] do { |
||
if i := stat(f := dir || "input.txt") then { |
if i := stat(f := dir || "input.txt") then { |
||
Line 703: | Line 718: | ||
} |
} |
||
else stop("failure to stat ",f) |
else stop("failure to stat ",f) |
||
}</ |
}</syntaxhighlight> |
||
Notes: |
Notes: |
||
* Icon and Unicon accept both / and \ for directory separators. |
* Icon and Unicon accept both / and \ for directory separators. |
||
Line 711: | Line 726: | ||
=={{header|J}}== |
=={{header|J}}== |
||
The standard file access library only supports reading the file modification time. |
The standard file access library only supports reading the file modification time. |
||
< |
<syntaxhighlight lang="j"> load 'files' |
||
fstamp 'input.txt' |
fstamp 'input.txt' |
||
2009 8 24 20 34 30</ |
2009 8 24 20 34 30</syntaxhighlight> |
||
It is possible to set the time but it has to be done through OS specific external calls. |
It is possible to set the time but it has to be done through OS specific external calls. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<lang |
<syntaxhighlight lang="java"> |
||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
public static void main(String[] args) { |
|||
File file = new File("file.txt"); |
|||
/* get */ |
|||
/* returns '0' if the file does not exist */ |
|||
System.out.printf("%tD %1$tT%n", file.lastModified()); |
|||
/* set */ |
|||
file.setLastModified(System.currentTimeMillis()); |
|||
} |
|||
</syntaxhighlight> |
|||
<pre> |
|||
05/11/23 11:56:58 |
|||
⚫ | |||
<br /> |
|||
An alternate demonstration |
|||
<syntaxhighlight lang="java">import java.io.File; |
|||
import java.util.Date; |
import java.util.Date; |
||
public class FileModificationTimeTest { |
public class FileModificationTimeTest { |
||
Line 736: | Line 768: | ||
test("directory", new File("docs")); |
test("directory", new File("docs")); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{works with|JScript}} |
{{works with|JScript}} |
||
Get only. |
Get only. |
||
< |
<syntaxhighlight lang="javascript">var fso = new ActiveXObject("Scripting.FileSystemObject"); |
||
var f = fso.GetFile('input.txt'); |
var f = fso.GetFile('input.txt'); |
||
var mtime = f.DateLastModified;</ |
var mtime = f.DateLastModified;</syntaxhighlight> |
||
The following works in all browsers, including IE10. |
The following works in all browsers, including IE10. |
||
< |
<syntaxhighlight lang="javascript">var file = document.getElementById("fileInput").files.item(0); |
||
var last_modified = file.lastModifiedDate;</ |
var last_modified = file.lastModifiedDate;</syntaxhighlight> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
Line 753: | Line 785: | ||
A C extension could provide access to the ''utime'' system call if desired. |
A C extension could provide access to the ''utime'' system call if desired. |
||
< |
<syntaxhighlight lang="javascript">/* File modification times, in Jsi */ |
||
var fileTime = File.mtime('fileModificationTime.jsi'); |
var fileTime = File.mtime('fileModificationTime.jsi'); |
||
Line 761: | Line 793: | ||
fileTime = File.mtime('fileModificationTime.jsi'); |
fileTime = File.mtime('fileModificationTime.jsi'); |
||
puts("Mod time now: ", strftime(fileTime * 1000));</ |
puts("Mod time now: ", strftime(fileTime * 1000));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 769: | Line 801: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Dates |
||
fname, _ = mktemp() |
fname, _ = mktemp() |
||
Line 776: | Line 808: | ||
println("\nTouch this file.") |
println("\nTouch this file.") |
||
touch(fname) |
touch(fname) |
||
println("The modification time of $fname is now ", Dates.unix2datetime(mtime(fname)))</ |
println("The modification time of $fname is now ", Dates.unix2datetime(mtime(fname)))</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
import java.io.File |
import java.io.File |
||
Line 792: | Line 824: | ||
println("%tc".format(lastModified())) |
println("%tc".format(lastModified())) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lang}}== |
|||
{{libheader|lang-io-module}} |
|||
<syntaxhighlight lang="lang"> |
|||
# Load the IO module |
|||
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">" |
|||
ln.loadModule(<pathToIO.lm>) |
|||
$file = [[io]]::fp.openFile(input.txt) |
|||
$modTime = [[io]]::fp.getModificationDate($file) |
|||
fn.println(Mod Time: $modTime) |
|||
[[io]]::fp.setModificationDate($file, fn.currentTimeMillis()) |
|||
$modTime = [[io]]::fp.getModificationDate($file) |
|||
fn.println(Mod Time now: $modTime) |
|||
[[io]]::fp.closeFile($file) |
|||
</syntaxhighlight> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Line 798: | Line 850: | ||
File modification date is updated on save of file. |
File modification date is updated on save of file. |
||
< |
<syntaxhighlight lang="lasso">local(f) = file('input.txt') |
||
handle => { #f->close } |
handle => { #f->close } |
||
#f->modificationDate->format('%-D %r') |
#f->modificationDate->format('%-D %r') |
||
// result: 12/2/2010 11:04:15 PM</ |
// result: 12/2/2010 11:04:15 PM</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">require "lfs" |
||
local attributes = lfs.attributes("input.txt") |
local attributes = lfs.attributes("input.txt") |
||
if attributes then |
if attributes then |
||
Line 816: | Line 868: | ||
else |
else |
||
print(path .. " does not exist.") |
print(path .. " does not exist.") |
||
end</ |
end</syntaxhighlight> |
||
=={{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. |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module CheckIt { |
Module CheckIt { |
||
\\ without *for wide output* we open for ANSI (1 byte per character) |
\\ without *for wide output* we open for ANSI (1 byte per character) |
||
Line 828: | Line 880: | ||
Close #f |
Close #f |
||
Print file.stamp("afile") 'it is a number in VB6 date format. |
Print file.stamp("afile") 'it is a number in VB6 date format. |
||
date k=file.stamp("afile", -2) // Version 12 has date type, convert number or string to date type |
|||
print k // this has type date but print as string using default system locale date and time format |
|||
\\ day format as for Greece |
\\ day format as for Greece |
||
Print Str$(File.Stamp("afile"),"hh:nn:ss dd/mm/yyyy") , "utc write time - by default" |
Print Str$(File.Stamp("afile"),"hh:nn:ss dd/mm/yyyy") , "utc write time - by default" |
||
Line 836: | Line 890: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Get file modification time: |
Get file modification time: |
||
< |
<syntaxhighlight lang="mathematica"> FileDate["file","Modification"]</syntaxhighlight> |
||
results is returned in format: {year,month,day,hour,minute,second}. Setting file modification time: |
results is returned in format: {year,month,day,hour,minute,second}. Setting file modification time: |
||
< |
<syntaxhighlight lang="mathematica"> SetFileDate["file",date,"Modification"]</syntaxhighlight> |
||
where date is specified as {year,month,day,hour,minute,second}. |
where date is specified as {year,month,day,hour,minute,second}. |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> f = dir('output.txt'); % struct f contains file information |
||
f.date % is string containing modification time |
f.date % is string containing modification time |
||
f.datenum % numerical format (number of days) |
f.datenum % numerical format (number of days) |
||
datestr(f.datenum) % is the same as f.date |
datestr(f.datenum) % is the same as f.date |
||
% see also: stat, lstat </ |
% see also: stat, lstat </syntaxhighlight> |
||
Modifying of file access time can be done through system calls: |
Modifying of file access time can be done through system calls: |
||
< |
<syntaxhighlight lang="matlab"> system('touch -t 201002032359.59 output.txt'); </syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
MAXScript has no method to set the mod time |
MAXScript has no method to set the mod time |
||
< |
<syntaxhighlight lang="maxscript">-- Returns a string containing the mod date for the file, e.g. "1/29/99 1:52:05 PM" |
||
getFileModDate "C:\myFile.txt"</ |
getFileModDate "C:\myFile.txt"</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE ModTime EXPORTS Main; |
||
IMPORT IO, Fmt, File, FS, Date, OSError; |
IMPORT IO, Fmt, File, FS, Date, OSError; |
||
Line 889: | Line 943: | ||
| OSError.E => IO.Put("Error: Failed to get file status.\n"); |
| OSError.E => IO.Put("Error: Failed to get file status.\n"); |
||
END; |
END; |
||
END ModTime.</ |
END ModTime.</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 895: | Line 949: | ||
</pre> |
</pre> |
||
This program sets the modification time to any value we wish: |
This program sets the modification time to any value we wish: |
||
< |
<syntaxhighlight lang="modula3">MODULE SetModTime EXPORTS Main; |
||
IMPORT Date, FS; |
IMPORT Date, FS; |
||
Line 916: | Line 970: | ||
FS.SetModificationTime("test.txt", Date.ToTime(date)); |
FS.SetModificationTime("test.txt", Date.ToTime(date)); |
||
END SetModTime.</ |
END SetModTime.</syntaxhighlight> |
||
We can see the output with the Unix command <tt>stat</tt> |
We can see the output with the Unix command <tt>stat</tt> |
||
{{Out}} |
{{Out}} |
||
Line 925: | Line 979: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 954: | Line 1,008: | ||
end |
end |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 967: | Line 1,021: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">;; print modification time |
||
(println (date (file-info "input.txt" 6))) |
(println (date (file-info "input.txt" 6))) |
||
;; set modification time to now (Unix) |
;; set modification time to now (Unix) |
||
(! "touch -m input.txt")</ |
(! "touch -m input.txt")</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import os, strutils, times |
||
if paramCount() == 0: quit(QuitSuccess) |
if paramCount() == 0: quit(QuitSuccess) |
||
Line 984: | Line 1,038: | ||
# Change last modification time to current time. |
# Change last modification time to current time. |
||
fileName.setLastModificationTime(now().toTime())</ |
fileName.setLastModificationTime(now().toTime())</syntaxhighlight> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
Get only |
Get only |
||
< |
<syntaxhighlight lang="objeck">use System.IO.File; |
||
class Program { |
class Program { |
||
Line 995: | Line 1,049: | ||
File->ModifiedTime("file_mod.obs")->ToString()->PrintLine(); |
File->ModifiedTime("file_mod.obs")->ToString()->PrintLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
< |
<syntaxhighlight lang="objc">NSFileManager *fm = [NSFileManager defaultManager]; |
||
// Pre-OS X 10.5 |
// Pre-OS X 10.5 |
||
Line 1,009: | Line 1,063: | ||
NSLog(@"%@", [[fm attributesOfItemAtPath:@"input.txt" error:NULL] fileModificationDate]); |
NSLog(@"%@", [[fm attributesOfItemAtPath:@"input.txt" error:NULL] fileModificationDate]); |
||
[fm setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] |
[fm setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] |
||
ofItemAtPath:@"input.txt" error:NULL];</ |
ofItemAtPath:@"input.txt" error:NULL];</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">#load "unix.cma";; |
||
open Unix;; |
open Unix;; |
||
let mtime = (stat filename).st_mtime;; (* seconds since the epoch *) |
let mtime = (stat filename).st_mtime;; (* seconds since the epoch *) |
||
Line 1,018: | Line 1,072: | ||
utimes filename (stat filename).st_atime (time ());; |
utimes filename (stat filename).st_atime (time ());; |
||
(* keep atime unchanged |
(* keep atime unchanged |
||
set mtime to current time *)</ |
set mtime to current time *)</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 1,024: | Line 1,078: | ||
Get only : |
Get only : |
||
< |
<syntaxhighlight lang="oforth">File new("myfile.txt") modified</syntaxhighlight> |
||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
The file modified time can only be read. |
The file modified time can only be read. |
||
< |
<syntaxhighlight lang="progress">FILE-INFO:FILE-NAME = 'c:/temp'. |
||
MESSAGE |
MESSAGE |
||
STRING( FILE-INFO:FILE-MOD-TIME, 'HH:MM:SS' ) |
STRING( FILE-INFO:FILE-MOD-TIME, 'HH:MM:SS' ) |
||
VIEW-AS ALERT-BOX</ |
VIEW-AS ALERT-BOX</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Getting the modification time: |
Getting the modification time: |
||
< |
<syntaxhighlight lang="oz">declare |
||
[Path] = {Module.link ['x-oz://system/os/Path.ozf']} |
[Path] = {Module.link ['x-oz://system/os/Path.ozf']} |
||
Modified = {Path.mtime "input.txt"} %% posix time |
Modified = {Path.mtime "input.txt"} %% posix time |
||
in |
in |
||
{Show {OsTime.localtime Modified}} %% human readable record</ |
{Show {OsTime.localtime Modified}} %% human readable record</syntaxhighlight> |
||
Setting the modification time is not possible, |
Setting the modification time is not possible, |
||
Line 1,051: | Line 1,105: | ||
{{works with|Perl|5}} |
{{works with|Perl|5}} |
||
< |
<syntaxhighlight lang="perl">my $mtime = (stat($file))[9]; # seconds since the epoch |
||
# you should use the more legible version below: |
# you should use the more legible version below: |
||
Line 1,059: | Line 1,113: | ||
utime(stat($file)->atime, time, $file); |
utime(stat($file)->atime, time, $file); |
||
# keep atime unchanged |
# keep atime unchanged |
||
# set mtime to current time</ |
# set mtime to current time</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o |
||
-- (however get as per the JavaScript entry above might be doable if needed)</span> |
-- (however get as per the JavaScript entry above might be doable if needed)</span> |
||
Line 1,072: | Line 1,126: | ||
<span style="color: #004080;">bool</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set_file_date</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">bool</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set_file_date</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_file_date</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">))</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_file_date</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,081: | Line 1,135: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
$filename = 'input.txt'; |
$filename = 'input.txt'; |
||
Line 1,089: | Line 1,143: | ||
time(), // set mtime to current time |
time(), // set mtime to current time |
||
fileatime($filename)); // keep atime unchanged |
fileatime($filename)); // keep atime unchanged |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(let File "test.file" |
||
(and |
(and |
||
(info File) |
(info File) |
||
(prinl (stamp (cadr @) (cddr @))) ) # Print date and time in UTC |
(prinl (stamp (cadr @) (cddr @))) ) # Print date and time in UTC |
||
(call 'touch File) ) # Set modification time to "now"</ |
(call 'touch File) ) # Set modification time to "now"</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2010-02-20 15:46:37</pre> |
<pre>2010-02-20 15:46:37</pre> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
< |
<syntaxhighlight lang="pop11">;;; Print modification time (seconds since Epoch) |
||
sysmodtime('file') =></ |
sysmodtime('file') =></syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell"> |
||
$modificationTime = (Get-ChildItem file.txt).LastWriteTime |
$modificationTime = (Get-ChildItem file.txt).LastWriteTime |
||
Set-ItemProperty file.txt LastWriteTime (Get-Date) |
Set-ItemProperty file.txt LastWriteTime (Get-Date) |
||
Line 1,114: | Line 1,168: | ||
$CreationTime = (Get-ChildItem file.txt).CreationTime |
$CreationTime = (Get-ChildItem file.txt).CreationTime |
||
Set-ItemProperty file.txt CreationTime(Get-Date) |
Set-ItemProperty file.txt CreationTime(Get-Date) |
||
</syntaxhighlight> |
|||
</lang> |
|||
You can also use alternates to get UTC time: |
You can also use alternates to get UTC time: |
||
Line 1,122: | Line 1,176: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Debug FormatDate("%yyyy/%mm/%dd", GetFileDate("file.txt",#PB_Date_Modified)) |
||
SetFileDate("file.txt",#PB_Date_Modified,Date(1987, 10, 23, 06, 43, 15)) |
SetFileDate("file.txt",#PB_Date_Modified,Date(1987, 10, 23, 06, 43, 15)) |
||
Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate("file.txt",#PB_Date_Modified))</ |
Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate("file.txt",#PB_Date_Modified))</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">import os |
||
#Get modification time: |
#Get modification time: |
||
Line 1,139: | Line 1,193: | ||
#Set the access and modification times to the current time: |
#Set the access and modification times to the current time: |
||
os.utime('path', None)</ |
os.utime('path', None)</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
See [http://tolstoy.newcastle.edu.au/R/e4/devel/08/02/0639.html this R-devel mailing list thread] for more information. |
See [http://tolstoy.newcastle.edu.au/R/e4/devel/08/02/0639.html this R-devel mailing list thread] for more information. |
||
< |
<syntaxhighlight lang="r"># Get the value |
||
file.info(filename)$mtime |
file.info(filename)$mtime |
||
Line 1,149: | Line 1,203: | ||
shell("copy /b /v filename +,,>nul") |
shell("copy /b /v filename +,,>nul") |
||
# and on Unix (untested) |
# and on Unix (untested) |
||
shell("touch -m filename")</ |
shell("touch -m filename")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(file-or-directory-modify-seconds "foo.rkt") |
(file-or-directory-modify-seconds "foo.rkt") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{Works with|rakudo|2018.03}} |
{{Works with|rakudo|2018.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>use NativeCall; |
||
class utimbuf is repr('CStruct') { |
class utimbuf is repr('CStruct') { |
||
Line 1,181: | Line 1,235: | ||
sysutime($file, $ubuff); |
sysutime($file, $ubuff); |
||
}</ |
}</syntaxhighlight> |
||
Sets the last access time to now, |
Sets the last access time to now, |
||
while restoring the modification time to what it was before. |
while restoring the modification time to what it was before. |
||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
< |
<syntaxhighlight lang="rapidq">name$ = DIR$("input.txt", 0) |
||
PRINT "File date: "; FileRec.Date |
PRINT "File date: "; FileRec.Date |
||
PRINT "File time: "; FileRec.Time</ |
PRINT "File time: "; FileRec.Time</syntaxhighlight> |
||
=={{header|REALbasic}}== |
=={{header|REALbasic}}== |
||
The ''Date'' object has properties which correspond to various date formats |
The ''Date'' object has properties which correspond to various date formats |
||
such as SQLDateTime (YYYY-MM-DD HH:MM:SS), DayOfWeek, DayOfYear, and TotalSeconds since 12:00AM, January 1, 1904, among others. |
such as SQLDateTime (YYYY-MM-DD HH:MM:SS), DayOfWeek, DayOfYear, and TotalSeconds since 12:00AM, January 1, 1904, among others. |
||
<syntaxhighlight lang="realbasic"> |
|||
<lang REALbasic> |
|||
Function getModDate(f As FolderItem) As Date |
Function getModDate(f As FolderItem) As Date |
||
Return f.ModificationDate |
Return f.ModificationDate |
||
End Function</ |
End Function</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{works with|Regina REXX}} |
{{works with|Regina REXX}} |
||
The REXX language has no facility to update a file's modification time. |
The REXX language has no facility to update a file's modification time. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program obtains and displays a file's time of modification. */ |
||
parse arg $ . /*obtain required argument from the CL.*/ |
parse arg $ . /*obtain required argument from the CL.*/ |
||
if $=='' then do; say "***error*** no filename was specified."; exit 13; end |
if $=='' then do; say "***error*** no filename was specified."; exit 13; end |
||
Line 1,209: | Line 1,263: | ||
say 'timestamp of last modification: ' q /*display the modification time info. */ |
say 'timestamp of last modification: ' q /*display the modification time info. */ |
||
/*stick a fork in it, we're all done. */ |
/*stick a fork in it, we're all done. */ |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''output''' when using the file ID for this particular REXX program (source file): <tt> FILEMTIME.REX </tt> |
'''output''' when using the file ID for this particular REXX program (source file): <tt> FILEMTIME.REX </tt> |
||
<pre> |
<pre> |
||
Line 1,217: | Line 1,271: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see GetFileInfo( "test.ring" ) |
see GetFileInfo( "test.ring" ) |
||
Line 1,227: | Line 1,281: | ||
aInfo = split(cLine," ") |
aInfo = split(cLine," ") |
||
return aInfo |
return aInfo |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,239: | Line 1,293: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">#Get modification time: |
||
modtime = File.mtime('filename') |
modtime = File.mtime('filename') |
||
Line 1,249: | Line 1,303: | ||
#Set the access and modification times to the current time: |
#Set the access and modification times to the current time: |
||
File.utime(nil, nil, 'path')</ |
File.utime(nil, nil, 'path')</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">files #f, DefaultDir$ + "\*.*" ' all files in the default directory |
||
print "hasanswer: ";#f HASANSWER() ' does it exist |
print "hasanswer: ";#f HASANSWER() ' does it exist |
||
Line 1,274: | Line 1,328: | ||
print |
print |
||
end if |
end if |
||
next</ |
next</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">use std::fs; |
||
fn main() -> std::io::Result<()> { |
fn main() -> std::io::Result<()> { |
||
Line 1,289: | Line 1,343: | ||
Ok(()) |
Ok(()) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala">import java.io.File |
||
import java.util.Date |
import java.util.Date |
||
Line 1,310: | Line 1,364: | ||
// main |
// main |
||
List(new File("output.txt"), new File("docs")).foreach(test) |
List(new File("output.txt"), new File("docs")).foreach(test) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "osfiles.s7i"; |
include "osfiles.s7i"; |
||
include "time.s7i"; |
include "time.s7i"; |
||
Line 1,323: | Line 1,377: | ||
modificationTime := getMTime("data.txt"); |
modificationTime := getMTime("data.txt"); |
||
setMTime("data.txt", modificationTime); |
setMTime("data.txt", modificationTime); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var file = File.new(__FILE__); |
||
say file.stat.mtime; # seconds since the epoch |
say file.stat.mtime; # seconds since the epoch |
||
# keep atime unchanged |
# keep atime unchanged |
||
# set mtime to current time |
# set mtime to current time |
||
file.utime(file.stat.atime, Time.now);</ |
file.utime(file.stat.atime, Time.now);</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
Line 1,337: | Line 1,391: | ||
This code gets a raw value: |
This code gets a raw value: |
||
< |
<syntaxhighlight lang="slate">slate[1]> (File newNamed: 'LICENSE') fileInfo modificationTimestamp. |
||
1240349799</ |
1240349799</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">|a| |
||
a := File name: 'input.txt'. |
a := File name: 'input.txt'. |
||
(a lastModifyTime) printNl.</ |
(a lastModifyTime) printNl.</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">val mtime = OS.FileSys.modTime filename; (* returns a Time.time data structure *) |
||
(* unfortunately it seems like you have to set modification & access times together *) |
(* unfortunately it seems like you have to set modification & access times together *) |
||
OS.FileSys.setTime (filename, NONE); (* sets modification & access time to now *) |
OS.FileSys.setTime (filename, NONE); (* sets modification & access time to now *) |
||
(* equivalent to: *) |
(* equivalent to: *) |
||
OS.FileSys.setTime (filename, SOME (Time.now ()))</ |
OS.FileSys.setTime (filename, SOME (Time.now ()))</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Assuming that the variable <tt>filename</tt> holds the name of the file... |
Assuming that the variable <tt>filename</tt> holds the name of the file... |
||
< |
<syntaxhighlight lang="tcl"># Get the modification time: |
||
set timestamp [file mtime $filename] |
set timestamp [file mtime $filename] |
||
# Set the modification time to ‘now’: |
# Set the modification time to ‘now’: |
||
file mtime $filename [clock seconds]</ |
file mtime $filename [clock seconds]</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
TUSCRIPT does not allow to modify/set the timestamp of a file, |
TUSCRIPT does not allow to modify/set the timestamp of a file, |
||
but it is able to read it: |
but it is able to read it: |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
file="rosetta.txt" |
file="rosetta.txt" |
||
Line 1,370: | Line 1,424: | ||
modified=MODIFIED (file) |
modified=MODIFIED (file) |
||
PRINT "file ",file," last modified: ",modified |
PRINT "file ",file," last modified: ",modified |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,385: | Line 1,439: | ||
To get the timestamp in seconds since the epoch: |
To get the timestamp in seconds since the epoch: |
||
< |
<syntaxhighlight lang="bash">T=`stat -c %Y $F`</syntaxhighlight> |
||
To get the timestamp in human-readable format: |
To get the timestamp in human-readable format: |
||
< |
<syntaxhighlight lang="bash">T=`stat -c %y $F`</syntaxhighlight> |
||
Note that the only difference between the above examples is capital Y vs lower-case y. |
Note that the only difference between the above examples is capital Y vs lower-case y. |
||
Line 1,395: | Line 1,449: | ||
To set file F to time T, where T is in a human-readable format: |
To set file F to time T, where T is in a human-readable format: |
||
< |
<syntaxhighlight lang="bash"># Note the quotation marks -- very important! |
||
T="2000-01-01 01:02:03.040506070 -0800" |
T="2000-01-01 01:02:03.040506070 -0800" |
||
touch -c -d "$T" $F</ |
touch -c -d "$T" $F</syntaxhighlight> |
||
To set file F to time T, where T is in the format [[CC]YY]MMDDhhmm[.ss] (the square brackets mark optional parts): |
To set file F to time T, where T is in the format [[CC]YY]MMDDhhmm[.ss] (the square brackets mark optional parts): |
||
< |
<syntaxhighlight lang="bash">T=200102030405.06 |
||
touch -c -t $T $F</ |
touch -c -t $T $F</syntaxhighlight> |
||
If the year is left out, the current year is used. |
If the year is left out, the current year is used. |
||
If the seconds are left out, 0 (zero) is used. |
If the seconds are left out, 0 (zero) is used. |
||
Leaving out the optional parts of the above results in this: |
Leaving out the optional parts of the above results in this: |
||
< |
<syntaxhighlight lang="bash">T=02030405 |
||
touch -c -t $T $F</ |
touch -c -t $T $F</syntaxhighlight> |
||
If no time is specified, then the timestamp is set to the current time: |
If no time is specified, then the timestamp is set to the current time: |
||
<lang |
<syntaxhighlight lang="bash">touch -c $F</syntaxhighlight> |
||
There are, of course, other ways to do both tasks -- for example, one could use <code>ls -l --time-style=full-iso</code> and then process the resulting list with some other tool (e.g. [[awk]] or [[Perl]]). |
There are, of course, other ways to do both tasks -- for example, one could use <code>ls -l --time-style=full-iso</code> and then process the resulting list with some other tool (e.g. [[awk]] or [[Perl]]). |
||
Line 1,416: | Line 1,470: | ||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
{{works with|Cygnus/X Ursa}} |
{{works with|Cygnus/X Ursa}} |
||
< |
<syntaxhighlight lang="ursa">decl java.util.Date d |
||
decl file f |
decl file f |
||
Line 1,423: | Line 1,477: | ||
out d endl console |
out d endl console |
||
f.setlastmodified 10</ |
f.setlastmodified 10</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
{{works with|Windows Script Host|*}} |
{{works with|Windows Script Host|*}} |
||
VBScript provides no way to set the last modification time. It does allow you to retrieve it, however. |
VBScript provides no way to set the last modification time. It does allow you to retrieve it, however. |
||
<syntaxhighlight lang="vbscript"> |
|||
<lang VBScript> |
|||
WScript.Echo CreateObject("Scripting.FileSystemObject").GetFile("input.txt").DateLastModified |
WScript.Echo CreateObject("Scripting.FileSystemObject").GetFile("input.txt").DateLastModified |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Display file's last modification time as number of seconds since midnight. |
Display file's last modification time as number of seconds since midnight. |
||
< |
<syntaxhighlight lang="vedit">Num_Type(File_Stamp_Time("input.txt"))</syntaxhighlight> |
||
Displays file's last modification date and time as a string. |
Displays file's last modification date and time as a string. |
||
< |
<syntaxhighlight lang="vedit">File_Stamp_String(10, "input.txt") |
||
Reg_Type(10)</ |
Reg_Type(10)</syntaxhighlight> |
||
Vedit Macro Language has <span class="plainlinks">[http://seoph1.cafe24.com/wordpress/ <span style="color:black;font-weight:normal; text-decoration:none!important; background:none!important; text-decoration:none;">Blog in SEO</span>] no method to set the modification time |
Vedit Macro Language has <span class="plainlinks">[http://seoph1.cafe24.com/wordpress/ <span style="color:black;font-weight:normal; text-decoration:none!important; background:none!important; text-decoration:none;">Blog in SEO</span>] no method to set the modification time |
||
Line 1,445: | Line 1,499: | ||
{{works with|Visual Basic .NET|9.0+}} |
{{works with|Visual Basic .NET|9.0+}} |
||
< |
<syntaxhighlight lang="vbnet">Dim file As New IO.FileInfo("test.txt") |
||
'Creation Time |
'Creation Time |
||
Line 1,457: | Line 1,511: | ||
'Access Time |
'Access Time |
||
Dim accessTime = file.LastAccessTime |
Dim accessTime = file.LastAccessTime |
||
file.LastAccessTime = accessTime.AddHours(1)</ |
file.LastAccessTime = accessTime.AddHours(1)</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 1,464: | Line 1,518: | ||
{{libheader|Wren-date}} |
{{libheader|Wren-date}} |
||
As there is currently no way for Wren-cli to get or set file modification times, we instead use an embedded script so the C host can do this for us. |
As there is currently no way for Wren-cli to get or set file modification times, we instead use an embedded script so the C host can do this for us. |
||
<syntaxhighlight lang="wren">/* File_modification_time_wren */ |
|||
<lang ecmascript>/* file_mod_time_wren */ |
|||
import "./date" for Date |
import "./date" for Date |
||
Line 1,496: | Line 1,550: | ||
} |
} |
||
st = Stat.new(fileName) // update info |
st = Stat.new(fileName) // update info |
||
System.print("File modification time changed to %(UT2Date.call(st.mtime)).")</ |
System.print("File modification time changed to %(UT2Date.call(st.mtime)).")</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this in the following C program, build and run it: |
We now embed this in the following C program, build and run it: |
||
< |
<syntaxhighlight lang="c">/* gcc File_modification_time.c -o File_modification_time -lwren -lm */ |
||
#include <sys/stat.h> |
#include <sys/stat.h> |
||
Line 1,637: | Line 1,691: | ||
WrenVM* vm = wrenNewVM(&config); |
WrenVM* vm = wrenNewVM(&config); |
||
const char* module = "main"; |
const char* module = "main"; |
||
const char* fileName = " |
const char* fileName = "File_modification_time.wren"; |
||
char *script = readFile(fileName); |
char *script = readFile(fileName); |
||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
WrenInterpretResult result = wrenInterpret(vm, module, script); |
||
Line 1,653: | Line 1,707: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,663: | Line 1,717: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">info:=File.info("input.txt"); |
||
// -->T(size,last status change time,last mod time,isDir,mode), from stat(2) |
// -->T(size,last status change time,last mod time,isDir,mode), from stat(2) |
||
info.println(); |
info.println(); |
||
Time.Date.ctime(info[2]).println(); |
Time.Date.ctime(info[2]).println(); |
||
File.setModTime("input.txt",Time.Clock.mktime(2014,2,1,0,0,0)); |
File.setModTime("input.txt",Time.Clock.mktime(2014,2,1,0,0,0)); |
||
File.info("input.txt")[2] : Time.Date.ctime(_).println();</ |
File.info("input.txt")[2] : Time.Date.ctime(_).println();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
Latest revision as of 10:23, 5 December 2023
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Get and set the modification time of a file.
Ada
Ada does not allow you to change the date of a file but you can definitely read it:
with Ada.Directories; use Ada.Directories;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
procedure File_Time_Test is
begin
Put_Line (Image (Modification_Time ("file_time_test.adb")));
end File_Time_Test;
ALGOL 68
Definition of the PROC is lifted from the Algol68 Genie documentation.
PROC get output = (STRING cmd) VOID:
IF STRING sh cmd = " " + cmd + " ; 2>&1";
STRING output;
execve output ("/bin/sh", ("sh", "-c", sh cmd), "", output) >= 0
THEN print (output) FI;
get output ("rm -rf WTC_1"); CO Ensure file doesn't exist CO
get output ("touch WTC_1"); CO Create file CO
get output ("ls -l --time-style=full-iso WTC_1"); CO Display its last modified time CO
get output ("touch -t 200109111246.40 WTC_1"); CO Change its last modified time CO
get output ("ls -l --time-style=full-iso WTC_1") CO Verify it changed CO
- Output:
-rw-r--r-- 1 pcl pcl 0 2015-08-10 17:57:50.279854695 +0100 WTC_1 -rw-r--r-- 1 pcl pcl 0 2001-09-11 12:46:40.000000000 +0100 WTC_1
AutoHotkey
FileGetTime, OutputVar, output.txt
MsgBox % OutputVar
FileSetTime, 20080101, output.txt
FileGetTime, OutputVar, output.txt
MsgBox % OutputVar
AWK
@load "filefuncs"
BEGIN {
name = "input.txt"
# display time
stat(name, fd)
printf("%s\t%s\n", name, strftime("%a %b %e %H:%M:%S %Z %Y", fd["mtime"]) )
# change time
cmd = "touch -t 201409082359.59 " name
system(cmd)
close(cmd)
}
Non-GNU awk's don't have direct access to the filesystem but can execute system-commands.
#!/bin/awk -f
BEGIN { # file modification time on Unix, using stat
fn ="input.txt"
cmd="stat " fn
print "#", cmd
system(cmd) # just execute cmd
cmd="stat -c %Y " fn # seconds since the epoch
print "#", cmd
system(cmd)
cmd="stat -c %y " fn # human-readable format
print "#", cmd
system(cmd)
print "##"
cmd | getline x # get output from cmd
#print x
close(cmd)
n=split(x,stat," ")
#for (i in stat) { print i, stat[i] }
print "file:", fn
print "date:", stat[1], "time:", stat[2]
### change filetime with touch:
cmd="touch -t 201409082359.59 " fn
print "#", cmd; system(cmd)
cmd="stat " fn
print "#", cmd; system(cmd)
}
- Output:
# stat input.txt File: `input.txt' Size: 126 Blocks: 8 IO Block: 4096 regular file Device: 700h/1792d Inode: 2195620 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 48/ apache) Gid: ( 48/ apache) Access: 2014-11-05 20:18:39.000000000 -0600 Modify: 2014-11-05 20:18:39.000000000 -0600 Change: 2014-11-05 20:18:39.000000000 -0600 # stat -c %Y input.txt 1415240319 # stat -c %y input.txt 2014-11-05 20:18:39.000000000 -0600 ## file: input.txt date: 2014-11-05 time: 20:18:39.000000000
- See also
File modification time#Batch_File, #Run_BASIC, #UNIX_Shell
Batch File
for %%f in (file.txt) do echo.%%~tf
The date/time format is dependent on the user's locale, like the contents of the %DATE%
and %TIME%
built-ins.
There is no built-in way of setting a file's modification time.
BBC BASIC
DIM ft{dwLowDateTime%, dwHighDateTime%}
DIM st{wYear{l&,h&}, wMonth{l&,h&}, wDayOfWeek{l&,h&}, \
\ wDay{l&,h&}, wHour{l&,h&}, wMinute{l&,h&}, \
\ wSecond{l&,h&}, wMilliseconds{l&,h&} }
REM File is assumed to exist:
file$ = @tmp$ + "rosetta.tmp"
REM Get and display the modification time:
file% = OPENIN(file$)
SYS "GetFileTime", @hfile%(file%), 0, 0, ft{}
CLOSE #file%
SYS "FileTimeToSystemTime", ft{}, st{}
date$ = STRING$(16, CHR$0)
time$ = STRING$(16, CHR$0)
SYS "GetDateFormat", 0, 0, st{}, 0, date$, LEN(date$) TO N%
date$ = LEFT$(date$, N%-1)
SYS "GetTimeFormat", 0, 0, st{}, 0, time$, LEN(time$) TO N%
time$ = LEFT$(time$, N%-1)
PRINT date$ " " time$
REM Set the modification time to the current time:
SYS "GetSystemTime", st{}
SYS "SystemTimeToFileTime", st{}, ft{}
file% = OPENUP(file$)
SYS "SetFileTime", @hfile%(file%), 0, 0, ft{}
CLOSE #file%
C
POSIX utime()
utime() has a precision of one second. This program would truncate the time to the last second, losing precision if the filesystem is more precise.
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <utime.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
time_t mtime;
struct utimbuf new_times;
if (stat(filename, &foo) < 0) {
perror(filename);
return 1;
}
mtime = foo.st_mtime; /* seconds since the epoch */
new_times.actime = foo.st_atime; /* keep atime unchanged */
new_times.modtime = time(NULL); /* set mtime to current time */
if (utime(filename, &new_times) < 0) {
perror(filename);
return 1;
}
return 0;
}
BSD utimes()
With BSD, utime() is obsolete. utimes() has a precision of 1 microsecond (where 1 second = 1000000 microseconds).
#include <sys/stat.h>
#include <sys/time.h>
#include <err.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
struct timeval new_times[2];
if (stat(filename, &foo) < 0)
err(1, "%s", filename);
/* keep atime unchanged */
TIMESPEC_TO_TIMEVAL(&new_times[0], &foo.st_atim);
/* set mtime to current time */
gettimeofday(&new_times[1], NULL);
if (utimes(filename, new_times) < 0)
err(1, "%s", filename);
return 0;
}
POSIX utimensat()
utimensat(2) has a precision of 1 nanosecond (where 1 second = 10**9 nanoseconds).
Program needs to be linked with -lrt
.
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
struct timespec new_times[2];
if (stat(filename, &foo) < 0) {
perror(filename);
return 1;
}
/* keep atime unchanged */
new_times[0] = foo.st_atim;
/* set mtime to current time */
clock_gettime(CLOCK_REALTIME, &new_times[1]);
if (utimensat(AT_FDCWD, filename, new_times, 0) < 0) {
perror(filename);
return 1;
}
return 0;
}
Windows
Declare FILETIME modtime;
and then use GetFileTime(fh, NULL, NULL, &modtime);
to get the file modification time, or SetFileTime(fh, NULL, NULL, &modtime);
to set it.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
/* Print "message: last Win32 error" to stderr. */
void
oops(const wchar_t *message)
{
wchar_t *buf;
DWORD error;
buf = NULL;
error = GetLastError();
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, 0, (wchar_t *)&buf, 0, NULL);
if (buf) {
fwprintf(stderr, L"%ls: %ls", message, buf);
LocalFree(buf);
} else {
/* FormatMessageW failed. */
fwprintf(stderr, L"%ls: unknown error 0x%x\n",
message, error);
}
}
int
setmodtime(const wchar_t *path)
{
FILETIME modtime;
SYSTEMTIME st;
HANDLE fh;
wchar_t date[80], time[80];
fh = CreateFileW(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
0, NULL, OPEN_EXISTING, 0, NULL);
if (fh == INVALID_HANDLE_VALUE) {
oops(path);
return 1;
}
/*
* Use GetFileTime() to get the file modification time.
*/
if (GetFileTime(fh, NULL, NULL, &modtime) == 0)
goto fail;
FileTimeToSystemTime(&modtime, &st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
time, sizeof time / sizeof time[0]) == 0)
goto fail;
wprintf(L"%ls: Last modified at %s at %s (UTC).\n",
path, date, time);
/*
* Use SetFileTime() to change the file modification time
* to the current time.
*/
GetSystemTime(&st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
time, sizeof time / sizeof time[0]) == 0)
goto fail;
SystemTimeToFileTime(&st, &modtime);
if (SetFileTime(fh, NULL, NULL, &modtime) == 0)
goto fail;
wprintf(L"%ls: Changed to %s at %s (UTC).\n", path, date, time);
CloseHandle(fh);
return 0;
fail:
oops(path);
CloseHandle(fh);
return 1;
}
/*
* Show the file modification time, and change it to the current time.
*/
int
main()
{
int argc, i, r;
wchar_t **argv;
/* MinGW never provides wmain(argc, argv). */
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv == NULL) {
oops(L"CommandLineToArgvW");
exit(1);
}
if (argc < 2) {
fwprintf(stderr, L"usage: %ls file...\n", argv[0]);
exit(1);
}
r = 0;
for (i = 1; argv[i] != NULL; i++)
if (setmodtime(argv[i])) r = 1;
return r;
}
C#
using System;
using System.IO;
Console.WriteLine(File.GetLastWriteTime("file.txt"));
File.SetLastWriteTime("file.txt", DateTime.Now);
C++
compiled with g++ -lboost_filesystem <sourcefile> -o <destination file>
#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>
int main( int argc , char *argv[ ] ) {
if ( argc != 2 ) {
std::cerr << "Error! Syntax: moditime <filename>!\n" ;
return 1 ;
}
boost::filesystem::path p( argv[ 1 ] ) ;
if ( boost::filesystem::exists( p ) ) {
std::time_t t = boost::filesystem::last_write_time( p ) ;
std::cout << "On " << std::ctime( &t ) << " the file " << argv[ 1 ]
<< " was modified the last time!\n" ;
std::cout << "Setting the modification time to now:\n" ;
std::time_t n = std::time( 0 ) ;
boost::filesystem::last_write_time( p , n ) ;
t = boost::filesystem::last_write_time( p ) ;
std::cout << "Now the modification time is " << std::ctime( &t ) << std::endl ;
return 0 ;
} else {
std::cout << "Could not find file " << argv[ 1 ] << '\n' ;
return 2 ;
}
}
Clojure
(import '(java.io File)
'(java.util Date))
(Date. (.lastModified (File. "output.txt")))
(Date. (.lastModified (File. "docs")))
(.setLastModified (File. "output.txt")
(.lastModified (File. "docs")))
Common Lisp
Common Lisp doesn't provide a way to set the modification time. The write date can be accessed, however, using file-write-date:
(file-write-date "input.txt")
Implementations may, in addition, provide other ways of accessing these file attributes, e.g., POSIX bindings:
(1.0.30, OS X, Intel),
(sb-posix:utime
takes an access time and a modification time rather than an array of two times.)
(let* ((filename "input.txt")
(stat (sb-posix:stat filename))
(mtime (sb-posix:stat-mtime stat)))
(sb-posix:utime filename
(sb-posix:stat-atime stat)
(sb-posix:time)))
D
import std.stdio;
import std.file: getTimes, setTimes, SysTime;
void main() {
auto fname = "unixdict.txt";
SysTime fileAccessTime, fileModificationTime;
getTimes(fname, fileAccessTime, fileModificationTime);
writeln(fileAccessTime, "\n", fileModificationTime);
setTimes(fname, fileAccessTime, fileModificationTime);
}
For Windows systems there is also getTimesWin(), that gives the file creation time too.
Delphi
function GetModifiedDate(const aFilename: string): TDateTime;
var
hFile: Integer;
iDosTime: Integer;
begin
hFile := FileOpen(aFilename, fmOpenRead);
iDosTime := FileGetDate(hFile);
FileClose(hFile);
if (hFile = -1) or (iDosTime = -1) then raise Exception.Create('Cannot read file: ' + sFilename);
Result := FileDateToDateTime(iDosTime);
end;
procedure ChangeModifiedDate(const aFilename: string; aDateTime: TDateTime);
begin
FileSetDate(aFileName, DateTimeToFileDate(aDateTime));
end;
E
E follows the Java File interface, except that it replaces boolean success/failure returns with an ejector parameter, so that the default behavior if the client does not handle the case is not to continue ignoring the failure.
def strdate(date) {
return E.toString(<unsafe:java.util.makeDate>(date))
}
def test(type, file) {
def t := file.lastModified()
println(`The following $type called ${file.getPath()} ${
if (t == 0) { "does not exist." } else { `was modified at ${strdate(t)}` }}`)
println(`The following $type called ${file.getPath()} ${
escape ne { file.setLastModified(timer.now(), ne); "was modified to current time." } catch _ { "does not exist." }}`)
println(`The following $type called ${file.getPath()} ${
escape ne { file.setLastModified(t, ne); "was modified to previous time." } catch _ { "does not exist." }}`)
}
test("file", <file:output.txt>)
test("directory", <file:docs>)
Elixir
iex(1)> info = File.stat!("input.txt") %File.Stat{access: :read_write, atime: {{2015, 10, 29}, {20, 44, 28}}, ctime: {{2015, 9, 20}, {9, 5, 58}}, gid: 0, inode: 0, links: 1, major_device: 3, minor_device: 0, mode: 33206, mtime: {{2015, 10, 29}, {20, 44, 28}}, size: 45, type: :regular, uid: 0} iex(2)> info.mtime {{2015, 10, 29}, {20, 44, 28}} iex(3)> File.touch!("input.txt") :ok iex(4)> File.stat!("input.txt") %File.Stat{access: :read_write, atime: {{2016, 3, 7}, {23, 12, 35}}, ctime: {{2016, 3, 7}, {23, 12, 35}}, gid: 0, inode: 0, links: 1, major_device: 3, minor_device: 0, mode: 33206, mtime: {{2016, 3, 7}, {23, 12, 35}}, size: 45, type: :regular, uid: 0}
Emacs 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
set-file-times
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 file-attributes
and set-file-times
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. set-file-times
from within Emacs doesn't update those buffer times and so looks like an external change.
Erlang
-module( file_modification_time ).
-include_lib("kernel/include/file.hrl").
-export( [task/0] ).
task() ->
File = "input.txt",
{ok, File_info} = file:read_file_info( File ),
io:fwrite( "Modification time ~p~n", [File_info#file_info.mtime] ),
ok = file:write_file_info( File, File_info#file_info{mtime=calendar:local_time()} ).
F#
open System
open System.IO
[<EntryPoint>]
let main args =
Console.WriteLine(File.GetLastWriteTime(args.[0]))
File.SetLastWriteTime(args.[0], DateTime.Now)
0
Factor
"foo.txt" file-info modified>> .
Setting the modified time is not cross-platform, so here's a Unix version.
USE: io.files.info.unix
"foo.txt" now 2 hours time+ set-file-modified-time
Fortran
No facility. The INQUIRE statement for files has no keywords to ascertain the disc file's creation time, last access time, last modification time, and much else also in a standardised way so there is no way to modify these items either. A particular installation may offer library routines (written in assembler perhaps), or a modified compiler with extra features, but in general, there is no facility - other than performing a file action at the desired time.
FreeBASIC
' FB 1.05.0 Win64
' This example is taken directly from the FB documentation (see [http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFiledatetime])
#include "vbcompat.bi" '' to use Format function
Dim filename As String, d As Double
Print "Enter a filename: "
Line Input filename
If FileExists(filename) Then
Print "File last modified: ";
d = FileDateTime( filename )
Print Format( d, "yyyy-mm-dd hh:mm AM/PM" )
Else
Print "File not found"
End If
Sleep
Sample input/output:
- Output:
Enter a filename: c:\windows\system32\kernel32.dll File last modified: 2016-09-07 05:39 AM
Frink
Frink runs on a Java Virtual Machine (JVM) and can use any of its methods. The underlying code returns the modification time as the number of milliseconds since 1970-01-01 00:00 UTC. The following converts between Frink's human-friendly time systems and the milliseconds since the epoch.
f = newJava["java.io.File", "FileModificationTime.frink"]
epoch = #1970 UTC#
f.setLastModified[(#2022-01-01 5:00 AM# - epoch) / ms]
println[f.lastModified[] ms + epoch]
- Output:
AD 2022-01-01 AM 05:00:00.000 (Sat) Mountain Standard Time
FutureBasic
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
CFURLRef url = fn URLByAppendingPathComponent( desktopURL, @"file.txt" )
CFDictionaryRef dict = fn FileManagerAttributesOfItemAtURL( url )
print dict[@NSFileModificationDate] // Read file's current date
// Reset file date to current
fn FileManagerSetAttributesOfItemAtURL( CFURLRef url, @{NSFileModificationDate:fn DateNow )
Gambas
' There is no built in command in Gambas to 'set' the modification time of a file
' A shell call to 'touch' would do it
Public Sub Main()
Dim stInfo As Stat = Stat(User.home &/ "Rosetta.txt")
Print "Rosetta.txt was last modified " & Format(stInfo.LastModified, "dd/mm/yyy hh:nn:ss")
End
Output:
Rosetta.txt was last modified 28/05/2017 16:39:39
Go
package main
import (
"fmt"
"os"
"syscall"
"time"
)
var filename = "input.txt"
func main() {
foo, err := os.Stat(filename)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("mod time was:", foo.ModTime())
mtime := time.Now()
atime := mtime // a default, because os.Chtimes has an atime parameter.
// but see if there's a real atime that we can preserve.
if ss, ok := foo.Sys().(*syscall.Stat_t); ok {
atime = time.Unix(ss.Atim.Sec, ss.Atim.Nsec)
}
os.Chtimes(filename, atime, mtime)
fmt.Println("mod time now:", mtime)
}
GUISS
In Graphical User Interface Support Script, we can can only use facilities that the underlying user interface provides, so we can display file timestamps, but cannot alter them. In this example, we get the date and timestamp for the file Foobar.txt.
Start,My Documents,Rightclick:Icon:Foobar.txt,Properties
Haskell
import System.Posix.Files
import System.Posix.Time
do status <- getFileStatus filename
let atime = accessTime status
mtime = modificationTime status -- seconds since the epoch
curTime <- epochTime
setFileTimes filename atime curTime -- keep atime unchanged
-- set mtime to current time
Alternative (only gets modification time):
import System.Directory
import System.Time
do ct <- getModificationTime filename
cal <- toCalendarTime ct
putStrLn (calendarTimeToString cal)
HicEst
CHARACTER timestamp*18
timestamp = ' ' ! blank timestamp will read:
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp) ! 20100320141940.525
timestamp = '19991231235950' ! set timestamp to Millenium - 10 seconds
SYSTEM(FIle="File_modification_time.hic", FileTime=timestamp)
Icon and Unicon
Icon doesn't support 'stat' or 'utime'; however, information can be obtained by use of the system function to access command line.
every dir := !["./","/"] do {
if i := stat(f := dir || "input.txt") then {
write("info for ",f ," mtime= ",ctime(i.mtime),", atime=",ctime(i.ctime), ", atime=",ctime(i.atime))
utime(f,i.atime,i.mtime-1024)
i := stat(f)
write("update for ",f ," mtime= ",ctime(i.mtime),", atime=",ctime(i.ctime), ", atime=",ctime(i.atime))
}
else stop("failure to stat ",f)
}
Notes:
- Icon and Unicon accept both / and \ for directory separators.
- 'ctime' formats an integer representing an epoch time into human readable form
- 'utime' updates the atime and mtime values
J
The standard file access library only supports reading the file modification time.
load 'files'
fstamp 'input.txt'
2009 8 24 20 34 30
It is possible to set the time but it has to be done through OS specific external calls.
Java
public static void main(String[] args) {
File file = new File("file.txt");
/* get */
/* returns '0' if the file does not exist */
System.out.printf("%tD %1$tT%n", file.lastModified());
/* set */
file.setLastModified(System.currentTimeMillis());
}
05/11/23 11:56:58
An alternate demonstration
import java.io.File;
import java.util.Date;
public class FileModificationTimeTest {
public static void test(String type, File file) {
long t = file.lastModified();
System.out.println("The following " + type + " called " + file.getPath() +
(t == 0 ? " does not exist." : " was modified at " + new Date(t).toString() )
);
System.out.println("The following " + type + " called " + file.getPath() +
(!file.setLastModified(System.currentTimeMillis()) ? " does not exist." : " was modified to current time." )
);
System.out.println("The following " + type + " called " + file.getPath() +
(!file.setLastModified(t) ? " does not exist." : " was modified to previous time." )
);
}
public static void main(String args[]) {
test("file", new File("output.txt"));
test("directory", new File("docs"));
}
}
JavaScript
Get only.
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile('input.txt');
var mtime = f.DateLastModified;
The following works in all browsers, including IE10.
var file = document.getElementById("fileInput").files.item(0);
var last_modified = file.lastModifiedDate;
Jsish
Getting the mod time is a builtin. Setting the time is currently not, so touch is used here.
A C extension could provide access to the utime system call if desired.
/* File modification times, in Jsi */
var fileTime = File.mtime('fileModificationTime.jsi');
puts("Last mod time was: ", strftime(fileTime * 1000));
exec('touch fileModificationTime.jsi');
fileTime = File.mtime('fileModificationTime.jsi');
puts("Mod time now: ", strftime(fileTime * 1000));
- Output:
prompt$ jsish fileModificationTime.jsi Last mod time was: 2019-05-08 13:21:10 Mod time now: 2019-05-08 13:23:35
Julia
using Dates
fname, _ = mktemp()
println("The modification time of $fname is ", Dates.unix2datetime(mtime(fname)))
println("\nTouch this file.")
touch(fname)
println("The modification time of $fname is now ", Dates.unix2datetime(mtime(fname)))
Kotlin
// version 1.0.6
import java.io.File
fun main(args: Array<String>) {
val filePath = "input.txt" // or whatever
val file = File(filePath)
with (file) {
println("%tc".format(lastModified()))
// update to current time, say
setLastModified(System.currentTimeMillis())
println("%tc".format(lastModified()))
}
}
Lang
# Load the IO module
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">"
ln.loadModule(<pathToIO.lm>)
$file = [[io]]::fp.openFile(input.txt)
$modTime = [[io]]::fp.getModificationDate($file)
fn.println(Mod Time: $modTime)
[[io]]::fp.setModificationDate($file, fn.currentTimeMillis())
$modTime = [[io]]::fp.getModificationDate($file)
fn.println(Mod Time now: $modTime)
[[io]]::fp.closeFile($file)
Lasso
Note this is retrieve only.
File modification date is updated on save of file.
local(f) = file('input.txt')
handle => { #f->close }
#f->modificationDate->format('%-D %r')
// result: 12/2/2010 11:04:15 PM
Lua
require "lfs"
local attributes = lfs.attributes("input.txt")
if attributes then
print(path .. " was last modified " .. os.date("%c", attributes.modification) .. ".")
-- set access and modification time to now ...
lfs.touch("input.txt")
-- ... or set modification time to now, keep original access time
lfs.touch("input.txt", attributes.access, os.time())
else
print(path .. " does not exist.")
end
M2000 Interpreter
Most of the file statements/functions use current directory as path.
Module CheckIt {
\\ without *for wide output* we open for ANSI (1 byte per character)
\\ but here we need it only for the creation of a file
Open "afile" for output as #f
Close #f
Print file.stamp("afile") 'it is a number in VB6 date format.
date k=file.stamp("afile", -2) // Version 12 has date type, convert number or string to date type
print k // this has type date but print as string using default system locale date and time format
\\ day format as for Greece
Print Str$(File.Stamp("afile"),"hh:nn:ss dd/mm/yyyy") , "utc write time - by default"
Print Str$(File.Stamp("afile" ,1),"hh:nn:ss dd/mm/yyyy") , "utc write time, 1"
Print Str$(File.Stamp("afile" ,-1),"hh:nn:ss dd/mm/yyyy"), "local write time, -1"
Print Str$(File.Stamp("afile" ,2),"hh:nn:ss dd/mm/yyyy"), "utc creation time, 2"
Print Str$(File.Stamp("afile" ,-2),"hh:nn:ss dd/mm/yyyy"), "local creation time, -2"
}
Checkit
Mathematica / Wolfram Language
Get file modification time:
FileDate["file","Modification"]
results is returned in format: {year,month,day,hour,minute,second}. Setting file modification time:
SetFileDate["file",date,"Modification"]
where date is specified as {year,month,day,hour,minute,second}.
MATLAB / Octave
f = dir('output.txt'); % struct f contains file information
f.date % is string containing modification time
f.datenum % numerical format (number of days)
datestr(f.datenum) % is the same as f.date
% see also: stat, lstat
Modifying of file access time can be done through system calls:
system('touch -t 201002032359.59 output.txt');
MAXScript
MAXScript has no method to set the mod time
-- Returns a string containing the mod date for the file, e.g. "1/29/99 1:52:05 PM"
getFileModDate "C:\myFile.txt"
Modula-3
MODULE ModTime EXPORTS Main;
IMPORT IO, Fmt, File, FS, Date, OSError;
TYPE dateArray = ARRAY [0..5] OF TEXT;
VAR
file: File.Status;
date: Date.T;
PROCEDURE DateArray(date: Date.T): dateArray =
BEGIN
RETURN
dateArray{Fmt.Int(date.year), Fmt.Int(ORD(date.month) + 1), Fmt.Int(date.day),
Fmt.Int(date.hour), Fmt.Int(date.minute), Fmt.Int(date.second)};
END DateArray;
BEGIN
TRY
file := FS.Status("test.txt");
date := Date.FromTime(file.modificationTime);
IO.Put(Fmt.FN("%s-%02s-%02s %02s:%02s:%02s", DateArray(date)));
IO.Put("\n");
EXCEPT
| OSError.E => IO.Put("Error: Failed to get file status.\n");
END;
END ModTime.
- Output:
2011-07-14 14:12:46
This program sets the modification time to any value we wish:
MODULE SetModTime EXPORTS Main;
IMPORT Date, FS;
<*FATAL ANY*>
VAR
date: Date.T;
BEGIN
(* Set the modification time to January 1st, 1999 *)
date.year := 1999;
date.month := Date.Month.Jan;
date.day := 1;
date.hour := 0;
date.minute := 0;
date.second := 0;
date.offset := 21601;
date.zone := "CST";
FS.SetModificationTime("test.txt", Date.ToTime(date));
END SetModTime.
We can see the output with the Unix command stat
- Output:
stat test.txt | grep Mod Modify: 1999-01-01 00:00:01.000000000 -0600
NetRexx
/* NetRexx */
options replace format comments java crossref symbols binary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
parse arg fileName
if fileName = '' then fileName = 'data/tempfile01'
mfile = File(fileName)
mtime = mfile.lastModified()
dtime = Date(mtime).toString()
say 'File' fileName 'last modified at' dtime
say
if mfile.setLastModified(0) then do
dtime = Date(mfile.lastModified()).toString()
say 'File modification time altered...'
say 'File' fileName 'now last modified at' dtime
say
say 'Resetting...'
mfile.setLastModified(mtime)
dtime = Date(mfile.lastModified()).toString()
say 'File' fileName 'reset to last modified at' dtime
end
else do
say 'Unable to modify time for file' fileName
end
return
- Output:
File data/tempfile01 last modified at Fri May 24 14:45:55 PDT 2013 File modification time altered... File data/tempfile01 now last modified at Wed Dec 31 16:00:00 PST 1969 Resetting... File data/tempfile01 reset to last modified at Fri May 24 14:45:55 PDT 2013
NewLISP
;; print modification time
(println (date (file-info "input.txt" 6)))
;; set modification time to now (Unix)
(! "touch -m input.txt")
Nim
import os, strutils, times
if paramCount() == 0: quit(QuitSuccess)
let fileName = paramStr(1)
# Get and display last modification time.
var mtime = fileName.getLastModificationTime()
echo "File \"$1\" last modification time: $2".format(fileName, mtime.format("YYYY-MM-dd HH:mm:ss"))
# Change last modification time to current time.
fileName.setLastModificationTime(now().toTime())
Objeck
Get only
use System.IO.File;
class Program {
function : Main(args : String[]) ~ Nil {
File->ModifiedTime("file_mod.obs")->ToString()->PrintLine();
}
}
Objective-C
NSFileManager *fm = [NSFileManager defaultManager];
// Pre-OS X 10.5
NSLog(@"%@", [[fm fileAttributesAtPath:@"input.txt" traverseLink:YES] fileModificationDate]);
[fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
atPath:@"input.txt"];
// OS X 10.5+
NSLog(@"%@", [[fm attributesOfItemAtPath:@"input.txt" error:NULL] fileModificationDate]);
[fm setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
ofItemAtPath:@"input.txt" error:NULL];
OCaml
#load "unix.cma";;
open Unix;;
let mtime = (stat filename).st_mtime;; (* seconds since the epoch *)
utimes filename (stat filename).st_atime (time ());;
(* keep atime unchanged
set mtime to current time *)
Oforth
Get only :
File new("myfile.txt") modified
OpenEdge/Progress
The file modified time can only be read.
FILE-INFO:FILE-NAME = 'c:/temp'.
MESSAGE
STRING( FILE-INFO:FILE-MOD-TIME, 'HH:MM:SS' )
VIEW-AS ALERT-BOX
Oz
Getting the modification time:
declare
[Path] = {Module.link ['x-oz://system/os/Path.ozf']}
Modified = {Path.mtime "input.txt"} %% posix time
in
{Show {OsTime.localtime Modified}} %% human readable record
Setting the modification time is not possible, unless implemented as an extension in C or C++.
Pascal
See Delphi
Perl
my $mtime = (stat($file))[9]; # seconds since the epoch
# you should use the more legible version below:
use File::stat qw(stat);
my $mtime = stat($file)->mtime; # seconds since the epoch
utime(stat($file)->atime, time, $file);
# keep atime unchanged
# set mtime to current time
Phix
without js -- file i/o -- (however get as per the JavaScript entry above might be doable if needed) constant filename = "test.txt" ?get_file_date(filename) include timedate.e ?format_timedate(get_file_date(filename)) bool res = set_file_date(filename) ?format_timedate(get_file_date(filename))
- Output:
{2019,5,2,14,46,40} "2:46pm Thursday May 02nd, 2019" "12:25pm Wednesday May 08nd, 2019"
PHP
<?php
$filename = 'input.txt';
$mtime = filemtime($filename); // seconds since the epoch
touch($filename,
time(), // set mtime to current time
fileatime($filename)); // keep atime unchanged
?>
PicoLisp
(let File "test.file"
(and
(info File)
(prinl (stamp (cadr @) (cddr @))) ) # Print date and time in UTC
(call 'touch File) ) # Set modification time to "now"
- Output:
2010-02-20 15:46:37
Pop11
;;; Print modification time (seconds since Epoch)
sysmodtime('file') =>
PowerShell
$modificationTime = (Get-ChildItem file.txt).LastWriteTime
Set-ItemProperty file.txt LastWriteTime (Get-Date)
$LastReadTime = (Get-ChildItem file.txt).LastAccessTime
Set-ItemProperty file.txt LastAccessTime(Get-Date)
$CreationTime = (Get-ChildItem file.txt).CreationTime
Set-ItemProperty file.txt CreationTime(Get-Date)
You can also use alternates to get UTC time: LastWriteTimeUtc LastAccessTimeUtc CreationTimeUtc
PureBasic
Debug FormatDate("%yyyy/%mm/%dd", GetFileDate("file.txt",#PB_Date_Modified))
SetFileDate("file.txt",#PB_Date_Modified,Date(1987, 10, 23, 06, 43, 15))
Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate("file.txt",#PB_Date_Modified))
Python
import os
#Get modification time:
modtime = os.path.getmtime('filename')
#Set the access and modification times:
os.utime('path', (actime, mtime))
#Set just the modification time:
os.utime('path', (os.path.getatime('path'), mtime))
#Set the access and modification times to the current time:
os.utime('path', None)
R
See this R-devel mailing list thread for more information.
# Get the value
file.info(filename)$mtime
#To set the value, we need to rely on shell commands. The following works under windows.
shell("copy /b /v filename +,,>nul")
# and on Unix (untested)
shell("touch -m filename")
Racket
#lang racket
(file-or-directory-modify-seconds "foo.rkt")
Raku
(formerly Perl 6)
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);
}
Sets the last access time to now, while restoring the modification time to what it was before.
RapidQ
name$ = DIR$("input.txt", 0)
PRINT "File date: "; FileRec.Date
PRINT "File time: "; FileRec.Time
REALbasic
The Date object has properties which correspond to various date formats such as SQLDateTime (YYYY-MM-DD HH:MM:SS), DayOfWeek, DayOfYear, and TotalSeconds since 12:00AM, January 1, 1904, among others.
Function getModDate(f As FolderItem) As Date
Return f.ModificationDate
End Function
REXX
The REXX language has no facility to update a file's modification time.
/*REXX program obtains and displays a file's time of modification. */
parse arg $ . /*obtain required argument from the CL.*/
if $=='' then do; say "***error*** no filename was specified."; exit 13; end
q=stream($, 'C', "QUERY TIMESTAMP") /*get file's modification time info. */
if q=='' then q="specified file doesn't exist." /*set an error indication message. */
say 'For file: ' $ /*display the file ID information. */
say 'timestamp of last modification: ' q /*display the modification time info. */
/*stick a fork in it, we're all done. */
output when using the file ID for this particular REXX program (source file): FILEMTIME.REX
For file: FILEMTIM.REX timestamp of last modification: 2015-05-30 14:22:26
Ring
load "stdlib.ring"
see GetFileInfo( "test.ring" )
func GetFileInfo cFile
cOutput = systemcmd("dir /T:W " + cFile )
aList = str2list(cOutput)
cLine = aList[6]
aInfo = split(cLine," ")
return aInfo
Output:
2017. 08. 27. 10:31 189 test.ring
Ruby
#Get modification time:
modtime = File.mtime('filename')
#Set the access and modification times:
File.utime(actime, mtime, 'path')
#Set just the modification time:
File.utime(File.atime('path'), mtime, 'path')
#Set the access and modification times to the current time:
File.utime(nil, nil, 'path')
Run BASIC
files #f, DefaultDir$ + "\*.*" ' all files in the default directory
print "hasanswer: ";#f HASANSWER() ' does it exist
print "rowcount: ";#f ROWCOUNT() ' number of files in the directory
print '
#f DATEFORMAT("mm/dd/yy") ' set format of file date to template given
#f TIMEFORMAT("hh:mm:ss") ' set format of file time to template given
for i = 1 to #f rowcount() ' loop through the files
if #f hasanswer() then ' or loop with while #f hasanswer()
print "nextfile info: ";#f nextfile$(" ") ' set the delimiter for nextfile info
print "name: ";name$ ' file name
print "size: ";#f SIZE() ' file size
print "date: ";#f DATE$() ' file date
print "time: ";#f TIME$() ' file time
print "isdir: ";#f ISDIR() ' is it a directory or file
name$ = #f NAME$()
shell$("touch -t 201002032359.59 ";name$;""") ' shell to os to set date
print
end if
next
Rust
use std::fs;
fn main() -> std::io::Result<()> {
let metadata = fs::metadata("foo.txt")?;
if let Ok(time) = metadata.accessed() {
println!("{:?}", time);
} else {
println!("Not supported on this platform");
}
Ok(())
}
Scala
import java.io.File
import java.util.Date
object FileModificationTime extends App {
def test(file: File) {
val (t, init) = (file.lastModified(),
s"The following ${if (file.isDirectory()) "directory" else "file"} called ${file.getPath()}")
println(init + (if (t == 0) " does not exist." else " was modified at " + new Date(t).toInstant()))
println(init +
(if (file.setLastModified(System.currentTimeMillis())) " was modified to current time." else " does not exist."))
println(init +
(if (file.setLastModified(t)) " was reset to previous time." else " does not exist."))
}
// main
List(new File("output.txt"), new File("docs")).foreach(test)
}
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;
Sidef
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);
Slate
Modifying the timestamp value is not currently a built-in feature. This code gets a raw value:
slate[1]> (File newNamed: 'LICENSE') fileInfo modificationTimestamp.
1240349799
Smalltalk
|a|
a := File name: 'input.txt'.
(a lastModifyTime) printNl.
Standard ML
val mtime = OS.FileSys.modTime filename; (* returns a Time.time data structure *)
(* unfortunately it seems like you have to set modification & access times together *)
OS.FileSys.setTime (filename, NONE); (* sets modification & access time to now *)
(* equivalent to: *)
OS.FileSys.setTime (filename, SOME (Time.now ()))
Tcl
Assuming that the variable filename holds the name of the file...
# Get the modification time:
set timestamp [file mtime $filename]
# Set the modification time to ‘now’:
file mtime $filename [clock seconds]
TUSCRIPT
TUSCRIPT does not allow to modify/set the timestamp of a file, but it is able to read it:
$$ MODE TUSCRIPT
file="rosetta.txt"
ERROR/STOP OPEN (file,READ,-std-)
modified=MODIFIED (file)
PRINT "file ",file," last modified: ",modified
- Output:
file rosetta.txt last modified: 2011-12-14 23:50:48
UNIX Shell
There are several ways to handle files' timestamps in most *nix systems. For these examples, we'll assume you need to retrieve the timestamp to a variable. These examples use stat and touch from the GNU Core Utilities, under bash, but they should work with any POSIX-compliant implementation under any sh-compatible shell.
For all of these examples, $F
is a variable holding the filename to examine, while T
(and $T
) is the timestamp.
To get the timestamp in seconds since the epoch:
T=`stat -c %Y $F`
To get the timestamp in human-readable format:
T=`stat -c %y $F`
Note that the only difference between the above examples is capital Y vs lower-case y.
In the following examples, the "-c" argument to touch tells it to not create any files that don't already exist.
To set file F to time T, where T is in a human-readable format:
# Note the quotation marks -- very important!
T="2000-01-01 01:02:03.040506070 -0800"
touch -c -d "$T" $F
To set file F to time T, where T is in the format [[CC]YY]MMDDhhmm[.ss] (the square brackets mark optional parts):
T=200102030405.06
touch -c -t $T $F
If the year is left out, the current year is used. If the seconds are left out, 0 (zero) is used. Leaving out the optional parts of the above results in this:
T=02030405
touch -c -t $T $F
If no time is specified, then the timestamp is set to the current time:
touch -c $F
There are, of course, other ways to do both tasks -- for example, one could use ls -l --time-style=full-iso
and then process the resulting list with some other tool (e.g. awk or Perl).
Ursa
decl java.util.Date d
decl file f
f.open "example.txt"
d.setTime (f.lastmodified)
out d endl console
f.setlastmodified 10
VBScript
VBScript provides no way to set the last modification time. It does allow you to retrieve it, however.
WScript.Echo CreateObject("Scripting.FileSystemObject").GetFile("input.txt").DateLastModified
Vedit macro language
Display file's last modification time as number of seconds since midnight.
Num_Type(File_Stamp_Time("input.txt"))
Displays file's last modification date and time as a string.
File_Stamp_String(10, "input.txt")
Reg_Type(10)
Vedit Macro Language has Blog in SEO no method to set the modification time
Visual Basic .NET
Platform: .NET
Dim file As New IO.FileInfo("test.txt")
'Creation Time
Dim createTime = file.CreationTime
file.CreationTime = createTime.AddHours(1)
'Write Time
Dim writeTime = file.LastWriteTime
file.LastWriteTime = writeTime.AddHours(1)
'Access Time
Dim accessTime = file.LastAccessTime
file.LastAccessTime = accessTime.AddHours(1)
Wren
As there is currently no way for Wren-cli to get or set file modification times, we instead use an embedded script so the C host can do this for us.
/* File_modification_time_wren */
import "./date" for Date
foreign class Stat {
construct new(fileName) {}
foreign atime
foreign mtime
}
foreign class Utimbuf {
construct new(actime, modtime) {}
foreign utime(fileName)
}
// gets a Date object from a Unix time in seconds
var UT2Date = Fn.new { |ut| Date.unixEpoch.addSeconds(ut) }
Date.default = "yyyy|-|mm|-|dd| |hh|:|MM|:|ss" // default format for printing
var fileName = "temp.txt"
var st = Stat.new(fileName)
System.print("'%(fileName)' was last modified on %(UT2Date.call(st.mtime)).")
var utb = Utimbuf.new(st.atime, 0) // atime unchanged, mtime = current time
if (utb.utime(fileName) < 0) {
System.print("There was an error changing the file modification time.")
return
}
st = Stat.new(fileName) // update info
System.print("File modification time changed to %(UT2Date.call(st.mtime)).")
We now embed this in the following C program, build and run it:
/* gcc File_modification_time.c -o File_modification_time -lwren -lm */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utime.h>
#include "wren.h"
/* Stat functions */
void Stat_allocate(WrenVM* vm) {
struct stat *pStat = (struct stat*)wrenSetSlotNewForeign(vm, 0, 0, sizeof(struct stat));
const char *filename = wrenGetSlotString(vm, 1);
if (stat(filename, pStat) < 0) {
perror(filename);
exit(1);
}
}
void Stat_atime(WrenVM* vm) {
struct stat *pStat = (struct stat*)wrenGetSlotForeign(vm, 0);
wrenSetSlotDouble(vm, 0, (double)pStat->st_atime);
}
void Stat_mtime(WrenVM* vm) {
struct stat *pStat = (struct stat*)wrenGetSlotForeign(vm, 0);
wrenSetSlotDouble(vm, 0, (double)pStat->st_mtime);
}
/* Utimbuf functions */
void Utimbuf_allocate(WrenVM* vm) {
struct utimbuf *pUtimbuf = (struct utimbuf*)wrenSetSlotNewForeign(vm, 0, 0, sizeof(struct utimbuf));
time_t actime = (time_t)wrenGetSlotDouble(vm, 1);
if (!actime) actime = time(NULL);
pUtimbuf->actime = actime;
time_t modtime = (time_t)wrenGetSlotDouble(vm, 2);
if (!modtime) modtime = time(NULL);
pUtimbuf->modtime = modtime;
}
void Utimbuf_utime(WrenVM* vm) {
const struct utimbuf *pUtimbuf = (const struct utimbuf*)wrenGetSlotForeign(vm, 0);
const char *filename = wrenGetSlotString(vm, 1);
int res = utime(filename, pUtimbuf);
wrenSetSlotDouble(vm, 0, (double)res);
}
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Stat") == 0) {
methods.allocate = Stat_allocate;
} else if (strcmp(className, "Utimbuf") == 0) {
methods.allocate = Utimbuf_allocate;
}
}
return methods;
}
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Stat") == 0) {
if(!isStatic && strcmp(signature, "atime") == 0) return Stat_atime;
if(!isStatic && strcmp(signature, "mtime") == 0) return Stat_mtime;
} else if (strcmp(className, "Utimbuf") == 0) {
if(!isStatic && strcmp(signature, "utime(_)") == 0) return Utimbuf_utime;
}
}
return NULL;
}
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
size_t ret = fread(script, 1, fsize, f);
if (ret != fsize) printf("Error reading %s\n", fileName);
fclose(f);
script[fsize] = 0;
return script;
}
static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
if( result.source) free((void*)result.source);
}
WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
WrenLoadModuleResult result = {0};
if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
result.onComplete = loadModuleComplete;
char fullName[strlen(name) + 6];
strcpy(fullName, name);
strcat(fullName, ".wren");
result.source = readFile(fullName);
}
return result;
}
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
config.loadModuleFn = &loadModule;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "File_modification_time.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}
- Output:
Sample output:
'temp.txt' was last modified on 2021-12-05 14:14:30. File modification time changed to 2021-12-05 14:23:42.
zkl
info:=File.info("input.txt");
// -->T(size,last status change time,last mod time,isDir,mode), from stat(2)
info.println();
Time.Date.ctime(info[2]).println();
File.setModTime("input.txt",Time.Clock.mktime(2014,2,1,0,0,0));
File.info("input.txt")[2] : Time.Date.ctime(_).println();
- Output:
L(391,1394910932,1394910932,False,33156) Sat Mar 15 12:15:32 2014 Sat Feb 1 00:00:00 2014
- Programming Tasks
- File modification time
- Date and time
- Befunge/Omit
- EasyLang/Omit
- HTML/Omit
- Locomotive Basic/Omit
- M4/Omit
- Maxima/Omit
- PARI/GP/Omit
- Retro/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Axe/Omit
- ZX Spectrum Basic/Omit
- Ada
- ALGOL 68
- AutoHotkey
- AWK
- Batch File
- BBC BASIC
- C
- POSIX
- BSD libc
- Win32
- C sharp
- C++
- Clojure
- Common Lisp
- D
- Delphi
- E
- Elixir
- Emacs Lisp
- Erlang
- F Sharp
- Factor
- Fortran
- FreeBASIC
- Frink
- FutureBasic
- Gambas
- Go
- GUISS
- Haskell
- HicEst
- Icon
- Unicon
- J
- Java
- JavaScript
- Jsish
- Julia
- Kotlin
- Lang
- Lang-io-module
- Lasso
- Lua
- M2000 Interpreter
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- MAXScript
- Modula-3
- NetRexx
- NewLISP
- Nim
- Objeck
- Objective-C
- OCaml
- Oforth
- OpenEdge/Progress
- Oz
- Pascal
- Perl
- Phix
- PHP
- PicoLisp
- Pop11
- PowerShell
- PureBasic
- Python
- R
- Racket
- Raku
- RapidQ
- REALbasic
- REXX
- Ring
- Ruby
- Run BASIC
- Rust
- Scala
- Seed7
- Sidef
- Slate
- Smalltalk
- Standard ML
- Tcl
- TUSCRIPT
- UNIX Shell
- Ursa
- VBScript
- Vedit macro language
- Visual Basic .NET
- Wren
- Wren-date
- Zkl