[Qt-interest] Iterate over weeks of a timespan

Jorge Abrines curruscataphractus at gmail.com
Wed Jan 21 16:22:57 CET 2009


Hi Sven,

I think this snip attached may help you. Use 'qmake -project' to create sample 
project, 'qmake && make' to build it.

Regards,

Jorge

PD: Reply on any questions

Sven Grunewaldt wrote:
> Sven Grunewaldt schrieb:
>> 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
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> Oups, forgot to mention the position of the first problem.
> It occures here when the loop is not in the end year:
> 
>> 		if (i == endYear)
>> 			lastWeek = endDate.weekNumber();
>> 		else
>> 			lastWeek = QDate(i, 12, 31).weekNumber();
> 
> The last day of december could be the first week of the next year, but I
> need the last week of year "i". At least in my approach...
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 513 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090121/2a0f0ee4/attachment.bin 


More information about the Qt-interest-old mailing list