diff options
| author | Robert Günzler <r@gnzler.io> | 2025-10-03 17:38:27 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2025-10-03 18:40:50 +0200 |
| commit | ea7deae11b488c4d4b6a15f8e6272bd7a5550866 (patch) | |
| tree | 213eb72bbc6bede25ac67afe76827d96bbbef80f /src/fs.zig | |
| parent | 109b73a3936c90aa89cd12c60aacf51439d31370 (diff) | |
| download | site-ea7deae11b488c4d4b6a15f8e6272bd7a5550866.tar.gz | |
split formatting out of get_mtime
this allows changing how the time value should be formatted to prepare for using this to make pandoc renders reproducable Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rw-r--r-- | src/fs.zig | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/fs.zig b/src/fs.zig index 8ae9d8e..49041e2 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -100,7 +100,7 @@ pub fn join_path_and_filename(alloc: std.mem.Allocator, paths: []const []const u return try std.fs.path.join(alloc, paths2); } -pub fn get_mtime(alloc: std.mem.Allocator, path: []const u8) ![]const u8 { +pub fn get_mtime(alloc: std.mem.Allocator, path: []const u8) !std.os.linux.timespec { const attr = try alloc.create(std.os.linux.Stat); defer alloc.destroy(attr); @@ -108,16 +108,22 @@ pub fn get_mtime(alloc: std.mem.Allocator, path: []const u8) ![]const u8 { defer alloc.free(pathZ); // use stat to get mtime - _ = std.os.linux.stat(pathZ, attr); + const rc = std.os.linux.stat(pathZ, attr); + if (rc < 0) return error.UnknownStatError; - // use libc strftime to format time string - const mtime = attr.mtime(); - const mtime_str = try alloc.alloc(u8, 512); + return attr.mtime(); +} + +// use libc strftime to format time string +pub fn format_mtime(alloc: std.mem.Allocator, mtime: std.os.linux.timespec, fmt: [*c]const u8) ![]const u8 { + const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX); + const tm = c.localtime(&mtime.sec); const n = c.strftime( mtime_str.ptr, mtime_str.len, - "%F", - c.localtime(&mtime.sec), + fmt, + tm, ); + if (n < 0) return error.UnknownStrftimeError; return try alloc.realloc(mtime_str, n); } |