diff --git a/std/fs/file.zig b/std/fs/file.zig index 63c18c1611..bf0ef20034 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -248,12 +248,15 @@ pub const File = struct { } const st = try os.fstat(self.handle); + const atime = st.atime(); + const mtime = st.mtime(); + const ctime = st.ctime(); return Stat{ .size = @bitCast(u64, st.size), .mode = st.mode, - .atime = st.atim.tv_sec * std.time.ns_per_s + st.atim.tv_nsec, - .mtime = st.mtim.tv_sec * std.time.ns_per_s + st.mtim.tv_nsec, - .ctime = st.ctim.tv_sec * std.time.ns_per_s + st.ctim.tv_nsec, + .atime = atime.tv_sec * std.time.ns_per_s + atime.tv_nsec, + .mtime = mtime.tv_sec * std.time.ns_per_s + mtime.tv_nsec, + .ctime = ctime.tv_sec * std.time.ns_per_s + ctime.tv_nsec, }; } diff --git a/std/os/bits/darwin.zig b/std/os/bits/darwin.zig index 39dae6e7a1..b8d229dbe9 100644 --- a/std/os/bits/darwin.zig +++ b/std/os/bits/darwin.zig @@ -44,6 +44,11 @@ pub const mach_timebase_info_data = extern struct { }; /// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. pub const Stat = extern struct { dev: i32, mode: u16, @@ -52,14 +57,14 @@ pub const Stat = extern struct { uid: u32, gid: u32, rdev: i32, - atime: usize, - atimensec: usize, - mtime: usize, - mtimensec: usize, - ctime: usize, - ctimensec: usize, - birthtime: usize, - birthtimensec: usize, + atimesec: isize, + atimensec: isize, + mtimesec: isize, + mtimensec: isize, + ctimesec: isize, + ctimensec: isize, + birthtimesec: isize, + birthtimensec: isize, size: i64, blocks: i64, blksize: i32, @@ -67,6 +72,27 @@ pub const Stat = extern struct { gen: u32, lspare: i32, qspare: [2]i64, + + pub fn atime(self: Stat) timespec { + return timespec{ + .tv_sec = self.atimesec, + .tv_nsec = self.atimensec, + }; + } + + pub fn mtime(self: Stat) timespec { + return timespec{ + .tv_sec = self.mtimesec, + .tv_nsec = self.mtimensec, + }; + } + + pub fn ctime(self: Stat) timespec { + return timespec{ + .tv_sec = self.ctimesec, + .tv_nsec = self.ctimensec, + }; + } }; pub const timespec = extern struct { diff --git a/std/os/bits/freebsd.zig b/std/os/bits/freebsd.zig index c73887d648..198857983e 100644 --- a/std/os/bits/freebsd.zig +++ b/std/os/bits/freebsd.zig @@ -73,6 +73,12 @@ pub const msghdr_const = extern struct { msg_flags: i32, }; +/// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. pub const Stat = extern struct { dev: u64, ino: u64, @@ -96,6 +102,18 @@ pub const Stat = extern struct { flags: u32, gen: u64, __spare: [10]u64, + + pub fn atime(self: Stat) timespec { + return self.atim; + } + + pub fn mtime(self: Stat) timespec { + return self.mtim; + } + + pub fn ctime(self: Stat) timespec { + return self.ctim; + } }; pub const timespec = extern struct { diff --git a/std/os/bits/linux/arm64.zig b/std/os/bits/linux/arm64.zig index bd381c16e7..e19b631292 100644 --- a/std/os/bits/linux/arm64.zig +++ b/std/os/bits/linux/arm64.zig @@ -396,6 +396,11 @@ pub const msghdr_const = extern struct { }; /// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. pub const Stat = extern struct { dev: u64, ino: u64, @@ -414,6 +419,18 @@ pub const Stat = extern struct { mtim: timespec, ctim: timespec, __unused: [3]isize, + + pub fn atime(self: Stat) timespec { + return self.atim; + } + + pub fn mtime(self: Stat) timespec { + return self.mtim; + } + + pub fn ctime(self: Stat) timespec { + return self.ctim; + } }; pub const timespec = extern struct { diff --git a/std/os/bits/linux/x86_64.zig b/std/os/bits/linux/x86_64.zig index d85cac564c..499f925280 100644 --- a/std/os/bits/linux/x86_64.zig +++ b/std/os/bits/linux/x86_64.zig @@ -493,6 +493,11 @@ pub const msghdr_const = extern struct { }; /// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. pub const Stat = extern struct { dev: u64, ino: u64, @@ -511,6 +516,18 @@ pub const Stat = extern struct { mtim: timespec, ctim: timespec, __unused: [3]isize, + + pub fn atime(self: Stat) timespec { + return self.atim; + } + + pub fn mtime(self: Stat) timespec { + return self.mtim; + } + + pub fn ctime(self: Stat) timespec { + return self.ctim; + } }; pub const timespec = extern struct { diff --git a/std/os/bits/netbsd.zig b/std/os/bits/netbsd.zig index d83ea82b06..ef58bd1356 100644 --- a/std/os/bits/netbsd.zig +++ b/std/os/bits/netbsd.zig @@ -73,6 +73,12 @@ pub const msghdr_const = extern struct { msg_flags: i32, }; +/// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. pub const Stat = extern struct { dev: u64, mode: u32, @@ -94,6 +100,18 @@ pub const Stat = extern struct { flags: u32, gen: u32, __spare: [2]u32, + + pub fn atime(self: Stat) timespec { + return self.atim; + } + + pub fn mtime(self: Stat) timespec { + return self.mtim; + } + + pub fn ctime(self: Stat) timespec { + return self.ctim; + } }; pub const timespec = extern struct {