- Zig 95.5%
- Python 4.5%
| .forgejo/workflows | add SPDX headers ( #39 ) | |
| benchmark | add SPDX headers ( #39 ) | |
| docs | add SPDX headers ( #39 ) | |
| examples | add SPDX headers ( #39 ) | |
| lib | bump tzdb | |
| scripts | add SPDX headers ( #39 ) | |
| tests | add SPDX headers ( #39 ) | |
| tz @71f28b9ab3 | bump tzdb | |
| .gitattributes | re-upload cleaned-up version | |
| .gitignore | Revise C imports / system libraries ( #37 ) | |
| .gitmodules | re-upload cleaned-up version | |
| .pre-commit-config.yaml | add SPDX headers ( #39 ) | |
| build.zig | bump tzdb | |
| build.zig.zon | bump tzdb | |
| CHANGELOG.md | bump tzdb | |
| LICENSE | add SPDX headers ( #39 ) | |
| README.md | bump tzdb | |
Zig ⚡ tzdata License: Unlicense
zdt
Time(追記) z (追記ここまで)oned (追記) D (追記ここまで)ate(追記) t (追記ここまで)ime in Zig. ⏰ Opinionated, and mostly for my own entertainment.
Core Features
- datetime - including duration arithmetic
- timezones - IANA tzdb / TZif, POSIX, fixed offsets
- parsers/formatters - ISO8601 (datetime and durations), custom datetime parsing/formatting à la
strptime/strftime
Resources
Demo
conststd=@import("std");constbuiltin=@import("builtin");constzdt=@import("zdt");pubfnmain(init:std.process.Init)!void{// Can use an allocator for the time zones as the size of the rule-files varies.constallocator=init.arena.allocator();constio=init.io;// zdt embeds the IANA tz database (about 700k of raw data).// If you pass 'null' instead of the allocator, a fixed-size structure will be used,// see ex_timezones.zig - this is faster, but requires more memory.vartz_LA=tryzdt.Timezone.fromTzdata(io,"America/Los_Angeles",allocator);defertz_LA.deinit();// You can also use your system's tz data at runtime;// this will very likely not work on Windows, so we use the embedded version here as well.vartz_Paris=switch(builtin.os.tag){.windows=>tryzdt.Timezone.fromTzdata(io,"Europe/Paris",allocator),else=>tryzdt.Timezone.fromSystemTzdata(io,"Europe/Paris",zdt.Timezone.tzdb_prefix,allocator),};defertz_Paris.deinit();// ISO8601 parser on-board, accepts wide variety of compatible formatsconsta_datetime=tryzdt.Datetime.fromISO8601("2022年03月07日");constthis_time_LA=trya_datetime.tzLocalize(.{.tz=&tz_LA});// string output requires buffer memory...varbuf=std.mem.zeroes([16]u8);varw:std.Io.Writer=.fixed(&buf);trythis_time_LA.toString("%I %p, %Z",&w);constthis_time_Paris:zdt.Datetime=trythis_time_LA.tzConvert(.{.tz=&tz_Paris});// '{f}' directive gives ISO8601 format by default;std.debug.print("Time, LA : {f} ({s})\n... that's {f} in Paris ({s})\n\n",// we can also print the buffer from above:.{this_time_LA,buf,this_time_Paris,this_time_Paris.tzAbbreviation()},);// Time, LA : 2022年03月07日T00:00:00-08:00 (12 am, PST)// ... that's 2022年03月07日T09:00:00+01:00 in Parisconstwall_diff:zdt.Duration=trythis_time_Paris.diffWall(this_time_LA);constabs_diff:zdt.Duration=this_time_Paris.diff(this_time_LA);std.debug.print("Wall clock time difference: {f}\nAbsolute time difference: {f}\n\n",.{wall_diff,abs_diff});// Wall clock time difference: PT9H// Absolute time difference: PT0S// Easteregg:std.debug.print("Easter this year is on {f}\n",.{tryzdt.Datetime.EasterDate(zdt.Datetime.nowUTC(io).year)},);// Easter this year is on 2026年04月05日T00:00:00}Zig version requirements
v0.10.x: Zig 0.17 masterv0.9.x: Zig 0.16v0.8.x: Zig 0.15 / 0.15.2v0.7.x: Zig 0.14 / 0.14.1v0.6.x: Zig 0.14 / 0.14.1v0.5.x: Zig 0.13 / 0.14v0.4.x: Zig 0.13
zdt 'main' branch targets Zig 'master', but might not be up-to-date with the latest developments. If you just want to use the library, use a tagged version that suites your Zig version.
Operating Systems
zdt primarily targets Linux since I abandoned Windows completely. If something doesn't work for you on Mac, Windows or other OS: contributions welcome!
IANA timezone database version
v0.10.0+:2026c(current)v0.9.4:2026c(current)v0.9.3:2026bv0.9.1+:2026av0.8.3+:2026av0.8.2:2025cv0.6.2+:2025bv0.4.5+:2025av0.2.2+:2024b<= v0.2.1:2024a
Dependencies
- Free from dependencies besides Zig std
- Requires system libc to work with localized month / day names
- NOTE: if you use this library, make sure to link libc in your build.zig
- Time zone database:
zdtcomes with eggert/tz. The database is compiled and shipped withzdt. - if you wish to use your own version of the IANA time zone db, you can set a path to it using the
-Dprefix-tzdb="path/to/your/tzdb"option. See alsozig build --help.
Development
See changelog.
To update the time zone database and the version info, run the following build step: zig build update-tzdb. Some of the code generation is done with Python scripts, which require Python >= 3.9 but no third party packages, i.e. a system installation will do.
Credits
Special thanks to the creators of the following resources:
- inspiration for early version of string-to-datetime parser, and most of the POSIX TZ code: leroycep/zig-tzif
- date <--> days since Unix epoch conversion, algorithms: cassioneri/eaf
- general support from ziggit.dev
LLM usage disclosure
- the core of the library was written "by hand", without LLM-based assistance, since my priority was on learning and understanding, not output quantity.
- source code files (helper scripts etc.) that contain large language model (LLM) generated code do specify so in the SPDX header, see 'FileComment'.
License
Unlicense / public domain, see LICENSE.