Results 1 to 2 of 2

Thread: Problem with comparing two QDateTime objects

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Problem with comparing two QDateTime objects

    Hey @all,

    i've found a misterious problem, or is this thing a bug, or i have something miss unterstood.

    I created an example where a function/slot is called on a specified date and time, but the time (QTime) doesn't equal at any time.
    Here is the output:

    Timer started
    Date is equal
    QTime("11:38:00") > QTime("11:37:57")
    Date is equal
    QTime("11:38:00") > QTime("11:37:58")
    Date is equal
    QTime("11:38:00") > QTime("11:37:59")
    Date is equal
    QTime("11:38:00") < QTime("11:38:00") =====> Here the time must also be equal, but why not?
    Date is equal
    QTime("11:38:00") < QTime("11:38:01")
    Date is equal
    QTime("11:38:00") < QTime("11:38:02")
    Date is equal
    QTime("11:38:00") < QTime("11:38:03")

    And here is the source where i check these things:
    Qt Code:
    1. Example::Example(QObject *parent) : QObject(parent) {
    2. dt.setDate(QDate(2008, 5, 15));
    3. dt.setTime(QTime(11, 38));
    4. int secs = 1;
    5. int msecs = secs * 1000;
    6. timer_check = new QTimer(this);
    7. connect(timer_check, SIGNAL(timeout()), SLOT(doSomething()));
    8. timer_check->start(msecs);
    9. cout << "Timer started" << endl;
    10. }
    11.  
    12. Example::~Example() {
    13.  
    14. }
    15.  
    16. void Example::doSomething() {
    17. emit checkTime();
    18. }
    19.  
    20. void Example::checkTime() {
    21. QDateTime dt_cur = QDateTime::currentDateTime();
    22. QTime t = dt_cur.time();
    23. QDate d = dt_cur.date();
    24. if(dt_cur == dt)
    25. cout << "Time reached ;-)" << endl;
    26. if(dt.date() == d) {
    27. cout << "Date is equal" << endl;
    28. }
    29. if(dt.time() == t) {
    30. cout << "Time is equal" << endl;
    31. }
    32. else {
    33. if(dt.time() > t)
    34. qDebug() << dt.time() << " > " << t;
    35. else if(dt.time() >= t)
    36. qDebug() << dt.time() << " >= " << t;
    37. else if(dt.time() < t)
    38. qDebug() << dt.time() << " < " << t;
    39. else if(dt.time() <= t)
    40. qDebug() << dt.time() << " <= " << t;
    41. else if(dt.time() == t)
    42. qDebug() << dt.time() << " == " << t;
    43. else
    44. qDebug() << dt.time() << " ?? " << t;
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    Why does the time/datetime doesn't match? Do I something wrong within the comparison?

    Regards
    NoRulez

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with comparing two QDateTime objects

    According to me you are facing the same problem as comparing real numbers. QTime considers also miliseconds and you'll have a tough time trying to match them as well

    You can overcome the problem by:
    Qt Code:
    1. QTime t1;
    2. QTime t2;
    3. if(t2.secsTo(t1)==0){
    4. qDebug() << "Equal";
    5. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.