在 Zig 中,获取当前挂钟真实时间(Wall-clock / Real time)推荐通过 std.Io.Clock.real.now(io) 完成,旧版本的 std.time.timestamp() 已被统一的 I/O 时钟机制替代。
通过返回的 Timestamp 对象,可以方便地获取秒级、毫秒级和纳秒级 Unix 时间戳:
now.toSeconds():获取 Unix 秒级时间戳;now.toMilliseconds():获取毫秒级时间戳;now.toNanoseconds():获取纳秒级时间戳。
结合标准库中的 std.time.epoch 模块,可以将秒级时间戳解析并计算为 UTC 的年、月、日、时、分、秒等具体日历分量:
epoch_seconds.getEpochDay():获取 Epoch 纪元天数;epoch_day.calculateYearDay():计算具体公历年份;year_day.calculateMonthDay():计算具体月份与当月第几天;epoch_seconds.getDaySeconds():获取当天已过去的秒数,并提取时、分、秒。
//! Demonstrate Unix timestamps and UTC date/time calculation in Zig.
const std = @import("std");
const Io = std.Io;
const print = std.debug.print;
pub fn main(init: std.process.Init) !void {
const io = init.io;
// 1. Wall-clock time is retrieved via Io.Clock.real.
const now = Io.Clock.real.now(io);
const now_secs = now.toSeconds();
const now_millis = now.toMilliseconds();
const now_nanos = now.toNanoseconds();
print("Current Unix timestamp (seconds): {d}\n", .{now_secs});
print("Current Unix timestamp (milliseconds): {d}\n", .{now_millis});
print("Current Unix timestamp (nanoseconds): {d}\n", .{now_nanos});
// 2. Break down current Unix timestamp into UTC date and time components.
const epoch_seconds: std.time.epoch.EpochSeconds = .{ .secs = @intCast(now_secs) };
const epoch_day = epoch_seconds.getEpochDay();
const year_day = epoch_day.calculateYearDay();
const month_day = year_day.calculateMonthDay();
const day_seconds = epoch_seconds.getDaySeconds();
print("Current UTC Date & Time: {d:0>4}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2}:{d:0>2}\n", .{
year_day.year,
month_day.month.numeric(),
month_day.day_index + 1, // day_index is 0-indexed
day_seconds.getHoursIntoDay(),
day_seconds.getMinutesIntoHour(),
day_seconds.getSecondsIntoMinute(),
});
// 3. Verification with a fixed timestamp: 2025-01-01 00:00:00 UTC (1735689600)
const fixed_secs: u64 = 1735689600;
const fixed_epoch: std.time.epoch.EpochSeconds = .{ .secs = fixed_secs };
const fixed_year_day = fixed_epoch.getEpochDay().calculateYearDay();
const fixed_month_day = fixed_year_day.calculateMonthDay();
const fixed_day_seconds = fixed_epoch.getDaySeconds();
try std.testing.expectEqual(2025, fixed_year_day.year);
try std.testing.expectEqual(@as(u8, 1), fixed_month_day.month.numeric());
try std.testing.expectEqual(0, fixed_month_day.day_index);
try std.testing.expectEqual(0, fixed_day_seconds.getHoursIntoDay());
try std.testing.expectEqual(0, fixed_day_seconds.getMinutesIntoHour());
try std.testing.expectEqual(0, fixed_day_seconds.getSecondsIntoMinute());
}