shithub: scc

Download patch

ref: 3ffe44ba0b84317b63bfcdbfe4bc1418f0ae59ca
parent: f8c319364a27e91e36a5142ad195f5c14192ccdc
author: Naveen Narayanan <[email protected]>
date: Mon Sep 28 14:26:08 EDT 2020

libc: Set tm_yday in mktime.c

This patch implements the correct behavior of mktime which is to
ignore the original value of tm_yday and set it appropriately post
normalization.

--- a/src/libc/time/mktime.c
+++ b/src/libc/time/mktime.c
@@ -32,7 +32,7 @@
 static int
 normalize(struct tm *tm)
 {
-	int mon, day, year;
+	int mon, day, year, yday;
 	struct tm aux = *tm;
 
 	if (!norm(&tm->tm_sec, &tm->tm_min, 60)   ||
@@ -43,6 +43,7 @@
 	}
 
 	day = tm->tm_mday;
+	yday = 0;
 	year = 1900 + tm->tm_year;
 	_daysmon[FEB] = FEBDAYS(year);
 
@@ -68,9 +69,13 @@
 		}
 	}
 
+	for (int i = 0; i < mon; i++)
+		yday += _daysmon[i];
+
 	tm->tm_mon = mon;
 	tm->tm_year = year - 1900;
 	tm->tm_mday = day;
+	tm->tm_yday = yday + day - 1;
 	tm->tm_wday = (_newyear(tm->tm_year) + tm->tm_yday) % 7;
 
 	return 1;