this repo has no description
at main 42 lines 1.4 kB view raw
1const std = @import("std"); 2const cron = @import("cron"); 3 4pub fn main() !void { 5 const now = std.time.timestamp() * 1_000_000; 6 7 // Parse a cron expression 8 const schedule = try cron.Cron.parse("*/15 * * * *"); 9 std.debug.print("expression: */15 * * * * (every 15 minutes)\n\n", .{}); 10 11 // Get next 5 occurrences using nextN 12 std.debug.print("next 5 occurrences (via nextN):\n", .{}); 13 var buf: [5]i64 = undefined; 14 const count = schedule.nextN(now, 5, &buf); 15 for (buf[0..count]) |ts| { 16 printDateTime(ts); 17 } 18 19 // Using the iterator pattern 20 std.debug.print("\nusing iterator:\n", .{}); 21 const weekdays = try cron.Cron.parse("0 9 * * 1-5"); // 9am weekdays 22 var iter = weekdays.iter(now); 23 24 var i: usize = 0; 25 while (iter.next()) |ts| { 26 printDateTime(ts); 27 i += 1; 28 if (i >= 3) break; 29 } 30 31 // Check if a time matches 32 std.debug.print("\nmatches check:\n", .{}); 33 const midnight = try cron.Cron.parse("0 0 * * *"); 34 std.debug.print(" does midnight cron match now? {}\n", .{midnight.matches(now)}); 35} 36 37fn printDateTime(micros: i64) void { 38 const dt = cron.DateTime.fromTimestamp(@divFloor(micros, 1_000_000)); 39 std.debug.print(" {d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2}:{d:0>2}\n", .{ 40 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, 41 }); 42}