Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / util_time.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <utils_i18n.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <time.h>
22
23 #include "util_time.h"
24 #include "log.h"
25
26 static i18n_udatepg_h _util_time_generator_get(const char *timezone_id)
27 {
28         static i18n_udatepg_h generator;
29         static char *tz;
30
31         if (tz && !strcmp(tz, timezone_id)) {
32                 return generator;
33         }
34
35         if (generator) {
36                 i18n_udatepg_destroy(generator);
37                 generator = NULL;
38         }
39
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));
43                 return NULL;
44         }
45         free(tz);
46         tz = strdup(timezone_id);
47         return generator;
48 }
49
50 static i18n_udate_format_h __util_time_date_formatter_get(const char *locale, const char *timezone_id, const char *skeleton)
51 {
52         int status;
53         i18n_uchar u_skeleton[64] = {0,};
54         int32_t skeleton_len = 0, pattern_len;
55
56         i18n_uchar u_best_pattern[64] = {0,};
57         int32_t u_best_pattern_capacity;
58         i18n_udate_format_h formatter = NULL;
59
60         const i18n_udatepg_h generator = _util_time_generator_get(timezone_id);
61         if (!generator) {
62                 ERR("_util_time_generator_get failed");
63                 return NULL;
64         }
65
66         i18n_ustring_copy_ua_n(u_skeleton, skeleton, strlen(skeleton));
67         skeleton_len = i18n_ustring_get_length(u_skeleton);
68
69         u_best_pattern_capacity =
70                 (int32_t) (sizeof(u_best_pattern) / sizeof((u_best_pattern)[0]));
71
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));
76                 return NULL;
77         }
78
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");
85                 return NULL;
86         }
87
88         return formatter;
89 }
90
91 static i18n_udate_format_h __util_time_time_formatter_get(bool use24hformat, const char *locale, const char *timezone_id)
92 {
93         char buf[128] = { 0, };
94         int status;
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, };
99
100         i18n_udate_format_h formatter = NULL;
101
102         const i18n_udatepg_h generator = _util_time_generator_get(timezone_id);
103         if (!generator) {
104                 ERR("_util_time_generator_get failed");
105                 return NULL;
106         }
107
108         if (use24hformat) {
109                 snprintf(buf, sizeof(buf), "%s", "HH:mm");
110         } else {
111                 /* set time format 12 */
112                 snprintf(buf, sizeof(buf), "%s", "h:mm");
113         }
114
115         i18n_ustring_copy_ua_n(u_pattern, buf, sizeof(u_pattern));
116
117         u_best_pattern_capacity =
118                 (int32_t) (sizeof(u_best_pattern) / sizeof((u_best_pattern)[0]));
119
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));
124                 return NULL;
125         }
126
127         i18n_ustring_copy_au(a_best_pattern, u_best_pattern);
128
129         char *saveptr= NULL;
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);
134         }
135
136         i18n_uchar u_timezone_id[64] = {0,};
137         i18n_ustring_copy_ua_n(u_timezone_id, timezone_id, sizeof(u_timezone_id));
138
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");
143                 return NULL;
144         }
145
146         return formatter;
147 }
148
149 static i18n_udate_format_h __util_time_ampm_formatter_get(const char *locale, const char *timezone_id)
150 {
151         int status;
152
153         i18n_uchar u_best_pattern[64] = {0,};
154         i18n_udate_format_h formatter = NULL;
155
156         i18n_ustring_copy_ua(u_best_pattern, "a");
157
158         i18n_uchar u_timezone_id[64] = {0,};
159         i18n_ustring_copy_ua_n(u_timezone_id, timezone_id, sizeof(u_timezone_id));
160
161         status = i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale, u_timezone_id, -1,
162                         u_best_pattern, -1, &formatter);
163
164         if (status != I18N_ERROR_NONE) {
165                 ERR("i18n_udate_create() failed");
166                 return NULL;
167         }
168
169         return formatter;
170 }
171
172 static int __util_time_formatted_time_get(i18n_udate_format_h formatter, time_t tt, char *buf, int buf_len)
173 {
174         if (!formatter) return -1;
175
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;
179         int status;
180
181         u_formatted_str_capacity = (int32_t)(sizeof(u_formatted_str) / sizeof((u_formatted_str)[0]));
182
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");
186                 return -1;
187         }
188
189         i18n_ustring_copy_au_n(buf,u_formatted_str, buf_len - 1);
190         DBG("time(%d) formatted(%s)", tt, buf);
191
192         return (int)i18n_ustring_get_length(u_formatted_str);
193 }
194
195 bool util_time_formatted_time_get(time_t time, const char *locale, const char *timezone, bool use24hformat, char **str_time, char **str_meridiem)
196 {
197         struct tm st;
198         char buf_time[512] = {0,};
199         char buf_ampm[512] = {0,};
200         localtime_r(&time, &st);
201
202         i18n_udate_format_h timef, ampmf;
203
204         timef = __util_time_time_formatter_get(use24hformat, locale, timezone);
205         __util_time_formatted_time_get(timef, time, buf_time, sizeof(buf_time)-1);
206
207         if (!use24hformat) {
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);
210                 if (ampm_len > 4) {
211                         if (st.tm_hour >= 0 && st.tm_hour < 12) {
212                                 snprintf(buf_ampm, sizeof(buf_ampm)-1, "AM");
213                         } else {
214                                 snprintf(buf_ampm, sizeof(buf_ampm)-1, "PM");
215                         }
216                 }
217         }
218
219         if (str_time) *str_time = strdup(buf_time);
220         if (str_meridiem) *str_meridiem = strdup(buf_ampm);
221
222         return true;
223 }
224
225 bool util_time_formatted_date_get(time_t time, const char *locale, const char *timezone, const char *skeleton, char **str_date)
226 {
227         struct tm st;
228         char buf_date[512] = {0,};
229         localtime_r(&time, &st);
230         i18n_udate_format_h datef;
231
232         datef = __util_time_date_formatter_get(locale, timezone, skeleton ? skeleton : "MMMMEd");
233
234         __util_time_formatted_time_get(datef, time, buf_date, sizeof(buf_date));
235         if (str_date != NULL) {
236                 *str_date = strdup(buf_date);
237                 return true;
238         }
239         return false;
240 }