fix: rm std.os usage
This commit is contained in:
parent
17f2ca351f
commit
ae1fb8a2fb
2 changed files with 21 additions and 35 deletions
|
|
@ -522,18 +522,20 @@ pub fn run(allocator: std.mem.Allocator, io_optional: ?std.Io) !void {
|
|||
try instant_zdt.toString(allocator, .{}),
|
||||
});
|
||||
|
||||
const now_zdt = try Temporal.Now.zonedDateTimeISO();
|
||||
defer now_zdt.deinit();
|
||||
std.log.info(
|
||||
\\Now Coverage
|
||||
\\ - timeZoneId(): {s}
|
||||
\\ - zonedDateTimeISO(): {s}
|
||||
\\
|
||||
\\
|
||||
, .{
|
||||
Temporal.Now.timeZoneId(),
|
||||
try now_zdt.toString(allocator, .{}),
|
||||
});
|
||||
if (io_optional) |io| {
|
||||
const now_zdt = try Temporal.Now.zonedDateTimeISO(io);
|
||||
defer now_zdt.deinit();
|
||||
std.log.info(
|
||||
\\Now Coverage
|
||||
\\ - timeZoneId(): {s}
|
||||
\\ - zonedDateTimeISO(): {s}
|
||||
\\
|
||||
\\
|
||||
, .{
|
||||
Temporal.Now.timeZoneId(),
|
||||
try now_zdt.toString(allocator, .{}),
|
||||
});
|
||||
}
|
||||
|
||||
const date_cal = try Temporal.PlainDate.calInit(2024, 2, 2, "iso8601");
|
||||
const date_from = try Temporal.PlainDate.from("2024-02-02");
|
||||
|
|
|
|||
30
src/Now.zig
30
src/Now.zig
|
|
@ -87,9 +87,10 @@ pub fn timeZoneId() []const u8 {
|
|||
/// 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)
|
||||
pub fn zonedDateTimeISO() !ZonedDateTime {
|
||||
pub fn zonedDateTimeISO(io: std.Io) !ZonedDateTime {
|
||||
const now = try instant(io);
|
||||
const time_zone = try ZonedDateTime.TimeZone.init(timeZoneId());
|
||||
return ZonedDateTime.fromEpochNanoseconds(currentEpochNanoseconds(), time_zone);
|
||||
return ZonedDateTime.fromEpochNanoseconds(now.epochNanoseconds(), time_zone);
|
||||
}
|
||||
|
||||
const CurrentParts = struct {
|
||||
|
|
@ -134,26 +135,6 @@ fn currentParts(io: std.Io) CurrentParts {
|
|||
};
|
||||
}
|
||||
|
||||
fn currentEpochNanoseconds() i128 {
|
||||
return switch (builtin.os.tag) {
|
||||
.windows => blk: {
|
||||
const epoch_ns = std.time.epoch.windows * std.time.ns_per_s;
|
||||
break :blk @as(i128, @intCast(std.os.windows.ntdll.RtlGetSystemTimePrecise())) * 100 + epoch_ns;
|
||||
},
|
||||
else => blk: {
|
||||
var timespec: std.posix.timespec = undefined;
|
||||
switch (std.posix.errno(std.posix.system.clock_gettime(.REALTIME, ×pec))) {
|
||||
.SUCCESS => {},
|
||||
else => return 0,
|
||||
}
|
||||
|
||||
const seconds: i128 = @intCast(timespec.sec);
|
||||
const nanos: i128 = @intCast(timespec.nsec);
|
||||
break :blk seconds * std.time.ns_per_s + nanos;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ---------- Tests ---------------------
|
||||
test instant {
|
||||
const io = std.testing.io;
|
||||
|
|
@ -190,9 +171,12 @@ test timeZoneId {
|
|||
try std.testing.expectEqualStrings("UTC", tz);
|
||||
}
|
||||
test zonedDateTimeISO {
|
||||
const zdt = try zonedDateTimeISO();
|
||||
const io = std.testing.io;
|
||||
const zdt = try zonedDateTimeISO(io);
|
||||
defer zdt.deinit();
|
||||
|
||||
std.log.info("{d}", .{zdt.epochNanoseconds()});
|
||||
|
||||
try std.testing.expect(zdt.epochNanoseconds() > 0);
|
||||
const tz = try zdt.timeZoneId(std.testing.allocator);
|
||||
defer std.testing.allocator.free(tz);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue