(not a) bug in QDate::weekNbr()?
-
Hi all -
I know it's hard to believe, but I ran across this while starting to implement a date/time picker. I wrote a routine to calculate the number of weeks (including partials) in a given month, and ran into this when testing March, 2020.
#include <QDateTime> int weeksInMonth(int year, int month) { QDate qDate = QDate::currentDate(); int day; int firstWeek; int lastWeek; int nbrWeeks = 0; int yearNbr; do { day = 1; qDate.setDate(year, month, day); firstWeek = qDate.weekNumber(&yearNbr); if (yearNbr != year) { firstWeek = 0; } qDebug().nospace() << "first week for " << year << "/" << month << " is " << firstWeek; day = qDate.daysInMonth(); qDate.setDate(year, month, day); lastWeek = qDate.weekNumber(&yearNbr); if (yearNbr != year) { // this only can happen (to lastWeek) in December, // so we can derive the last week // based on the weekday of 12/1. if (qDate.dayOfWeek() >= 5) { // 5 here means Thursday lastWeek = firstWeek + 5; } else { lastWeek = firstWeek + 4; } } qDebug().nospace() << "last week for " << year << "/" << month << " is " << lastWeek; nbrWeeks = lastWeek - firstWeek + 1; } while (false); return nbrWeeks; } int main(int argc, char *argv[]) { int nbrWeeks; int year = 2020; int month = 3; nbrWeeks = weeksInMonth(year, month); qDebug().nospace() << "number of weeks in " << year << "/" << month << " is " << nbrWeeks; return 0; }
Here's the output:
first week for 2020/3 is 9 last week for 2020/3 is 14 number of weeks in 2020/3 is 6
And here's the real thing:
Am I missing something, or is this a bug?
Thanks...
-
Hi all -
I know it's hard to believe, but I ran across this while starting to implement a date/time picker. I wrote a routine to calculate the number of weeks (including partials) in a given month, and ran into this when testing March, 2020.
#include <QDateTime> int weeksInMonth(int year, int month) { QDate qDate = QDate::currentDate(); int day; int firstWeek; int lastWeek; int nbrWeeks = 0; int yearNbr; do { day = 1; qDate.setDate(year, month, day); firstWeek = qDate.weekNumber(&yearNbr); if (yearNbr != year) { firstWeek = 0; } qDebug().nospace() << "first week for " << year << "/" << month << " is " << firstWeek; day = qDate.daysInMonth(); qDate.setDate(year, month, day); lastWeek = qDate.weekNumber(&yearNbr); if (yearNbr != year) { // this only can happen (to lastWeek) in December, // so we can derive the last week // based on the weekday of 12/1. if (qDate.dayOfWeek() >= 5) { // 5 here means Thursday lastWeek = firstWeek + 5; } else { lastWeek = firstWeek + 4; } } qDebug().nospace() << "last week for " << year << "/" << month << " is " << lastWeek; nbrWeeks = lastWeek - firstWeek + 1; } while (false); return nbrWeeks; } int main(int argc, char *argv[]) { int nbrWeeks; int year = 2020; int month = 3; nbrWeeks = weeksInMonth(year, month); qDebug().nospace() << "number of weeks in " << year << "/" << month << " is " << nbrWeeks; return 0; }
Here's the output:
first week for 2020/3 is 9 last week for 2020/3 is 14 number of weeks in 2020/3 is 6
And here's the real thing:
Am I missing something, or is this a bug?
Thanks...
Please read the docs:
"Returns the ISO 8601 week number (1 to 53)."
https://en.wikipedia.org/wiki/ISO_8601#Week_dates
"...beginning with Monday and ending with Sunday. "
Just because the US thinks they can ignore the iso everywhere doesn't mean the iso changes just because of them.
-
Please read the docs:
"Returns the ISO 8601 week number (1 to 53)."
https://en.wikipedia.org/wiki/ISO_8601#Week_dates
"...beginning with Monday and ending with Sunday. "
Just because the US thinks they can ignore the iso everywhere doesn't mean the iso changes just because of them.
@Christian-Ehrlicher ah, yes. So, I guess I'll need to modify my routine to accept a start-of-week argument. Of course, this means I'll have to refine my calendar display as well. Thanks for pointing this out.
-
M mzimmers has marked this topic as solved on