const std = @import("std");
const Module = @import("../module.zig");
const Self = @This();

module: Module,

pub fn init(allocator: std.mem.Allocator) !Self {
    _ = allocator;
    return .{
        .module = .{ .getBlockFn = getBlock },
    };
}

pub fn getBlock(module: *Module, allocator: std.mem.Allocator) !Module.Block {
    _ = module;
    // const self: *const Self = @fieldParentPtr("module", module);

    var tz_file = try std.fs.openFileAbsolute("/etc/localtime", .{});
    defer tz_file.close();
    var tz = try std.tz.Tz.parse(allocator, tz_file.reader());
    defer tz.deinit();

    const timestamp = std.time.timestamp() + tz.timetypes[tz.timetypes.len - 1].offset + std.time.s_per_day;

    const epoch_seconds = std.time.epoch.EpochSeconds{ .secs = @intCast(timestamp) };
    const day_seconds = epoch_seconds.getDaySeconds();
    const epoch_day = epoch_seconds.getEpochDay();
    const year_day = epoch_day.calculateYearDay();
    const month_day = year_day.calculateMonthDay();

    return .{
        .full_text = try std.fmt.allocPrint(allocator, "{d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2}:{d:0>2}", .{
            year_day.year,
            @intFromEnum(month_day.month),
            month_day.day_index,
            day_seconds.getHoursIntoDay(),
            day_seconds.getMinutesIntoHour(),
            day_seconds.getSecondsIntoMinute(),
        }),
    };
}
