Zimeg is a powerful time library for the Zig programming language that provides a convenient way to work with time durations and intervals. It offers a comprehensive set of features for managing time-related tasks, including timers, tickers.
- Time Struct: Represents a duration of time in nanoseconds, with support for various time units such as seconds, minutes, hours, days, and weeks.
- Time Manipulation: Perform various operations on time values, including addition, subtraction, multiplication, division, and comparison.
- Current Time: Retrieve the current clock time with
Time.now()
. - Ticker: Create periodic events with a specified time interval using the
Ticker
struct. - Timer: Manage countdown timers with the
Timer
struct, allowing you to start, restart, and check if a timer has expired.
Use zig fetch
command to save the library's URL and its hash to a build.zig.zon
file.
zig fetch --save https://github.com/engusmaze/zimeg/archive/5debb0e84d24147151be28d706838a9105a90917.tar.gz
After saving the library's URL, you need to make it importable by your code in the build.zig
file. This involves specifying the dependency and adding it to an executable or library.
pub fn build(b: *std.Build) void {
// ...
const zimeg = b.dependency("zimeg", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zimeg", zimeg.module("zimeg"));
}
Once the dependency is specified in the build.zig
file, you can import the library into your Zig code using the @import
directive.
const zimeg = @import("zimeg");
const Time = zimeg.Time;
const Timer = zimeg.Timer;
const Ticker = zimeg.Ticker;
const one_second = zimeg.Time.fromSeconds(1);
const two_minutes = zimeg.Time.fromMinutes(2);
const duration = one_second.add(two_minutes);
std.debug.print("Duration: {} seconds\n", .{duration.toSeconds()});
var ticker = zimeg.Ticker.start(zimeg.Time.fromMillisecs(100));
while (true) {
ticker.waitNextTick();
std.debug.print("This prints every 100 milliseconds!\n", .{});
}
var timer = zimeg.Timer.start(zimeg.Time.fromSeconds(10));
timer.waitNextTick();
std.debug.print("Timer expired!\n", .{});
Contributions to zimeg are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the project's repository.