about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.zig35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/main.zig b/src/main.zig
index ef4e592..e3bfb44 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -60,6 +60,23 @@ fn joinIn(allocator: std.mem.Allocator, paths: []const []const u8, filename_part
     return try std.fs.path.join(allocator, paths2);
 }
 
+fn getMtime(allocator: std.mem.Allocator, srcpath: []const u8) ![]u8 {
+    const attr = try allocator.create(std.os.linux.Stat);
+    defer allocator.destroy(attr);
+    const srcpathZ = try allocator.dupeZ(u8, srcpath);
+    defer allocator.free(srcpathZ);
+    _ = std.os.linux.stat(srcpathZ, attr);
+    const mtime = attr.mtime();
+    const mtime_str = try allocator.alloc(u8, 512);
+    const n = c.strftime(
+        mtime_str.ptr,
+        mtime_str.len,
+        "%F",
+        c.localtime(&mtime.sec),
+    );
+    return try allocator.realloc(mtime_str, n);
+}
+
 fn portal(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8, insert_heading: bool) anyerror!void {
     const basename = try std.mem.replaceOwned(u8, allocator, refname, " ", "_");
     defer allocator.free(basename);
@@ -173,22 +190,8 @@ fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8
 }
 
 fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, srcpath: []const u8) !void {
-    const mtime = blk: {
-        const attr = try allocator.create(std.os.linux.Stat);
-        defer allocator.destroy(attr);
-        const srcpathZ = try allocator.dupeZ(u8, srcpath);
-        defer allocator.free(srcpathZ);
-        _ = std.os.linux.stat(srcpathZ, attr);
-        const mtime = attr.mtime();
-        var mtime_str: [256]u8 = undefined;
-        const n = c.strftime(
-            &mtime_str,
-            mtime_str.len,
-            "%F",
-            c.localtime(&mtime.sec),
-        );
-        break :blk mtime_str[0..n];
-    };
+    const mtime = try getMtime(allocator, srcpath);
+    defer allocator.free(mtime);
 
     // HEAD
     try f.writer().print(