Skip to content

Commit ae4f713

Browse files
committed
feat: online docs
1 parent 906c0cd commit ae4f713

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

.github/workflows/docs.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# https://github.com/actions/starter-workflows/blob/main/pages/static.yml
2+
on:
3+
push:
4+
branches: ["master"]
5+
workflow_dispatch:
6+
7+
permissions:
8+
pages: write
9+
contents: read
10+
id-token: write
11+
12+
concurrency:
13+
group: pages
14+
cancel-in-progress: false
15+
16+
jobs:
17+
build:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Check out repository
22+
uses: actions/checkout@v3
23+
- name: Setup Zig
24+
uses: goto-bus-stop/setup-zig@v2
25+
- name: Generate docs
26+
run: |
27+
zig build docs
28+
29+
deploy:
30+
needs: build
31+
runs-on: ubuntu-latest
32+
environment:
33+
name: github-pages
34+
url: ${{ steps.deployment.outputs.page_url }}
35+
steps:
36+
- name: Setup Pages
37+
uses: actions/configure-pages@v4
38+
- name: Upload artifact
39+
uses: actions/upload-pages-artifact@v2
40+
with:
41+
# Upload entire repository
42+
path: zig-out/docs/
43+
- name: Deploy to GitHub Pages
44+
id: deployment
45+
uses: actions/deploy-pages@v3

build.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ pub fn build(b: *std.Build) void {
3838
// running `zig build`).
3939
b.installArtifact(lib);
4040

41+
// Docs
42+
const docs_step = b.step("docs", "Emit docs");
43+
const docs_install = b.addInstallDirectory(.{
44+
.source_dir = lib.getEmittedDocs(),
45+
.install_dir = .prefix,
46+
.install_subdir = "docs",
47+
});
48+
docs_step.dependOn(&docs_install.step);
49+
b.default_step.dependOn(docs_step);
50+
4151
const mod = b.addModule("cron", .{
4252
.source_file = .{ .path = "src/lib.zig" },
4353
.dependencies = &.{.{ .name = "datetime", .module = datetime_module }},

src/cron.zig

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,57 @@ const BitSet = std.bit_set.IntegerBitSet(130);
1313

1414
const LEN = 48;
1515

16+
/// A sturct for parsing cron strings and calculating next and previous execution datetimes.
17+
/// Example:
18+
/// ```zig
19+
/// const std = @import("std");
20+
/// const Cron = @import("cron").Cron;
21+
/// const datetime = @import("datetime").datetime;
22+
///
23+
/// fn job1(i: usize) !void {
24+
/// const now = datetime.Datetime.now();
25+
///
26+
/// var buf: [64]u8 = undefined;
27+
/// const dt_str = try now.formatISO8601Buf(&buf, false);
28+
/// std.log.info("{s} {d}th execution", .{ dt_str, i });
29+
/// }
30+
///
31+
/// pub fn main() !void {
32+
/// var c = Cron.init();
33+
/// // At every minute.
34+
/// try c.parse("*/1 * * * *");
35+
///
36+
/// for (0..5) |i| {
37+
/// const now = datetime.Datetime.now();
38+
///
39+
/// // Get the next run time
40+
/// const next_dt = try c.next(now);
41+
/// const duration = next_dt.sub(now);
42+
/// // convert to nanoseconds
43+
/// const nanos = duration.totalSeconds() * std.time.ns_per_s + duration.nanoseconds;
44+
///
45+
/// // wait next
46+
/// std.time.sleep(@intCast(nanos));
47+
///
48+
/// try job1(i + 1);
49+
/// }
50+
/// }
51+
/// ```
1652
pub const Cron = struct {
1753
buf: [LEN]u8,
1854
expr: CronExpr,
1955

2056
const Self = @This();
2157

58+
/// Initialize cron
2259
pub fn init() Self {
2360
return Self{
2461
.buf = undefined,
2562
.expr = undefined,
2663
};
2764
}
2865

66+
/// Parse cron expression
2967
pub fn parse(self: *Self, input: []const u8) !void {
3068
var buf_: [LEN]u8 = undefined;
3169
const lower_input = std.ascii.lowerString(&buf_, input);
@@ -156,6 +194,7 @@ pub const Cron = struct {
156194
unreachable;
157195
}
158196

197+
/// Calculates and returns the datetime of the next scheduled execution based on the parsed cron schedule and the provided starting datetime.
159198
pub fn next(self: *Self, now: datetime.Datetime) !datetime.Datetime {
160199
// reset nanoseconds
161200
var future = now.shift(.{ .nanoseconds = -@as(i32, @intCast(now.time.nanosecond)) }).shiftSeconds(1);
@@ -221,6 +260,7 @@ pub const Cron = struct {
221260
return future;
222261
}
223262

263+
/// Calculates and returns the datetime of the most recent past scheduled execution based on the parsed cron schedule and the provided starting datetime.
224264
pub fn previous(self: *Self, now: datetime.Datetime) !datetime.Datetime {
225265
// reset nanoseconds
226266
var future = now.shift(.{ .nanoseconds = -@as(i32, @intCast(now.time.nanosecond)) }).shiftSeconds(-1);

0 commit comments

Comments
 (0)