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