KCalendarCore

utils.cpp
1/*
2 This file is part of the kcalcore library.
3
4 SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
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
16void 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;
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
37void KCalendarCore::deserializeKDateTimeAsQDateTime(QDataStream &in, QDateTime &dt)
38{
39 QDate date;
40 QTime time;
41 quint8 ts;
42 quint8 flags;
43
44 in >> date >> time >> ts;
45 switch (static_cast<uchar>(ts)) {
46 case 'u':
47 dt = QDateTime(date, time, QTimeZone::UTC);
48 break;
49 case 'o': {
50 int offset;
51 in >> offset;
52 dt = QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offset));
53 break;
54 }
55 case 'z': {
56 QString tzid;
57 in >> tzid;
58 dt = QDateTime(date, time, QTimeZone(tzid.toUtf8()));
59 break;
60 }
61 case 'c':
62 dt = QDateTime(date, time, QTimeZone::LocalTime);
63 break;
64 }
65
66 // unused, we don't have a special handling for date-only QDateTime
67 in >> flags;
68}
69
70QDateTime KCalendarCore::deserializeKDateTimeAsQDateTime(QDataStream &in)
71{
72 QDateTime dt;
73 QDate date;
74 QTime time;
75 quint8 ts;
76 quint8 flags;
77
78 in >> date >> time >> ts;
79 switch (static_cast<uchar>(ts)) {
80 case 'u':
81 dt = QDateTime(date, time, QTimeZone::UTC);
82 break;
83 case 'o': {
84 int offset;
85 in >> offset;
86 dt = QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offset));
87 break;
88 }
89 case 'z': {
90 QString tzid;
91 in >> tzid;
92 dt = QDateTime(date, time, QTimeZone(tzid.toUtf8()));
93 break;
94 }
95 case 'c':
96 dt = QDateTime(date, time, QTimeZone::LocalTime);
97 break;
98 }
99
100 // unused, we don't have a special handling for date-only QDateTime
101 in >> flags;
102
103 return dt;
104}
105
106void KCalendarCore::serializeQTimeZoneAsSpec(QDataStream &out, const QTimeZone &tz)
107{
108 out << static_cast<quint8>('z') << (tz.isValid() ? QString::fromUtf8(tz.id()) : QString());
109}
110
111void KCalendarCore::deserializeSpecAsQTimeZone(QDataStream &in, QTimeZone &tz)
112{
113 quint8 ts;
114 in >> ts;
115 switch (static_cast<uchar>(ts)) {
116 case 'u':
117 tz = QTimeZone::utc();
118 return;
119 case 'o': {
120 int offset;
121 in >> offset;
122 tz = QTimeZone(offset);
123 return;
124 }
125 case 'z': {
126 QString tzid;
127 in >> tzid;
128 tz = QTimeZone(tzid.toUtf8());
129 return;
130 }
131 case 'c':
133 break;
134 }
135}
136
137void KCalendarCore::serializeQDateTimeList(QDataStream &out, const QList<QDateTime> &list)
138{
139 out << (qint32)list.size();
140 for (const auto &i : list) {
141 serializeQDateTimeAsKDateTime(out, i);
142 }
143}
144
145void KCalendarCore::deserializeQDateTimeList(QDataStream &in, QList<QDateTime> &list)
146{
147 int size;
148 in >> size;
149 list.clear();
150 list.reserve(size);
151 for (int i = 0; i < size; ++i) {
152 QDateTime dt;
153 deserializeKDateTimeAsQDateTime(in, dt);
154 list << dt;
155 }
156}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
bool isValid(int year, int month, int day)
QDate date() const const
int offsetFromUtc() const const
QTime time() const const
Qt::TimeSpec timeSpec() const const
QTimeZone timeZone() const const
void clear()
void reserve(qsizetype size)
qsizetype size() const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
bool isValid(int h, int m, int s, int ms)
QTimeZone fromSecondsAheadOfUtc(int offset)
QByteArray id() const const
bool isValid() const const
QTimeZone systemTimeZone()
QTimeZone utc()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.