File indexing completed on 2024-07-21 12:05:00

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2010 Intel Corporation
0005     SPDX-FileContributor: Mateu Batle Sastre <mbatle@collabora.co.uk>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0008 */
0009 
0010 #include "atticautils.h"
0011 #include <QStringList>
0012 
0013 using namespace Attica;
0014 
0015 QDateTime Utils::parseQtDateTimeIso8601(const QString &str)
0016 {
0017     QDateTime result;
0018     QStringList list;
0019     QString datetime;
0020 
0021     int tzsign = 0;
0022     if (str.indexOf(QLatin1String("+")) != -1) {
0023         list = str.split(QStringLiteral("+"));
0024         datetime = list[0];
0025         tzsign = 1;
0026     } else if (str.indexOf(QLatin1String("-")) != -1) {
0027         list = str.split(QStringLiteral("-"));
0028         datetime = list[0];
0029         tzsign = -1;
0030     } else {
0031         datetime = str;
0032     }
0033 
0034     // parse date time
0035     result = QDateTime::fromString(datetime, Qt::ISODate);
0036     result.setTimeSpec(Qt::UTC);
0037 
0038     // parse timezone
0039     if (list.count() == 2) {
0040         QString tz = list[1];
0041         int hh = 0;
0042         int mm = 0;
0043         int tzsecs = 0;
0044         if (tz.indexOf(QLatin1Char(':')) != -1) {
0045             QStringList tzlist = tz.split(QLatin1Char(':'));
0046             if (tzlist.count() == 2) {
0047                 hh = tzlist[0].toInt();
0048                 mm = tzlist[1].toInt();
0049             }
0050         } else {
0051 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0052             QStringView sv(tz);
0053             hh = sv.left(2).toInt();
0054             mm = sv.mid(2).toInt();
0055 #else
0056             hh = tz.leftRef(2).toInt();
0057             mm = tz.midRef(2).toInt();
0058 #endif
0059         }
0060 
0061         tzsecs = 60 * 60 * hh + 60 * mm;
0062         result = result.addSecs(-tzsecs * tzsign);
0063     }
0064 
0065     return result;
0066 }
0067 
0068 const char *Utils::toString(QNetworkAccessManager::Operation operation)
0069 {
0070     switch (operation) {
0071     case QNetworkAccessManager::GetOperation:
0072         return "Get";
0073     case QNetworkAccessManager::HeadOperation:
0074         return "Head";
0075     case QNetworkAccessManager::PutOperation:
0076         return "Put";
0077     case QNetworkAccessManager::PostOperation:
0078         return "Post";
0079     case QNetworkAccessManager::DeleteOperation:
0080         return "Delete";
0081     case QNetworkAccessManager::CustomOperation:
0082         return "Custom";
0083     default:
0084         return "unknown";
0085     }
0086     return "invalid";
0087 }