KPublicTransport

mergeutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "mergeutil_p.h"
8 
9 #include <QDateTime>
10 #include <QDebug>
11 #include <QString>
12 #include <QTimeZone>
13 #include <QUrl>
14 
15 using namespace KPublicTransport;
16 
17 static QDateTime applyTimeZone(QDateTime dt, const QDateTime &refDt)
18 {
19  if (dt.timeSpec() != Qt::LocalTime) {
20  return dt;
21  }
22 
23  if (refDt.timeSpec() == Qt::TimeZone) {
24  dt.setTimeZone(refDt.timeZone());
25  } else if (refDt.timeSpec() == Qt::OffsetFromUTC) {
26  dt.setOffsetFromUtc(refDt.offsetFromUtc());
27  }
28 
29  return dt;
30 }
31 
32 int MergeUtil::distance(const QDateTime& lhs, const QDateTime& rhs)
33 {
34  const auto ldt = applyTimeZone(lhs, rhs);
35  const auto rdt = applyTimeZone(rhs, lhs);
36  return std::abs(ldt.secsTo(rdt));
37 }
38 
39 bool MergeUtil::isBefore(const QDateTime& lhs, const QDateTime& rhs)
40 {
41  const auto ldt = applyTimeZone(lhs, rhs);
42  const auto rdt = applyTimeZone(rhs, lhs);
43  return ldt < rdt;
44 }
45 
46 QDateTime MergeUtil::mergeDateTimeEqual(const QDateTime &lhs, const QDateTime &rhs)
47 {
48  // anything is better than invalid, timezone is best
49  if (!rhs.isValid() || lhs.timeSpec() == Qt::TimeZone) {
50  return lhs;
51  }
52  if (!lhs.isValid() || rhs.timeSpec() == Qt::TimeZone) {
53  return rhs;
54  }
55 
56  // UTC offset it better than local time
57  if (lhs.timeSpec() == Qt::OffsetFromUTC || rhs.timeSpec() == Qt::LocalTime) {
58  return lhs;
59  }
60  return rhs;
61 }
62 
63 QDateTime MergeUtil::mergeDateTimeMax(const QDateTime &lhs, const QDateTime &rhs)
64 {
65  // if only one side is valid, prefer that one
66  if (!lhs.isValid()) {
67  return rhs;
68  }
69  if (!rhs.isValid()) {
70  return lhs;
71  }
72 
73  auto dt = isBefore(lhs, rhs) ? rhs : lhs;
74  if (dt.timeSpec() == Qt::TimeZone) {
75  return dt;
76  }
77 
78  // recover timezone of we can
79  if (lhs.timeSpec() == Qt::TimeZone) {
80  dt.setTimeZone(lhs.timeZone());
81  return dt;
82  } else if (rhs.timeSpec() == Qt::TimeZone) {
83  dt.setTimeZone(rhs.timeZone());
84  return dt;
85  }
86 
87  // recover UTC offset if we can
88  if (dt.timeSpec() == Qt::OffsetFromUTC) {
89  return dt;
90  } else if (lhs.timeSpec() == Qt::OffsetFromUTC) {
92  } else if (rhs.timeSpec() == Qt::OffsetFromUTC) {
94  }
95 
96  return dt;
97 }
98 
99 QString MergeUtil::mergeString(const QString &lhs, const QString &rhs)
100 {
101  // ### this is deterministic, but not necessarily ideal
102  // we could prefer mixed case over all caps, unicode over transliterated, etc
103  if (lhs.size() == rhs.size()) {
104  return lhs < rhs ? lhs : rhs;
105  }
106  return lhs.size() < rhs.size() ? rhs : lhs;
107 }
void setOffsetFromUtc(int offsetSeconds)
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
bool isBefore(const QVariant &lhs, const QVariant &rhs)
int size() const const
LocalTime
void setTimeZone(const QTimeZone &toZone)
Qt::TimeSpec timeSpec() const const
bool isValid() const const
int offsetFromUtc() const const
QTimeZone timeZone() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.