Add Objective-C specific type converters to QDateTime
This patch adds the Objective-C NSDate/CDateRef converters to QDateTime [ChangeLog][QtCore][Objective-C] Added NSDate/CDateRef converters for QDateTime Task-number: QTBUG-37116 Change-Id: I937ea927083a2767b5b17f10a48bf453c9ff8b01 Reviewed-by: Jake Petroules <jake.petroules@petroules.com> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
This commit is contained in:
parent
10e3ce9e16
commit
1cc079c29b
@ -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
|
||||
|
||||
|
@ -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();
|
||||
|
76
src/corelib/tools/qdatetime_mac.mm
Normal file
76
src/corelib/tools/qdatetime_mac.mm
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
|
82
tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm
Normal file
82
tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm
Normal file
@ -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];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user