diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-11-08 09:20:04 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-11-08 09:20:04 +0000 |
| commit | cb8b6cb286e923dfc8b7fe0fa9d30a4da3b84e95 (patch) | |
| tree | 16b51d09ce95ac239ac112f65e4d81d41549dc04 | |
| parent | 28e7ee24b62c06c64e63d49c24bf6e4337a6941c (diff) | |
| download | qxmpp-cb8b6cb286e923dfc8b7fe0fa9d30a4da3b84e95.tar.gz | |
add methods for parsing/serialising timezone offsets
| -rw-r--r-- | src/QXmppUtils.cpp | 37 | ||||
| -rw-r--r-- | src/QXmppUtils.h | 2 | ||||
| -rw-r--r-- | tests/tests.cpp | 15 | ||||
| -rw-r--r-- | tests/tests.h | 1 |
4 files changed, 55 insertions, 0 deletions
diff --git a/src/QXmppUtils.cpp b/src/QXmppUtils.cpp index 60970948..6fe76fe7 100644 --- a/src/QXmppUtils.cpp +++ b/src/QXmppUtils.cpp @@ -146,6 +146,43 @@ QString datetimeToString(const QDateTime &dt) return utc.toString("yyyy-MM-ddThh:mm:ssZ"); } +/// Parses a timezone offset (in seconds) from a string. +/// +/// \param str +/// + +int timezoneOffsetFromString(const QString &str) +{ + QRegExp tzRe("(Z|([+-])([0-9]{2}):([0-9]{2}))"); + if (!tzRe.exactMatch(str)) + return 0; + + // No offset from UTC + if (tzRe.cap(1) == "Z") + return 0; + + // Calculate offset + const int offset = tzRe.cap(3).toInt() * 3600 + + tzRe.cap(4).toInt() * 60; + if (tzRe.cap(2) == "-") + return -offset; + else + return offset; +} + +/// Serializes a timezone offset (in seconds) to a string. +/// +/// \param secs + +QString timezoneOffsetToString(int secs) +{ + if (!secs) + return QString::fromLatin1("Z"); + + const QTime tzoTime = QTime(0, 0, 0).addSecs(qAbs(secs)); + return (secs < 0 ? "-" : "+") + tzoTime.toString("hh:mm"); +} + QString jidToDomain(const QString &jid) { return jidToBareJid(jid).split("@").last(); diff --git a/src/QXmppUtils.h b/src/QXmppUtils.h index 2ce37bd9..03d8df96 100644 --- a/src/QXmppUtils.h +++ b/src/QXmppUtils.h @@ -41,6 +41,8 @@ class QStringList; // XEP-0082: XMPP Date and Time Profiles QDateTime datetimeFromString(const QString &str); QString datetimeToString(const QDateTime &dt); +int timezoneOffsetFromString(const QString &str); +QString timezoneOffsetToString(int secs); QString jidToDomain(const QString& jid); QString jidToResource(const QString& jid); diff --git a/tests/tests.cpp b/tests/tests.cpp index 0e985b74..53e499fc 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -138,6 +138,21 @@ void TestUtils::testLibVersion() QCOMPARE(QXmppVersion(), QString("0.2.90")); } +void TestUtils::testTimezoneOffset() +{ + // parsing + QCOMPARE(timezoneOffsetFromString("Z"), 0); + QCOMPARE(timezoneOffsetFromString("+00:00"), 0); + QCOMPARE(timezoneOffsetFromString("-00:00"), 0); + QCOMPARE(timezoneOffsetFromString("+01:30"), 5400); + QCOMPARE(timezoneOffsetFromString("-01:30"), -5400); + + // serialization + QCOMPARE(timezoneOffsetToString(0), QLatin1String("Z")); + QCOMPARE(timezoneOffsetToString(5400), QLatin1String("+01:30")); + QCOMPARE(timezoneOffsetToString(-5400), QLatin1String("-01:30")); +} + template <class T> static void parsePacket(T &packet, const QByteArray &xml) { diff --git a/tests/tests.h b/tests/tests.h index 5be6b0af..59a5b23d 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -35,6 +35,7 @@ private slots: void testJid(); void testMime(); void testLibVersion(); + void testTimezoneOffset(); }; class TestPackets : public QObject |
