diff --git a/build.zig b/build.zig index 232a29d..5c6539d 100644 --- a/build.zig +++ b/build.zig @@ -1,21 +1,10 @@ const std = @import("std"); +const utils = @import("utils"); pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); - // const optimize = b.standardOptimizeOption(.{}); - - const datetime = b.addModule("jetzig_datetime", .{ + utils.addTest(b, "datetime", b.addModule("datetime", .{ .root_source_file = b.path("src/root.zig"), - .target = target, - }); - - const test_step = b.step("test", "Run tests"); - test_step.dependOn( - &b.addRunArtifact( - b.addTest(.{ - .name = "datetime", - .root_module = datetime, - }), - ).step, - ); + .target = b.standardTargetOptions(.{}), + .optimize = b.standardOptimizeOption(.{}), + })); } diff --git a/build.zig.zon b/build.zig.zon index 2fac969..228dfa5 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -32,44 +32,11 @@ // Once all dependencies are fetched, `zig build` no longer requires // internet connectivity. .dependencies = .{ - // See `zig fetch --save ` for a command-line interface for adding dependencies. - //.example = .{ - // // When updating this field to a new URL, be sure to delete the corresponding - // // `hash`, otherwise you are communicating that you expect to find the old hash at - // // the new URL. If the contents of a URL change this will result in a hash mismatch - // // which will prevent zig from using it. - // .url = "https://example.com/foo.tar.gz", - // - // // This is computed from the file contents of the directory of files that is - // // obtained after fetching `url` and applying the inclusion rules given by - // // `paths`. - // // - // // This field is the source of truth; packages do not come from a `url`; they - // // come from a `hash`. `url` is just one of many possible mirrors for how to - // // obtain a package matching this `hash`. - // // - // // Uses the [multihash](https://multiformats.io/multihash/) format. - // .hash = "...", - // - // // When this is provided, the package is found in a directory relative to the - // // build root. In this case the package's hash is irrelevant and therefore not - // // computed. This field and `url` are mutually exclusive. - // .path = "foo", - // - // // When this is set to `true`, a package is declared to be lazily - // // fetched. This makes the dependency only get fetched if it is - // // actually used. - // .lazy = false, - //}, + .utils = .{ + .url = "git+https://tangled.org/poecoh.com/utils#692a1bb210da57cd7106ee440be11012c682f208", + .hash = "utils-0.0.0-XVWM05MZAAB917q8nsXw1feTB2xBvJ0zSfFvvqV0udO_", + }, }, - // Specifies the set of files and directories that are included in this package. - // Only files and directories listed here are included in the `hash` that - // is computed for this package. Only files listed here will remain on disk - // when using the zig package manager. As a rule of thumb, one should list - // files required for compilation plus any license(s). - // Paths are relative to the build root. Use the empty string (`""`) to refer to - // the build root itself. - // A directory listed here means that all files within, recursively, are included. .paths = .{ "build.zig", "build.zig.zon", diff --git a/src/Date.zig b/src/Date.zig index fb9cfa3..59f6b67 100644 --- a/src/Date.zig +++ b/src/Date.zig @@ -123,6 +123,10 @@ pub fn weekday(self: Date) Weekday { return @enumFromInt(dow); } +pub fn fromTimestamp(ns: i96) Date { + _ = ns; +} + test "Date: get week day" { const date1 = try parse(.rfc3339, "2025-05-25"); try t.expectEqual("Sunday", date1.weekday().name()); diff --git a/src/DateTime.zig b/src/DateTime.zig index c460fe0..605faef 100644 --- a/src/DateTime.zig +++ b/src/DateTime.zig @@ -78,12 +78,12 @@ pub fn sleep(io: Io, duration: Io.Duration) Cancelable!void { try io.sleep(duration, .real); } -/// returns the duration between, honestly, I'm not sure +/// returns the duration between a past datetime and now pub fn durationToNow(self: DateTime, io: Io) Duration { return self.timestamp.untilNow(io); } -/// Returns the duration between now and this datetime +/// Returns the duration between now and a future datetime pub fn durationfromNow(self: DateTime, io: Io) Duration { return self.timestamp.durationFromNow(io); } @@ -175,6 +175,7 @@ pub fn time(self: DateTime) Time { // https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html pub fn fromUtc(year: i32, month: u8, day: u8, hour: u8, min: u8, sec: u8, micros: u32) !DateTime { + // verify valid _ = try Date.init(@intCast(year), try .fromInt(month), day); _ = try Time.init(hour, min, sec, micros); diff --git a/src/Time.zig b/src/Time.zig index 816c9ab..4d952ec 100644 --- a/src/Time.zig +++ b/src/Time.zig @@ -1,9 +1,7 @@ const Time = @This(); - const std = @import("std"); const Allocator = std.mem.Allocator; const Stringify = std.json.Stringify; - const datetime = @import("root.zig"); const Error = datetime.Error; const Parser = @import("Parser.zig"); diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index 8743b2e..0000000 --- a/src/main.zig +++ /dev/null @@ -1,71 +0,0 @@ -const std = @import("std"); -const Io = std.Io; - -const jetzig_datetime = @import("jetzig_datetime"); - -pub fn main(init: std.process.Init) !void { - // Prints to stderr, unbuffered, ignoring potential errors. - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - - // This is appropriate for anything that lives as long as the process. - const arena: std.mem.Allocator = init.arena.allocator(); - - // Accessing command line arguments: - const args = try init.minimal.args.toSlice(arena); - for (args) |arg| { - std.log.info("arg: {s}", .{arg}); - } - - // In order to do I/O operations need an `Io` instance. - const io = init.io; - - // Stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - var stdout_buffer: [1024]u8 = undefined; - var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer); - const stdout_writer = &stdout_file_writer.interface; - - try jetzig_datetime.printAnotherMessage(stdout_writer); - - try stdout_writer.flush(); // Don't forget to flush! -} - -test "simple test" { - const gpa = std.testing.allocator; - var list: std.ArrayList(i32) = .empty; - defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak! - try list.append(gpa, 42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); -} - -test "fuzz example" { - try std.testing.fuzz({}, testOne, .{}); -} - -fn testOne(context: void, smith: *std.testing.Smith) !void { - _ = context; - // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! - - const gpa = std.testing.allocator; - var list: std.ArrayList(u8) = .empty; - defer list.deinit(gpa); - while (!smith.eos()) switch (smith.value(enum { add_data, dup_data })) { - .add_data => { - const slice = try list.addManyAsSlice(gpa, smith.value(u4)); - smith.bytes(slice); - }, - .dup_data => { - if (list.items.len == 0) continue; - if (list.items.len > std.math.maxInt(u32)) return error.SkipZigTest; - const len = smith.valueRangeAtMost(u32, 1, @min(32, list.items.len)); - const off = smith.valueRangeAtMost(u32, 0, @intCast(list.items.len - len)); - try list.appendSlice(gpa, list.items[off..][0..len]); - try std.testing.expectEqualSlices( - u8, - list.items[off..][0..len], - list.items[list.items.len - len ..], - ); - }, - }; -} diff --git a/src/root.zig b/src/root.zig index 47f2ee1..ef81d6d 100644 --- a/src/root.zig +++ b/src/root.zig @@ -8,12 +8,12 @@ pub const Zone = @import("Zone.zig"); pub const Weekday = enum(u3) { sunday = 0, - monday, - tuesday, - wednesday, - thursday, - friday, - saturday, + monday = 1, + tuesday = 2, + wednesday = 3, + thursday = 4, + friday = 5, + saturday = 6, pub fn name(self: Weekday) []const u8 { return utils.titleCase(Weekday, self); @@ -38,8 +38,6 @@ pub const Month = enum(u4) { november = 11, december = 12, - /// Convert int to Month, months are 1 indexed. - /// invalid options will set month to .invalid. pub fn fromInt(int: u8) error{InvalidMonth}!Month { if (int < 1 or int > 12) return error.InvalidMonth; return @enumFromInt(int);