summaryrefslogtreecommitdiff
path: root/src/cmd/time.zig
blob: 5c86b49e600de1af284ff1c16c1ad11a56cef8ef (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
const std = @import("std");
const Allocator = std.mem.Allocator;

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);
        var writer = ctx.output_writer;
        try writer.write(output);
        return CommandStatus{ .Code = 0 };
    }
};