fix: use Duration instead of abi.c.Duration
This commit is contained in:
parent
acd7ed05cb
commit
ca29ace7fa
2 changed files with 99 additions and 117 deletions
|
|
@ -1,11 +1,10 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const abi = @import("abi.zig");
|
const abi = @import("abi.zig");
|
||||||
const c = abi.c;
|
|
||||||
const temporal = @import("temporal.zig");
|
const temporal = @import("temporal.zig");
|
||||||
|
|
||||||
const Duration = @This();
|
const Duration = @This();
|
||||||
|
|
||||||
_inner: *c.Duration,
|
_inner: *abi.c.Duration,
|
||||||
|
|
||||||
/// Construct a Duration from years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.
|
/// Construct a Duration from years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.
|
||||||
/// Equivalent to `Temporal.Duration.from()` or the constructor.
|
/// Equivalent to `Temporal.Duration.from()` or the constructor.
|
||||||
|
|
@ -21,7 +20,7 @@ pub fn init(
|
||||||
microseconds_val: f64,
|
microseconds_val: f64,
|
||||||
nanoseconds_val: f64,
|
nanoseconds_val: f64,
|
||||||
) !Duration {
|
) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_try_new(
|
return wrapDuration(abi.c.temporal_rs_Duration_try_new(
|
||||||
years_val,
|
years_val,
|
||||||
months_val,
|
months_val,
|
||||||
weeks_val,
|
weeks_val,
|
||||||
|
|
@ -38,138 +37,138 @@ pub fn init(
|
||||||
/// Parse an ISO 8601 duration string (Temporal.Duration.from).
|
/// Parse an ISO 8601 duration string (Temporal.Duration.from).
|
||||||
pub fn from(text: []const u8) !Duration {
|
pub fn from(text: []const u8) !Duration {
|
||||||
const view = abi.toDiplomatStringView(text);
|
const view = abi.toDiplomatStringView(text);
|
||||||
return wrapDuration(c.temporal_rs_Duration_from_utf8(view));
|
return wrapDuration(abi.c.temporal_rs_Duration_from_utf8(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an ISO 8601 UTF-16 duration string.
|
/// Parse an ISO 8601 UTF-16 duration string.
|
||||||
fn fromUtf16(text: []const u16) !Duration {
|
fn fromUtf16(text: []const u16) !Duration {
|
||||||
const view = abi.toDiplomatString16View(text);
|
const view = abi.toDiplomatString16View(text);
|
||||||
return wrapDuration(c.temporal_rs_Duration_from_utf16(view));
|
return wrapDuration(abi.c.temporal_rs_Duration_from_utf16(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a Duration from a partial duration (where some fields may be omitted).
|
/// Create a Duration from a partial duration (where some fields may be omitted).
|
||||||
fn fromPartialDuration(partial: PartialDuration) !Duration {
|
fn fromPartialDuration(partial: PartialDuration) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_from_partial_duration(partial));
|
return wrapDuration(abi.c.temporal_rs_Duration_from_partial_duration(partial));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the time portion of the duration is within valid ranges.
|
/// Check if the time portion of the duration is within valid ranges.
|
||||||
fn isTimeWithinRange(self: Duration) bool {
|
fn isTimeWithinRange(self: Duration) bool {
|
||||||
return c.temporal_rs_Duration_is_time_within_range(self._inner);
|
return abi.c.temporal_rs_Duration_is_time_within_range(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the years component of the duration.
|
/// Get the years component of the duration.
|
||||||
pub fn years(self: Duration) i64 {
|
pub fn years(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_years(self._inner);
|
return abi.c.temporal_rs_Duration_years(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the months component of the duration.
|
/// Get the months component of the duration.
|
||||||
pub fn months(self: Duration) i64 {
|
pub fn months(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_months(self._inner);
|
return abi.c.temporal_rs_Duration_months(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the weeks component of the duration.
|
/// Get the weeks component of the duration.
|
||||||
pub fn weeks(self: Duration) i64 {
|
pub fn weeks(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_weeks(self._inner);
|
return abi.c.temporal_rs_Duration_weeks(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the days component of the duration.
|
/// Get the days component of the duration.
|
||||||
pub fn days(self: Duration) i64 {
|
pub fn days(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_days(self._inner);
|
return abi.c.temporal_rs_Duration_days(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the hours component of the duration.
|
/// Get the hours component of the duration.
|
||||||
pub fn hours(self: Duration) i64 {
|
pub fn hours(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_hours(self._inner);
|
return abi.c.temporal_rs_Duration_hours(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the minutes component of the duration.
|
/// Get the minutes component of the duration.
|
||||||
pub fn minutes(self: Duration) i64 {
|
pub fn minutes(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_minutes(self._inner);
|
return abi.c.temporal_rs_Duration_minutes(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the seconds component of the duration.
|
/// Get the seconds component of the duration.
|
||||||
pub fn seconds(self: Duration) i64 {
|
pub fn seconds(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_seconds(self._inner);
|
return abi.c.temporal_rs_Duration_seconds(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the milliseconds component of the duration.
|
/// Get the milliseconds component of the duration.
|
||||||
pub fn milliseconds(self: Duration) i64 {
|
pub fn milliseconds(self: Duration) i64 {
|
||||||
return c.temporal_rs_Duration_milliseconds(self._inner);
|
return abi.c.temporal_rs_Duration_milliseconds(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the microseconds component of the duration.
|
/// Get the microseconds component of the duration.
|
||||||
pub fn microseconds(self: Duration) f64 {
|
pub fn microseconds(self: Duration) f64 {
|
||||||
return c.temporal_rs_Duration_microseconds(self._inner);
|
return abi.c.temporal_rs_Duration_microseconds(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the nanoseconds component of the duration.
|
/// Get the nanoseconds component of the duration.
|
||||||
pub fn nanoseconds(self: Duration) f64 {
|
pub fn nanoseconds(self: Duration) f64 {
|
||||||
return c.temporal_rs_Duration_nanoseconds(self._inner);
|
return abi.c.temporal_rs_Duration_nanoseconds(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the sign of the duration: positive (1), zero (0), or negative (-1).
|
/// Get the sign of the duration: positive (1), zero (0), or negative (-1).
|
||||||
pub fn sign(self: Duration) Sign {
|
pub fn sign(self: Duration) Sign {
|
||||||
return Sign.fromCApi(c.temporal_rs_Duration_sign(self._inner));
|
return Sign.fromCApi(abi.c.temporal_rs_Duration_sign(self._inner));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the duration is zero (all fields are zero).
|
/// Check if the duration is zero (all fields are zero).
|
||||||
pub fn blank(self: Duration) bool {
|
pub fn blank(self: Duration) bool {
|
||||||
return c.temporal_rs_Duration_is_zero(self._inner);
|
return abi.c.temporal_rs_Duration_is_zero(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a new Duration with the absolute value (all components positive).
|
/// Returns a new Duration with the absolute value (all components positive).
|
||||||
pub fn abs(self: Duration) Duration {
|
pub fn abs(self: Duration) Duration {
|
||||||
const ptr: *c.Duration = c.temporal_rs_Duration_abs(self._inner) orelse unreachable;
|
const ptr: *abi.c.Duration = abi.c.temporal_rs_Duration_abs(self._inner) orelse unreachable;
|
||||||
return .{ ._inner = ptr };
|
return .{ ._inner = ptr };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a new Duration with all components negated.
|
/// Returns a new Duration with all components negated.
|
||||||
pub fn negated(self: Duration) Duration {
|
pub fn negated(self: Duration) Duration {
|
||||||
const ptr: *c.Duration = c.temporal_rs_Duration_negated(self._inner) orelse unreachable;
|
const ptr: *abi.c.Duration = abi.c.temporal_rs_Duration_negated(self._inner) orelse unreachable;
|
||||||
return .{ ._inner = ptr };
|
return .{ ._inner = ptr };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add two durations together (Temporal.Duration.prototype.add).
|
/// Add two durations together (Temporal.Duration.prototype.add).
|
||||||
pub fn add(self: Duration, other: Duration) !Duration {
|
pub fn add(self: Duration, other: Duration) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_add(self._inner, other._inner));
|
return wrapDuration(abi.c.temporal_rs_Duration_add(self._inner, other._inner));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtract another duration from this one (Temporal.Duration.prototype.subtract).
|
/// Subtract another duration from this one (Temporal.Duration.prototype.subtract).
|
||||||
pub fn subtract(self: Duration, other: Duration) !Duration {
|
pub fn subtract(self: Duration, other: Duration) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_subtract(self._inner, other._inner));
|
return wrapDuration(abi.c.temporal_rs_Duration_subtract(self._inner, other._inner));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Round the duration according to the specified options (Temporal.Duration.prototype.round).
|
/// Round the duration according to the specified options (Temporal.Duration.prototype.round).
|
||||||
pub fn round(self: Duration, options: RoundingOptions, relative_to: RelativeTo) !Duration {
|
pub fn round(self: Duration, options: RoundingOptions, relative_to: RelativeTo) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_round(self._inner, options, relative_to));
|
return wrapDuration(abi.c.temporal_rs_Duration_round(self._inner, options, relative_to));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Round the duration with an explicit provider.
|
/// Round the duration with an explicit provider.
|
||||||
fn roundWithProvider(self: Duration, options: RoundingOptions, relative_to: RelativeTo, provider: *const c.Provider) !Duration {
|
fn roundWithProvider(self: Duration, options: RoundingOptions, relative_to: RelativeTo, provider: *const abi.c.Provider) !Duration {
|
||||||
return wrapDuration(c.temporal_rs_Duration_round_with_provider(self._inner, options, relative_to, provider));
|
return wrapDuration(abi.c.temporal_rs_Duration_round_with_provider(self._inner, options, relative_to, provider));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compare two durations (Temporal.Duration.compare).
|
/// Compare two durations (Temporal.Duration.compare).
|
||||||
pub fn compare(self: Duration, other: Duration, relative_to: RelativeTo) !i8 {
|
pub fn compare(self: Duration, other: Duration, relative_to: RelativeTo) !i8 {
|
||||||
const res = c.temporal_rs_Duration_compare(self._inner, other._inner, relative_to);
|
const res = abi.c.temporal_rs_Duration_compare(self._inner, other._inner, relative_to);
|
||||||
return abi.success(res) orelse return error.TemporalError;
|
return abi.success(res) orelse return error.TemporalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compare two durations with an explicit provider.
|
/// Compare two durations with an explicit provider.
|
||||||
fn compareWithProvider(self: Duration, other: Duration, relative_to: RelativeTo, provider: *const c.Provider) !i8 {
|
fn compareWithProvider(self: Duration, other: Duration, relative_to: RelativeTo, provider: *const abi.c.Provider) !i8 {
|
||||||
const res = c.temporal_rs_Duration_compare_with_provider(self._inner, other._inner, relative_to, provider);
|
const res = abi.c.temporal_rs_Duration_compare_with_provider(self._inner, other._inner, relative_to, provider);
|
||||||
return abi.success(res) orelse return error.TemporalError;
|
return abi.success(res) orelse return error.TemporalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the total value of the duration in the specified unit (Temporal.Duration.prototype.total).
|
/// Get the total value of the duration in the specified unit (Temporal.Duration.prototype.total).
|
||||||
pub fn total(self: Duration, unit: Unit, relative_to: RelativeTo) !f64 {
|
pub fn total(self: Duration, unit: Unit, relative_to: RelativeTo) !f64 {
|
||||||
const res = c.temporal_rs_Duration_total(self._inner, unit.toCApi(), relative_to);
|
const res = abi.c.temporal_rs_Duration_total(self._inner, unit.toCApi(), relative_to);
|
||||||
return abi.success(res) orelse return error.TemporalError;
|
return abi.success(res) orelse return error.TemporalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the total value of the duration with an explicit provider.
|
/// Get the total value of the duration with an explicit provider.
|
||||||
fn totalWithProvider(self: Duration, unit: Unit, relative_to: RelativeTo, provider: *const c.Provider) !f64 {
|
fn totalWithProvider(self: Duration, unit: Unit, relative_to: RelativeTo, provider: *const abi.c.Provider) !f64 {
|
||||||
const res = c.temporal_rs_Duration_total_with_provider(self._inner, unit.toCApi(), relative_to, provider);
|
const res = abi.c.temporal_rs_Duration_total_with_provider(self._inner, unit.toCApi(), relative_to, provider);
|
||||||
return abi.success(res) orelse return error.TemporalError;
|
return abi.success(res) orelse return error.TemporalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,7 +177,7 @@ pub fn toString(self: Duration, allocator: std.mem.Allocator, options: ToStringR
|
||||||
var write = abi.DiplomatWrite.init(allocator);
|
var write = abi.DiplomatWrite.init(allocator);
|
||||||
defer write.deinit();
|
defer write.deinit();
|
||||||
|
|
||||||
const res = c.temporal_rs_Duration_to_string(self._inner, options, &write.inner);
|
const res = abi.c.temporal_rs_Duration_to_string(self._inner, options, &write.inner);
|
||||||
try handleVoidResult(res);
|
try handleVoidResult(res);
|
||||||
|
|
||||||
return try write.toOwnedSlice();
|
return try write.toOwnedSlice();
|
||||||
|
|
@ -196,12 +195,12 @@ pub fn toLocaleString(self: Duration, allocator: std.mem.Allocator) ![]u8 {
|
||||||
|
|
||||||
/// Clone the underlying duration.
|
/// Clone the underlying duration.
|
||||||
fn clone(self: Duration) Duration {
|
fn clone(self: Duration) Duration {
|
||||||
const ptr: *c.Duration = c.temporal_rs_Duration_clone(self._inner) orelse unreachable;
|
const ptr: *abi.c.Duration = abi.c.temporal_rs_Duration_clone(self._inner) orelse unreachable;
|
||||||
return .{ ._inner = ptr };
|
return .{ ._inner = ptr };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Duration) void {
|
pub fn deinit(self: Duration) void {
|
||||||
c.temporal_rs_Duration_destroy(self._inner);
|
abi.c.temporal_rs_Duration_destroy(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Helpers -----------------------------------------------------------------
|
// --- Helpers -----------------------------------------------------------------
|
||||||
|
|
@ -235,26 +234,26 @@ pub const ToStringOptions = struct {
|
||||||
smallest_unit: ?Unit = null,
|
smallest_unit: ?Unit = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const PartialDuration = c.PartialDuration;
|
pub const PartialDuration = abi.c.PartialDuration;
|
||||||
|
|
||||||
/// Relative-to context for duration operations.
|
/// Relative-to context for duration operations.
|
||||||
pub const RelativeTo = extern struct {
|
pub const RelativeTo = extern struct {
|
||||||
plain_date: ?*c.PlainDate,
|
plain_date: ?*abi.c.PlainDate,
|
||||||
zoned_date_time: ?*c.ZonedDateTime,
|
zoned_date_time: ?*abi.c.ZonedDateTime,
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Public type aliases and enums ------------------------------------------
|
// --- Public type aliases and enums ------------------------------------------
|
||||||
|
|
||||||
const OptionU8 = c.OptionU8;
|
const OptionU8 = abi.c.OptionU8;
|
||||||
const OptionU32 = c.OptionU32;
|
const OptionU32 = abi.c.OptionU32;
|
||||||
const OptionI64 = c.OptionI64;
|
const OptionI64 = abi.c.OptionI64;
|
||||||
const OptionF64 = c.OptionF64;
|
const OptionF64 = abi.c.OptionF64;
|
||||||
const Precision = c.Precision;
|
const Precision = abi.c.Precision;
|
||||||
const Unit_option = c.Unit_option;
|
const Unit_option = abi.c.Unit_option;
|
||||||
const RoundingMode_option = c.RoundingMode_option;
|
const RoundingMode_option = abi.c.RoundingMode_option;
|
||||||
|
|
||||||
pub const RoundingOptions = c.RoundingOptions;
|
pub const RoundingOptions = abi.c.RoundingOptions;
|
||||||
pub const ToStringRoundingOptions = c.ToStringRoundingOptions;
|
pub const ToStringRoundingOptions = abi.c.ToStringRoundingOptions;
|
||||||
|
|
||||||
pub const Unit = temporal.Unit;
|
pub const Unit = temporal.Unit;
|
||||||
pub const RoundingMode = temporal.RoundingMode;
|
pub const RoundingMode = temporal.RoundingMode;
|
||||||
|
|
@ -371,10 +370,10 @@ test toString {
|
||||||
}
|
}
|
||||||
|
|
||||||
test fromPartialDuration {
|
test fromPartialDuration {
|
||||||
const empty_i64 = abi.toOption(c.OptionI64, null);
|
const empty_i64 = abi.toOption(abi.c.OptionI64, null);
|
||||||
const empty_f64 = abi.toOption(c.OptionF64, null);
|
const empty_f64 = abi.toOption(abi.c.OptionF64, null);
|
||||||
|
|
||||||
const partial = c.PartialDuration{
|
const partial = abi.c.PartialDuration{
|
||||||
.years = empty_i64,
|
.years = empty_i64,
|
||||||
.months = empty_i64,
|
.months = empty_i64,
|
||||||
.weeks = empty_i64,
|
.weeks = empty_i64,
|
||||||
|
|
|
||||||
117
src/Instant.zig
117
src/Instant.zig
|
|
@ -1,34 +1,17 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const abi = @import("abi.zig");
|
const abi = @import("abi.zig");
|
||||||
const c = abi.c;
|
|
||||||
const temporal = @import("temporal.zig");
|
const temporal = @import("temporal.zig");
|
||||||
|
|
||||||
|
const Duration = @import("Duration.zig");
|
||||||
const Instant = @This();
|
const Instant = @This();
|
||||||
|
|
||||||
_inner: *c.Instant,
|
_inner: *abi.c.Instant,
|
||||||
epoch_milliseconds: i64,
|
epoch_milliseconds: i64,
|
||||||
epoch_nanoseconds: i128,
|
epoch_nanoseconds: i128,
|
||||||
|
|
||||||
pub const Unit = temporal.Unit;
|
pub const Unit = temporal.Unit;
|
||||||
pub const RoundingMode = temporal.RoundingMode;
|
pub const RoundingMode = temporal.RoundingMode;
|
||||||
pub const Sign = temporal.Sign;
|
pub const Sign = temporal.Sign;
|
||||||
const TimeZone = c.TimeZone;
|
|
||||||
const TimeZone_option = c.TimeZone_option;
|
|
||||||
const Precision = c.Precision;
|
|
||||||
const Unit_option = c.Unit_option;
|
|
||||||
const RoundingMode_option = c.RoundingMode_option;
|
|
||||||
const DifferenceSettings = c.DifferenceSettings;
|
|
||||||
const RoundingOptions = c.RoundingOptions;
|
|
||||||
const ToStringRoundingOptions = c.ToStringRoundingOptions;
|
|
||||||
const OptionU8 = c.OptionU8;
|
|
||||||
const OptionU32 = c.OptionU32;
|
|
||||||
const TemporalError = c.TemporalError;
|
|
||||||
const DiplomatStringView = c.DiplomatStringView;
|
|
||||||
const DiplomatString16View = c.DiplomatString16View;
|
|
||||||
const DiplomatWrite = c.DiplomatWrite;
|
|
||||||
const Duration = c.Duration;
|
|
||||||
const ZonedDateTime = c.ZonedDateTime;
|
|
||||||
const Provider = c.Provider;
|
|
||||||
|
|
||||||
/// Construct from epoch nanoseconds (Temporal.Instant.fromEpochNanoseconds).
|
/// Construct from epoch nanoseconds (Temporal.Instant.fromEpochNanoseconds).
|
||||||
pub fn init(epoch_ns: i128) !Instant {
|
pub fn init(epoch_ns: i128) !Instant {
|
||||||
|
|
@ -37,60 +20,60 @@ pub fn init(epoch_ns: i128) !Instant {
|
||||||
|
|
||||||
/// Construct from epoch milliseconds.
|
/// Construct from epoch milliseconds.
|
||||||
pub fn fromEpochMilliseconds(epoch_ms: i64) !Instant {
|
pub fn fromEpochMilliseconds(epoch_ms: i64) !Instant {
|
||||||
return wrapInstant(c.temporal_rs_Instant_from_epoch_milliseconds(epoch_ms));
|
return wrapInstant(abi.c.temporal_rs_Instant_from_epoch_milliseconds(epoch_ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct from epoch nanoseconds (Temporal.Instant.fromEpochNanoseconds).
|
/// Construct from epoch nanoseconds (Temporal.Instant.fromEpochNanoseconds).
|
||||||
pub fn fromEpochNanoseconds(epoch_ns: i128) !Instant {
|
pub fn fromEpochNanoseconds(epoch_ns: i128) !Instant {
|
||||||
const parts = abi.toI128Nanoseconds(epoch_ns);
|
const parts = abi.toI128Nanoseconds(epoch_ns);
|
||||||
return wrapInstant(c.temporal_rs_Instant_try_new(parts));
|
return wrapInstant(abi.c.temporal_rs_Instant_try_new(parts));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an ISO 8601 string (Temporal.Instant.from).
|
/// Parse an ISO 8601 string (Temporal.Instant.from).
|
||||||
pub fn from(text: []const u8) !Instant {
|
pub fn from(text: []const u8) !Instant {
|
||||||
const view = abi.toDiplomatStringView(text);
|
const view = abi.toDiplomatStringView(text);
|
||||||
return wrapInstant(c.temporal_rs_Instant_from_utf8(view));
|
return wrapInstant(abi.c.temporal_rs_Instant_from_utf8(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an ISO 8601 UTF-16 string (Temporal.Instant.from).
|
/// Parse an ISO 8601 UTF-16 string (Temporal.Instant.from).
|
||||||
fn fromUtf16(text: []const u16) !Instant {
|
fn fromUtf16(text: []const u16) !Instant {
|
||||||
const view = abi.toDiplomatString16View(text);
|
const view = abi.toDiplomatString16View(text);
|
||||||
return wrapInstant(c.temporal_rs_Instant_from_utf16(view));
|
return wrapInstant(abi.c.temporal_rs_Instant_from_utf16(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a Duration to this instant (Temporal.Instant.prototype.add).
|
/// Add a Duration to this instant (Temporal.Instant.prototype.add).
|
||||||
pub fn add(self: Instant, duration: *const Duration) !Instant {
|
pub fn add(self: Instant, duration: *Duration) !Instant {
|
||||||
return wrapInstant(c.temporal_rs_Instant_add(self._inner, duration));
|
return wrapInstant(abi.c.temporal_rs_Instant_add(self._inner, duration._inner));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtract a Duration from this instant (Temporal.Instant.prototype.subtract).
|
/// Subtract a Duration from this instant (Temporal.Instant.prototype.subtract).
|
||||||
pub fn subtract(self: Instant, duration: *const Duration) !Instant {
|
pub fn subtract(self: Instant, duration: *Duration) !Instant {
|
||||||
return wrapInstant(c.temporal_rs_Instant_subtract(self._inner, duration));
|
return wrapInstant(abi.c.temporal_rs_Instant_subtract(self._inner, duration._inner));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Difference until another instant (Temporal.Instant.prototype.until).
|
/// Difference until another instant (Temporal.Instant.prototype.until).
|
||||||
pub fn until(self: Instant, other: Instant, settings: DifferenceSettings) !DurationHandle {
|
pub fn until(self: Instant, other: Instant, settings: abi.c.DifferenceSettings) !DurationHandle {
|
||||||
return wrapDuration(c.temporal_rs_Instant_until(self._inner, other._inner, settings));
|
return wrapDuration(abi.c.temporal_rs_Instant_until(self._inner, other._inner, settings));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Difference since another instant (Temporal.Instant.prototype.since).
|
/// Difference since another instant (Temporal.Instant.prototype.since).
|
||||||
pub fn since(self: Instant, other: Instant, settings: DifferenceSettings) !DurationHandle {
|
pub fn since(self: Instant, other: Instant, settings: abi.c.DifferenceSettings) !DurationHandle {
|
||||||
return wrapDuration(c.temporal_rs_Instant_since(self._inner, other._inner, settings));
|
return wrapDuration(abi.c.temporal_rs_Instant_since(self._inner, other._inner, settings));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Round this instant (Temporal.Instant.prototype.round).
|
/// Round this instant (Temporal.Instant.prototype.round).
|
||||||
pub fn round(self: Instant, options: RoundingOptions) !Instant {
|
pub fn round(self: Instant, options: abi.c.RoundingOptions) !Instant {
|
||||||
return wrapInstant(c.temporal_rs_Instant_round(self._inner, options));
|
return wrapInstant(abi.c.temporal_rs_Instant_round(self._inner, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compare two instants (Temporal.Instant.compare).
|
/// Compare two instants (Temporal.Instant.compare).
|
||||||
pub fn compare(a: Instant, b: Instant) i8 {
|
pub fn compare(a: Instant, b: Instant) i8 {
|
||||||
return c.temporal_rs_Instant_compare(a._inner, b._inner);
|
return abi.c.temporal_rs_Instant_compare(a._inner, b._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equality check (Temporal.Instant.prototype.equals).
|
/// Equality check (Temporal.Instant.prototype.equals).
|
||||||
pub fn equals(a: Instant, b: Instant) bool {
|
pub fn equals(a: Instant, b: Instant) bool {
|
||||||
return c.temporal_rs_Instant_equals(a._inner, b._inner);
|
return abi.c.temporal_rs_Instant_equals(a._inner, b._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert to string using compiled TZ data; caller owns returned slice.
|
/// Convert to string using compiled TZ data; caller owns returned slice.
|
||||||
|
|
@ -101,21 +84,21 @@ pub fn toString(self: Instant, allocator: std.mem.Allocator, opts: ToStringOptio
|
||||||
var write = abi.DiplomatWrite.init(allocator);
|
var write = abi.DiplomatWrite.init(allocator);
|
||||||
defer write.deinit();
|
defer write.deinit();
|
||||||
|
|
||||||
const res = c.temporal_rs_Instant_to_ixdtf_string_with_compiled_data(self._inner, zone_opt, rounding, &write.inner);
|
const res = abi.c.temporal_rs_Instant_to_ixdtf_string_with_compiled_data(self._inner, zone_opt, rounding, &write.inner);
|
||||||
try handleVoidResult(res);
|
try handleVoidResult(res);
|
||||||
|
|
||||||
return try write.toOwnedSlice();
|
return try write.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert to string using an explicit provider.
|
/// Convert to string using an explicit provider.
|
||||||
fn toStringWithProvider(self: Instant, allocator: std.mem.Allocator, provider: *const Provider, opts: ToStringOptions) ![]u8 {
|
fn toStringWithProvider(self: Instant, allocator: std.mem.Allocator, provider: *const abi.c.Provider, opts: ToStringOptions) ![]u8 {
|
||||||
const zone_opt = abi.toTimeZoneOption(opts.time_zone);
|
const zone_opt = abi.toTimeZoneOption(opts.time_zone);
|
||||||
const rounding = optsToRounding(opts);
|
const rounding = optsToRounding(opts);
|
||||||
|
|
||||||
var write = abi.DiplomatWrite.init(allocator);
|
var write = abi.DiplomatWrite.init(allocator);
|
||||||
defer write.deinit();
|
defer write.deinit();
|
||||||
|
|
||||||
const res = c.temporal_rs_Instant_to_ixdtf_string_with_provider(self._inner, zone_opt, rounding, provider, &write.inner);
|
const res = abi.c.temporal_rs_Instant_to_ixdtf_string_with_provider(self._inner, zone_opt, rounding, provider, &write.inner);
|
||||||
try handleVoidResult(res);
|
try handleVoidResult(res);
|
||||||
|
|
||||||
return try write.toOwnedSlice();
|
return try write.toOwnedSlice();
|
||||||
|
|
@ -132,23 +115,23 @@ pub fn toLocaleString(self: Instant, allocator: std.mem.Allocator) ![]u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert to ZonedDateTime using built-in provider (Temporal.Instant.prototype.toZonedDateTimeISO).
|
/// Convert to ZonedDateTime using built-in provider (Temporal.Instant.prototype.toZonedDateTimeISO).
|
||||||
pub fn toZonedDateTimeISO(self: Instant, zone: TimeZone) !ZonedDateTimeHandle {
|
pub fn toZonedDateTimeISO(self: Instant, zone: abi.c.TimeZone) !ZonedDateTimeHandle {
|
||||||
return wrapZonedDateTime(c.temporal_rs_Instant_to_zoned_date_time_iso(self._inner, zone));
|
return wrapZonedDateTime(abi.c.temporal_rs_Instant_to_zoned_date_time_iso(self._inner, zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert to ZonedDateTime using an explicit provider.
|
/// Convert to ZonedDateTime using an explicit provider.
|
||||||
fn toZonedDateTimeIsoWithProvider(self: Instant, zone: TimeZone, provider: *const Provider) !ZonedDateTimeHandle {
|
fn toZonedDateTimeIsoWithProvider(self: Instant, zone: abi.c.TimeZone, provider: *const abi.c.Provider) !ZonedDateTimeHandle {
|
||||||
return wrapZonedDateTime(c.temporal_rs_Instant_to_zoned_date_time_iso_with_provider(self._inner, zone, provider));
|
return wrapZonedDateTime(abi.c.temporal_rs_Instant_to_zoned_date_time_iso_with_provider(self._inner, zone, provider));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clone the underlying instant.
|
/// Clone the underlying instant.
|
||||||
fn clone(self: Instant) Instant {
|
fn clone(self: Instant) Instant {
|
||||||
const ptr = c.temporal_rs_Instant_clone(self._inner) orelse unreachable;
|
const ptr = abi.c.temporal_rs_Instant_clone(self._inner) orelse unreachable;
|
||||||
return .{ ._inner = ptr, .epoch_milliseconds = c.temporal_rs_Instant_epoch_milliseconds(ptr), .epoch_nanoseconds = abi.fromI128Nanoseconds(c.temporal_rs_Instant_epoch_nanoseconds(ptr)) };
|
return .{ ._inner = ptr, .epoch_milliseconds = abi.c.temporal_rs_Instant_epoch_milliseconds(ptr), .epoch_nanoseconds = abi.fromI128Nanoseconds(abi.c.temporal_rs_Instant_epoch_nanoseconds(ptr)) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Instant) void {
|
pub fn deinit(self: Instant) void {
|
||||||
c.temporal_rs_Instant_destroy(self._inner);
|
abi.c.temporal_rs_Instant_destroy(self._inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Helpers -----------------------------------------------------------------
|
// --- Helpers -----------------------------------------------------------------
|
||||||
|
|
@ -157,8 +140,8 @@ fn wrapInstant(res: anytype) !Instant {
|
||||||
const ptr = (abi.success(res) orelse return error.TemporalError) orelse return error.TemporalError;
|
const ptr = (abi.success(res) orelse return error.TemporalError) orelse return error.TemporalError;
|
||||||
return .{
|
return .{
|
||||||
._inner = ptr,
|
._inner = ptr,
|
||||||
.epoch_milliseconds = c.temporal_rs_Instant_epoch_milliseconds(ptr),
|
.epoch_milliseconds = abi.c.temporal_rs_Instant_epoch_milliseconds(ptr),
|
||||||
.epoch_nanoseconds = abi.fromI128Nanoseconds(c.temporal_rs_Instant_epoch_nanoseconds(ptr)),
|
.epoch_nanoseconds = abi.fromI128Nanoseconds(abi.c.temporal_rs_Instant_epoch_nanoseconds(ptr)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,19 +159,19 @@ fn handleVoidResult(res: anytype) !void {
|
||||||
_ = abi.success(res) orelse return error.TemporalError;
|
_ = abi.success(res) orelse return error.TemporalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn defaultPrecision() Precision {
|
fn defaultPrecision() abi.c.Precision {
|
||||||
return .{ .is_minute = false, .precision = abi.toOption(c.OptionU8, null) };
|
return .{ .is_minute = false, .precision = abi.toOption(abi.c.OptionU8, null) };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn defaultToStringRoundingOptions() ToStringRoundingOptions {
|
fn defaultToStringRoundingOptions() abi.c.ToStringRoundingOptions {
|
||||||
return abi.to_string_rounding_options_auto;
|
return abi.to_string_rounding_options_auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert ToStringOptions to ToStringRoundingOptions for the C API
|
/// Convert ToStringOptions to ToStringRoundingOptions for the C API
|
||||||
fn optsToRounding(opts: ToStringOptions) ToStringRoundingOptions {
|
fn optsToRounding(opts: ToStringOptions) abi.c.ToStringRoundingOptions {
|
||||||
// If smallest_unit is specified, use it; otherwise use fractional_second_digits for precision
|
// If smallest_unit is specified, use it; otherwise use fractional_second_digits for precision
|
||||||
const precision = if (opts.fractional_second_digits) |digits|
|
const precision = if (opts.fractional_second_digits) |digits|
|
||||||
Precision{ .is_minute = false, .precision = abi.toOption(c.OptionU8, digits) }
|
abi.c.Precision{ .is_minute = false, .precision = abi.toOption(abi.c.OptionU8, digits) }
|
||||||
else
|
else
|
||||||
defaultPrecision();
|
defaultPrecision();
|
||||||
|
|
||||||
|
|
@ -204,7 +187,7 @@ fn optsToRounding(opts: ToStringOptions) ToStringRoundingOptions {
|
||||||
|
|
||||||
fn parseDuration(text: []const u8) !DurationHandle {
|
fn parseDuration(text: []const u8) !DurationHandle {
|
||||||
const view = abi.toDiplomatStringView(text);
|
const view = abi.toDiplomatStringView(text);
|
||||||
return wrapDuration(c.temporal_rs_Duration_from_utf8(view));
|
return wrapDuration(abi.c.temporal_rs_Duration_from_utf8(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Public helper types -----------------------------------------------------
|
// --- Public helper types -----------------------------------------------------
|
||||||
|
|
@ -228,22 +211,22 @@ pub const ToStringOptions = struct {
|
||||||
|
|
||||||
/// Time zone to use. Either a time zone identifier string or null for UTC.
|
/// Time zone to use. Either a time zone identifier string or null for UTC.
|
||||||
/// Note: In the Zig API, this must be pre-resolved to a TimeZone struct.
|
/// Note: In the Zig API, this must be pre-resolved to a TimeZone struct.
|
||||||
time_zone: ?TimeZone = null,
|
time_zone: ?abi.c.TimeZone = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const DurationHandle = struct {
|
const DurationHandle = struct {
|
||||||
ptr: *Duration,
|
ptr: *abi.c.Duration,
|
||||||
|
|
||||||
pub fn deinit(self: DurationHandle) void {
|
pub fn deinit(self: DurationHandle) void {
|
||||||
c.temporal_rs_Duration_destroy(self.ptr);
|
abi.c.temporal_rs_Duration_destroy(self.ptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const ZonedDateTimeHandle = struct {
|
const ZonedDateTimeHandle = struct {
|
||||||
ptr: *ZonedDateTime,
|
ptr: *abi.c.ZonedDateTime,
|
||||||
|
|
||||||
pub fn deinit(self: ZonedDateTimeHandle) void {
|
pub fn deinit(self: ZonedDateTimeHandle) void {
|
||||||
c.temporal_rs_ZonedDateTime_destroy(self.ptr);
|
abi.c.temporal_rs_ZonedDateTime_destroy(self.ptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -297,14 +280,14 @@ test subtract {
|
||||||
const base = try Instant.fromEpochMilliseconds(0);
|
const base = try Instant.fromEpochMilliseconds(0);
|
||||||
defer base.deinit();
|
defer base.deinit();
|
||||||
|
|
||||||
var dur = try parseDuration("PT1H30M");
|
var dur = try Duration.from("PT1H30M");
|
||||||
defer dur.deinit();
|
defer dur.deinit();
|
||||||
|
|
||||||
const added = try base.add(dur.ptr);
|
const added = try base.add(&dur);
|
||||||
defer added.deinit();
|
defer added.deinit();
|
||||||
try std.testing.expectEqual(@as(i64, 5_400_000), added.epoch_milliseconds);
|
try std.testing.expectEqual(@as(i64, 5_400_000), added.epoch_milliseconds);
|
||||||
|
|
||||||
const subbed = try added.subtract(dur.ptr);
|
const subbed = try added.subtract(&dur);
|
||||||
defer subbed.deinit();
|
defer subbed.deinit();
|
||||||
try std.testing.expectEqual(@as(i64, 0), subbed.epoch_milliseconds);
|
try std.testing.expectEqual(@as(i64, 0), subbed.epoch_milliseconds);
|
||||||
}
|
}
|
||||||
|
|
@ -343,11 +326,11 @@ test until {
|
||||||
const later = try Instant.fromEpochMilliseconds(3_600_000);
|
const later = try Instant.fromEpochMilliseconds(3_600_000);
|
||||||
defer later.deinit();
|
defer later.deinit();
|
||||||
|
|
||||||
const settings = DifferenceSettings{
|
const settings = abi.c.DifferenceSettings{
|
||||||
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
||||||
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(abi.c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
var until_handle = try earlier.until(later, settings);
|
var until_handle = try earlier.until(later, settings);
|
||||||
|
|
@ -367,11 +350,11 @@ test since {
|
||||||
const later = try Instant.fromEpochMilliseconds(3_600_000);
|
const later = try Instant.fromEpochMilliseconds(3_600_000);
|
||||||
defer later.deinit();
|
defer later.deinit();
|
||||||
|
|
||||||
const settings = DifferenceSettings{
|
const settings = abi.c.DifferenceSettings{
|
||||||
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
.largest_unit = abi.toUnitOption(Unit.hour.toCApi()),
|
||||||
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.trunc.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(abi.c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
var until_handle = try earlier.until(later, settings);
|
var until_handle = try earlier.until(later, settings);
|
||||||
|
|
@ -389,11 +372,11 @@ test round {
|
||||||
const inst = try Instant.fromEpochNanoseconds(1_609_459_245_123_456_789);
|
const inst = try Instant.fromEpochNanoseconds(1_609_459_245_123_456_789);
|
||||||
defer inst.deinit();
|
defer inst.deinit();
|
||||||
|
|
||||||
const opts = RoundingOptions{
|
const opts = abi.c.RoundingOptions{
|
||||||
.largest_unit = abi.toUnitOption(null),
|
.largest_unit = abi.toUnitOption(null),
|
||||||
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
.smallest_unit = abi.toUnitOption(Unit.second.toCApi()),
|
||||||
.rounding_mode = abi.toRoundingModeOption(RoundingMode.half_expand.toCApi()),
|
.rounding_mode = abi.toRoundingModeOption(RoundingMode.half_expand.toCApi()),
|
||||||
.increment = abi.toOption(c.OptionU32, null),
|
.increment = abi.toOption(abi.c.OptionU32, null),
|
||||||
};
|
};
|
||||||
|
|
||||||
const rounded = try inst.round(opts);
|
const rounded = try inst.round(opts);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue