chore: add todo for timezone()

This commit is contained in:
Nurul Huda (Apon) 2026-06-08 13:31:57 +06:00
parent a4c2e4cd7a
commit b03d42e72d
No known key found for this signature in database
GPG key ID: 5D3F1DE2855A2F79
3 changed files with 23 additions and 8 deletions

View file

@ -523,8 +523,10 @@ pub fn run(allocator: std.mem.Allocator, io_optional: ?std.Io) !void {
}); });
if (io_optional) |io| { if (io_optional) |io| {
const now_zdt = try Temporal.Now.zonedDateTimeISO(io); const now_zdt = try Temporal.Now.zonedDateTimeISO(allocator, io);
defer now_zdt.deinit(); defer now_zdt.deinit();
const now_tz_id = try Temporal.Now.timeZoneId(allocator);
defer allocator.free(now_tz_id);
std.log.info( std.log.info(
\\Now Coverage \\Now Coverage
\\ - timeZoneId(): {s} \\ - timeZoneId(): {s}
@ -532,7 +534,7 @@ pub fn run(allocator: std.mem.Allocator, io_optional: ?std.Io) !void {
\\ \\
\\ \\
, .{ , .{
Temporal.Now.timeZoneId(), now_tz_id,
try now_zdt.toString(allocator, .{}), try now_zdt.toString(allocator, .{}),
}); });
} }

View file

@ -62,6 +62,7 @@ pub fn fromEpochMilliseconds(epoch_ms: i64) !Instant {
/// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/fromEpochNanoseconds /// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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(abi.c.temporal_rs_Instant_try_new(parts)); return wrapInstant(abi.c.temporal_rs_Instant_try_new(parts));
} }

View file

@ -79,17 +79,28 @@ pub fn plainTimeISO(io: std.Io) !PlainTime {
/// Returns a time zone identifier representing the system's current time zone. /// Returns a time zone identifier representing the system's current time zone.
/// ///
/// The caller owns the returned slice and must free it with `allocator`.
///
/// TODO: Detect the system's actual IANA time zone instead of always returning
/// "UTC". This requires platform-specific logic:
/// - Linux/macOS: resolve the `/etc/localtime` symlink to `.../zoneinfo/<Area>/<City>`.
/// - Windows: read the time zone from the registry and map the Windows zone
/// name to an IANA identifier via the CLDR `windowsZones` table.
/// - WASI: no host time zone is available; "UTC" is the only honest answer.
///
/// See: [MDN Temporal.Now.timeZoneId](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/timeZoneId) /// See: [MDN Temporal.Now.timeZoneId](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/timeZoneId)
pub fn timeZoneId() []const u8 { pub fn timeZoneId(allocator: std.mem.Allocator) ![]const u8 {
return "UTC"; return allocator.dupe(u8, "UTC");
} }
/// Returns the current date and time as a [`Temporal.ZonedDateTime`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime) object, in the ISO 8601 calendar and the specified time zone. /// Returns the current date and time as a [`Temporal.ZonedDateTime`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime) object, in the ISO 8601 calendar and the specified time zone.
/// ///
/// See: [MDN Temporal.Now.zonedDateTimeISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/zonedDateTimeISO) /// See: [MDN Temporal.Now.zonedDateTimeISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/zonedDateTimeISO)
pub fn zonedDateTimeISO(io: std.Io) !ZonedDateTime { pub fn zonedDateTimeISO(allocator: std.mem.Allocator, io: std.Io) !ZonedDateTime {
const now = try instant(io); const now = try instant(io);
const time_zone = try ZonedDateTime.TimeZone.init(timeZoneId()); const tz_id = try timeZoneId(allocator);
defer allocator.free(tz_id);
const time_zone = try ZonedDateTime.TimeZone.init(tz_id);
return ZonedDateTime.fromEpochNanoseconds(now.epochNanoseconds(), time_zone); return ZonedDateTime.fromEpochNanoseconds(now.epochNanoseconds(), time_zone);
} }
@ -167,12 +178,13 @@ test plainTimeISO {
try std.testing.expect(t.second() < 60); try std.testing.expect(t.second() < 60);
} }
test timeZoneId { test timeZoneId {
const tz = timeZoneId(); const tz = try timeZoneId(std.testing.allocator);
defer std.testing.allocator.free(tz);
try std.testing.expectEqualStrings("UTC", tz); try std.testing.expectEqualStrings("UTC", tz);
} }
test zonedDateTimeISO { test zonedDateTimeISO {
const io = std.testing.io; const io = std.testing.io;
const zdt = try zonedDateTimeISO(io); const zdt = try zonedDateTimeISO(std.testing.allocator, io);
defer zdt.deinit(); defer zdt.deinit();
std.log.info("{d}", .{zdt.epochNanoseconds()}); std.log.info("{d}", .{zdt.epochNanoseconds()});