1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Copyright (C) 2014 Drew Parsons <dparsons@emerall.com>
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtCore/QSet>
42 #include "qtimezone.h"
43 #include "qtimezoneprivate_p.h"
44
45 QT_BEGIN_NAMESPACE
46
47 /*
48 Private
49
50 Android implementation
51 */
52
53 // Create the system default time zone
QAndroidTimeZonePrivate()54 QAndroidTimeZonePrivate::QAndroidTimeZonePrivate()
55 : QTimeZonePrivate()
56 {
57 // Keep in sync with systemTimeZoneId():
58 androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
59 "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
60 m_id = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;").toString().toUtf8();
61 }
62
63 // Create a named time zone
QAndroidTimeZonePrivate(const QByteArray & ianaId)64 QAndroidTimeZonePrivate::QAndroidTimeZonePrivate(const QByteArray &ianaId)
65 : QTimeZonePrivate()
66 {
67 init(ianaId);
68 }
69
QAndroidTimeZonePrivate(const QAndroidTimeZonePrivate & other)70 QAndroidTimeZonePrivate::QAndroidTimeZonePrivate(const QAndroidTimeZonePrivate &other)
71 : QTimeZonePrivate(other)
72 {
73 androidTimeZone = other.androidTimeZone;
74 m_id = other.id();
75 }
76
~QAndroidTimeZonePrivate()77 QAndroidTimeZonePrivate::~QAndroidTimeZonePrivate()
78 {
79 }
80
getDisplayName(QJNIObjectPrivate zone,jint style,jboolean dst,const QLocale & locale)81 static QJNIObjectPrivate getDisplayName(QJNIObjectPrivate zone, jint style, jboolean dst,
82 const QLocale &locale)
83 {
84 QJNIObjectPrivate jlanguage
85 = QJNIObjectPrivate::fromString(QLocale::languageToString(locale.language()));
86 QJNIObjectPrivate jcountry
87 = QJNIObjectPrivate::fromString(QLocale::countryToString(locale.country()));
88 QJNIObjectPrivate
89 jvariant = QJNIObjectPrivate::fromString(QLocale::scriptToString(locale.script()));
90 QJNIObjectPrivate jlocale("java.util.Locale",
91 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
92 static_cast<jstring>(jlanguage.object()),
93 static_cast<jstring>(jcountry.object()),
94 static_cast<jstring>(jvariant.object()));
95
96 return zone.callObjectMethod("getDisplayName",
97 "(ZILjava/util/Locale;)Ljava/lang/String;",
98 dst, style, jlocale.object());
99 }
100
init(const QByteArray & ianaId)101 void QAndroidTimeZonePrivate::init(const QByteArray &ianaId)
102 {
103 const QString iana = QString::fromUtf8(ianaId);
104 androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
105 "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;",
106 static_cast<jstring>(QJNIObjectPrivate::fromString(iana).object()));
107
108 // The ID or display name of the zone we've got, if it looks like what we asked for:
109 const auto match = [iana](const QJNIObjectPrivate &jname) -> QByteArray {
110 const QString name = jname.toString();
111 if (iana.compare(name, Qt::CaseInsensitive))
112 return name.toUtf8();
113
114 return QByteArray();
115 };
116
117 // Painfully, JNI gives us back a default zone object if it doesn't
118 // recognize the name; so check for whether ianaId is a recognized name of
119 // the zone object we got and ignore the zone if not.
120 // Try checking ianaId against getID(), getDisplayName():
121 m_id = match(androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;"));
122 for (int style = 1; m_id.isEmpty() && style >= 0; --style) {
123 for (int dst = 1; m_id.isEmpty() && dst >= 0; --dst) {
124 for (int pick = 2; m_id.isEmpty() && pick >= 0; --pick) {
125 QLocale locale = (pick == 0 ? QLocale::system()
126 : pick == 1 ? QLocale() : QLocale::c());
127 m_id = match(getDisplayName(androidTimeZone, style, jboolean(dst), locale));
128 }
129 }
130 }
131 }
132
clone() const133 QAndroidTimeZonePrivate *QAndroidTimeZonePrivate::clone() const
134 {
135 return new QAndroidTimeZonePrivate(*this);
136 }
137
138
displayName(QTimeZone::TimeType timeType,QTimeZone::NameType nameType,const QLocale & locale) const139 QString QAndroidTimeZonePrivate::displayName(QTimeZone::TimeType timeType, QTimeZone::NameType nameType,
140 const QLocale &locale) const
141 {
142 QString name;
143
144 if (androidTimeZone.isValid()) {
145 jboolean daylightTime = (timeType == QTimeZone::DaylightTime); // treat QTimeZone::GenericTime as QTimeZone::StandardTime
146
147 // treat all NameTypes as java TimeZone style LONG (value 1), except of course QTimeZone::ShortName which is style SHORT (value 0);
148 jint style = (nameType == QTimeZone::ShortName ? 0 : 1);
149
150 name = getDisplayName(androidTimeZone, style, daylightTime, locale).toString();
151 }
152
153 return name;
154 }
155
abbreviation(qint64 atMSecsSinceEpoch) const156 QString QAndroidTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const
157 {
158 if ( isDaylightTime( atMSecsSinceEpoch ) )
159 return displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, QLocale() );
160 else
161 return displayName(QTimeZone::StandardTime, QTimeZone::ShortName, QLocale() );
162 }
163
offsetFromUtc(qint64 atMSecsSinceEpoch) const164 int QAndroidTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const
165 {
166 // offsetFromUtc( ) is invoked when androidTimeZone may not yet be set at startup,
167 // so a validity test is needed here
168 if ( androidTimeZone.isValid() )
169 // the java method getOffset() returns milliseconds, but QTimeZone returns seconds
170 return androidTimeZone.callMethod<jint>( "getOffset", "(J)I", static_cast<jlong>(atMSecsSinceEpoch) ) / 1000;
171 else
172 return 0;
173 }
174
standardTimeOffset(qint64 atMSecsSinceEpoch) const175 int QAndroidTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const
176 {
177 // the java method does not use a reference time
178 Q_UNUSED( atMSecsSinceEpoch );
179 if ( androidTimeZone.isValid() )
180 // the java method getRawOffset() returns milliseconds, but QTimeZone returns seconds
181 return androidTimeZone.callMethod<jint>( "getRawOffset" ) / 1000;
182 else
183 return 0;
184 }
185
daylightTimeOffset(qint64 atMSecsSinceEpoch) const186 int QAndroidTimeZonePrivate::daylightTimeOffset(qint64 atMSecsSinceEpoch) const
187 {
188 return ( offsetFromUtc(atMSecsSinceEpoch) - standardTimeOffset(atMSecsSinceEpoch) );
189 }
190
hasDaylightTime() const191 bool QAndroidTimeZonePrivate::hasDaylightTime() const
192 {
193 if ( androidTimeZone.isValid() )
194 /* note: the Java function only tests for future DST transtions, not past */
195 return androidTimeZone.callMethod<jboolean>("useDaylightTime" );
196 else
197 return false;
198 }
199
isDaylightTime(qint64 atMSecsSinceEpoch) const200 bool QAndroidTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const
201 {
202 if ( androidTimeZone.isValid() ) {
203 QJNIObjectPrivate jDate( "java/util/Date", "(J)V", static_cast<jlong>(atMSecsSinceEpoch) );
204 return androidTimeZone.callMethod<jboolean>("inDaylightTime", "(Ljava/util/Date;)Z", jDate.object() );
205 }
206 else
207 return false;
208 }
209
data(qint64 forMSecsSinceEpoch) const210 QTimeZonePrivate::Data QAndroidTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const
211 {
212 if (androidTimeZone.isValid()) {
213 Data data;
214 data.atMSecsSinceEpoch = forMSecsSinceEpoch;
215 data.standardTimeOffset = standardTimeOffset(forMSecsSinceEpoch);
216 data.offsetFromUtc = offsetFromUtc(forMSecsSinceEpoch);
217 data.daylightTimeOffset = data.offsetFromUtc - data.standardTimeOffset;
218 data.abbreviation = abbreviation(forMSecsSinceEpoch);
219 return data;
220 } else {
221 return invalidData();
222 }
223 }
224
hasTransitions() const225 bool QAndroidTimeZonePrivate::hasTransitions() const
226 {
227 // java.util.TimeZone does not directly provide transitions
228 return false;
229 }
230
nextTransition(qint64 afterMSecsSinceEpoch) const231 QTimeZonePrivate::Data QAndroidTimeZonePrivate::nextTransition(qint64 afterMSecsSinceEpoch) const
232 {
233 // transitions not available on Android, so return an invalid data object
234 Q_UNUSED( afterMSecsSinceEpoch );
235 return invalidData();
236 }
237
previousTransition(qint64 beforeMSecsSinceEpoch) const238 QTimeZonePrivate::Data QAndroidTimeZonePrivate::previousTransition(qint64 beforeMSecsSinceEpoch) const
239 {
240 // transitions not available on Android, so return an invalid data object
241 Q_UNUSED( beforeMSecsSinceEpoch );
242 return invalidData();
243 }
244
systemTimeZoneId() const245 QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const
246 {
247 // Keep in sync with default constructor:
248 QJNIObjectPrivate androidSystemTimeZone = QJNIObjectPrivate::callStaticObjectMethod(
249 "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;");
250 return androidSystemTimeZone.callObjectMethod<jstring>("getID").toString().toUtf8();
251 }
252
availableTimeZoneIds() const253 QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const
254 {
255 QList<QByteArray> availableTimeZoneIdList;
256 QJNIObjectPrivate androidAvailableIdList = QJNIObjectPrivate::callStaticObjectMethod("java.util.TimeZone", "getAvailableIDs", "()[Ljava/lang/String;");
257
258 QJNIEnvironmentPrivate jniEnv;
259 int androidTZcount = jniEnv->GetArrayLength( static_cast<jarray>(androidAvailableIdList.object()) );
260
261 // need separate jobject and QAndroidJniObject here so that we can delete (DeleteLocalRef) the reference to the jobject
262 // (or else the JNI reference table fills after 512 entries from GetObjectArrayElement)
263 jobject androidTZobject;
264 QJNIObjectPrivate androidTZ;
265 for (int i=0; i<androidTZcount; i++ ) {
266 androidTZobject = jniEnv->GetObjectArrayElement( static_cast<jobjectArray>( androidAvailableIdList.object() ), i );
267 androidTZ = androidTZobject;
268 availableTimeZoneIdList.append( androidTZ.toString().toUtf8() );
269 jniEnv->DeleteLocalRef(androidTZobject);
270 }
271
272 return availableTimeZoneIdList;
273 }
274
275 QT_END_NAMESPACE
276