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