KPublicTransport

mergeutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
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
15using namespace KPublicTransport;
16
17static 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) {
27 }
28
29 return dt;
30}
31
32int 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
39bool 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
46QDateTime 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
63QDateTime 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
99QString 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}
bool isBefore(const QVariant &lhs, const QVariant &rhs)
Query operations and data types for accessing realtime public transport information from online servi...
bool isValid() const const
int offsetFromUtc() const const
void setTimeZone(const QTimeZone &toZone)
Qt::TimeSpec timeSpec() const const
QTimeZone timeZone() const const
qsizetype size() const const
LocalTime
QTimeZone fromSecondsAheadOfUtc(int offset)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.