1ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes/* 2ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * Copyright (C) 2010 The Android Open Source Project 3ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * 4ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * you may not use this file except in compliance with the License. 6ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * You may obtain a copy of the License at 7ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * 8ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * 10ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * Unless required by applicable law or agreed to in writing, software 11ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * See the License for the specific language governing permissions and 14ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes * limitations under the License. 15ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes */ 16ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 17ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#define LOG_TAG "TimeZoneNames" 18ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 19b3aacde3d8af759ee4a7b395c636ea360547d92dIan Rogers#include <memory> 20b3aacde3d8af759ee4a7b395c636ea360547d92dIan Rogers 21ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "IcuUtilities.h" 22ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "JNIHelp.h" 23ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "JniConstants.h" 24ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "JniException.h" 25a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes#include "ScopedIcuLocale.h" 26ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "ScopedJavaUnicodeString.h" 27ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "ScopedLocalRef.h" 28ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "ScopedUtfChars.h" 29ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "unicode/calendar.h" 30ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "unicode/timezone.h" 31ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes#include "unicode/tznames.h" 32ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 33a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughesstatic bool isUtc(const icu::UnicodeString& id) { 34a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kEtcUct("Etc/UCT", 7, US_INV); 35a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kEtcUtc("Etc/UTC", 7, US_INV); 36a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kEtcUniversal("Etc/Universal", 13, US_INV); 37a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kEtcZulu("Etc/Zulu", 8, US_INV); 38ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 39a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kUct("UCT", 3, US_INV); 40a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kUtc("UTC", 3, US_INV); 41a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kUniversal("Universal", 9, US_INV); 42a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kZulu("Zulu", 4, US_INV); 43ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 44ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes return id == kEtcUct || id == kEtcUtc || id == kEtcUniversal || id == kEtcZulu || 45ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes id == kUct || id == kUtc || id == kUniversal || id == kZulu; 46ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes} 47ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 48a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughesstatic bool setStringArrayElement(JNIEnv* env, jobjectArray array, int i, const icu::UnicodeString& s) { 49ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // Fill in whatever we got. We don't use the display names if they're "GMT[+-]xx:xx" 50ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // because icu4c doesn't use the up-to-date time zone transition data, so it gets these 51ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // wrong. TimeZone.getDisplayName creates accurate names on demand. 52ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // TODO: investigate whether it's worth doing that work once in the Java wrapper instead of on-demand. 53a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kGmt("GMT", 3, US_INV); 54ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes if (!s.isBogus() && !s.startsWith(kGmt)) { 55ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes ScopedLocalRef<jstring> javaString(env, env->NewString(s.getBuffer(), s.length())); 563020db601855fda8e33c870510204750b2386f3dElliott Hughes if (javaString.get() == NULL) { 573020db601855fda8e33c870510204750b2386f3dElliott Hughes return false; 583020db601855fda8e33c870510204750b2386f3dElliott Hughes } 59ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes env->SetObjectArrayElement(array, i, javaString.get()); 60ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes } 613020db601855fda8e33c870510204750b2386f3dElliott Hughes return true; 62ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes} 63ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 64a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughesstatic void TimeZoneNames_fillZoneStrings(JNIEnv* env, jclass, jstring javaLocaleName, jobjectArray result) { 65a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes ScopedIcuLocale icuLocale(env, javaLocaleName); 66a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes if (!icuLocale.valid()) { 67a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes return; 68a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes } 69ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 70ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes UErrorCode status = U_ZERO_ERROR; 71a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes std::unique_ptr<icu::TimeZoneNames> names(icu::TimeZoneNames::createInstance(icuLocale.locale(), status)); 72ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes if (maybeThrowIcuException(env, "TimeZoneNames::createInstance", status)) { 73ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes return; 74ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes } 75ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 76a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes const UDate now(icu::Calendar::getNow()); 77ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 78a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes static const icu::UnicodeString kUtc("UTC", 3, US_INV); 79ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 80ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes size_t id_count = env->GetArrayLength(result); 81ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes for (size_t i = 0; i < id_count; ++i) { 82ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes ScopedLocalRef<jobjectArray> java_row(env, 83ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes reinterpret_cast<jobjectArray>(env->GetObjectArrayElement(result, i))); 84ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes ScopedLocalRef<jstring> java_zone_id(env, 85ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes reinterpret_cast<jstring>(env->GetObjectArrayElement(java_row.get(), 0))); 86ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes ScopedJavaUnicodeString zone_id(env, java_zone_id.get()); 87ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes if (!zone_id.valid()) { 88ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes return; 89ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes } 90ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 91a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes icu::UnicodeString long_std; 92ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes names->getDisplayName(zone_id.unicodeString(), UTZNM_LONG_STANDARD, now, long_std); 93a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes icu::UnicodeString short_std; 94ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes names->getDisplayName(zone_id.unicodeString(), UTZNM_SHORT_STANDARD, now, short_std); 95a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes icu::UnicodeString long_dst; 96ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes names->getDisplayName(zone_id.unicodeString(), UTZNM_LONG_DAYLIGHT, now, long_dst); 97a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes icu::UnicodeString short_dst; 98ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes names->getDisplayName(zone_id.unicodeString(), UTZNM_SHORT_DAYLIGHT, now, short_dst); 99ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 100ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes if (isUtc(zone_id.unicodeString())) { 101ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // ICU doesn't have names for the UTC zones; it just says "GMT+00:00" for both 102ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // long and short names. We don't want this. The best we can do is use "UTC" 103ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // for everything (since we don't know how to say "Universal Coordinated Time" in 104ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // every language). 105ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes // TODO: check CLDR doesn't actually have this somewhere. 106ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes long_std = short_std = long_dst = short_dst = kUtc; 107ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes } 108ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 1093020db601855fda8e33c870510204750b2386f3dElliott Hughes bool okay = 1103020db601855fda8e33c870510204750b2386f3dElliott Hughes setStringArrayElement(env, java_row.get(), 1, long_std) && 1113020db601855fda8e33c870510204750b2386f3dElliott Hughes setStringArrayElement(env, java_row.get(), 2, short_std) && 1123020db601855fda8e33c870510204750b2386f3dElliott Hughes setStringArrayElement(env, java_row.get(), 3, long_dst) && 1133020db601855fda8e33c870510204750b2386f3dElliott Hughes setStringArrayElement(env, java_row.get(), 4, short_dst); 1143020db601855fda8e33c870510204750b2386f3dElliott Hughes if (!okay) { 1153020db601855fda8e33c870510204750b2386f3dElliott Hughes return; 1163020db601855fda8e33c870510204750b2386f3dElliott Hughes } 117ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes } 118ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes} 119ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes 120a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughesstatic jstring TimeZoneNames_getExemplarLocation(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaTz) { 121a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes ScopedIcuLocale icuLocale(env, javaLocaleName); 122a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes if (!icuLocale.valid()) { 123a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes return NULL; 124a94266074c7b82720fd2cecfb37ab8da85f1b296Elliott Hughes } 125383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes 126383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes UErrorCode status = U_ZERO_ERROR; 127a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes std::unique_ptr<icu::TimeZoneNames> names(icu::TimeZoneNames::createInstance(icuLocale.locale(), status)); 128383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes if (maybeThrowIcuException(env, "TimeZoneNames::createInstance", status)) { 129383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes return NULL; 130383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes } 131383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes 132383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes ScopedJavaUnicodeString tz(env, javaTz); 133383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes if (!tz.valid()) { 134383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes return NULL; 135383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes } 136383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes 137a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes icu::UnicodeString s; 138a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes const UDate now(icu::Calendar::getNow()); 139383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes names->getDisplayName(tz.unicodeString(), UTZNM_EXEMPLAR_LOCATION, now, s); 140383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes return env->NewString(s.getBuffer(), s.length()); 141383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes} 142383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes 143ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughesstatic JNINativeMethod gMethods[] = { 144ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes NATIVE_METHOD(TimeZoneNames, fillZoneStrings, "(Ljava/lang/String;[[Ljava/lang/String;)V"), 145383b6c74eb7088c545841357e482c4fd6218cb9dElliott Hughes NATIVE_METHOD(TimeZoneNames, getExemplarLocation, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), 146ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes}; 147ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughesvoid register_libcore_icu_TimeZoneNames(JNIEnv* env) { 148ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes jniRegisterNativeMethods(env, "libcore/icu/TimeZoneNames", gMethods, NELEM(gMethods)); 149ac7cf58962995825464af08ae6fa5e006c94f3faElliott Hughes} 150