Library contents

R Sys.setFileTime

Restore and verify a file modification time.

A simple example

Sys.setFileTime

  1. Start with A practice file and a chosen time
  2. Do this Set the modification time and read it back
  3. Result The chosen time, at filesystem precision
The timestamp is metadata. Changing it does not change the text in the file.

Syntax

R syntax
Sys.setFileTime(issue, stamp)
Run these examples in R

Save one example at a time as Lesson.R. Use R 4.x.

Rscript --vanilla Lesson.R

Restore a newsletter publication date

A club restores a newsletter from its backup. The archive uses the publication timestamp to sort issues, so the migration sets a known whole-second time and reads it back before indexing.

From the programstamp <- as.POSIXct(1704067200, origin = "1970-01-01", tz = "UTC")
Result1704067200
Lesson.R
run_example <- function() {
    base <- tempfile("lesson-")
    stopifnot(dir.create(base))
    on.exit(unlink(base, recursive = TRUE))
    issue <- file.path(base, "issue")
    stopifnot(file.create(issue))
    stamp <- as.POSIXct(1704067200, origin = "1970-01-01", tz = "UTC")
    stopifnot(Sys.setFileTime(issue, stamp))
    cat(sprintf("%.0f\n", as.numeric(file.mtime(issue))))
}
run_example()
Output
1704067200

The restored instant is midnight UTC on 1 January 2024, expressed in Unix seconds.

Advertisements

Preserve a concert archive date

The school archives its concert program under the original performance date. After writing the export, set its modification time to noon UTC on the recorded day. Which date does the metadata report back?

Given7 March 2025, noon UTC
Worked result2025-03-07

Restore a newsletter date

The editor restores the date on a copied issue. After writing the export, set its modification time to noon UTC on the recorded day. Which date does the metadata report back?

Given29 February 2024, noon UTC
Worked result2024-02-29

What to watch for

Filesystems store different time resolutions. Compare at a supported precision.

Documentation for Sys.setFileTime