Skip to content

clickingbuttons/datetime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

datetime

zig-version tests docs

Generic Date, Time, and DateTime library.

Installation

build.zig.zon

.{
    .name = "yourProject",
    .version = "0.0.1",

    .dependencies = .{
        .@"datetime" = .{
            .url = "https://github.com/clickingbuttons/datetime/archive/refs/tags/latest-release.tar.gz",
        },
    },
}

build.zig

const datetime = b.dependency("datetime", .{
    .target = target,
    .optimize = optimize,
});
your_lib_or_exe.root_module.addImport("datetime", datetime.module("datetime"));

Run zig build and then copy the expected hash into build.zig.zon.

Usage

Check out the demos. Here's a simple one:

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

test "now" {
    const date = datetime.Date.now();
    std.debug.print("today's date is {rfc3339}\n", .{ date });

    const time = datetime.Time.now();
    std.debug.print("today's time is {rfc3339}\n", .{ time });

    const nanotime = datetime.time.Nano.now();
    std.debug.print("today's nanotime is {rfc3339}\n", .{ nanotime });

    const dt = datetime.DateTime.now();
    std.debug.print("today's date and time is {rfc3339}\n", .{ dt });

    const NanoDateTime = datetime.datetime.Advanced(datetime.Date, datetime.time.Nano, false);
    const ndt = NanoDateTime.now();
    std.debug.print("today's date and nanotime is {rfc3339}\n", .{ ndt });
}

Features:

  • Convert to/from epoch subseconds using world's fastest known algorithm. 1
  • Choose:
    • Date's Year type for more or less precision.
    • Time's Subsecond type for more or less precision.
    • Date's epoch for epoch subsecond conversion.
    • Whether DateTime has an OffsetSeconds field.
  • Durations with addition.
  • RFC3339 parsing and formatting.
    • Timezone offset.
  • Localization
  • Leap seconds

Why yet another date time library?

  • I frequently use different precisions for years, subseconds, and UTC offsets.
  • Systems use different epochs and Zig aims to be a sytems language.

Footnotes

  1. Euclidean Affine Functions by Cassio and Neri.

About

Generic Date, Time, and DateTime library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages