Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2013 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 "DateIntervalFormat"
     18 
     19 #include "IcuUtilities.h"
     20 #include "JniConstants.h"
     21 #include "ScopedJavaUnicodeString.h"
     22 #include "UniquePtr.h"
     23 #include "cutils/log.h"
     24 #include "unicode/dtitvfmt.h"
     25 
     26 static jlong DateIntervalFormat_createDateIntervalFormat(JNIEnv* env, jclass, jstring javaSkeleton, jstring javaLocaleName, jstring javaTzName) {
     27   Locale locale = getLocale(env, javaLocaleName);
     28 
     29   ScopedJavaUnicodeString skeletonHolder(env, javaSkeleton);
     30   if (!skeletonHolder.valid()) {
     31     return 0;
     32   }
     33 
     34   UErrorCode status = U_ZERO_ERROR;
     35   DateIntervalFormat* formatter(DateIntervalFormat::createInstance(skeletonHolder.unicodeString(), locale, status));
     36   if (maybeThrowIcuException(env, "DateIntervalFormat::createInstance", status)) {
     37     return 0;
     38   }
     39 
     40   ScopedJavaUnicodeString tzNameHolder(env, javaTzName);
     41   if (!tzNameHolder.valid()) {
     42     return 0;
     43   }
     44   formatter->adoptTimeZone(TimeZone::createTimeZone(tzNameHolder.unicodeString()));
     45 
     46   return reinterpret_cast<uintptr_t>(formatter);
     47 }
     48 
     49 static void DateIntervalFormat_destroyDateIntervalFormat(JNIEnv*, jclass, jlong address) {
     50   delete reinterpret_cast<DateIntervalFormat*>(address);
     51 }
     52 
     53 static jstring DateIntervalFormat_formatDateInterval(JNIEnv* env, jclass, jlong address, jlong fromDate, jlong toDate) {
     54   DateIntervalFormat* formatter(reinterpret_cast<DateIntervalFormat*>(address));
     55   DateInterval date_interval(fromDate, toDate);
     56 
     57   UnicodeString s;
     58   FieldPosition pos = 0;
     59   UErrorCode status = U_ZERO_ERROR;
     60   formatter->format(&date_interval, s, pos, status);
     61   if (maybeThrowIcuException(env, "DateIntervalFormat::format", status)) {
     62     return NULL;
     63   }
     64 
     65   return env->NewString(s.getBuffer(), s.length());
     66 }
     67 
     68 static JNINativeMethod gMethods[] = {
     69   NATIVE_METHOD(DateIntervalFormat, createDateIntervalFormat, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J"),
     70   NATIVE_METHOD(DateIntervalFormat, destroyDateIntervalFormat, "(J)V"),
     71   NATIVE_METHOD(DateIntervalFormat, formatDateInterval, "(JJJ)Ljava/lang/String;"),
     72 };
     73 void register_libcore_icu_DateIntervalFormat(JNIEnv* env) {
     74   jniRegisterNativeMethods(env, "libcore/icu/DateIntervalFormat", gMethods, NELEM(gMethods));
     75 }
     76