summaryrefslogtreecommitdiff
path: root/src/cmd/date.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/date.zig')
-rw-r--r--src/cmd/date.zig68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/cmd/date.zig b/src/cmd/date.zig
new file mode 100644
index 0000000..c6db0a2
--- /dev/null
+++ b/src/cmd/date.zig
@@ -0,0 +1,68 @@
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+const print = std.debug.print;
+
+const types = @import("./types.zig");
+const CommandStatus = types.CommandStatus;
+const OutputCapture = types.OutputCapture;
+const InputSource = types.InputSource;
+
+fn isLeapYear(year: u32) bool {
+ return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0);
+}
+
+pub const Date = struct {
+ pub fn eval(date: Date, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
+ _ = date;
+ _ = input_source;
+
+ const timestamp = std.time.timestamp();
+ const epoch_seconds = @as(u64, @intCast(timestamp));
+ const epoch_day = @divFloor(epoch_seconds, std.time.s_per_day);
+
+ // Calculate days since Unix epoch (1970-01-01)
+ // Unix epoch is 719163 days since year 1 AD
+ const days_since_year_1 = epoch_day + 719163;
+
+ // Simple algorithm to convert days to year/month/day
+ var year: u32 = 1;
+ var remaining_days = days_since_year_1;
+
+ // Find the year
+ while (true) {
+ const days_in_year: u64 = if (isLeapYear(year)) 366 else 365;
+ if (remaining_days < days_in_year) break;
+ remaining_days -= days_in_year;
+ year += 1;
+ }
+
+ // Days in each month (non-leap year)
+ const days_in_month = [_]u32{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+ var month: u32 = 1;
+ for (days_in_month, 1..) |days, m| {
+ var month_days = days;
+ // Adjust February for leap years
+ if (m == 2 and isLeapYear(year)) {
+ month_days = 29;
+ }
+
+ if (remaining_days < month_days) {
+ month = @intCast(m);
+ break;
+ }
+ remaining_days -= month_days;
+ }
+
+ const day = remaining_days + 1; // Days are 1-indexed
+
+ const output = try std.fmt.allocPrint(allocator, "Current date is {d:0>2}/{d:0>2}/{d}\n", .{ month, day, year });
+ defer allocator.free(output);
+ if (output_capture) |capture| {
+ try capture.write(output);
+ } else {
+ print("{s}", .{output});
+ }
+ return CommandStatus{ .Code = 0 };
+ }
+};