diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0) -set(PIM_VERSION "5.7.40") +set(PIM_VERSION "5.7.41") project(KCalUtils VERSION ${PIM_VERSION}) # ECM setup @@ -22,7 +22,6 @@ include(ECMCoverageOption) include(KPIMGrantleeMacros) - set(CALENDARUTILS_LIB_VERSION ${PIM_VERSION}) set(CALENDARCORE_LIB_VERSION "5.7.40") set(IDENTITYMANAGER_LIB_VERSION "5.7.40") @@ -51,15 +50,14 @@ add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x060000) add_definitions(-DQT_NO_URL_CAST_FROM_STRING) if(BUILD_TESTING) - add_definitions(-DBUILD_TESTING) -endif(BUILD_TESTING) - + add_definitions(-DBUILD_TESTING) +endif() ########### Targets ########### add_subdirectory(src) if(BUILD_TESTING) - add_subdirectory(autotests) + add_subdirectory(autotests) endif() ########### CMake Config Files ########### @@ -79,15 +77,15 @@ ) install(EXPORT KF5CalendarUtilsTargets - DESTINATION "${CMAKECONFIG_INSTALL_DIR}" - FILE KF5CalendarUtilsTargets.cmake - NAMESPACE KF5:: + DESTINATION "${CMAKECONFIG_INSTALL_DIR}" + FILE KF5CalendarUtilsTargets.cmake + NAMESPACE KF5:: ) install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/kcalutils_version.h - DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} - COMPONENT Devel + ${CMAKE_CURRENT_BINARY_DIR}/kcalutils_version.h + DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} + COMPONENT Devel ) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/autotests/teststringify.h b/autotests/teststringify.h --- a/autotests/teststringify.h +++ b/autotests/teststringify.h @@ -31,6 +31,7 @@ void testIncidenceStrings(); void testAttendeeStrings(); void testDateTimeStrings(); + void testUTCoffsetStrings(); }; #endif diff --git a/autotests/teststringify.cpp b/autotests/teststringify.cpp --- a/autotests/teststringify.cpp +++ b/autotests/teststringify.cpp @@ -81,3 +81,30 @@ { //TODO } + +void StringifyTest::testUTCoffsetStrings() +{ + QTimeZone tz1(5 * 60 * 60); //5 hrs + QCOMPARE(Stringify::tzUTCOffsetStr(tz1), QStringLiteral("+05:00")); + + QTimeZone tz2(-5 * 60 * 60); //-5 hrs + QCOMPARE(Stringify::tzUTCOffsetStr(tz2), QStringLiteral("-05:00")); + + QTimeZone tz3(0); + QCOMPARE(Stringify::tzUTCOffsetStr(tz3), QStringLiteral("+00:00")); + + QTimeZone tz4(30 * 60 * 60); //30 hrs -- out-of-range + QCOMPARE(Stringify::tzUTCOffsetStr(tz4), QStringLiteral("+00:00")); + + QTimeZone tz5((5 * 60 * 60) + (30 * 60)); //5:30 + QCOMPARE(Stringify::tzUTCOffsetStr(tz5), QStringLiteral("+05:30")); + + QTimeZone tz6(-((11 * 60 * 60) + (59 * 60))); //-11:59 + QCOMPARE(Stringify::tzUTCOffsetStr(tz6), QStringLiteral("-11:59")); + + QTimeZone tz7(12 * 60 * 60); //12:00 + QCOMPARE(Stringify::tzUTCOffsetStr(tz7), QStringLiteral("+12:00")); + + QTimeZone tz8(-((12 * 60 * 60) + (59 * 60))); //-12:59 + QCOMPARE(Stringify::tzUTCOffsetStr(tz8), QStringLiteral("-12:59")); +} diff --git a/src/stringify.h b/src/stringify.h --- a/src/stringify.h +++ b/src/stringify.h @@ -34,8 +34,10 @@ #include "kcalutils_export.h" -#include -#include +#include +#include + +#include namespace KCalCore { class Exception; @@ -75,6 +77,13 @@ KCALUTILS_EXPORT QString attendeeRole(KCalCore::Attendee::Role role); KCALUTILS_EXPORT QString attendeeStatus(KCalCore::Attendee::PartStat status); +/** + Returns a string containing the UTC offset of the specified QTimeZone @p tz (relative to the current date). + The format is [+-]HH::MM, according to standards. + @since 5.8 +*/ +KCALUTILS_EXPORT QString tzUTCOffsetStr(const QTimeZone &tz); + /** Build a translated message representing an exception */ diff --git a/src/stringify.cpp b/src/stringify.cpp --- a/src/stringify.cpp +++ b/src/stringify.cpp @@ -5,6 +5,7 @@ Copyright (c) 2004 Reinhold Kainhofer Copyright (c) 2005 Rafal Rzepecki Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company + Copyright (c) 2017 Allen Winter This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -35,7 +36,7 @@ */ #include "stringify.h" -#include +#include using namespace KCalCore; #include @@ -83,7 +84,9 @@ QStringList Stringify::incidenceSecrecyList() { const QStringList list { - incidenceSecrecy(Incidence::SecrecyPublic), incidenceSecrecy(Incidence::SecrecyPrivate), incidenceSecrecy(Incidence::SecrecyConfidential) + incidenceSecrecy(Incidence::SecrecyPublic), + incidenceSecrecy(Incidence::SecrecyPrivate), + incidenceSecrecy(Incidence::SecrecyConfidential) }; return list; @@ -270,3 +273,20 @@ return i18nc("@item unknown status", "Unknown Status: %1", int(status)); } } + +QString Stringify::tzUTCOffsetStr(const QTimeZone &tz) +{ + int currentOffset = tz.offsetFromUtc(QDateTime::currentDateTimeUtc()); + int absOffset = qAbs(currentOffset); + int utcOffsetHrs = absOffset / 3600; // in hours + int utcOffsetMins = (absOffset % 3600) / 60; // in minutes + + const QString hrStr = QStringLiteral("%1").arg(utcOffsetHrs, 2, 10, QLatin1Char('0')); + const QString mnStr = QStringLiteral("%1").arg(utcOffsetMins, 2, 10, QLatin1Char('0')); + + if (currentOffset < 0) { + return QStringLiteral("-%1:%2").arg(hrStr, mnStr); + } else { + return QStringLiteral("+%1:%2").arg(hrStr, mnStr); + } +}