bugfixes in time and date handling
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 5 Sep 2023 00:21:07 +0000 (19:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 5 Sep 2023 00:21:07 +0000 (19:21 -0500)
core/time/date.onyx
core/time/time.onyx

index e84a90d5fa1a0aceef5902097316960b4a680aaf..aea4ba857e0ff846cc601115a57b0c4eabe86084 100644 (file)
@@ -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;
index a2e7c053699e9bffb3d7b9882eeb147a6ffe8bd6..f60f8da2d323fb202c287a1efd6b226e5fa79a37 100644 (file)
@@ -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
         };