KD SOAP API Documentation: KDDateTime.cpp Source File

| 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
20KDDateTime::KDDateTime()
21 : d(new KDDateTimeData)
22{
23}
24
25 KDDateTime::KDDateTime(const KDDateTime &rhs)
26 : QDateTime(rhs)
27 , d(rhs.d)
28{
29}
30
31 KDDateTime::KDDateTime(const QDateTime &rhs)
32 : QDateTime(rhs)
33 , d(new KDDateTimeData)
34{
35}
36
37 KDDateTime &KDDateTime::operator=(const KDDateTime &rhs)
38{
39 if (this != &rhs) {
40 QDateTime::operator=(rhs);
41 d.operator=(rhs.d);
42 }
43 return *this;
44}
45
46KDDateTime::~KDDateTime()
47{
48}
49
50KDDateTime::operator QVariant() const
51{
52 return QVariant::fromValue(*this);
53}
54
55 QString KDDateTime::timeZone() const
56{
57 return d->mTimeZone;
58}
59
60 void 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")) {
67 setTimeSpec(Qt::UTC);
68 } else if (timeZone.isEmpty()) {
69 setTimeSpec(Qt::LocalTime);
70 } else {
71 setTimeSpec(Qt::OffsetFromUTC);
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
88 KDDateTime KDDateTime::fromDateString(const QString &s)
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
109 QString KDDateTime::toDateString() const
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.h
KDDateTime
Definition KDDateTime.h:28
KDDateTime::operator=
KDDateTime & operator=(const KDDateTime &)
Definition KDDateTime.cpp:37
KDDateTime::fromDateString
static KDDateTime fromDateString(const QString &s)
Definition KDDateTime.cpp:88
KDDateTime::KDDateTime
KDDateTime()
Definition KDDateTime.cpp:20
KDDateTime::toDateString
QString toDateString() const
Definition KDDateTime.cpp:109
KDDateTime::~KDDateTime
~KDDateTime()
Definition KDDateTime.cpp:46
KDDateTime::timeZone
QString timeZone() const
Definition KDDateTime.cpp:55
KDDateTime::setTimeZone
void setTimeZone(const QString &timeZone)
Definition KDDateTime.cpp:60
QDateTime
QDateTime::fromString
QDateTime fromString(const QString &string, Qt::DateFormat format)
QDateTime::operator=
QDateTime & operator=(const QDateTime &other)
QDateTime::setOffsetFromUtc
void setOffsetFromUtc(int offsetSeconds)
QDateTime::setTimeSpec
void setTimeSpec(Qt::TimeSpec spec)
QDateTime::time
QTime time() const const
QDateTime::toString
QString toString(Qt::DateFormat format) const const
QLatin1Char
QLatin1String
QSharedData
QString
QString::chop
void chop(int n)
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString::isEmpty
bool isEmpty() const const
QString::leftRef
QStringRef leftRef(int n) const const
QString::midRef
QStringRef midRef(int position, int n) const const
QString::right
QString right(int n) const const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringRef::toInt
int toInt(bool *ok, int base) const const
QStringView
QStringView::first
QChar first() const const
Qt::ISODate
ISODate
Qt::UTC
UTC
QVariant
QVariant::fromValue
QVariant fromValue(const T &value)

© 2010-2024 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 Sun Jan 7 2024 11:47:36 for KD SOAP API Documentation by doxygen 1.9.8