Skip to content

Commit 801a950

Browse files
e0ffandrewrk
authored andcommitted
std.time.epoch: change getDaysInMonth to accept the year as an argument
1 parent a5900e3 commit 801a950

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

lib/std/crypto/Certificate.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,10 @@ const Date = struct {
607607
}
608608

609609
{
610-
const is_leap = std.time.epoch.isLeapYear(date.year);
611610
var month: u4 = 1;
612611
while (month < date.month) : (month += 1) {
613612
const days: u64 = std.time.epoch.getDaysInMonth(
614-
@as(std.time.epoch.YearLeapKind, @enumFromInt(@intFromBool(is_leap))),
613+
date.year,
615614
@as(std.time.epoch.Month, @enumFromInt(month)),
616615
);
617616
sec += days * std.time.epoch.secs_per_day;

lib/std/os/uefi.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ pub const Time = extern struct {
141141
pub const unspecified_timezone: i16 = 0x7ff;
142142

143143
fn daysInYear(year: u16, max_month: u4) u9 {
144-
const leap_year: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
145144
var days: u9 = 0;
146145
var month: u4 = 0;
147146
while (month < max_month) : (month += 1) {
148-
days += std.time.epoch.getDaysInMonth(leap_year, @enumFromInt(month + 1));
147+
days += std.time.epoch.getDaysInMonth(year, @enumFromInt(month + 1));
149148
}
150149
return days;
151150
}

lib/std/time/epoch.zig

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ pub fn getDaysInYear(year: Year) u9 {
6464
return if (isLeapYear(year)) 366 else 365;
6565
}
6666

67-
pub const YearLeapKind = enum(u1) { not_leap, leap };
68-
6967
pub const Month = enum(u4) {
7068
jan = 1,
7169
feb,
@@ -87,13 +85,13 @@ pub const Month = enum(u4) {
8785
}
8886
};
8987

90-
/// Get the number of days in the given month
91-
pub fn getDaysInMonth(leap_year: YearLeapKind, month: Month) u5 {
88+
/// Get the number of days in the given month and year
89+
pub fn getDaysInMonth(year: Year, month: Month) u5 {
9290
return switch (month) {
9391
.jan => 31,
94-
.feb => @as(u5, switch (leap_year) {
95-
.leap => 29,
96-
.not_leap => 28,
92+
.feb => @as(u5, switch (isLeapYear(year)) {
93+
true => 29,
94+
false => 28,
9795
}),
9896
.mar => 31,
9997
.apr => 30,
@@ -116,9 +114,8 @@ pub const YearAndDay = struct {
116114
pub fn calculateMonthDay(self: YearAndDay) MonthAndDay {
117115
var month: Month = .jan;
118116
var days_left = self.day;
119-
const leap_kind: YearLeapKind = if (isLeapYear(self.year)) .leap else .not_leap;
120117
while (true) {
121-
const days_in_month = getDaysInMonth(leap_kind, month);
118+
const days_in_month = getDaysInMonth(self.year, month);
122119
if (days_left < days_in_month)
123120
break;
124121
days_left -= days_in_month;

0 commit comments

Comments
 (0)