summaryrefslogtreecommitdiff
path: root/src/cmd/time.zig
blob: c30ca47c146dbafc07292a6860489b2eb5ed6fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const std = @import("std");
const Allocator = std.mem.Allocator;
const print = std.debug.print;

const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
const CommandContext = types.CommandContext;

pub const Time = struct {
    pub fn eval(time: Time, ctx: CommandContext) !CommandStatus {
        _ = time;

        const timestamp = std.time.timestamp();
        const epoch_seconds = @as(u64, @intCast(timestamp));
        const day_seconds = epoch_seconds % std.time.s_per_day;
        const hours = day_seconds / std.time.s_per_hour;
        const minutes = (day_seconds % std.time.s_per_hour) / std.time.s_per_min;
        const seconds = day_seconds % std.time.s_per_min;

        const output = try std.fmt.allocPrint(ctx.allocator, "Current time is {d:0>2}:{d:0>2}:{d:0>2}\n", .{ hours, minutes, seconds });
        defer ctx.allocator.free(output);
        if (ctx.output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};