diff options
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 33 | ||||
-rw-r--r-- | src/corelib/tools/qdatetime.h | 16 | ||||
-rw-r--r-- | src/corelib/tools/qdatetime_mac.mm | 76 | ||||
-rw-r--r-- | src/corelib/tools/tools.pri | 3 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/qdatetime.pro | 5 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 12 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm | 82 |
7 files changed, 226 insertions, 1 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 3acde53208..a0b88a65d0 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -4197,6 +4197,39 @@ qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW #error "What system is this?" #endif +/*! \fn QDateTime QDateTime::fromCFDate(CFDateRef date) + \since 5.5 + + Constructs a new QDateTime containing a copy of the CFDate \a date. + + \sa toCFDate() +*/ + +/*! \fn CFDateRef QDateTime::toCFDate() const + \since 5.5 + + Creates a CFDate from a QDateTime. The caller owns the CFDate object + and is responsible for releasing it. + + \sa fromCFDate() +*/ + +/*! \fn QDateTime QDateTime::fromNSDate(const NSDate *date) + \since 5.5 + + Constructs a new QDateTime containing a copy of the NSDate \a date. + + \sa toNSDate() +*/ + +/*! \fn NSDate QDateTime::toNSDate() const + \since 5.5 + + Creates an NSDate from a QDateTime. The NSDate object is autoreleased. + + \sa fromNSDate() +*/ + /*! \since 4.2 diff --git a/src/corelib/tools/qdatetime.h b/src/corelib/tools/qdatetime.h index 597655f557..4ef94f72d7 100644 --- a/src/corelib/tools/qdatetime.h +++ b/src/corelib/tools/qdatetime.h @@ -40,6 +40,13 @@ #include <limits> +#ifdef Q_OS_MAC +Q_FORWARD_DECLARE_CF_TYPE(CFDate); +# ifdef __OBJC__ +Q_FORWARD_DECLARE_OBJC_CLASS(NSDate); +# endif +#endif + QT_BEGIN_NAMESPACE class QTimeZone; @@ -295,6 +302,15 @@ public: #endif static qint64 currentMSecsSinceEpoch() Q_DECL_NOTHROW; +#if defined(Q_OS_MAC) || defined(Q_QDOC) + static QDateTime fromCFDate(CFDateRef date); + CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED; +# if defined(__OBJC__) || defined(Q_QDOC) + static QDateTime fromNSDate(const NSDate *date); + NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED; +# endif +#endif + private: friend class QDateTimePrivate; void detach(); diff --git a/src/corelib/tools/qdatetime_mac.mm b/src/corelib/tools/qdatetime_mac.mm new file mode 100644 index 0000000000..c8a1d22928 --- /dev/null +++ b/src/corelib/tools/qdatetime_mac.mm @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Petroules Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdatetime.h" + +#import <Foundation/Foundation.h> + +QT_BEGIN_NAMESPACE + +QDateTime QDateTime::fromCFDate(CFDateRef date) +{ + if (!date) + return QDateTime(); + return QDateTime::fromMSecsSinceEpoch(static_cast<qint64>((CFDateGetAbsoluteTime(date) + + kCFAbsoluteTimeIntervalSince1970) * 1000)); +} + +CFDateRef QDateTime::toCFDate() const +{ + return CFDateCreate(kCFAllocatorDefault, (static_cast<CFAbsoluteTime>(toMSecsSinceEpoch()) + / 1000) - kCFAbsoluteTimeIntervalSince1970); +} + +QDateTime QDateTime::fromNSDate(const NSDate *date) +{ + if (!date) + return QDateTime(); + return QDateTime::fromMSecsSinceEpoch(static_cast<qint64>([date timeIntervalSince1970] * 1000)); +} + +NSDate *QDateTime::toNSDate() const +{ + return [NSDate + dateWithTimeIntervalSince1970:static_cast<NSTimeInterval>(toMSecsSinceEpoch()) / 1000]; +} + +QT_END_NAMESPACE diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri index adda91b739..2e77f683a4 100644 --- a/src/corelib/tools/tools.pri +++ b/src/corelib/tools/tools.pri @@ -129,7 +129,8 @@ false: SOURCES += $$NO_PCH_SOURCES # Hack for QtCreator OBJECTIVE_SOURCES += tools/qlocale_mac.mm \ tools/qtimezoneprivate_mac.mm \ tools/qstring_mac.mm \ - tools/qbytearray_mac.mm + tools/qbytearray_mac.mm \ + tools/qdatetime_mac.mm } else:blackberry { SOURCES += tools/qelapsedtimer_unix.cpp tools/qlocale_blackberry.cpp tools/qtimezoneprivate_tz.cpp diff --git a/tests/auto/corelib/tools/qdatetime/qdatetime.pro b/tests/auto/corelib/tools/qdatetime/qdatetime.pro index 0a89fe7645..25d11443e4 100644 --- a/tests/auto/corelib/tools/qdatetime/qdatetime.pro +++ b/tests/auto/corelib/tools/qdatetime/qdatetime.pro @@ -11,3 +11,8 @@ win32-msvc|win32-msvc9x { QMAKE_CXXFLAGS_RELEASE -= -O1 } DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 + +mac { + OBJECTIVE_SOURCES += tst_qdatetime_mac.mm + LIBS += -framework Foundation +} diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 51c3a19d63..8876bb3d34 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -145,6 +145,8 @@ private slots: void invalid() const; + void macTypes(); + private: enum { LocalTimeIsUtc = 0, LocalTimeAheadOfUtc = 1, LocalTimeBehindUtc = -1} localTimeType; bool europeanTimeZone; @@ -2981,5 +2983,15 @@ void tst_QDateTime::invalid() const QCOMPARE(tzDate.timeSpec(), Qt::TimeZone); } +void tst_QDateTime::macTypes() +{ +#ifndef Q_OS_MAC + QSKIP("This is a Apple-only test"); +#else + extern void tst_QDateTime_macTypes(); // in qdatetime_mac.mm + tst_QDateTime_macTypes(); +#endif +} + QTEST_APPLESS_MAIN(tst_QDateTime) #include "tst_qdatetime.moc" diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm b/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm new file mode 100644 index 0000000000..d03ae3faeb --- /dev/null +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Petroules Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/QDateTime> +#include <QtTest/QtTest> + +#include <CoreFoundation/CoreFoundation.h> +#include <Foundation/Foundation.h> + +void tst_QDateTime_macTypes() +{ + // QDateTime <-> CFDate + { + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const CFDateRef cfDate = qtDateTime.toCFDate(); + QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTime); + CFRelease(cfDate); + } + { + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const CFDateRef cfDate = qtDateTime.toCFDate(); + QDateTime qtDateTimeCopy(qtDateTime); + qtDateTime.setTime_t(10000); // modify + QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTimeCopy); + } + // QDateTime <-> NSDate + { + NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const NSDate *nsDate = qtDateTime.toNSDate(); + QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTime); + [autoreleasepool release]; + } + { + NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const NSDate *nsDate = qtDateTime.toNSDate(); + QDateTime qtDateTimeCopy(qtDateTime); + qtDateTime.setTime_t(10000); // modify + QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTimeCopy); + [autoreleasepool release]; + } +} |