Skip to content

Zig SDK

Install

cd sdk/zig
zig build

Or add as a dependency in build.zig.zon:

.dependencies = .{
    .timenow = .{
        .url = "https://github.com/koke1997/timenow/archive/refs/heads/main.tar.gz",
    },
},

No external dependencies — uses std.http from the Zig standard library.

Quick Start

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

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var client = timenow.Client.init(allocator, "http://localhost:8090");

    // Solar time for London
    const solar = try client.getSolar(51.5074, -0.1278);
    defer allocator.free(solar);
    std.debug.print("Solar: {s}\n", .{solar});

    // Social jetlag
    const jetlag = try client.getJetlag(39.47, 75.99);
    defer allocator.free(jetlag);
    std.debug.print("Jetlag: {s}\n", .{jetlag});

    // Health check
    const health = try client.getHealth();
    defer allocator.free(health);
    std.debug.print("Health: {s}\n", .{health});
}

Methods

Method Signature Returns API endpoint
getSolar (lat: f64, lng: f64) ![]const u8 GET /solar
getSolarWithDt (lat: f64, lng: f64, dt: ?[]const u8) ![]const u8 GET /solar
getJetlag (lat: f64, lng: f64) ![]const u8 GET /solar/jetlag
getPrayer (lat: f64, lng: f64) ![]const u8 GET /solar/prayer
getPrayerWithDate (lat: f64, lng: f64, date: ?[]const u8) ![]const u8 GET /solar/prayer
getAlmanac (lat: f64, lng: f64) ![]const u8 GET /solar/almanac
getReform (lat: f64, lng: f64) ![]const u8 GET /solar/reform
getTimetable (lat1, lng1, lat2, lng2: f64, depart_utc: []const u8) ![]const u8 GET /solar/timetable
getDeviationMap (fmt: ?[]const u8) ![]const u8 GET /world/deviation-map
getHealth () ![]const u8 GET /health

Types

pub const TimeNowError = error{
    HttpError,
    InvalidUrl,
};

pub const Client = struct {
    base_url: []const u8,
    allocator: Allocator,

    pub fn init(allocator: Allocator, base_url: ?[]const u8) Client;
};

All methods return raw JSON as []const u8. The caller owns the memory and must free it.

Error Handling

Uses Zig's error union type (![]const u8):

const result = client.getSolar(999, 0) catch |err| {
    std.debug.print("Error: {}\n", .{err});
    return;
};
defer allocator.free(result);

Errors: TimeNowError.HttpError (non-OK status), TimeNowError.InvalidUrl (bad URL).