master
  1//! Epoch reference times in terms of their difference from
  2//! UTC 1970-01-01 in seconds.
  3const std = @import("../std.zig");
  4const testing = std.testing;
  5const math = std.math;
  6
  7/// Jan 01, 1970 AD
  8pub const posix = 0;
  9/// Jan 01, 1980 AD
 10pub const dos = 315532800;
 11/// Jan 01, 2001 AD
 12pub const ios = 978307200;
 13/// Nov 17, 1858 AD
 14pub const openvms = -3506716800;
 15/// Jan 01, 1900 AD
 16pub const zos = -2208988800;
 17/// Jan 01, 1601 AD
 18pub const windows = -11644473600;
 19/// Jan 01, 1978 AD
 20pub const amiga = 252460800;
 21/// Dec 31, 1967 AD
 22pub const pickos = -63244800;
 23/// Jan 06, 1980 AD
 24pub const gps = 315964800;
 25/// Jan 01, 0001 AD
 26pub const clr = -62135769600;
 27
 28pub const unix = posix;
 29pub const android = posix;
 30pub const os2 = dos;
 31pub const bios = dos;
 32pub const vfat = dos;
 33pub const ntfs = windows;
 34pub const ntp = zos;
 35pub const jbase = pickos;
 36pub const aros = amiga;
 37pub const morphos = amiga;
 38pub const brew = gps;
 39pub const atsc = gps;
 40pub const go = clr;
 41
 42/// The type that holds the current year, i.e. 2016
 43pub const Year = u16;
 44
 45pub const epoch_year = 1970;
 46pub const secs_per_day: u17 = 24 * 60 * 60;
 47
 48pub fn isLeapYear(year: Year) bool {
 49    if (@mod(year, 4) != 0)
 50        return false;
 51    if (@mod(year, 100) != 0)
 52        return true;
 53    return (0 == @mod(year, 400));
 54}
 55
 56test isLeapYear {
 57    try testing.expectEqual(false, isLeapYear(2095));
 58    try testing.expectEqual(true, isLeapYear(2096));
 59    try testing.expectEqual(false, isLeapYear(2100));
 60    try testing.expectEqual(true, isLeapYear(2400));
 61}
 62
 63pub fn getDaysInYear(year: Year) u9 {
 64    return if (isLeapYear(year)) 366 else 365;
 65}
 66
 67pub const Month = enum(u4) {
 68    jan = 1,
 69    feb,
 70    mar,
 71    apr,
 72    may,
 73    jun,
 74    jul,
 75    aug,
 76    sep,
 77    oct,
 78    nov,
 79    dec,
 80
 81    /// return the numeric calendar value for the given month
 82    /// i.e. jan=1, feb=2, etc
 83    pub fn numeric(self: Month) u4 {
 84        return @intFromEnum(self);
 85    }
 86};
 87
 88/// Get the number of days in the given month and year
 89pub fn getDaysInMonth(year: Year, month: Month) u5 {
 90    return switch (month) {
 91        .jan => 31,
 92        .feb => @as(u5, switch (isLeapYear(year)) {
 93            true => 29,
 94            false => 28,
 95        }),
 96        .mar => 31,
 97        .apr => 30,
 98        .may => 31,
 99        .jun => 30,
100        .jul => 31,
101        .aug => 31,
102        .sep => 30,
103        .oct => 31,
104        .nov => 30,
105        .dec => 31,
106    };
107}
108
109pub const YearAndDay = struct {
110    year: Year,
111    /// The number of days into the year (0 to 365)
112    day: u9,
113
114    pub fn calculateMonthDay(self: YearAndDay) MonthAndDay {
115        var month: Month = .jan;
116        var days_left = self.day;
117        while (true) {
118            const days_in_month = getDaysInMonth(self.year, month);
119            if (days_left < days_in_month)
120                break;
121            days_left -= days_in_month;
122            month = @as(Month, @enumFromInt(@intFromEnum(month) + 1));
123        }
124        return .{ .month = month, .day_index = @as(u5, @intCast(days_left)) };
125    }
126};
127
128pub const MonthAndDay = struct {
129    month: Month,
130    day_index: u5, // days into the month (0 to 30)
131};
132
133/// days since epoch Jan 1, 1970
134pub const EpochDay = struct {
135    day: u47, // u47 = u64 - u17 (because day = sec(u64) / secs_per_day(u17)
136    pub fn calculateYearDay(self: EpochDay) YearAndDay {
137        var year_day = self.day;
138        var year: Year = epoch_year;
139        while (true) {
140            const year_size = getDaysInYear(year);
141            if (year_day < year_size)
142                break;
143            year_day -= year_size;
144            year += 1;
145        }
146        return .{ .year = year, .day = @as(u9, @intCast(year_day)) };
147    }
148};
149
150/// seconds since start of day
151pub const DaySeconds = struct {
152    secs: u17, // max is 24*60*60 = 86400
153
154    /// the number of hours past the start of the day (0 to 23)
155    pub fn getHoursIntoDay(self: DaySeconds) u5 {
156        return @as(u5, @intCast(@divTrunc(self.secs, 3600)));
157    }
158    /// the number of minutes past the hour (0 to 59)
159    pub fn getMinutesIntoHour(self: DaySeconds) u6 {
160        return @as(u6, @intCast(@divTrunc(@mod(self.secs, 3600), 60)));
161    }
162    /// the number of seconds past the start of the minute (0 to 59)
163    pub fn getSecondsIntoMinute(self: DaySeconds) u6 {
164        return math.comptimeMod(self.secs, 60);
165    }
166};
167
168/// seconds since epoch Jan 1, 1970 at 12:00 AM
169pub const EpochSeconds = struct {
170    secs: u64,
171
172    /// Returns the number of days since the epoch as an EpochDay.
173    /// Use EpochDay to get information about the day of this time.
174    pub fn getEpochDay(self: EpochSeconds) EpochDay {
175        return EpochDay{ .day = @as(u47, @intCast(@divTrunc(self.secs, secs_per_day))) };
176    }
177
178    /// Returns the number of seconds into the day as DaySeconds.
179    /// Use DaySeconds to get information about the time.
180    pub fn getDaySeconds(self: EpochSeconds) DaySeconds {
181        return DaySeconds{ .secs = math.comptimeMod(self.secs, secs_per_day) };
182    }
183};
184
185fn testEpoch(secs: u64, expected_year_day: YearAndDay, expected_month_day: MonthAndDay, expected_day_seconds: struct {
186    /// 0 to 23
187    hours_into_day: u5,
188    /// 0 to 59
189    minutes_into_hour: u6,
190    /// 0 to 59
191    seconds_into_minute: u6,
192}) !void {
193    const epoch_seconds = EpochSeconds{ .secs = secs };
194    const epoch_day = epoch_seconds.getEpochDay();
195    const day_seconds = epoch_seconds.getDaySeconds();
196    const year_day = epoch_day.calculateYearDay();
197    try testing.expectEqual(expected_year_day, year_day);
198    try testing.expectEqual(expected_month_day, year_day.calculateMonthDay());
199    try testing.expectEqual(expected_day_seconds.hours_into_day, day_seconds.getHoursIntoDay());
200    try testing.expectEqual(expected_day_seconds.minutes_into_hour, day_seconds.getMinutesIntoHour());
201    try testing.expectEqual(expected_day_seconds.seconds_into_minute, day_seconds.getSecondsIntoMinute());
202}
203
204test "epoch decoding" {
205    try testEpoch(0, .{ .year = 1970, .day = 0 }, .{
206        .month = .jan,
207        .day_index = 0,
208    }, .{ .hours_into_day = 0, .minutes_into_hour = 0, .seconds_into_minute = 0 });
209
210    try testEpoch(31535999, .{ .year = 1970, .day = 364 }, .{
211        .month = .dec,
212        .day_index = 30,
213    }, .{ .hours_into_day = 23, .minutes_into_hour = 59, .seconds_into_minute = 59 });
214
215    try testEpoch(1622924906, .{ .year = 2021, .day = 31 + 28 + 31 + 30 + 31 + 4 }, .{
216        .month = .jun,
217        .day_index = 4,
218    }, .{ .hours_into_day = 20, .minutes_into_hour = 28, .seconds_into_minute = 26 });
219
220    try testEpoch(1625159473, .{ .year = 2021, .day = 31 + 28 + 31 + 30 + 31 + 30 }, .{
221        .month = .jul,
222        .day_index = 0,
223    }, .{ .hours_into_day = 17, .minutes_into_hour = 11, .seconds_into_minute = 13 });
224}