From: Brendan Hansen Date: Tue, 5 Sep 2023 00:21:07 +0000 (-0500) Subject: bugfixes in time and date handling X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2b2761df068d476cc85ec9a2e5340485b06ff323;p=onyx.git bugfixes in time and date handling --- diff --git a/core/time/date.onyx b/core/time/date.onyx index e84a90d5..aea4ba85 100644 --- a/core/time/date.onyx +++ b/core/time/date.onyx @@ -21,15 +21,20 @@ Date :: struct { return now()->as_date(); } - add_months :: (d: Date, days: i32) -> Date { + add_months :: (d: Date, months: i32) -> Date { nd := d; - nd.month += 1; - if nd.month >= 12 { - nd.month = 0; + nd.month += months; + while nd.month >= 12 { + nd.month -= 12; nd.year += 1; } + while nd.month < 0 { + nd.month += 12; + nd.year -= 1; + } + return nd; } @@ -57,6 +62,32 @@ Date :: struct { return nd; } + day_of_week :: (d: Date) -> i32 { + if d.year < 1700 do return -1; + + #persist month_key := i32.[ + 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 + ]; + + #persist century_key := i32.[ + 4, 2, 0, 6, 4, 2, 0 + ]; + + dig := d.year % 100; + dig += dig / 4; + dig += month_key[d.month]; + dig += century_key[(d.year / 100) - 17]; + dig += d.day; + + if d.year % 4 == 0 && (d.year % 100 != 0 || d.year % 400 == 0) { + if d.month == 0 || d.month == 1 { + dig -= 1; + } + } + + return dig % 7; + } + is_before :: (d1, d2: Date) -> bool { if d1.year != d2.year do return d1.year < d2.year; if d1.month != d2.month do return d1.month < d2.month; diff --git a/core/time/time.onyx b/core/time/time.onyx index a2e7c053..f60f8da2 100644 --- a/core/time/time.onyx +++ b/core/time/time.onyx @@ -28,7 +28,7 @@ Timestamp :: struct #size (sizeof u32 * 12) { from_date :: (d: Date) -> Timestamp { return .{ - year = d.year + 1900, + year = d.year - 1900, mday = d.day, mon = d.month };