KD SOAP API Documentation  2.1
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-2022 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 
14 class KDDateTimeData : public QSharedData
15 {
16 public:
17  QString mTimeZone;
18 };
19 
21  : d(new KDDateTimeData)
22 {
23 }
24 
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 
38 {
39  if (this != &rhs) {
40  QDateTime::operator=(rhs);
41  d.operator=(rhs.d);
42  }
43  return *this;
44 }
45 
47 {
48 }
49 
50 KDDateTime::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  const int hours = timeZone.left(pos).toInt();
75  const int minutes = timeZone.mid(pos + 1).toInt();
76  const int offset = hours * 3600 + minutes * 60;
77  setOffsetFromUtc(offset);
78  }
79  }
80 }
81 
83 {
84  KDDateTime kdt;
85  QString tz;
86  QString baseString = s;
87  if (s.endsWith(QLatin1Char('Z'))) {
88  tz = QString::fromLatin1("Z");
89  baseString.chop(1);
90  } else {
91  QString maybeTz = s.right(6);
92  if (maybeTz.startsWith(QLatin1Char('+')) || maybeTz.startsWith(QLatin1Char('-'))) {
93  tz = maybeTz;
94  baseString.chop(6);
95  }
96  }
97  // qDebug() << "KDDateTime: Parsing" << baseString << "tz=" << tz;
98  kdt = QDateTime::fromString(baseString, Qt::ISODate);
99  kdt.setTimeZone(tz);
100  return kdt;
101 }
102 
104 {
105  QString str;
106  if (time().msec()) {
107  // include milli-seconds
108  str = toString(QLatin1String("yyyy-MM-ddThh:mm:ss.zzz"));
109  str += d->mTimeZone;
110  } else {
111  str = toString(Qt::ISODate); // includes the timezone (since Qt 4.8)
112  }
113  return str;
114 }
KDDateTime & operator=(const KDDateTime &)
Definition: KDDateTime.cpp:37
static KDDateTime fromDateString(const QString &s)
Definition: KDDateTime.cpp:82
QString toDateString() const
Definition: KDDateTime.cpp:103
QString timeZone() const
Definition: KDDateTime.cpp:55
void setTimeZone(const QString &timeZone)
Definition: KDDateTime.cpp:60

© 2010-2022 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 Tue Jun 13 2023 12:18:34 for KD SOAP API Documentation by doxygen 1.9.1