Function updateTimes [src]
The underlying file system may have a different granularity than nanoseconds,
and therefore this function cannot guarantee any precision will be stored.
Further, the maximum value is limited by the system ABI. When a value is provided
that exceeds this range, the value is clamped to the maximum.
TODO: integrate with async I/O
Prototype
pub fn updateTimes( self: File, atime: i128, mtime: i128, ) UpdateTimesError!void
Parameters
self: File
atime: i128access timestamp in nanoseconds
mtime: i128last modification timestamp in nanoseconds
Possible Errors
times is NULL, or both nsec values are UTIME_NOW, and either:
- the effective user ID of the caller does not match the owner of the file, the caller does not have write access to the file, and the caller is not privileged (Linux: does not have either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability); or,
- the file is marked immutable (see chattr(1)).
The caller attempted to change one or both timestamps to a value other than the current time, or to change one of the timestamps to the current time while leaving the other timestamp unchanged, (i.e., times is not NULL, neither nsec field is UTIME_NOW, and neither nsec field is UTIME_OMIT) and either:
- the caller's effective user ID does not match the owner of file, and the caller is not privileged (Linux: does not have the CAP_FOWNER capability); or,
- the file is marked append-only or immutable (see chattr(1)).
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
Source
pub fn updateTimes(
self: File,
/// access timestamp in nanoseconds
atime: i128,
/// last modification timestamp in nanoseconds
mtime: i128,
) UpdateTimesError!void {
if (builtin.os.tag == .windows) {
const atime_ft = windows.nanoSecondsToFileTime(atime);
const mtime_ft = windows.nanoSecondsToFileTime(mtime);
return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);
}
const times = [2]posix.timespec{
posix.timespec{
.sec = math.cast(isize, @divFloor(atime, std.time.ns_per_s)) orelse maxInt(isize),
.nsec = math.cast(isize, @mod(atime, std.time.ns_per_s)) orelse maxInt(isize),
},
posix.timespec{
.sec = math.cast(isize, @divFloor(mtime, std.time.ns_per_s)) orelse maxInt(isize),
.nsec = math.cast(isize, @mod(mtime, std.time.ns_per_s)) orelse maxInt(isize),
},
};
try posix.futimens(self.handle, ×);
}