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:44 UTC

[27/38] git commit: BB10: Try to use strftime to get month/weekday names.

BB10: Try to use strftime to get month/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/13780feb
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/13780feb
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/13780feb

Branch: refs/heads/lyon-g11n
Commit: 13780feb779884cb52aaad2a5c5bdba64194e1af
Parents: 96bdea1
Author: Lianghui Chen <li...@blackberry.com>
Authored: Mon Jul 28 17:26:01 2014 -0400
Committer: Lianghui Chen <li...@blackberry.com>
Committed: Mon Jul 28 17:40:32 2014 -0400

----------------------------------------------------------------------
 .../native/src/globalization_ndk.cpp            | 77 ++++++++++++++++++--
 1 file changed, 69 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/13780feb/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 7b70d95..9828701 100644
--- a/src/blackberry10/native/src/globalization_ndk.cpp
+++ b/src/blackberry10/native/src/globalization_ndk.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <ctime>
+#include <list>
 #include <string>
 #include <json/reader.h>
 #include <json/writer.h>
@@ -184,6 +186,22 @@ std::string resultInJson(const std::string& pattern, const std::string& code,
     return writer.write(root);
 }
 
+std::string resultInJson(const std::list<std::string>& names)
+{
+    Json::Value result;
+    std::list<std::string>::const_iterator end = names.end();
+    std::list<std::string>::const_iterator iter = names.begin();
+    for (; iter != end; ++iter)
+        result.append(*iter);
+
+    Json::Value root;
+    root["result"] = result;
+
+    Json::FastWriter writer;
+    return writer.write(root);
+}
+
+
 GlobalizationNDK::GlobalizationNDK(GlobalizationJS *parent) {
 	m_pParent = parent;
 }
@@ -603,18 +621,61 @@ std::string GlobalizationNDK::getDateNames(const std::string& args)
             // names = syms->getShortWeekdays(count);
     }
 
-    if (!names) {
+    std::list<std::string> utf8Names;
+
+    if (names && count) {
+        for (int i = 0; i < count; ++i) {
+            std::string utf8;
+            (names + i)->toUTF8String(utf8);
+            utf8Names.push_back(utf8);
+        }
+
+        delete syms;
+    } else {
         delete syms;
 
-        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!");
-    }
+        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";
+        }
+
+        struct tm ti = {0};
+        char buffer [80];
 
-    std::string utf8Names;
-    names->toUTF8String(utf8Names);
+        // 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);
 
-    delete syms;
+            if (item == kNamesMonths)
+                ti.tm_mon++;
+            else
+                ti.tm_wday++;
+
+            if (!len)
+                continue;
+
+            utf8Names.push_back(std::string(buffer, len));
+        }
+
+        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);
 }