Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. (not a) bug in QDate::weekNbr()?

(not a) bug in QDate::weekNbr()?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 246 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    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:
    calendar.JPG

    Am I missing something, or is this a bug?

    Thanks...

    Christian EhrlicherC 1 Reply Last reply
    0
    • mzimmersM mzimmers

      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:
      calendar.JPG

      Am I missing something, or is this a bug?

      Thanks...

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      mzimmersM 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        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.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved