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 {...@@ -607,11 +607,10 @@ const Date = struct {
607 }607 }
608608
609 {609 {
610 const is_leap = std.time.epoch.isLeapYear(date.year);
611 var month: u4 = 1;610 var month: u4 = 1;
612 while (month < date.month) : (month += 1) {611 while (month < date.month) : (month += 1) {
613 const days: u64 = std.time.epoch.getDaysInMonth(612 const days: u64 = std.time.epoch.getDaysInMonth(
614 @as(std.time.epoch.YearLeapKind, @enumFromInt(@intFromBool(is_leap))),613 date.year,
615 @as(std.time.epoch.Month, @enumFromInt(month)),614 @as(std.time.epoch.Month, @enumFromInt(month)),
616 );615 );
617 sec += days * std.time.epoch.secs_per_day;616 sec += days * std.time.epoch.secs_per_day;
lib/std/os/uefi.zig+1-2
...@@ -141,11 +141,10 @@ pub const Time = extern struct {...@@ -141,11 +141,10 @@ pub const Time = extern struct {
141 pub const unspecified_timezone: i16 = 0x7ff;141 pub const unspecified_timezone: i16 = 0x7ff;
142142
143 fn daysInYear(year: u16, max_month: u4) u9 {143 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;
145 var days: u9 = 0;144 var days: u9 = 0;
146 var month: u4 = 0;145 var month: u4 = 0;
147 while (month < max_month) : (month += 1) {146 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));
149 }148 }
150 return days;149 return days;
151 }150 }
lib/std/time/epoch.zig+6-9
...@@ -64,8 +64,6 @@ pub fn getDaysInYear(year: Year) u9 {...@@ -64,8 +64,6 @@ pub fn getDaysInYear(year: Year) u9 {
64 return if (isLeapYear(year)) 366 else 365;64 return if (isLeapYear(year)) 366 else 365;
65}65}
6666
67pub const YearLeapKind = enum(u1) { not_leap, leap };
68
69pub const Month = enum(u4) {67pub const Month = enum(u4) {
70 jan = 1,68 jan = 1,
71 feb,69 feb,
...@@ -87,13 +85,13 @@ pub const Month = enum(u4) {...@@ -87,13 +85,13 @@ pub const Month = enum(u4) {
87 }85 }
88};86};
8987
90/// Get the number of days in the given month88/// Get the number of days in the given month and year
91pub fn getDaysInMonth(leap_year: YearLeapKind, month: Month) u5 {89pub fn getDaysInMonth(year: Year, month: Month) u5 {
92 return switch (month) {90 return switch (month) {
93 .jan => 31,91 .jan => 31,
94 .feb => @as(u5, switch (leap_year) {92 .feb => @as(u5, switch (isLeapYear(year)) {
95 .leap => 29,93 true => 29,
96 .not_leap => 28,94 false => 28,
97 }),95 }),
98 .mar => 31,96 .mar => 31,
99 .apr => 30,97 .apr => 30,
...@@ -116,9 +114,8 @@ pub const YearAndDay = struct {...@@ -116,9 +114,8 @@ pub const YearAndDay = struct {
116 pub fn calculateMonthDay(self: YearAndDay) MonthAndDay {114 pub fn calculateMonthDay(self: YearAndDay) MonthAndDay {
117 var month: Month = .jan;115 var month: Month = .jan;
118 var days_left = self.day;116 var days_left = self.day;
119 const leap_kind: YearLeapKind = if (isLeapYear(self.year)) .leap else .not_leap;
120 while (true) {117 while (true) {
121 const days_in_month = getDaysInMonth(leap_kind, month);118 const days_in_month = getDaysInMonth(self.year, month);
122 if (days_left < days_in_month)119 if (days_left < days_in_month)
123 break;120 break;
124 days_left -= days_in_month;121 days_left -= days_in_month;