diff options
-rw-r--r-- | alarm-app/src/List/AlarmItem.cpp | 2 | ||||
-rw-r--r-- | lib-apps-common/inc/I18n/Date.h | 48 | ||||
-rw-r--r-- | lib-apps-common/inc/I18n/DateFormatter.h | 61 | ||||
-rw-r--r-- | lib-apps-common/inc/I18n/String.h | 45 | ||||
-rw-r--r-- | lib-apps-common/src/I18n/Date.cpp | 37 | ||||
-rw-r--r-- | lib-apps-common/src/I18n/DateFormatter.cpp | 112 | ||||
-rw-r--r-- | lib-apps-common/src/I18n/String.cpp | 41 | ||||
-rw-r--r-- | lib-common/inc/Common/Format.h | 9 | ||||
-rw-r--r-- | lib-common/src/Common/Format.cpp | 20 |
9 files changed, 357 insertions, 18 deletions
diff --git a/alarm-app/src/List/AlarmItem.cpp b/alarm-app/src/List/AlarmItem.cpp index c53bf45..9704753 100644 --- a/alarm-app/src/List/AlarmItem.cpp +++ b/alarm-app/src/List/AlarmItem.cpp @@ -84,7 +84,7 @@ char *AlarmItem::getText(Evas_Object *parent, const char *part) if (m_Alarm.getRepeat()) { return strdup(Common::formatRepeat(m_Alarm.getRepeat())); } else { - return strdup(Common::formatDate(m_Alarm.getDate())); + return strdup(Common::formatDate(m_Alarm.getDate()).c_str()); } } diff --git a/lib-apps-common/inc/I18n/Date.h b/lib-apps-common/inc/I18n/Date.h new file mode 100644 index 0000000..3cf6d37 --- /dev/null +++ b/lib-apps-common/inc/I18n/Date.h @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I18N_DATE_H +#define I18N_DATE_H + +#include <ctime> +#include <utils_i18n_types.h> + +namespace I18n +{ + namespace Date + { + /** + * @return Current local date. + */ + EXPORT_API tm getCurrentDate(); + + /** + * @brief Convert local date to UTC date. + * @param[in] date Date to convert + * @return UTC date. + */ + EXPORT_API tm convertToUtc(const tm &date); + + /** + * @brief Convert local date to i18n_udate. + * @param[in] date Date to convert + * @return Date in i18n_udate format. + */ + EXPORT_API i18n_udate convertToUdate(const tm &date); + } +} + +#endif /* I18N_DATE_H */ diff --git a/lib-apps-common/inc/I18n/DateFormatter.h b/lib-apps-common/inc/I18n/DateFormatter.h new file mode 100644 index 0000000..81bdd80 --- /dev/null +++ b/lib-apps-common/inc/I18n/DateFormatter.h @@ -0,0 +1,61 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I18N_DATE_FORMATTER_H +#define I18N_DATE_FORMATTER_H + +#include "I18n/String.h" +#include <system_settings.h> + +namespace I18n +{ + /** + * @brief Performs locale-dependent date formatting based on pattern skeleton. + */ + class EXPORT_API DateFormatter + { + public: + /** + * @brief Create formatter. + * @param[in] skeleton Date pattern skeleton + */ + explicit DateFormatter(const char *skeleton); + DateFormatter(const DateFormatter &that) = delete; + DateFormatter(DateFormatter &&that); + ~DateFormatter(); + + DateFormatter & operator=(const DateFormatter &that) = delete; + DateFormatter & operator=(DateFormatter &&that); + + /** + * @brief Create string representation for given date. + * @param[in] date Date to format + * @return Formatted date. + */ + std::string formatDate(const tm &date); + + private: + void initialize(); + void initializePattern(const char *lang); + void onLocaleChanged(system_settings_key_e key); + + String m_Skeleton; + String m_Pattern; + i18n_udate_format_h m_Formatter; + }; +} + +#endif /* I18N_DATE_FORMATTER_H */ diff --git a/lib-apps-common/inc/I18n/String.h b/lib-apps-common/inc/I18n/String.h new file mode 100644 index 0000000..4d78002 --- /dev/null +++ b/lib-apps-common/inc/I18n/String.h @@ -0,0 +1,45 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I18N_STRING_H +#define I18N_STRING_H + +#include <string> +#include <utils_i18n.h> + +namespace I18n +{ + /** + * @brief String consisting of i18n_uchar. + */ + typedef std::basic_string<i18n_uchar> String; + + /** + * @brief Convert I18n string into UTF-8. + * @param[in] str I18n string + * @return UTF-8 string. + */ + EXPORT_API std::string toUtf8(const String &str); + + /** + * @brief Convert UTF-8 string into I18n. + * @param[in] str UTF-8 string + * @return I18n string. + */ + EXPORT_API String fromUtf8(const std::string &str); +} + +#endif /* I18N_STRING_H */ diff --git a/lib-apps-common/src/I18n/Date.cpp b/lib-apps-common/src/I18n/Date.cpp new file mode 100644 index 0000000..8f7612c --- /dev/null +++ b/lib-apps-common/src/I18n/Date.cpp @@ -0,0 +1,37 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "I18n/Date.h" + +using namespace I18n; + +tm Date::getCurrentDate() +{ + time_t now = time(nullptr); + return *localtime(&now); +} + +tm Date::convertToUtc(const tm &date) +{ + time_t time = mktime((tm *) &date); + return *gmtime(&time); +} + +i18n_udate Date::convertToUdate(const tm &date) +{ + tm utcDate = convertToUtc(date); + return mktime(&utcDate) * 1000.0; +} diff --git a/lib-apps-common/src/I18n/DateFormatter.cpp b/lib-apps-common/src/I18n/DateFormatter.cpp new file mode 100644 index 0000000..20ec294 --- /dev/null +++ b/lib-apps-common/src/I18n/DateFormatter.cpp @@ -0,0 +1,112 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "I18n/DateFormatter.h" +#include "I18n/Date.h" +#include "System/Settings.h" + +#include <utils_i18n_udate.h> +#include <utils_i18n_udatepg.h> + +using namespace I18n; +using namespace System; +using namespace std::placeholders; + +DateFormatter::DateFormatter(const char *skeleton) +{ + if (skeleton) { + m_Skeleton = fromUtf8(skeleton); + } + initialize(); + + Settings::addCallback(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, + { std::bind(&DateFormatter::onLocaleChanged, this, _1), this }); +} + +DateFormatter::DateFormatter(DateFormatter &&that) + : m_Formatter(nullptr) +{ + Settings::addCallback(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, + { std::bind(&DateFormatter::onLocaleChanged, this, _1), this }); + *this = std::move(that); +} + +DateFormatter::~DateFormatter() +{ + Settings::removeCallback(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, this); + i18n_udate_destroy(m_Formatter); +} + +DateFormatter &I18n::DateFormatter::operator=(DateFormatter &&that) +{ + m_Skeleton = std::move(that.m_Skeleton); + m_Pattern = std::move(that.m_Pattern); + std::swap(m_Formatter, that.m_Formatter); + return *this; +} + +std::string DateFormatter::formatDate(const tm &date) +{ + i18n_udate udate = Date::convertToUdate(date); + + int length = 0; + i18n_uchar fakeValue = 0; + i18n_udate_format_date(m_Formatter, udate, &fakeValue, 0, nullptr, &length); + + String result(length, 0); + i18n_udate_format_date(m_Formatter, udate, &result[0], result.size(), nullptr, &length); + + return toUtf8(result); +} + +void DateFormatter::initialize() +{ + char *lang = nullptr; + system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &lang); + + if (m_Formatter) { + i18n_udate_destroy(m_Formatter); + m_Formatter = nullptr; + } + + initializePattern(lang); + i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, lang, nullptr, 0, + m_Pattern.c_str(), -1, &m_Formatter); + + free(lang); +} + +void DateFormatter::initializePattern(const char *lang) +{ + i18n_udatepg_h generator = nullptr; + i18n_udatepg_create(lang, &generator); + + int length = 0; + i18n_uchar fakeValue = 0; + i18n_udatepg_get_best_pattern(generator, m_Skeleton.c_str(), m_Skeleton.size(), + &fakeValue, 0, &length); + + m_Pattern.resize(length); + i18n_udatepg_get_best_pattern(generator, m_Skeleton.c_str(), m_Skeleton.size(), + &m_Pattern[0], m_Pattern.size(), &length); + + i18n_udatepg_destroy(generator); +} + +void DateFormatter::onLocaleChanged(system_settings_key_e key) +{ + initialize(); +} diff --git a/lib-apps-common/src/I18n/String.cpp b/lib-apps-common/src/I18n/String.cpp new file mode 100644 index 0000000..554c9d0 --- /dev/null +++ b/lib-apps-common/src/I18n/String.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "I18n/String.h" + +std::string I18n::toUtf8(const I18n::String &str) +{ + int length = 0; + i18n_error_code_e err = I18N_ERROR_NONE; + i18n_ustring_to_UTF8(nullptr, 0, &length, str.c_str(), -1, &err); + + std::string result(length, 0); + i18n_ustring_to_UTF8(&result[0], result.size(), &length, str.c_str(), -1, &err); + + return result; +} + +I18n::String I18n::fromUtf8(const std::string &str) +{ + int length = 0; + i18n_error_code_e err = I18N_ERROR_NONE; + i18n_ustring_from_UTF8(nullptr, 0, &length, str.c_str(), -1, &err); + + I18n::String result(length, 0); + i18n_ustring_from_UTF8(&result[0], result.size(), &length, str.c_str(), -1, &err); + + return result; +} diff --git a/lib-common/inc/Common/Format.h b/lib-common/inc/Common/Format.h index f50bb20..dc80557 100644 --- a/lib-common/inc/Common/Format.h +++ b/lib-common/inc/Common/Format.h @@ -18,6 +18,7 @@ #define COMMON_FORMAT_H #include <ctime> +#include <string> #include <tizen.h> #include "Utils/Range.h" @@ -56,17 +57,17 @@ namespace Common /** * @brief Create string representation for given date. - * @param[in] date Time to format + * @param[in] date Date to format * @return Formatted date. */ - EXPORT_API const char *formatDate(const tm &date); + EXPORT_API std::string formatDate(const tm &date); /** * @brief Create string representation for given date for Screen Reader. - * @param[in] date Time to format + * @param[in] date Date to format * @return Formatted date. */ - EXPORT_API const char *formatVerbalDate(const tm &date); + EXPORT_API std::string formatVerbalDate(const tm &date); /** * @brief Create string representation for weekly repeat mask. diff --git a/lib-common/src/Common/Format.cpp b/lib-common/src/Common/Format.cpp index e6be6f3..b56ca2b 100644 --- a/lib-common/src/Common/Format.cpp +++ b/lib-common/src/Common/Format.cpp @@ -15,11 +15,10 @@ */ #include "Common/Format.h" +#include "I18n/DateFormatter.h" #include <app_i18n.h> #include <array> -#include <string> -#include <system_settings.h> #define DAY_COUNT 7 #define HOUR_COUNT 24 @@ -27,7 +26,6 @@ #define SEC_COUNT 60 #define TIME_BUFFER_SIZE 64 -#define DATE_BUFFER_SIZE 32 #define REPEAT_BUFFER_SIZE 256 #define MESSAGE_BUFFER_SIZE 128 @@ -118,20 +116,16 @@ const char *Common::formatTime(const tm &time, int fontSize, const char *fontSty return buffer; } -const char *Common::formatDate(const tm &date) +std::string Common::formatDate(const tm &date) { - static char buffer[DATE_BUFFER_SIZE]; - /* TODO: Use i18n for locale-dependent pattern generation */ - strftime(buffer, sizeof(buffer), "%a, %d %b", &date); - return buffer; + static I18n::DateFormatter formatter("MMMdEEE"); + return formatter.formatDate(date); } -const char *Common::formatVerbalDate(const tm &date) +std::string Common::formatVerbalDate(const tm &date) { - static char buffer[DATE_BUFFER_SIZE]; - /* TODO: Use i18n for locale-dependent pattern generation */ - strftime(buffer, sizeof(buffer), "%A, %d %B", &date); - return buffer; + static I18n::DateFormatter formatter("MMMMdEEEE"); + return formatter.formatDate(date); } const char *Common::formatRepeat(int repeat) |