authorgravatar for dev@jhert.comJeremy Hertel <dev@jhert.com> 2024-09-01 20:07:15-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-03-08 14:25:28-05:00
log801a95035c4562bea1c8b80ae5fc8e05b9b22a2d
tree23f746e1ff2938b0ada6ad8621c47c559484e9e6
parenta5900e310e6424d32c641cacd2007cc504e8caf6

std.time.epoch: change getDaysInMonth to accept the year as an argument


3 files changed, 8 insertions(+), 13 deletions(-)

lib/std/crypto/Certificate.zig+1-2
......@@ -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+1-2
......@@ -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+6-9
......@@ -64,8 +64,6 @@ pub fn getDaysInYear(year: Year) u9 {
6464 return if (isLeapYear(year)) 366 else 365;
6565}
6666
67pub const YearLeapKind = enum(u1) { not_leap, leap };
68
6967pub 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
91pub fn getDaysInMonth(leap_year: YearLeapKind, month: Month) u5 {
88/// Get the number of days in the given month and year
89pub 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;