2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <utils_i18n.h>
23 #include "util_time.h"
26 static i18n_udatepg_h _util_time_generator_get(const char *timezone_id)
28 static i18n_udatepg_h generator;
31 if (tz && !strcmp(tz, timezone_id)) {
36 i18n_udatepg_destroy(generator);
40 int ret = i18n_udatepg_create(timezone_id, &generator);
41 if (ret != I18N_ERROR_NONE) {
42 ERR("i18n_udatepg_create failed: %s", get_error_message(ret));
46 tz = strdup(timezone_id);
50 static i18n_udate_format_h __util_time_date_formatter_get(const char *locale, const char *timezone_id, const char *skeleton)
53 i18n_uchar u_skeleton[64] = {0,};
54 int32_t skeleton_len = 0, pattern_len;
56 i18n_uchar u_best_pattern[64] = {0,};
57 int32_t u_best_pattern_capacity;
58 i18n_udate_format_h formatter = NULL;
60 const i18n_udatepg_h generator = _util_time_generator_get(timezone_id);
62 ERR("_util_time_generator_get failed");
66 i18n_ustring_copy_ua_n(u_skeleton, skeleton, strlen(skeleton));
67 skeleton_len = i18n_ustring_get_length(u_skeleton);
69 u_best_pattern_capacity =
70 (int32_t) (sizeof(u_best_pattern) / sizeof((u_best_pattern)[0]));
72 status = i18n_udatepg_get_best_pattern(generator, u_skeleton, skeleton_len,
73 u_best_pattern, u_best_pattern_capacity, &pattern_len);
74 if (status != I18N_ERROR_NONE) {
75 ERR("i18n_udatepg_get_best_pattern failed: %s", get_error_message(status));
79 i18n_uchar u_timezone_id[64] = {0,};
80 i18n_ustring_copy_ua_n(u_timezone_id, timezone_id, sizeof(u_timezone_id));
81 status = i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale, u_timezone_id, -1,
82 u_best_pattern, -1, &formatter);
83 if (status != I18N_ERROR_NONE) {
84 ERR("i18n_udate_create() failed");
91 static i18n_udate_format_h __util_time_time_formatter_get(bool use24hformat, const char *locale, const char *timezone_id)
93 char buf[128] = { 0, };
95 i18n_uchar u_pattern[128] = { 0, };
96 i18n_uchar u_best_pattern[128] = { 0, };
97 int32_t u_best_pattern_capacity, u_best_pattern_len;
98 char a_best_pattern[128] = { 0, };
100 i18n_udate_format_h formatter = NULL;
102 const i18n_udatepg_h generator = _util_time_generator_get(timezone_id);
104 ERR("_util_time_generator_get failed");
109 snprintf(buf, sizeof(buf), "%s", "HH:mm");
111 /* set time format 12 */
112 snprintf(buf, sizeof(buf), "%s", "h:mm");
115 i18n_ustring_copy_ua_n(u_pattern, buf, sizeof(u_pattern));
117 u_best_pattern_capacity =
118 (int32_t) (sizeof(u_best_pattern) / sizeof((u_best_pattern)[0]));
120 status = i18n_udatepg_get_best_pattern(generator, u_pattern, sizeof(u_pattern),
121 u_best_pattern, u_best_pattern_capacity, &u_best_pattern_len);
122 if (status != I18N_ERROR_NONE) {
123 ERR("i18n_udatepg_get_best_pattern() failed: %s", get_error_message(status));
127 i18n_ustring_copy_au(a_best_pattern, u_best_pattern);
130 char *a_best_pattern_fixed = strtok_r(a_best_pattern, "a", &saveptr);
131 a_best_pattern_fixed = strtok_r(a_best_pattern_fixed, " ", &saveptr);
132 if (a_best_pattern_fixed) {
133 i18n_ustring_copy_ua(u_best_pattern, a_best_pattern_fixed);
136 i18n_uchar u_timezone_id[64] = {0,};
137 i18n_ustring_copy_ua_n(u_timezone_id, timezone_id, sizeof(u_timezone_id));
139 status = i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale, u_timezone_id, -1,
140 u_best_pattern, -1, &formatter);
141 if (status != I18N_ERROR_NONE) {
142 ERR("i18n_udate_create() failed");
149 static i18n_udate_format_h __util_time_ampm_formatter_get(const char *locale, const char *timezone_id)
153 i18n_uchar u_best_pattern[64] = {0,};
154 i18n_udate_format_h formatter = NULL;
156 i18n_ustring_copy_ua(u_best_pattern, "a");
158 i18n_uchar u_timezone_id[64] = {0,};
159 i18n_ustring_copy_ua_n(u_timezone_id, timezone_id, sizeof(u_timezone_id));
161 status = i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale, u_timezone_id, -1,
162 u_best_pattern, -1, &formatter);
164 if (status != I18N_ERROR_NONE) {
165 ERR("i18n_udate_create() failed");
172 static int __util_time_formatted_time_get(i18n_udate_format_h formatter, time_t tt, char *buf, int buf_len)
174 if (!formatter) return -1;
176 i18n_udate u_time = (i18n_udate)tt * 1000;
177 i18n_uchar u_formatted_str[64] = {0,};
178 int32_t u_formatted_str_capacity, buf_needed;
181 u_formatted_str_capacity = (int32_t)(sizeof(u_formatted_str) / sizeof((u_formatted_str)[0]));
183 status = i18n_udate_format_date(formatter, u_time, u_formatted_str, u_formatted_str_capacity, NULL, &buf_needed);
184 if (status != I18N_ERROR_NONE) {
185 ERR("i18n_udate_format_date() failed");
189 i18n_ustring_copy_au_n(buf,u_formatted_str, buf_len - 1);
190 DBG("time(%d) formatted(%s)", tt, buf);
192 return (int)i18n_ustring_get_length(u_formatted_str);
195 bool util_time_formatted_time_get(time_t time, const char *locale, const char *timezone, bool use24hformat, char **str_time, char **str_meridiem)
198 char buf_time[512] = {0,};
199 char buf_ampm[512] = {0,};
200 localtime_r(&time, &st);
202 i18n_udate_format_h timef, ampmf;
204 timef = __util_time_time_formatter_get(use24hformat, locale, timezone);
205 __util_time_formatted_time_get(timef, time, buf_time, sizeof(buf_time)-1);
208 ampmf = __util_time_ampm_formatter_get(locale, timezone);
209 int ampm_len = __util_time_formatted_time_get(ampmf, time, buf_ampm, sizeof(buf_ampm)-1);
211 if (st.tm_hour >= 0 && st.tm_hour < 12) {
212 snprintf(buf_ampm, sizeof(buf_ampm)-1, "AM");
214 snprintf(buf_ampm, sizeof(buf_ampm)-1, "PM");
219 if (str_time) *str_time = strdup(buf_time);
220 if (str_meridiem) *str_meridiem = strdup(buf_ampm);
225 bool util_time_formatted_date_get(time_t time, const char *locale, const char *timezone, const char *skeleton, char **str_date)
228 char buf_date[512] = {0,};
229 localtime_r(&time, &st);
230 i18n_udate_format_h datef;
232 datef = __util_time_date_formatter_get(locale, timezone, skeleton ? skeleton : "MMMMEd");
234 __util_time_formatted_time_get(datef, time, buf_date, sizeof(buf_date));
235 if (str_date != NULL) {
236 *str_date = strdup(buf_date);