a date and time library written in zig. Timezone, DST, and leap second aware -- fork updated to writergate
Find a file
stacksmash 6a8520d6e7 regenerate src/location.zig
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 18:36:10 -04:00
.github/workflows Windows bool is now an enum 2026-04-16 04:55:35 -08:00
.latch Update README example for current API 2026-04-16 04:55:52 -08:00
bench use Ben Joffe's fast algorithm in daysFromCivil 2025-12-12 19:45:22 -06:00
gen update zig-dev.3144 new api 。 2026-04-16 04:55:35 -08:00
src regenerate src/location.zig 2026-05-03 18:36:10 -04:00
.gitignore update gitignore 2024-06-07 06:18:05 -05:00
build.zig adapt to zig 0.15.1 2025-09-11 06:56:00 -05:00
build.zig.zon update to zig 0.16.0-dev.2533+355c62600 2026-04-16 04:55:35 -08:00
LICENSE add license, readme 2024-03-16 08:51:25 -05:00
README.md Update README example for current API 2026-04-16 04:55:52 -08:00

zeit

A time library written in zig.

Install

zig fetch --save git+https://github.com/rockorager/zeit?ref=main

Or install a tag instead of main.

Usage

API Documentation

const std = @import("std");
const zeit = @import("zeit");

pub fn main() !void {
    const allocator = std.heap.page_allocator;
    var threaded = std.Io.Threaded.init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();

    // Get an instant in time. The default gets "now" in UTC
    const now = try zeit.instant(io, .{});

    // Load our local timezone. This needs an allocator. Optionally pass in a
    // zeit.EnvConfig to support TZ and TZDIR environment variables
    const local = try zeit.local(allocator, io, .{});
    defer local.deinit();

    // Convert our instant to a new timezone
    const now_local = now.in(&local);

    // Generate date/time info for this instant
    const dt = now_local.time();

    // Print it out
    std.debug.print("{}", .{dt});

    // zeit.Time{
    //    .year = 2024,
    //    .month = zeit.Month.mar,
    //    .day = 16,
    //    .hour = 8,
    //    .minute = 38,
    //    .second = 29,
    //    .millisecond = 496,
    //    .microsecond = 706,
    //    .nanosecond = 64
    //    .offset = -18000,
    // }

    var buf: [256]u8 = undefined;
    var writer = std.Io.Writer.fixed(&buf);

    // Format using strftime specifier. Format strings are not required to be comptime
    try dt.strftime(&writer, "%Y-%m-%d %H:%M:%S %Z");
    std.debug.print("{s}\n", .{writer.buffered()});

    writer.end = 0;

    // Or...golang magic date specifiers. Format strings are not required to be comptime
    try dt.gofmt(&writer, "2006-01-02 15:04:05 MST");
    std.debug.print("{s}\n", .{writer.buffered()});

    // Load an arbitrary location using IANA location syntax. The location name
    // comes from an enum which will automatically map IANA location names to
    // Windows names, as needed. Pass an optional EnvConfig to support TZDIR
    const vienna = try zeit.loadTimeZone(allocator, io, .@"Europe/Vienna", .{});
    defer vienna.deinit();

    // Parse an Instant from an ISO8601 or RFC3339 string
    _ = try zeit.instant(io, .{
        .source = .{
            .iso8601 = "2024-03-16T08:38:29.496-1200",
        },
    });

    _ = try zeit.instant(io, .{
        .source = .{
            .rfc3339 = "2024-03-16T08:38:29.496706064-1200",
        },
    });
}