Saturday, July 5th 2008, 6:35am UTC+1

You are not logged in.

  • Login
  • Register

1

Thursday, May 15th 2008, 11:17am

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:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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:
Example::Example(QObject *parent) : QObject(parent) {
 dt.setDate(QDate(2008, 5, 15));
 dt.setTime(QTime(11, 38));
 int secs = 1;
 int msecs = secs * 1000;
    timer_check = new QTimer(this);
    connect(timer_check, SIGNAL(timeout()), SLOT(doSomething()));
    timer_check->start(msecs);
 cout << "Timer started" << endl;
}

Example::~Example() {

}

void Example::doSomething() {
 emit checkTime();
}

void Example::checkTime() {
 QDateTime dt_cur = QDateTime::currentDateTime();
 QTime t =  dt_cur.time();
 QDate d =  dt_cur.date();
 if(dt_cur == dt)
  cout << "Time reached ;-)" << endl;
 if(dt.date() == d) {
  cout << "Date is equal" << endl;
 }
 if(dt.time() == t) {
  cout << "Time is equal" << endl;
 }
 else {
  if(dt.time() > t)
   qDebug() << dt.time() << " > " << t;
  else if(dt.time() >= t)
   qDebug() << dt.time() << " >= " << t;
  else if(dt.time() < t)
   qDebug() << dt.time() << " < " << t;
  else if(dt.time() <= t)
   qDebug() << dt.time() << " <= " << t;
  else if(dt.time() == t)
   qDebug() << dt.time() << " == " << t;
  else
   qDebug() << dt.time() << " ?? " << t;
 }
}


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

Regards
N1Rulez
  • Go to the top of the page

Posts: 59

Location: Belarus/Germany

Occupation: Progr

2

Thursday, May 15th 2008, 11:52am

Quoted

Note: This function compares for strict equality of the QDateTime object. Use QDateTime::toUTC(), instead if you would like to compare times.


or better:

Quoted


uint QDateTime::toTime_t () const

Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).

On systems that do not support timezones, this function will behave as if local time were Qt::UTC.
  • Go to the top of the page

3

Thursday, May 15th 2008, 11:55am

RE: Problem with comparing two QDateTime objects

the difference is probably due to ms
Nicolas
  • Go to the top of the page

Rate this thread