Commit 6705bf16 authored by Allen Winter's avatar Allen Winter

new static method tzUTCOffsetStr for pretty printing UTC offset

Differential Revision: https://phabricator.kde.org/D9273
parent 9c31ff3a
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(ECMQtDeclareLoggingCategory)
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_NO_NARROWING_CONVERSIONS_IN_CONNECT)
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(FILES
)
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)
......@@ -81,3 +81,30 @@ void StringifyTest::testDateTimeStrings()
{
//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"));
}
......@@ -31,6 +31,7 @@ private Q_SLOTS:
void testIncidenceStrings();
void testAttendeeStrings();
void testDateTimeStrings();
void testUTCoffsetStrings();
};
#endif
......@@ -5,6 +5,7 @@
Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
Copyright (c) 2017 Allen Winter <winter@kde.org>
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 <kcalcore/exceptions.h>
#include <KCalCore/Exceptions>
using namespace KCalCore;
#include <KLocalizedString>
......@@ -83,7 +84,9 @@ QString Stringify::incidenceSecrecy(Incidence::Secrecy secrecy)
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 @@ QString Stringify::scheduleMessageStatus(ScheduleMessage::Status status)
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);
}
}
......@@ -34,8 +34,10 @@
#include "kcalutils_export.h"
#include <kcalcore/schedulemessage.h>
#include <kcalcore/todo.h>
#include <KCalCore/ScheduleMessage>
#include <KCalCore/Todo>
#include <QTimeZone>
namespace KCalCore {
class Exception;
......@@ -75,6 +77,13 @@ KCALUTILS_EXPORT QString todoCompletedDateTime(const KCalCore::Todo::Ptr &todo,
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
*/
......
Markdown is supported
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