Вы должны знать день недели первым!
Для этого используйте алгоритм Гаусса вариации Крайтчика:
https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
dayofweek(y, m, d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
int k = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
return k == 0 ? 6 : (k-1); // This will return 0 for monday...6 for sunday
}
Затем номер вашей недели будет смещен относительно первого дня месяца:
w = (d - 1 + dayofweek(y, m, 1))/7 + 1;
вот пример кода, работающий на меня:
#include <stdio.h>
#include <stdint.h>
static const int days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const char* dayofws[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
static int dayofweek(int y, int m, int d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
int k = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
return k == 0 ? 6 : (k-1); // This will return 0 for monday...6 for sunday
}
void main (void)
{
int y = 2020;
for (int m = 1; m <= 12; m++)
{
for (int d = 1; d <= days[m]; d++)
{
int w = (d - 1 + dayofweek(y, m, 1))/7 + 1;
printf("%d ", dayofweek(y, m, 1));
printf("%d/%02d/%02d : %d (%s)\n", y, m, d, w, dayofws[dayofweek(y, m, d)]);
}
printf("\n");
}
}