KD SOAP API Documentation 2.2
Loading...
Searching...
No Matches
KDDateTime.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of the KD Soap project.
4**
5** SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6**
7** SPDX-License-Identifier: MIT
8**
9****************************************************************************/
10#include "KDDateTime.h"
11#include <QDebug>
12#include <QSharedData>
13
14class KDDateTimeData : public QSharedData
15{
16public:
17 QString mTimeZone;
18};
19
21 : d(new KDDateTimeData)
22{
23}
24
26 : QDateTime(rhs)
27 , d(rhs.d)
28{
29}
30
32 : QDateTime(rhs)
33 , d(new KDDateTimeData)
34{
35}
36
38{
39 if (this != &rhs) {
41 d.operator=(rhs.d);
42 }
43 return *this;
44}
45
49
50KDDateTime::operator QVariant() const
51{
52 return QVariant::fromValue(*this);
53}
54
56{
57 return d->mTimeZone;
58}
59
60void KDDateTime::setTimeZone(const QString &timeZone)
61{
62 d->mTimeZone = timeZone;
63
64 // Just in case someone cares: set the time spec in QDateTime accordingly.
65 // We can't do this the other way round, there's no public API for the offset-from-utc case.
66 if (timeZone == QLatin1String("Z")) {
68 } else if (timeZone.isEmpty()) {
70 } else {
72 const int pos = timeZone.indexOf(QLatin1Char(':'));
73 if (pos > 0) {
74#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
75 const int hours = timeZone.leftRef(pos).toInt();
76 const int minutes = timeZone.midRef(pos + 1).toInt();
77#else
78 const QStringView timeZoneView(timeZone);
79 const int hours = timeZoneView.first(pos).toInt();
80 const int minutes = timeZoneView.sliced(pos + 1).toInt();
81#endif
82 const int offset = hours * 3600 + minutes * 60;
83 setOffsetFromUtc(offset);
84 }
85 }
86}
87
89{
90 KDDateTime kdt;
91 QString tz;
92 QString baseString = s;
93 if (s.endsWith(QLatin1Char('Z'))) {
94 tz = QString::fromLatin1("Z");
95 baseString.chop(1);
96 } else {
97 QString maybeTz = s.right(6);
98 if (maybeTz.startsWith(QLatin1Char('+')) || maybeTz.startsWith(QLatin1Char('-'))) {
99 tz = std::move(maybeTz);
100 baseString.chop(6);
101 }
102 }
103 // qDebug() << "KDDateTime: Parsing" << baseString << "tz=" << tz;
104 kdt = QDateTime::fromString(baseString, Qt::ISODate);
105 kdt.setTimeZone(tz);
106 return kdt;
107}
108
110{
111 QString str;
112 if (time().msec()) {
113 // include milli-seconds
114 str = toString(QLatin1String("yyyy-MM-ddThh:mm:ss.zzz"));
115 str += d->mTimeZone;
116 } else {
117 str = toString(Qt::ISODate); // includes the timezone (since Qt 4.8)
118 }
119 return str;
120}
KDDateTime & operator=(const KDDateTime &)
static KDDateTime fromDateString(const QString &s)
QString toDateString() const
QString timeZone() const
void setTimeZone(const QString &timeZone)
QDateTime fromString(const QString &string, Qt::DateFormat format)
QDateTime & operator=(const QDateTime &other)
void setOffsetFromUtc(int offsetSeconds)
void setTimeSpec(Qt::TimeSpec spec)
QTime time() const const
QString toString(Qt::DateFormat format) const const
void chop(int n)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString fromLatin1(const char *str, int size)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QStringRef leftRef(int n) const const
QStringRef midRef(int position, int n) const const
QString right(int n) const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QChar first() const const
int toInt(bool *ok, int base) const const
QVariant fromValue(const T &value)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-soap/
Generated on Mon Jul 8 2024 00:15:16 for KD SOAP API Documentation by doxygen 1.9.8