fix(zoned): tostring()

This commit is contained in:
Nurul Huda (Apon) 2026-01-26 23:30:39 +06:00
parent 9fd8780bf8
commit d2f98710f2
2 changed files with 5 additions and 19 deletions

View file

@ -425,7 +425,6 @@ test "props" {
const eraa = try ym.era(std.testing.allocator); const eraa = try ym.era(std.testing.allocator);
if (eraa) |e| { if (eraa) |e| {
defer std.testing.allocator.free(e); defer std.testing.allocator.free(e);
std.debug.print("Era: {s}\n", .{e});
try std.testing.expectEqualStrings("ce", e); try std.testing.expectEqualStrings("ce", e);
} else { } else {
try std.testing.expect(false); try std.testing.expect(false);
@ -433,7 +432,6 @@ test "props" {
const era_year = ym.eraYear(); const era_year = ym.eraYear();
if (era_year) |ey| { if (era_year) |ey| {
std.debug.print("Era Year: {d}\n", .{ey});
try std.testing.expectEqual(@as(i32, 2021), ey); try std.testing.expectEqual(@as(i32, 2021), ey);
} else { } else {
try std.testing.expect(false); try std.testing.expect(false);

View file

@ -238,32 +238,19 @@ pub fn toPlainTime(self: ZonedDateTime) !PlainTime {
/// Convert to string with options /// Convert to string with options
pub fn toString(self: ZonedDateTime, allocator: std.mem.Allocator, opts: ToStringOptions) ![]u8 { pub fn toString(self: ZonedDateTime, allocator: std.mem.Allocator, opts: ToStringOptions) ![]u8 {
const smallest = if (opts.smallest_unit) |u| abi.toUnitOption(u.toCApi()) else abi.toUnitOption(null);
const rounding = if (opts.rounding_mode) |r| abi.toRoundingModeOption(r.toCApi()) else abi.toRoundingModeOption(null);
const precision: abi.c.Precision = if (opts.fractional_second_digits) |digits|
.{ .is_minute = false, .precision = abi.toOption(abi.c.OptionU8, digits) }
else
.{ .is_minute = true, .precision = abi.toOption(abi.c.OptionU8, null) };
const rounding_opts = abi.c.ToStringRoundingOptions{
.precision = precision,
.smallest_unit = smallest,
.rounding_mode = rounding,
};
var write = abi.DiplomatWrite.init(allocator); var write = abi.DiplomatWrite.init(allocator);
defer write.deinit(); defer write.deinit();
_ = abi.c.temporal_rs_ZonedDateTime_to_ixdtf_string( const result = abi.c.temporal_rs_ZonedDateTime_to_ixdtf_string(
self._inner, self._inner,
opts.offset_display.toCApi(), opts.offset_display.toCApi(),
opts.time_zone_name.toCApi(), opts.time_zone_name.toCApi(),
opts.calendar_display.toCApi(), opts.calendar_display.toCApi(),
rounding_opts, abi.to_string_rounding_options_auto,
&write.inner, &write.inner,
); );
if (!result.is_ok) return error.TemporalError;
return try write.toOwnedSlice(); return try write.toOwnedSlice();
} }
@ -424,6 +411,7 @@ pub fn second(self: ZonedDateTime) u8 {
pub fn timeZoneId(self: ZonedDateTime, allocator: std.mem.Allocator) ![]u8 { pub fn timeZoneId(self: ZonedDateTime, allocator: std.mem.Allocator) ![]u8 {
const tz = abi.c.temporal_rs_ZonedDateTime_timezone(self._inner); const tz = abi.c.temporal_rs_ZonedDateTime_timezone(self._inner);
var write = abi.DiplomatWrite.init(allocator); var write = abi.DiplomatWrite.init(allocator);
defer write.deinit();
abi.c.temporal_rs_TimeZone_identifier(tz, &write.inner); abi.c.temporal_rs_TimeZone_identifier(tz, &write.inner);
@ -556,7 +544,6 @@ test toPlainTime {
} }
test toString { test toString {
if (true) return error.Todo;
const zdt = try from("2021-01-01T00:00:00+00:00[UTC]", null, .compatible, .reject); const zdt = try from("2021-01-01T00:00:00+00:00[UTC]", null, .compatible, .reject);
defer zdt.deinit(); defer zdt.deinit();
@ -564,6 +551,7 @@ test toString {
defer std.testing.allocator.free(str); defer std.testing.allocator.free(str);
try std.testing.expect(str.len > 0); try std.testing.expect(str.len > 0);
try std.testing.expectEqualStrings("2021-01-01T00:00:00+00:00[UTC]", str);
} }
test "props" { test "props" {