You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bh...@apache.org on 2014/07/30 21:06:46 UTC

[29/38] git commit: BB10: Use DateFormat to get the list of localized month and weekday names.

BB10: Use DateFormat to get the list of localized month and weekday names.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/0aa1a426
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/0aa1a426
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/0aa1a426

Branch: refs/heads/lyon-g11n
Commit: 0aa1a426a2b80e2c20b8ee486daa7964cd58ed8e
Parents: 09f84cf
Author: Lianghui Chen <li...@blackberry.com>
Authored: Tue Jul 29 16:56:46 2014 -0400
Committer: Lianghui Chen <li...@blackberry.com>
Committed: Tue Jul 29 16:56:46 2014 -0400

----------------------------------------------------------------------
 .../native/src/globalization_ndk.cpp            | 144 ++++++++++---------
 1 file changed, 79 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/0aa1a426/src/blackberry10/native/src/globalization_ndk.cpp
----------------------------------------------------------------------
diff --git a/src/blackberry10/native/src/globalization_ndk.cpp b/src/blackberry10/native/src/globalization_ndk.cpp
index 9828701..0210f0f 100644
--- a/src/blackberry10/native/src/globalization_ndk.cpp
+++ b/src/blackberry10/native/src/globalization_ndk.cpp
@@ -597,84 +597,98 @@ std::string GlobalizationNDK::getDateNames(const std::string& args)
             return errorInJson(PARSING_ERROR, error);
     }
 
-    UErrorCode status;
-    DateFormatSymbols* syms = new DateFormatSymbols(status);
-    if (!syms) {
-        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create DateFormatSymbols instance: %d.",
-                status);
-        return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormatSymbols instance!");
-    }
+    int count;
+    const char* pattern;
+    DateFormat::EStyle dstyle;
 
-    int count = 0;
-    const UnicodeString* names;
+    // Check ICU SimpleDateFormat document for patterns for months and days.
+    // http://www.icu-project.org/apiref/icu4c/classicu_1_1SimpleDateFormat.html
     if (item == kNamesMonths) {
-        if (type == kNamesWide)
-            names = syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
-        else
-            names = syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
-            // names = syms->getShortMonths(count);
+        count = 12;
+        if (type == kNamesWide) {
+            dstyle = DateFormat::kLong;
+            pattern = "MMMM";
+        } else {
+            dstyle = DateFormat::kShort;
+            pattern = "MMM";
+        }
     } else {
-        if (type == kNamesWide)
-            names = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
-        else
-            names = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
-            // names = syms->getShortWeekdays(count);
+        count = 7;
+        if (type == kNamesWide) {
+            dstyle = DateFormat::kLong;
+            pattern = "eeee";
+        } else {
+            dstyle = DateFormat::kShort;
+            pattern = "eee";
+        }
     }
 
-    std::list<std::string> utf8Names;
+    UErrorCode status = U_ZERO_ERROR;
+    const Locale& loc = Locale::getDefault();
+    DateFormat* df = DateFormat::createDateInstance(dstyle, loc);
 
-    if (names && count) {
-        for (int i = 0; i < count; ++i) {
-            std::string utf8;
-            (names + i)->toUTF8String(utf8);
-            utf8Names.push_back(utf8);
-        }
+    if (!df) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create DateFormat instance!");
+        return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!");
+    }
 
-        delete syms;
-    } else {
-        delete syms;
-
-        const char* format;
-        if (item == kNamesMonths) {
-            count = 12;
-            if (type == kNamesWide)
-                format = "%B";
-            else
-                format = "%b";
-        } else {
-            count = 7;
-            if (type == kNamesWide)
-                format = "%A";
-            else
-                format = "%a";
-        }
+    if (df->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
+        delete df;
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: DateFormat instance not SimpleDateFormat!");
+        return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!");
+    }
+
+    SimpleDateFormat* sdf = (SimpleDateFormat*) df;
+    sdf->applyLocalizedPattern(UnicodeString(pattern, -1), status);
 
-        struct tm ti = {0};
-        char buffer [80];
+    Calendar* cal = Calendar::createInstance(status);
+    if (!cal) {
+        delete sdf;
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create Calendar instance: %x.",
+                status);
+        return errorInJson(UNKNOWN_ERROR, "Unable to create Calendar instance!");
+    }
 
-        // We choose this day so it starts at Sunday and January.
-        ti.tm_year = 2014;
-        ti.tm_mon = 0;
-        ti.tm_wday = 0;
-        for (int i = 0; i < count; ++i) {
-            size_t len = strftime (buffer, 80, format, &ti);
+    UCalendarDaysOfWeek ud = cal->getFirstDayOfWeek(status);
+    if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) {
+        delete cal;
+        delete sdf;
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: failed to getFirstDayOfWeek: %d!",
+                status);
+        return errorInJson(PARSING_ERROR, "Failed to getFirstDayOfWeek!");
+    }
 
-            if (item == kNamesMonths)
-                ti.tm_mon++;
-            else
-                ti.tm_wday++;
+    if (ud == UCAL_SUNDAY)
+        cal->set(2014, 0, 5);
+    else
+        cal->set(2014, 0, 6);
 
-            if (!len)
-                continue;
+    std::list<std::string> utf8Names;
 
-            utf8Names.push_back(std::string(buffer, len));
-        }
+    for (int i = 0; i < count; ++i) {
+        UnicodeString ucs;
+        sdf->format(cal->getTime(status), ucs);
 
-        if (!utf8Names.size()) {
-            slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to get symbols: item: %d, type: %d.",
-                    item, type);
-            return errorInJson(UNKNOWN_ERROR, "Unable to get symbols!");
-        }
+        if (item == kNamesMonths)
+            cal->add(UCAL_MONTH, 1, status);
+        else
+            cal->add(UCAL_DAY_OF_MONTH, 1, status);
+
+        if (ucs.isEmpty())
+            continue;
+
+        std::string utf8;
+        ucs.toUTF8String(utf8);
+        utf8Names.push_back(utf8);
+    }
+
+    delete cal;
+    delete sdf;
+
+    if (!utf8Names.size()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to get symbols: item: %d, type: %d.",
+                item, type);
+        return errorInJson(UNKNOWN_ERROR, "Unable to get symbols!");
     }
 
     return resultInJson(utf8Names);