[Qt-interest] Iterate over weeks of a timespan
Sven Grunewaldt
strayer at olle-orks.org
Wed Jan 21 14:05:16 CET 2009
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...
More information about the Qt-interest-old
mailing list