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. Get the correct date days passed after current date

Get the correct date days passed after current date

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 788 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.
  • HolidayH Offline
    HolidayH Offline
    Holiday
    wrote on last edited by
    #1

    I faced with interesting problem
    I'm trying to calculate days diff between current date and previous saved

    Here is code
    QDateTime oNow = QDateTime::currentDateTime();
    QDateTime prev  = oNow.addDays(-50);
    // Can't use daysTo() here because it counts how many 'midnights' there are between the two dates
    
    qreal oTimeUnitsBefore = oNow.secsTo(prev) / (3600 * 24);
    

    Result will be -49, because DST used.

    Any thoughts how can I get a correct value?

    KroMignonK 1 Reply Last reply
    0
    • HolidayH Holiday

      @mrjj Thanks, that's work!
      But what if cvb will be the LocalTime value (as it reads from file from as string and converts to QDateTime with QDateTime::fromString() funct)? ToUtc() doesn't help in this case

      auto withTZ =  QDateTime::currentDateTime();
      auto current = QDateTime::currentDateTimeUtc();
      
      QDateTime prev = withTZ.addDays(-50);
      qint64  v1 = prev.toUTC().secsTo(current);
      qreal v2 = (3600 * 24);
      qreal v3 = v1 / v2;
      qDebug() << v1 << "/" << v2 << "=" << v3;
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @Holiday
      Hi. I don't know what it is you actually wish to calculate. I don't know what you mean by "days"?! Once you deal in local times and Daylight hour changes the meaning of "day" becomes ambiguous. There are two obvious definitions for "day" (there are others, connected with the sun or The Universe!):

      • 24 hours
      • The time/number of hours between a (local) wall-clock time-of-day one day and the same wall-clock time-of-day on another day.

      The first, 24 hours, is nice and easy! The second is not: one "day" per year has 23 hours, another has 25 hours, one day per year has two "2:30AM" wall-clock times and another day per year has no "2:30AM" wall clock time at all!

      You can only safely use the 3600 * 24 when you are talking about UTC, where every day has 24 hours. And that is what @mrjj's code is using. If you convert your local times to UTC and use that 24 hour division we know where we are.

      If you want to use local times and allow the two dates to cross daylight saving boundaries I think you have to be very exact about what answer you are expecting/wanting.

      1 Reply Last reply
      3
      • HolidayH Holiday

        I faced with interesting problem
        I'm trying to calculate days diff between current date and previous saved

        Here is code
        QDateTime oNow = QDateTime::currentDateTime();
        QDateTime prev  = oNow.addDays(-50);
        // Can't use daysTo() here because it counts how many 'midnights' there are between the two dates
        
        qreal oTimeUnitsBefore = oNow.secsTo(prev) / (3600 * 24);
        

        Result will be -49, because DST used.

        Any thoughts how can I get a correct value?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @Holiday said in Get the correct date days passed after current date:

        qreal oTimeUnitsBefore = oNow.secsTo(prev) / (3600 * 24);

        ==> this is wrong, you are dividing integer with integer, so the result will be an integer.

        If you want result to be a qreal, you should do:

         qreal oTimeUnitsBefore = oNow.secsTo(prev) / (3600 * 24.0);
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • HolidayH Offline
          HolidayH Offline
          Holiday
          wrote on last edited by Holiday
          #3

          Thanks for pointing that. But that's not the root cause and won't fix my problem

          KroMignonK 1 Reply Last reply
          0
          • HolidayH Holiday

            Thanks for pointing that. But that's not the root cause and won't fix my problem

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #4

            @Holiday said in Get the correct date days passed after current date:

            Thanks for pointing that. But that's not the root cause and won't fix my problem

            So I don't understand what your problem is.

            My understanding was you've got -49, but you expected -50 days.

            I am pretty sure qRound(oNow.secsTo(prev) / (3600 * 24.0)) will return -50.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • HolidayH Offline
              HolidayH Offline
              Holiday
              wrote on last edited by Holiday
              #5

              qRound yes, as it round value.

              Ok, let's change this example to excape qreal value.

              QDateTime zx = QDateTime::currentDateTime();
              QDateTime cvb = oNow.addDays(-50);
              
              int val = cvb.secsTo(zx) / (3600 * 24);
              

              As I set up -50 days, I'm expecting, that val will be 50, but due DST I receiving 49, because I got 50 days in seconds - 2 hour (DST change)

              P.S. And thanks for helping me with that

              mrjjM 1 Reply Last reply
              0
              • HolidayH Holiday

                qRound yes, as it round value.

                Ok, let's change this example to excape qreal value.

                QDateTime zx = QDateTime::currentDateTime();
                QDateTime cvb = oNow.addDays(-50);
                
                int val = cvb.secsTo(zx) / (3600 * 24);
                

                As I set up -50 days, I'm expecting, that val will be 50, but due DST I receiving 49, because I got 50 days in seconds - 2 hour (DST change)

                P.S. And thanks for helping me with that

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #6

                Hi
                So you want the class to ignore DST ?
                https://doc.qt.io/qt-5/qt.html#TimeSpec-enum
                ("Neither UTC nor OffsetFromUTC has any transitions.")

                    QDateTime oNow = QDateTime::currentDateTimeUtc();
                    QDateTime cvb = oNow.addDays(-50);
                    qint64  v1 = cvb.secsTo(oNow);
                    qreal v2 = (3600 * 24);
                    qreal v3 = v1 /  v2;
                    qDebug() << v1 << "/" << v2 << "=" << v3;
                

                4320000 / 86400 = 50

                ps. Not sure this is what you want :)

                1 Reply Last reply
                4
                • HolidayH Offline
                  HolidayH Offline
                  Holiday
                  wrote on last edited by
                  #7

                  @mrjj Thanks, that's work!
                  But what if cvb will be the LocalTime value (as it reads from file from as string and converts to QDateTime with QDateTime::fromString() funct)? ToUtc() doesn't help in this case

                  auto withTZ =  QDateTime::currentDateTime();
                  auto current = QDateTime::currentDateTimeUtc();
                  
                  QDateTime prev = withTZ.addDays(-50);
                  qint64  v1 = prev.toUTC().secsTo(current);
                  qreal v2 = (3600 * 24);
                  qreal v3 = v1 / v2;
                  qDebug() << v1 << "/" << v2 << "=" << v3;
                  
                  JonBJ 1 Reply Last reply
                  0
                  • HolidayH Holiday

                    @mrjj Thanks, that's work!
                    But what if cvb will be the LocalTime value (as it reads from file from as string and converts to QDateTime with QDateTime::fromString() funct)? ToUtc() doesn't help in this case

                    auto withTZ =  QDateTime::currentDateTime();
                    auto current = QDateTime::currentDateTimeUtc();
                    
                    QDateTime prev = withTZ.addDays(-50);
                    qint64  v1 = prev.toUTC().secsTo(current);
                    qreal v2 = (3600 * 24);
                    qreal v3 = v1 / v2;
                    qDebug() << v1 << "/" << v2 << "=" << v3;
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Holiday
                    Hi. I don't know what it is you actually wish to calculate. I don't know what you mean by "days"?! Once you deal in local times and Daylight hour changes the meaning of "day" becomes ambiguous. There are two obvious definitions for "day" (there are others, connected with the sun or The Universe!):

                    • 24 hours
                    • The time/number of hours between a (local) wall-clock time-of-day one day and the same wall-clock time-of-day on another day.

                    The first, 24 hours, is nice and easy! The second is not: one "day" per year has 23 hours, another has 25 hours, one day per year has two "2:30AM" wall-clock times and another day per year has no "2:30AM" wall clock time at all!

                    You can only safely use the 3600 * 24 when you are talking about UTC, where every day has 24 hours. And that is what @mrjj's code is using. If you convert your local times to UTC and use that 24 hour division we know where we are.

                    If you want to use local times and allow the two dates to cross daylight saving boundaries I think you have to be very exact about what answer you are expecting/wanting.

                    1 Reply Last reply
                    3
                    • HolidayH Offline
                      HolidayH Offline
                      Holiday
                      wrote on last edited by
                      #9

                      @JonB
                      Thanks for help. Yes, the main question in my problem was: what we mean by "days". 24 hour or days between midnight. When I found an answer on this question, I found a solution.
                      Huge thanks everyone for help!

                      1 Reply Last reply
                      1

                      • Login

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