Something went wrong. Try again.
Dates, times and timezones for Zig 0.17: IANA tzdata, ISO 8601, RFC 822, strftime, CLDR, Go, .NET and git date formats
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The day of the week.//!//! Weekdays are numbered from Sunday, which is what the C library and the//! RFC 822 grammar both assume. The week runs unbroken through the whole//! of the proleptic Gregorian calendar, so a weekday is a day count taken//! modulo seven and never has to be looked up; that is what lets//! `Date.dayOfWeek` derive one rather than carry it around. See//! `DayOfWeek.fromDayNumber`.
const std = @import("std");
const Year = @import("year.zig").Year;const Month = @import("month.zig").Month;const Day = @import("day.zig").Day;const Date = @import("Date.zig");
/// A day of the week, numbered from Sunday = 0 through Saturday = 6.pub const DayOfWeek = enum(u3) { Sun = 0, Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6,
/// What the `parse*Str` functions can fail with. There is only the one /// way to fail: the input did not begin with a name in the map. pub const ParseError = error{ParseError}; /// The result of a successful parse: the prefix of the input that was /// consumed and the day it named. pub const ParseStringResult = struct { str: []const u8, value: DayOfWeek, };
/// Parses a two-letter day name ("Su" ... "Sa") at the start of `left`, /// case-insensitively. pub fn parseVeryShortStr(left: []const u8) ParseError!ParseStringResult { return parseWithMap(DayOfWeek.very_short_map, left); }
test parseVeryShortStr { const result = try parseVeryShortStr("We"); try std.testing.expectEqual(DayOfWeek.Wed, result.value); try std.testing.expectEqualStrings("We", result.str);
// Only the two letters are consumed; the rest is the caller's. const trailing = try parseVeryShortStr("Sa, 06 Nov 1994"); try std.testing.expectEqual(DayOfWeek.Sat, trailing.value); try std.testing.expectEqualStrings("Sa", trailing.str);
try std.testing.expectError(error.ParseError, parseVeryShortStr("Xx")); }
/// Parses a three-letter day name ("Sun" ... "Sat") at the start of /// `left`, case-insensitively. pub fn parseShortStr(left: []const u8) ParseError!ParseStringResult { return parseWithMap(DayOfWeek.short_map, left); }
test parseShortStr { const result = try parseShortStr("Sun"); try std.testing.expectEqual(DayOfWeek.Sun, result.value); try std.testing.expectEqualStrings("Sun", result.str);
// Matching ignores case, so any spelling of the name is accepted. try std.testing.expectEqual(DayOfWeek.Fri, (try parseShortStr("FRI")).value); try std.testing.expectEqual(DayOfWeek.Fri, (try parseShortStr("fri")).value); }
/// Parses a full day name ("Sunday" ... "Saturday") at the start of /// `left`, case-insensitively. pub fn parseLongStr(left: []const u8) ParseError!ParseStringResult { return parseWithMap(DayOfWeek.long_map, left); }
test parseLongStr { const result = try parseLongStr("Wednesday"); try std.testing.expectEqual(DayOfWeek.Wed, result.value); try std.testing.expectEqualStrings("Wednesday", result.str);
// The long map holds only whole names, so an abbreviation of one // is not a match. try std.testing.expectError(error.ParseError, parseLongStr("Wed")); }
/// Returns the shortest prefix of `left` found in `map` along with its /// value, or `error.ParseError` if no prefix matches. fn parseWithMap(map: MapType, left: []const u8) ParseError!ParseStringResult { for (1..left.len + 1) |l| { if (map.get(left[0..l])) |dow| { return .{ .str = left[0..l], .value = dow, }; } } return error.ParseError; }
test parseWithMap { // The shortest matching prefix wins, which is why each caller // passes the map for the one length it means to accept. const short = try parseWithMap(short_map, "Sunday"); try std.testing.expectEqualStrings("Sun", short.str);
const long = try parseWithMap(long_map, "Sunday"); try std.testing.expectEqualStrings("Sunday", long.str); }
/// Returns the day after this one, wrapping from Saturday to Sunday. pub fn next(self: DayOfWeek) DayOfWeek { return switch (self) { .Sun => .Mon, .Mon => .Tue, .Tue => .Wed, .Wed => .Thu, .Thu => .Fri, .Fri => .Sat, .Sat => .Sun, }; }
test next { try std.testing.expectEqual(DayOfWeek.Mon, DayOfWeek.Sun.next()); // Saturday wraps round to Sunday. try std.testing.expectEqual(DayOfWeek.Sun, DayOfWeek.Sat.next()); }
/// Returns the day before this one, wrapping from Sunday to Saturday. pub fn prev(self: DayOfWeek) DayOfWeek { return switch (self) { .Sun => .Sat, .Mon => .Sun, .Tue => .Mon, .Wed => .Tue, .Thu => .Wed, .Fri => .Thu, .Sat => .Fri, }; }
test prev { try std.testing.expectEqual(DayOfWeek.Fri, DayOfWeek.Sat.prev()); // Sunday wraps back to Saturday. try std.testing.expectEqual(DayOfWeek.Sat, DayOfWeek.Sun.prev()); }
/// Returns the two-letter English name ("Su" ... "Sa"). pub fn veryShortName(self: DayOfWeek) []const u8 { return switch (self) { .Sun => "Su", .Mon => "Mo", .Tue => "Tu", .Wed => "We", .Thu => "Th", .Fri => "Fr", .Sat => "Sa", }; }
test veryShortName { try std.testing.expectEqualStrings("Su", DayOfWeek.Sun.veryShortName()); try std.testing.expectEqualStrings("Th", DayOfWeek.Thu.veryShortName()); }
/// Returns the three-letter English name ("Sun" ... "Sat"). pub fn shortName(self: DayOfWeek) []const u8 { return switch (self) { .Sun => "Sun", .Mon => "Mon", .Tue => "Tue", .Wed => "Wed", .Thu => "Thu", .Fri => "Fri", .Sat => "Sat", }; }
test shortName { try std.testing.expectEqualStrings("Sun", DayOfWeek.Sun.shortName()); try std.testing.expectEqualStrings("Sat", DayOfWeek.Sat.shortName()); }
/// Returns the full English name ("Sunday" ... "Saturday"). pub fn longName(self: DayOfWeek) []const u8 { return switch (self) { .Sun => "Sunday", .Mon => "Monday", .Tue => "Tuesday", .Wed => "Wednesday", .Thu => "Thursday", .Fri => "Friday", .Sat => "Saturday", }; }
test longName { try std.testing.expectEqualStrings("Sunday", DayOfWeek.Sun.longName()); try std.testing.expectEqualStrings("Wednesday", DayOfWeek.Wed.longName()); }
/// Returns the weekday number, Sunday = 0 through Saturday = 6. pub fn weekdayNumber(self: DayOfWeek) u3 { return @backingInt(self); }
test weekdayNumber { try std.testing.expectEqual(@as(u3, 0), DayOfWeek.Sun.weekdayNumber()); try std.testing.expectEqual(@as(u3, 6), DayOfWeek.Sat.weekdayNumber()); }
/// Returns the ISO 8601 weekday number, Monday = 1 through Sunday = 7: /// ISO 8601-1:2019, 4.3.6, and Table 2. pub fn isoWeekdayNumber(self: DayOfWeek) u3 { return if (self == .Sun) 7 else @backingInt(self); }
test isoWeekdayNumber { // ISO 8601 starts its week on Monday, so Sunday moves from the // front of the week to the back of it. try std.testing.expectEqual(@as(u3, 1), DayOfWeek.Mon.isoWeekdayNumber()); try std.testing.expectEqual(@as(u3, 6), DayOfWeek.Sat.isoWeekdayNumber()); try std.testing.expectEqual(@as(u3, 7), DayOfWeek.Sun.isoWeekdayNumber()); }
/// Returns the day of the week that falls `days` days after 1970-01-01 /// (which was a Thursday); negative values count backward. /// /// Hinnant's `weekday_from_days`, from the paper cited in `Date`. /// Adding 4 rebases the count on the Sunday four days before the epoch, /// since the epoch was a Thursday and Thursday is 4 in this numbering, /// so the remainder modulo 7 is then the weekday directly. /// The second branch is there because `@rem` truncates toward zero, /// so for a count more than four days before the epoch `days + 4` is /// negative and the remainder comes out in `[-6, 0]`; shifting by 5 /// instead and adding 6 maps that range onto `[0, 6]` without a /// division that floors. No table and no lookup: the week has run /// unbroken through the whole proleptic Gregorian calendar, so a /// weekday is nothing but the day count modulo seven. /// /// Ben Joffe's article on weekdays /// (<https://www.benjoffe.com/fast-day-of-week>) gathers faster ways to /// take that remainder. Most are for 32-bit day counts, which a /// `DayNumber` is not. The one that fits takes the low 64 bits of /// `days * ceil(2^64 / 7)`, adds a constant that turns the circle round /// to Thursday, and keeps the top three bits: since 7 is one less than /// 8, multiplying by about 8/7 lands each day in its own eighth of the /// circle, one eighth going unused each week. It was tried here on /// 2026-10-16. It agreed with this on 200 million day numbers, and is /// exact for any count within about 2.3 * 10^17 of the epoch. It /// took 3.3 ns a call to this one's 4.1 when each call waited on the one /// before, and 0.68 to 0.77 when they did not, but `Instant.asDateTime` /// and the `TimeZone` lookups, which call this, came out the same /// within the noise of `zig build bench`. So this stays, and the /// two magic constants do not come in. pub fn fromDayNumber(days: Date.DayNumber) DayOfWeek { const result = if (days >= -4) @rem(days + 4, 7) else @rem(days + 5, 7) + 6; std.debug.assert(result >= 0 and result <= 6); return @fromBackingInt(@intCast(result)); }
test fromDayNumber { // The epoch itself was a Thursday. try std.testing.expectEqual(DayOfWeek.Thu, fromDayNumber(0)); try std.testing.expectEqual(DayOfWeek.Fri, fromDayNumber(1)); try std.testing.expectEqual(DayOfWeek.Thu, fromDayNumber(7));
// Negative counts run backward into the days before it. try std.testing.expectEqual(DayOfWeek.Wed, fromDayNumber(-1)); }
/// The type the name maps below are built from. Comparison ignores /// case, so the maps only have to carry lower-case keys. pub const MapType = std.StaticStringMapWithEql(DayOfWeek, std.ascii.eqlIgnoreCase);
/// The two-letter day names, "su" through "sa". pub const very_short_map = MapType.initComptime( .{ .{ "su", .Sun }, .{ "mo", .Mon }, .{ "tu", .Tue }, .{ "we", .Wed }, .{ "th", .Thu }, .{ "fr", .Fri }, .{ "sa", .Sat }, }, );
/// The three-letter day names, "sun" through "sat". pub const short_map = MapType.initComptime( .{ .{ "sun", .Sun }, .{ "mon", .Mon }, .{ "tue", .Tue }, .{ "wed", .Wed }, .{ "thu", .Thu }, .{ "fri", .Fri }, .{ "sat", .Sat }, }, );
/// The full day names, "sunday" through "saturday". pub const long_map = MapType.initComptime( .{ .{ "sunday", .Sun }, .{ "monday", .Mon }, .{ "tuesday", .Tue }, .{ "wednesday", .Wed }, .{ "thursday", .Thu }, .{ "friday", .Fri }, .{ "saturday", .Sat }, }, );
/// Every prefix of every day name that names exactly one day, from two /// letters up to the full name. For callers that want to accept an /// abbreviation without fixing its length first; note that /// `parseWithMap` takes the shortest match, so against this map it /// stops after two letters and leaves the rest of the name behind. pub const any_map = MapType.initComptime( .{ .{ "su", .Sun }, .{ "mo", .Mon }, .{ "tu", .Tue }, .{ "we", .Wed }, .{ "th", .Thu }, .{ "fr", .Fri }, .{ "sa", .Sat },
.{ "sun", .Sun }, .{ "mon", .Mon }, .{ "tue", .Tue }, .{ "wed", .Wed }, .{ "thu", .Thu }, .{ "fri", .Fri }, .{ "sat", .Sat },
.{ "sund", .Sun }, .{ "mond", .Mon }, .{ "tues", .Tue }, .{ "wedn", .Wed }, .{ "thur", .Thu }, .{ "frid", .Fri }, .{ "satu", .Sat },
.{ "sunda", .Sun }, .{ "monda", .Mon }, .{ "tuesd", .Tue }, .{ "wedne", .Wed }, .{ "thurs", .Thu }, .{ "frida", .Fri }, .{ "satur", .Sat },
.{ "tuesda", .Tue }, .{ "wednes", .Wed }, .{ "thursd", .Thu }, .{ "saturd", .Sat },
.{ "wednesd", .Wed }, .{ "thursda", .Thu }, .{ "saturda", .Sat },
.{ "wednesda", .Wed },
.{ "sunday", .Sun }, .{ "monday", .Mon }, .{ "tuesday", .Tue }, .{ "wednesday", .Wed }, .{ "thursday", .Thu }, .{ "friday", .Fri }, .{ "saturday", .Sat }, }, );};
/// Returns the number of days (0-6) counting forward from `start` to `end`.////// Hinnant's `weekday_difference`, from the paper cited in `Date`, which/// treats `[Sun, Sat]` as a circular range: subtract, and add 7 back if/// that went negative. It is the arithmetic behind questions like "the/// fourth Saturday of May", where the answer is found by stepping from the/// weekday a month starts on to the one wanted.pub fn weekdayDifference(start: DayOfWeek, end: DayOfWeek) u3 { const d = @as(i4, end.weekdayNumber()) - @as(i4, start.weekdayNumber()); return if (d >= 0) @intCast(d) else @intCast(d + 7);}
test weekdayDifference { try std.testing.expectEqual(@as(u3, 2), weekdayDifference(.Sun, .Tue)); try std.testing.expectEqual(@as(u3, 0), weekdayDifference(.Wed, .Wed));
// Counting is always forward, so going back a day is six days on. try std.testing.expectEqual(@as(u3, 6), weekdayDifference(.Tue, .Mon));}
test "weekdayDifference" { const difference = [7][7]u3{ [_]u3{ 0, 1, 2, 3, 4, 5, 6 }, [_]u3{ 6, 0, 1, 2, 3, 4, 5 }, [_]u3{ 5, 6, 0, 1, 2, 3, 4 }, [_]u3{ 4, 5, 6, 0, 1, 2, 3 }, [_]u3{ 3, 4, 5, 6, 0, 1, 2 }, [_]u3{ 2, 3, 4, 5, 6, 0, 1 }, [_]u3{ 1, 2, 3, 4, 5, 6, 0 }, }; for (0..6) |start| { for (0..6) |end| { const result = weekdayDifference( @as(DayOfWeek, @fromBackingInt(@intCast(start))), @as(DayOfWeek, @fromBackingInt(@intCast(end))), ); try std.testing.expectEqual(difference[start][end], result); } }}
test "weekdayFromDays" { const tests = [_]struct { days: i32, result: DayOfWeek }{ .{ .days = -1, .result = .Wed, }, .{ .days = 0, .result = .Thu, }, .{ .days = 1, .result = .Fri, }, .{ .days = 19605, .result = .Tue, }, };
for (tests) |case| { const result = DayOfWeek.fromDayNumber(case.days); try std.testing.expectEqual(case.result, result); }}