[Qt-interest] Iterate over weeks of a timespan

Sven Grunewaldt strayer at olle-orks.org
Wed Jan 21 13:59:55 CET 2009


Hi,

I need to iterate over all the weeks of a specific time period
(f.e. 09.2006 to 06.2009)

My current approach:
	int startYear = 2006;
	int startMonth = 9;
	int endYear = 2009;
	int endMonth = 6;
	QDate startDate = QDate(startYear, startMonth, 1);
	QDate endDate = QDate(endYear, endMonth, 30); // I know that the  day
value here is stupid, it will be replaced by a Date Edit

	for (int i = startYear; i <= endYear; i++)
	{
		int firstWeek;
		int lastWeek;

		if (i == startYear)
			firstWeek = startDate.weekNumber();
		else
			firstWeek = QDate(i, 1, 1).weekNumber();

		if (i == endYear)
			lastWeek = endDate.weekNumber();
		else
			lastWeek = QDate(i, 12, 31).weekNumber();
		
		for (int j = firstWeek; j <= lastWeek; j++)
			do_something(i, j);
	}

The first problem is:
I can't figure out the last Week of a year.

The second problem is:
If the startMonth or endMonth happens to be at the very beginning or end
of a year, the week could 53 for the startMonth or 1 for the endMonth,
which breaks the whole for-loop.
I think I can solve this for myself, I only mention it to avoid confusion.

Sadly I'm at a complete loss how to solve this puzzle.
It would be great if someone could give me a pointer how to do it correctly.
Regards,
Sven Grunewaldt



More information about the Qt-interest-old mailing list