mirror of
https://github.com/jetzig-framework/jetcommon.git
synced 2026-09-01 22:53:18 -06:00
Implement basic wrappers of Zul date/time types
This commit is contained in:
commit
e6b4259508
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.zig-cache/
|
||||
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -0,0 +1,7 @@
|
||||
Copyright 2024 Robert Farrell
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# JetCommon
|
||||
|
||||
A collection of utilities and types used throughout the [Jetzig Web Framework](https://www.jetzig.dev/).
|
||||
33
build.zig
Normal file
33
build.zig
Normal file
@ -0,0 +1,33 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const lib = b.addStaticLibrary(.{
|
||||
.name = "jetcommon",
|
||||
.root_source_file = b.path("src/jetcommon.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const jetcommon_module = b.addModule("jetcommon", .{ .root_source_file = b.path("src/jetcommon.zig") });
|
||||
|
||||
const zul_module = b.dependency("zul", .{ .target = target, .optimize = optimize }).module("zul");
|
||||
|
||||
jetcommon_module.addImport("zul", zul_module);
|
||||
lib.root_module.addImport("zul", zul_module);
|
||||
|
||||
b.installArtifact(lib);
|
||||
const lib_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/jetcommon.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
lib_unit_tests.root_module.addImport("zul", zul_module);
|
||||
|
||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_lib_unit_tests.step);
|
||||
}
|
||||
16
build.zig.zon
Normal file
16
build.zig.zon
Normal file
@ -0,0 +1,16 @@
|
||||
.{
|
||||
.name = "jetcommon",
|
||||
.version = "0.1.0",
|
||||
.dependencies = .{
|
||||
.zul = .{
|
||||
.url = "https://github.com/karlseguin/zul/archive/08c989bf6871e87807a4668232913ee245425863.tar.gz",
|
||||
.hash = "12206f5d1e5bd4793fe952bbae891b7424a19026e0d296a1381074c7d21d5d76c1a1",
|
||||
},
|
||||
},
|
||||
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
},
|
||||
}
|
||||
12
src/jetcommon.zig
Normal file
12
src/jetcommon.zig
Normal file
@ -0,0 +1,12 @@
|
||||
/// Common types used in various libraries within the Jetzig Web Framework.
|
||||
const std = @import("std");
|
||||
|
||||
pub const types = @import("jetcommon/types.zig");
|
||||
|
||||
pub const DateTime = types.DateTime;
|
||||
pub const Time = types.Time;
|
||||
pub const Date = types.Date;
|
||||
|
||||
test {
|
||||
std.testing.refAllDeclsRecursive(@This());
|
||||
}
|
||||
6
src/jetcommon/time.zig
Normal file
6
src/jetcommon/time.zig
Normal file
@ -0,0 +1,6 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const DateTime = @import("time/DateTime.zig");
|
||||
pub const Date = @import("time/Date.zig");
|
||||
pub const Time = @import("time/Time.zig");
|
||||
pub const TimestampPrecision = enum { seconds, milliseconds, microseconds };
|
||||
34
src/jetcommon/time/Date.zig
Normal file
34
src/jetcommon/time/Date.zig
Normal file
@ -0,0 +1,34 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zul = @import("zul");
|
||||
|
||||
zul_date: zul.Date,
|
||||
|
||||
const Date = @This();
|
||||
|
||||
const Format = enum { rfc3339, iso8601 };
|
||||
|
||||
pub fn init(year: i16, month: u8, day: u8) !Date {
|
||||
const zul_date = try zul.Date.init(year, month, day);
|
||||
return .{ .zul_date = zul_date };
|
||||
}
|
||||
|
||||
pub fn parse(input: []const u8, fmt: Format) !Date {
|
||||
return switch (fmt) {
|
||||
.rfc3339 => .{ .zul_date = try zul.Date.parse(input, .rfc3339) },
|
||||
.iso8601 => .{ .zul_date = try zul.Date.parse(input, .iso8601) },
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: Date,
|
||||
comptime actual_format: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
try self.zul_date.format(actual_format, options, writer);
|
||||
}
|
||||
|
||||
test {
|
||||
_ = try Date.init(2024, 9, 24);
|
||||
}
|
||||
53
src/jetcommon/time/DateTime.zig
Normal file
53
src/jetcommon/time/DateTime.zig
Normal file
@ -0,0 +1,53 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zul = @import("zul");
|
||||
|
||||
const Time = @import("../time.zig").Time;
|
||||
const Date = @import("../time.zig").Date;
|
||||
const TimestampPrecision = @import("../time.zig").TimestampPrecision;
|
||||
|
||||
const DateTime = @This();
|
||||
|
||||
zul_datetime: zul.DateTime,
|
||||
|
||||
pub fn fromUTC(year: i16, month: u8, day: u8, hour: u8, min: u8, sec: u8, micros: u32) !DateTime {
|
||||
return .{ .zul_datetime = try zul.DateTime.initUTC(year, month, day, hour, min, sec, micros) };
|
||||
}
|
||||
|
||||
pub fn fromUnix(value: i64, precision: TimestampPrecision) !DateTime {
|
||||
const zul_datetime = switch (precision) {
|
||||
.seconds => try zul.DateTime.fromUnix(value, .seconds),
|
||||
.milliseconds => try zul.DateTime.fromUnix(value, .milliseconds),
|
||||
.microseconds => try zul.DateTime.fromUnix(value, .microseconds),
|
||||
};
|
||||
return .{ .zul_datetime = zul_datetime };
|
||||
}
|
||||
|
||||
pub fn now() DateTime {
|
||||
return .{ .zul_datetime = zul.DateTime.now() };
|
||||
}
|
||||
|
||||
pub fn date(self: DateTime) Date {
|
||||
return .{ .zul_date = self.zul_datetime.date() };
|
||||
}
|
||||
|
||||
pub fn time(self: DateTime) Time {
|
||||
return .{ .zul_time = self.zul_datetime.time() };
|
||||
}
|
||||
|
||||
pub fn unix(self: DateTime, precision: TimestampPrecision) i64 {
|
||||
return switch (precision) {
|
||||
.seconds => self.zul_datetime.unix(.seconds),
|
||||
.milliseconds => self.zul_datetime.unix(.milliseconds),
|
||||
.microseconds => self.zul_datetime.unix(.microseconds),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: DateTime,
|
||||
comptime actual_format: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
try self.zul_datetime.format(actual_format, options, writer);
|
||||
}
|
||||
22
src/jetcommon/time/Time.zig
Normal file
22
src/jetcommon/time/Time.zig
Normal file
@ -0,0 +1,22 @@
|
||||
const std = @import("std");
|
||||
|
||||
const zul = @import("zul");
|
||||
|
||||
zul_time: zul.Time,
|
||||
|
||||
const Time = @This();
|
||||
|
||||
pub const Format = enum {
|
||||
rfc3339,
|
||||
};
|
||||
|
||||
pub fn init(hour: u8, min: u8, sec: u8, micros: u32) !Time {
|
||||
const zul_time = try zul.Time.init(hour, min, sec, micros);
|
||||
return .{ .zul_time = zul_time };
|
||||
}
|
||||
|
||||
pub fn parse(input: []const u8, fmt: Format) !Time {
|
||||
return switch (fmt) {
|
||||
.rfc3339 => .{ .zul_time = try zul.Time.parse(input, .rfc3339) },
|
||||
};
|
||||
}
|
||||
5
src/jetcommon/types.zig
Normal file
5
src/jetcommon/types.zig
Normal file
@ -0,0 +1,5 @@
|
||||
pub const time = @import("time.zig");
|
||||
|
||||
pub const DateTime = time.DateTime;
|
||||
pub const Date = time.Date;
|
||||
pub const Time = time.Time;
|
||||
Loading…
x
Reference in New Issue
Block a user