shithub: scc

Download patch

ref: a763df064b21eb5e74e8fe723180f201a04a1382
parent: 8217c283951a053da7a26fd7dd03d66e14a71205
author: Hiltjo Posthuma <hiltjo@codemadness.org>
date: Fri Mar 25 16:51:50 EDT 2022

libc: gmtime: fix mday calculation for wrapping in the month

An example:

int
main(void)
{
	time_t t = 1617235200;
	struct tm *tm;

	// should be: 2021-04-01 02:00
	// scc showed: 2021-03-32 00:00
	tm = localtime(&t);
	printf("%04d-%02d-%02d %02d:%02d\n",
		tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
		tm->tm_hour, tm->tm_min);

	return 0;
}

Note that tm_yday already has the inclusive >= check and is correct.

--- a/src/libc/time/gmtime.c
+++ b/src/libc/time/gmtime.c
@@ -26,7 +26,7 @@
 	tm.tm_yday = day;
 
 	_daysmon[FEB] = FEBDAYS(tm.tm_year);
-	for (i = JAN; day > _daysmon[i]; i++)
+	for (i = JAN; day >= _daysmon[i]; i++)
 		day -= _daysmon[i];
 	tm.tm_mon = i;
 	tm.tm_mday = day + 1;