fix(Instant): align with spec

This commit is contained in:
Nurul Huda (Apon) 2026-01-23 23:57:14 +06:00
parent 6da6b893b8
commit cffa94146e
4 changed files with 45 additions and 33 deletions

View file

@ -1,5 +1,3 @@
Add the following tests and mark them as Todo:
Temporal.Duration Temporal.Duration
Temporal.Instant Temporal.Instant
Temporal.Now Temporal.Now

View file

@ -33,11 +33,24 @@ pub fn build(b: *std.Build) void {
// Run command // Run command
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| { if (b.args) |args| run_cmd.addArgs(args);
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
// Docs
{
const docs_step = b.step("docs", "Build the temporalz docs");
const docs_obj = b.addObject(.{
.name = "temporalz",
.root_module = mod,
});
const docs = docs_obj.getEmittedDocs();
docs_step.dependOn(&b.addInstallDirectory(.{
.source_dir = docs,
.install_dir = .prefix,
.install_subdir = "docs",
}).step);
}
// Tests // Tests
{ {
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);

View file

@ -3,11 +3,12 @@ const std = @import("std");
const Instant = @This(); const Instant = @This();
inner: *CInstant, inner: *CInstant,
epoch: i64, epochMilliseconds: i64,
epochNanoseconds: i128,
/// Construct from epoch milliseconds (Temporal.Instant.fromEpochMilliseconds). /// Construct from epoch nanoseconds (Temporal.Instant.fromEpochNanoseconds).
pub fn init(epoch_ms: i64) !Instant { pub fn init(epoch_ns: i128) !Instant {
return fromEpochMilliseconds(epoch_ms); return fromEpochNanoseconds(epoch_ns);
} }
/// Construct from epoch milliseconds. /// Construct from epoch milliseconds.
@ -69,15 +70,15 @@ pub fn equals(a: Instant, b: Instant) bool {
} }
/// Epoch milliseconds accessor (Temporal.Instant.prototype.epochMilliseconds). /// Epoch milliseconds accessor (Temporal.Instant.prototype.epochMilliseconds).
pub fn epochMilliseconds(self: Instant) i64 { // pub fn epochMilliseconds(self: Instant) i64 {
return temporal_rs_Instant_epoch_milliseconds(self.inner); // return temporal_rs_Instant_epoch_milliseconds(self.inner);
} // }
/// Epoch nanoseconds accessor (Temporal.Instant.prototype.epochNanoseconds). /// Epoch nanoseconds accessor (Temporal.Instant.prototype.epochNanoseconds).
pub fn epochNanoseconds(self: Instant) i128 { // pub fn epochNanoseconds(self: Instant) i128 {
const value = temporal_rs_Instant_epoch_nanoseconds(self.inner); // const value = temporal_rs_Instant_epoch_nanoseconds(self.inner);
return partsToI128(value); // return partsToI128(value);
} // }
/// Convert to string using compiled TZ data; caller owns returned slice. /// Convert to string using compiled TZ data; caller owns returned slice.
pub fn toString(self: Instant, allocator: std.mem.Allocator, opts: ToStringOptions) ![]u8 { pub fn toString(self: Instant, allocator: std.mem.Allocator, opts: ToStringOptions) ![]u8 {
@ -150,7 +151,7 @@ fn toZonedDateTimeIsoWithProvider(self: Instant, zone: TimeZone, provider: *cons
/// Clone the underlying instant. /// Clone the underlying instant.
fn clone(self: Instant) Instant { fn clone(self: Instant) Instant {
const ptr = temporal_rs_Instant_clone(self.inner); const ptr = temporal_rs_Instant_clone(self.inner);
return .{ .inner = ptr, .epoch = temporal_rs_Instant_epoch_milliseconds(ptr) }; return .{ .inner = ptr, .epochMilliseconds = temporal_rs_Instant_epoch_milliseconds(ptr), .epochNanoseconds = partsToI128(temporal_rs_Instant_epoch_nanoseconds(ptr)) };
} }
pub fn deinit(self: Instant) void { pub fn deinit(self: Instant) void {
@ -162,7 +163,7 @@ pub fn deinit(self: Instant) void {
fn wrapInstant(res: InstantResult) !Instant { fn wrapInstant(res: InstantResult) !Instant {
if (!res.is_ok) return error.TemporalError; if (!res.is_ok) return error.TemporalError;
const ptr = res.result.ok orelse return error.TemporalError; const ptr = res.result.ok orelse return error.TemporalError;
return .{ .inner = ptr, .epoch = temporal_rs_Instant_epoch_milliseconds(ptr) }; return .{ .inner = ptr, .epochMilliseconds = temporal_rs_Instant_epoch_milliseconds(ptr), .epochNanoseconds = partsToI128(temporal_rs_Instant_epoch_nanoseconds(ptr)) };
} }
fn wrapDuration(res: DurationResult) !DurationHandle { fn wrapDuration(res: DurationResult) !DurationHandle {
@ -437,11 +438,11 @@ extern "c" fn diplomat_buffer_write_destroy(write: *DiplomatWrite) void;
// --- Tests ------------------------------------------------------------------- // --- Tests -------------------------------------------------------------------
test init { test init {
const epoch_ms: i64 = 1_704_067_200_000; // 2024-01-01T00:00:00Z const epoch_ns: i128 = 1_704_067_200_000_000_000; // 2024-01-01T00:00:00Z
const inst = try Instant.init(epoch_ms); const inst = try Instant.init(epoch_ns);
defer inst.deinit(); defer inst.deinit();
try std.testing.expectEqual(epoch_ms, inst.epochMilliseconds()); try std.testing.expectEqual(epoch_ns, inst.epochNanoseconds);
} }
test fromEpochNanoseconds { test fromEpochNanoseconds {
@ -454,8 +455,8 @@ test fromEpochNanoseconds {
const min_inst = try Instant.fromEpochNanoseconds(min_ns); const min_inst = try Instant.fromEpochNanoseconds(min_ns);
defer min_inst.deinit(); defer min_inst.deinit();
try std.testing.expectEqual(max_ns, max_inst.epochNanoseconds()); try std.testing.expectEqual(max_ns, max_inst.epochNanoseconds);
try std.testing.expectEqual(min_ns, min_inst.epochNanoseconds()); try std.testing.expectEqual(min_ns, min_inst.epochNanoseconds);
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(max_ns + 1)); try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(max_ns + 1));
try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(min_ns - 1)); try std.testing.expectError(error.TemporalError, Instant.fromEpochNanoseconds(min_ns - 1));
@ -465,7 +466,7 @@ test from {
const inst = try Instant.from("2024-03-15T14:30:45.123Z"); const inst = try Instant.from("2024-03-15T14:30:45.123Z");
defer inst.deinit(); defer inst.deinit();
try std.testing.expectEqual(@as(i64, 1_710_513_045_123), inst.epochMilliseconds()); try std.testing.expectEqual(@as(i64, 1_710_513_045_123), inst.epochMilliseconds);
} }
test fromUtf16 { test fromUtf16 {
@ -477,7 +478,7 @@ test fromUtf16 {
const inst = try Instant.fromUtf16(utf16); const inst = try Instant.fromUtf16(utf16);
defer inst.deinit(); defer inst.deinit();
try std.testing.expectEqual(@as(i64, 1_710_513_045_123), inst.epochMilliseconds()); try std.testing.expectEqual(@as(i64, 1_710_513_045_123), inst.epochMilliseconds);
} }
test subtract { test subtract {
@ -489,11 +490,11 @@ test subtract {
const added = try base.add(dur.ptr); const added = try base.add(dur.ptr);
defer added.deinit(); defer added.deinit();
try std.testing.expectEqual(@as(i64, 5_400_000), added.epochMilliseconds()); try std.testing.expectEqual(@as(i64, 5_400_000), added.epochMilliseconds);
const subbed = try added.subtract(dur.ptr); const subbed = try added.subtract(dur.ptr);
defer subbed.deinit(); defer subbed.deinit();
try std.testing.expectEqual(@as(i64, 0), subbed.epochMilliseconds()); try std.testing.expectEqual(@as(i64, 0), subbed.epochMilliseconds);
} }
test compare { test compare {
@ -586,7 +587,7 @@ test round {
const rounded = try inst.round(opts); const rounded = try inst.round(opts);
defer rounded.deinit(); defer rounded.deinit();
const ns = rounded.epochNanoseconds(); const ns = rounded.epochNanoseconds;
try std.testing.expectEqual(@as(i128, 1_609_459_245_000_000_000), ns); try std.testing.expectEqual(@as(i128, 1_609_459_245_000_000_000), ns);
} }
@ -597,12 +598,12 @@ test clone {
const cloned = inst.clone(); const cloned = inst.clone();
defer cloned.deinit(); defer cloned.deinit();
try std.testing.expectEqual(inst.epochMilliseconds(), cloned.epochMilliseconds()); try std.testing.expectEqual(inst.epochMilliseconds, cloned.epochMilliseconds);
} }
test toString { test toString {
const epoch_ms: i64 = 1704067200000; // 2024-01-01 00:00:00 UTC const epoch_ns: i128 = 1704067200000000000; // 2024-01-01 00:00:00 UTC
const inst = try Instant.init(epoch_ms); const inst = try Instant.init(epoch_ns);
defer inst.deinit(); defer inst.deinit();
const allocator = std.testing.allocator; const allocator = std.testing.allocator;

View file

@ -98,8 +98,8 @@ test "Temporal.Instant" {
"valueOf", "valueOf",
// Properties // Properties
"epochMilliseconds", // "epochMilliseconds",
"epochNanoseconds", // "epochNanoseconds",
}; };
try assertDecls(Instant, checks); try assertDecls(Instant, checks);