KCalendarCore

utils.cpp
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2017 Daniel Vr├ítil <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "utils_p.h"
10 
11 #include <QDataStream>
12 #include <QTimeZone>
13 
14 // To remain backwards compatible we need to (de)serialize QDateTime the way KDateTime
15 // was (de)serialized
16 void KCalendarCore::serializeQDateTimeAsKDateTime(QDataStream &out, const QDateTime &dt)
17 {
18  out << dt.date() << dt.time();
19  switch (dt.timeSpec()) {
20  case Qt::UTC:
21  out << static_cast<quint8>('u');
22  break;
23  case Qt::OffsetFromUTC:
24  out << static_cast<quint8>('o') << dt.offsetFromUtc();
25  break;
26  case Qt::TimeZone:
27  serializeQTimeZoneAsSpec(out, dt.timeZone());
28  break;
29  case Qt::LocalTime:
30  out << static_cast<quint8>('c');
31  break;
32  }
33  const bool isDateOnly = dt.date().isValid() && !dt.time().isValid();
34  out << quint8(isDateOnly ? 0x01 : 0x00);
35 }
36 
37 void KCalendarCore::deserializeKDateTimeAsQDateTime(QDataStream &in, QDateTime &dt)
38 {
39  QDate date;
40  QTime time;
41  quint8 ts, flags;
42 
43  in >> date >> time >> ts;
44  switch (static_cast<uchar>(ts)) {
45  case 'u':
46  dt = QDateTime(date, time, Qt::UTC);
47  break;
48  case 'o': {
49  int offset;
50  in >> offset;
51  dt = QDateTime(date, time, Qt::OffsetFromUTC, offset);
52  break;
53  }
54  case 'z': {
55  QString tzid;
56  in >> tzid;
57  dt = QDateTime(date, time, QTimeZone(tzid.toUtf8()));
58  break;
59  }
60  case 'c':
61  dt = QDateTime(date, time, Qt::LocalTime);
62  break;
63  }
64 
65  // unused, we don't have a special handling for date-only QDateTime
66  in >> flags;
67 }
68 
69 void KCalendarCore::serializeQTimeZoneAsSpec(QDataStream &out, const QTimeZone &tz)
70 {
71  out << static_cast<quint8>('z') << (tz.isValid() ? QString::fromUtf8(tz.id()) : QString());
72 }
73 
74 void KCalendarCore::deserializeSpecAsQTimeZone(QDataStream &in, QTimeZone &tz)
75 {
76  quint8 ts;
77  in >> ts;
78  switch (static_cast<uchar>(ts)) {
79  case 'u':
80  tz = QTimeZone::utc();
81  return;
82  case 'o': {
83  int offset;
84  in >> offset;
85  tz = QTimeZone(offset);
86  return;
87  }
88  case 'z': {
89  QString tzid;
90  in >> tzid;
91  tz = QTimeZone(tzid.toUtf8());
92  return;
93  }
94  case 'c':
96  break;
97  }
98 }
99 
100 void KCalendarCore::serializeQDateTimeList(QDataStream &out, const QList<QDateTime> &list)
101 {
102  out << list.size();
103  for (const auto &i : list) {
104  serializeQDateTimeAsKDateTime(out, i);
105  }
106 }
107 
108 void KCalendarCore::deserializeQDateTimeList(QDataStream &in, QList<QDateTime> &list)
109 {
110  int size;
111  in >> size;
112  list.clear();
113  list.reserve(size);
114  for (int i = 0; i < size; ++i) {
115  QDateTime dt;
116  deserializeKDateTimeAsQDateTime(in, dt);
117  list << dt;
118  }
119 }
void clear()
void reserve(int alloc)
bool isValid() const const
QTime time() const const
int size() const const
QString fromUtf8(const char *str, int size)
QTimeZone utc()
QTimeZone systemTimeZone()
bool isValid() const const
Qt::TimeSpec timeSpec() const const
QByteArray id() const const
QDate date() 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 Sun Apr 18 2021 22:50:51 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.