QDateTime ts QDateTime currentDateTimeUtc QDateTime t1 ts addSecs -5 Q

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
QDateTime ts = QDateTime::currentDateTimeUtc();
QDateTime t1 = ts.addSecs(-5);
QDateTime t2 = ts.addSecs(5);
QVector<QDateTime> timeline;
int boo[] = {1,2,3,4,5,6};
foreach(int a, boo)
{
timeline.append(t1.addSecs(a));
}
QDateTime nearestTimestamp = QDateTime.fromMSecsSinceEpoch(0);
qint64 nearestRange = 999999999;
foreach(const QDateTime & curr, timeline)
{
qint64 range = qAbs(curr.msecsTo(ts));
if(range < nearestRange)
{
nearestRange = range;
nearestTimestamp = curr;
}
}