Time
Date and time manipulation based on the Rata Die calendar algorithm.
Types
Date
pub const Date = struct {
year: i32,
month: u8,
day: u8,
};Represents a calendar date (year, month, day).
Time
pub const Time = struct {
epoch: i64,
};Represents a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).
Date API
Construction
Date.ymd(year: i32, month: u8, day: u8) DateCreate date from year, month, day.
Date.parse(str: []const u8) !DateParse date from ISO 8601 string ("YYYY-MM-DD").
Date.today() Date
Date.yesterday() Date
Date.tomorrow() DateGet current/relative dates.
Date.startOf(unit: DateUnit) Date
Date.endOf(unit: DateUnit) DateGet start/end of current period.
Methods
date.add(unit: DateUnit, amount: i64) DateAdd or subtract date units. Units: .day, .month, .year.
Month-end dates are clamped when adding months:
Date.ymd(2024, 1, 31).add(.month, 1) // 2024-02-29date.setStartOf(unit: DateUnit) Date
date.setEndOf(unit: DateUnit) DateSet to start or end of period.
date.dayOfWeek() u8Get day of week (0 = Monday, 6 = Sunday).
date.cmp(other: Date) std.math.OrderCompare dates (returns .lt, .eq, or .gt).
date.format(writer: anytype) !voidFormat as "YYYY-MM-DD". Use with {f} format specifier.
Time API
Construction
Time.unix(epoch: i64) TimeCreate from Unix timestamp.
Time.now() TimeGet current time.
Time.today() Time
Time.tomorrow() TimeGet midnight today/tomorrow.
Time.startOf(unit: TimeUnit) Time
Time.endOf(unit: TimeUnit) TimeGet start/end of current period.
Component Access
time.date() DateExtract date part.
time.hour() u32
time.minute() u32
time.second() u32Get time-of-day components (0-23, 0-59, 0-59).
Component Modification
time.setDate(date: Date) TimeSet date while preserving time-of-day.
time.setHour(hr: u32) Time
time.setMinute(min: u32) Time
time.setSecond(sec: u32) TimeSet specific time components.
Arithmetic
time.add(unit: enum, amount: i64) TimeAdd or subtract time units. Units:
.seconds.minutes.hours.days.months.years
time.setStartOf(unit: TimeUnit) Time
time.setEndOf(unit: TimeUnit) TimeSet to start or end of period. Units: .second, .minute, .hour, .day, .month, .year.
time.next(unit: enum) TimeJump to start of next period. Units: .second, .minute, .hour, .day.
time.format(writer: anytype) !voidFormat as "YYYY-MM-DD HH:MM:SS UTC". Use with {f} format specifier.
Enums
pub const TimeUnit = enum { second, minute, hour, day, month, year };
pub const DateUnit = enum { day, month, year };Functions
pub fn isLeapYear(year: i32) boolCheck if year is a leap year.
Examples
Basic Usage
// Create and manipulate times
const t1 = tk.time.Time.unix(1234567890);
const t2 = t1.setHour(10).setMinute(15).setSecond(45);
const t3 = t2.add(.hours, 14).add(.minutes, 46);
// Format output
try writer.print("{f}", .{t3}); // "2009-02-14 01:02:03 UTC"Period Boundaries
const now = tk.time.Time.now();
// Start of periods
const day_start = now.setStartOf(.day);
const month_start = now.setStartOf(.month);
const year_start = now.setStartOf(.year);
// End of periods
const day_end = now.setEndOf(.day);
const month_end = now.setEndOf(.month);Date Ranges
const start = tk.time.Date.ymd(2024, 1, 1);
const end = tk.time.Date.ymd(2024, 1, 31);
var current = start;
while (current.cmp(end) != .gt) {
// Process date
current = current.add(.day, 1);
}Notes
Limitations
- UTC only - No timezone support
- Second precision - No subsecond precision
- Fixed format - No custom formatting (always ISO 8601)
Leap Year Handling
Leap years are automatically handled in all date operations. Month-end dates are clamped when necessary to ensure valid dates.