File Modification Time

From Rosetta Code

Jump to: navigation, search

Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.

Code examples should be formatted along the lines of one of the existing prototypes.

This task will attempt to get and set the modification time of a file.

Contents

[edit] Ada

Ada(well GNAT with its standard libraries) Does not allow you to change the date of a file but you can definitely read it.

 with gnat.OS_lib;           use gnat.os_lib;
 with ada.text_io;           use ada.text_io;
 with Ada.Integer_Text_IO;   use Ada.Integer_Text_IO;
 procedure timeTest is
    t : OS_Time ;
    procedure put_os_time(t : OS_Time) is
    begin
       put( GM_Hour(t) , 2 );
       put(":");
       put( GM_Minute(t) , 2 );
       put(":");
       put( GM_Second(t) , 2 );
       put(" ");
       put( GM_Day(t) , 2 );
       put("-");
       put( GM_Month(t) , 2 );
       put("-");
       put( GM_Year(t) , 2 );
       new_line;          
    end;
 begin
    t := File_Time_Stamp("ada.bat");
    put_os_time (t);
 end;

[edit] C

POSIX:

#include <time.h>
#include <utime.h>
#include <sys/stat.h>

const char *filename = "input.txt";

int main() {
  struct stat foo;
  time_t mtime;
  struct utimbuf new_times;

  stat(filename, &foo);
  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 */
  utime(filename, &new_times);

  return 0;
}

[edit] Java

import java.util.File;
import java.util.Time;
import java.util.Date;
public class FileModicationTimeTest {
   // if 0L is returned then the file does not exist
   public static long getFileModificationTime(String filename)
   {
       // Get the last modified time
       long modifiedTime = new File(filename).lastModified();
       return modifiedTime;
   }
   public static boolean setFileModificationTimeToNow(String filename)
       // Set the last modified time
       long newModifiedTime = System.currentTimeMillis();
       boolean success = new File(filename).setLastModified(newModifiedTime);
       return success;
   }
   public static boolean setFileModificationTime(String filename, long timeInMs)
       // Set the last modified time
       boolean success = new File(filename).setLastModified(newModifiedTime);
       return success;
   }
   public static void test(String type, String filename) {
       long t = getFileModificationTime(filename);
       System.out.println("The following " + type + " called " + filename +
            (t == 0 ? " does not exist : " was modified at " + new Time(t).toString() )
       );
       System.out.println("The following " + type + " called " + filename + 
            (!setFileModificationTimeToNow(filename) ? " does not exist. " : " was modified to current time." )
       );
       System.out.println("The following " + type + " called " + filename + 
            (!setFileModificationTime(filename,t) ? " does not exist. " : " was modified to previous time." )
       );
   }
   public static void main(String args[]) {
        test("file", "output.txt");
        test("file", File.seperator + "output.txt");
        test("directory", "docs");
        test("directory", File.seperator + "docs" + File.seperator);
   }
}

[edit] 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"

[edit] 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 *)

[edit] Perl

Works with: Perl version 5


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

[edit] Pop11

;;; Print modification time (seconds since Epoch)
sysmodtime('file') =>

[edit] 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 acces and modification times to the current time:
os.utime('path', None)

[edit] RapidQ

name$ = DIR$("input.txt", 0)
PRINT "File date: "; FileRec.Date
PRINT "File time: "; FileRec.Time

[edit] 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]


[edit] Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET version 9.0+

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)
Personal tools