Commit e0954456 authored by David Jarvie's avatar David Jarvie
Browse files

Fix conversion between QDateTime and KDateTime

parent 0a8f6a6d
......@@ -5492,7 +5492,7 @@ bool KAEventPrivate::convertStartOfDay(const Event::Ptr &event)
const KDateTime oldDt = q2k(event->dtStart());
const int adjustment = oldDt.time().secsTo(midnight);
if (adjustment) {
event->setDtStart(QDateTime(oldDt.date(), midnight, specToZone(oldDt.timeSpec())));
event->setDtStart(ktoq(KDateTime(oldDt.date(), midnight, oldDt.timeSpec())));
int deferralOffset = 0;
AlarmMap alarmMap;
readAlarms(event, &alarmMap);
......
......@@ -2,6 +2,7 @@
This file is part of the kalarmcal library.
Copyright (c) 2017 Daniel Vrátil <dvratil@kde.org>
Copyright (c) 2017 David Jarvie <djarvie@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -25,6 +26,25 @@
#include <KSystemTimeZones>
#include <QDebug>
KDateTime::Spec KAlarmCal::kTimeSpec(const QDateTime &dt)
{
if (dt.isValid()) {
switch (dt.timeSpec()) {
case Qt::LocalTime:
return KDateTime::LocalZone;
case Qt::UTC:
return KDateTime::UTC;
case Qt::OffsetFromUTC:
return KDateTime::Spec(KDateTime::OffsetFromUTC, dt.offsetFromUtc());
case Qt::TimeZone:
return KSystemTimeZones::zone(QString::fromLatin1(dt.timeZone().id()));
default:
break;
}
}
return KDateTime::Invalid;
}
KDateTime::Spec KAlarmCal::zoneToSpec(const QTimeZone& zone)
{
if (!zone.isValid())
......@@ -137,19 +157,36 @@ QTimeZone KAlarmCal::specToZone(const KDateTime::Spec &spec)
QDateTime KAlarmCal::k2q(const KDateTime &kdt)
{
if (kdt.isValid()) {
return QDateTime(kdt.date(), kdt.time(), specToZone(kdt.timeSpec()));
} else {
return QDateTime();
switch (kdt.timeType()) {
case KDateTime::LocalZone:
case KDateTime::ClockTime:
return QDateTime(kdt.date(), kdt.time(), Qt::LocalTime);
case KDateTime::UTC:
return QDateTime(kdt.date(), kdt.time(), Qt::UTC);
case KDateTime::OffsetFromUTC:
return QDateTime(kdt.date(), kdt.time(), Qt::OffsetFromUTC, kdt.timeSpec().utcOffset());
case KDateTime::TimeZone: {
auto tz = QTimeZone(kdt.timeZone().name().toUtf8());
if (!tz.isValid()) {
tz = resolveCustomTZ(kdt.timeZone());
qDebug() << "Resolved" << kdt.timeZone().name() << "to" << tz.id();
}
return QDateTime(kdt.date(), kdt.time(), tz);
}
case KDateTime::Invalid:
default:
break;
}
}
return QDateTime();
}
KDateTime KAlarmCal::q2k(const QDateTime &qdt, bool allDay)
{
if (qdt.isValid()) {
KDateTime kdt(qdt.date(), qdt.time(), zoneToSpec(qdt.timeZone()));
KDateTime kdt(qdt.date(), qdt.time(), kTimeSpec(qdt));
kdt.setDateOnly(allDay && qdt.time() == QTime(0, 0, 0));
return kdt;
} else {
return KDateTime();
}
return KDateTime();
}
......@@ -2,6 +2,7 @@
This file is part of the kalarmcal library.
Copyright (c) 2017 Daniel Vrátil <dvratil@kde.org>
Copyright (c) 2017 David Jarvie <djarvie@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -30,14 +31,19 @@
namespace KAlarmCal {
/** Get the KDateTime::Spec for a QDateTime */
KALARMCAL_EXPORT KDateTime::Spec kTimeSpec(const QDateTime &dt);
/** Convert a QTimeZone to a KDateTime::Spec */
KALARMCAL_EXPORT KDateTime::Spec zoneToSpec(const QTimeZone &zone);
/** Convert a QTimeZone to a KDateTime::Spec */
KALARMCAL_EXPORT QTimeZone specToZone(const KDateTime::Spec &spec);
/** Convert KDateTime to QDateTime, correctly preserves timespec */
/** Convert KDateTime to QDateTime, correctly preserving timespec */
KALARMCAL_EXPORT QDateTime k2q(const KDateTime &kdt);
/** Convert QDateTime to KDateTime, correctly preserving timespec */
KALARMCAL_EXPORT KDateTime q2k(const QDateTime &qdt, bool isAllDay = false);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment