diff options
| -rw-r--r-- | src/fs.zig | 20 | ||||
| -rw-r--r-- | src/gen.zig | 5 |
2 files changed, 16 insertions, 9 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); } diff --git a/src/gen.zig b/src/gen.zig index dbfb1ba..6d2f124 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -110,7 +110,8 @@ fn genSite(ctx: *Context) !void { // build a single html file fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u8, srcpath: []const u8) !void { const mtime = try fs.get_mtime(allocator, srcpath); - defer allocator.free(mtime); + const mtime_str = try fs.format_mtime(allocator, mtime, "%F"); + defer allocator.free(mtime_str); // HEAD try w.print( @@ -178,7 +179,7 @@ fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u .LICENSE_NAME = Context.params.LICENSE_NAME, .LICENSE_URL = Context.params.LICENSE_URL, .LICENSE_YEAR = Context.params.LICENSE_YEAR, - .MTIME = mtime, + .MTIME = mtime_str, .PATH = srcpath, .PUBLIC_INBOX = Context.params.PUBLIC_INBOX, .SOURCE_URL = Context.params.SOURCE_URL, |