tizen 2.3.1 release
[framework/api/base-utils.git] / src / utils_i18n_udate.c
1 /*
2  * Copyright (c) 2015 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 <unicode/udat.h>
18 #include <utils_i18n_udate.h>
19 #include <utils_i18n_private.h>
20
21 int i18n_udate_create ( i18n_udate_format_style_e time_style, i18n_udate_format_style_e date_style, const char *locale, const i18n_uchar *tz_id, int32_t tz_id_len, const i18n_uchar *pattern, int32_t pattern_len, i18n_udate_format_h *format )
22 {
23     retv_if( format == NULL, I18N_ERROR_INVALID_PARAMETER );
24
25     UErrorCode icu_error = U_ZERO_ERROR;
26     *format = udat_open(time_style, date_style, locale, tz_id, tz_id_len, pattern, pattern_len, &icu_error);
27     ERR("Error code : %d", icu_error);
28
29     i18n_error_code_e i18n_error;
30     ERR_MAPPING(icu_error, i18n_error);
31     return i18n_error;
32 }
33
34 int i18n_udate_destroy ( i18n_udate_format_h format )
35 {
36     retv_if( format == NULL, I18N_ERROR_INVALID_PARAMETER );
37
38     udat_close(format);
39     return I18N_ERROR_NONE;
40 }
41
42 int i18n_udate_format_date ( const i18n_udate_format_h format, i18n_udate date_to_format, i18n_uchar *result, int32_t result_len, i18n_ufield_position_h pos, int32_t *buf_size_needed )
43 {
44     retv_if( format == NULL || buf_size_needed == NULL, I18N_ERROR_INVALID_PARAMETER );
45
46     UErrorCode icu_error = U_ZERO_ERROR;
47     *buf_size_needed = udat_format(format, date_to_format, result, result_len, (UFieldPosition*)pos, &icu_error);
48     ERR("Error code : %d", icu_error);
49
50     i18n_error_code_e i18n_error;
51     ERR_MAPPING(icu_error, i18n_error);
52     return i18n_error;
53 }
54
55 // Newly Added APIs
56 int i18n_udate_to_calendar_date_field ( i18n_udate_format_field_e field, i18n_ucalendar_date_fields_e *date_field_type )
57 {
58     if (date_field_type == NULL)
59     {
60         return I18N_ERROR_INVALID_PARAMETER;
61     }
62
63     switch (field) {
64         case I18N_UDATE_FORMAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD:
65         case I18N_UDATE_FORMAT_TIMEZONE_ISO_FIELD:
66         case I18N_UDATE_FORMAT_TIMEZONE_ISO_LOCAL_FIELD:
67         case I18N_UDATE_FORMAT_FIELD_COUNT:
68             ERR("Unsupported field");
69             return I18N_ERROR_INVALID_PARAMETER;
70         default:
71             *date_field_type = (i18n_ucalendar_date_fields_e)udat_toCalendarDateField(field);
72     }
73
74     return I18N_ERROR_NONE;
75 }
76
77 int i18n_udate_clone ( const i18n_udate_format_h format, i18n_udate_format_h *format_clone )
78 {
79     if (format == NULL || format_clone == NULL)
80     {
81         return I18N_ERROR_INVALID_PARAMETER;
82     }
83
84     UErrorCode icu_error = U_ZERO_ERROR;
85     *format_clone = (i18n_udate_format_h)udat_clone((const UDateFormat *)format, &icu_error);
86     ERR("Error code : %d", icu_error);
87
88     i18n_error_code_e i18n_error;
89     ERR_MAPPING(icu_error, i18n_error);
90     return i18n_error;
91 }
92
93 int i18n_udate_parse ( const i18n_udate_format_h format, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos, i18n_udate *parsed_date )
94 {
95     if (format == NULL || text == NULL || parsed_date == NULL)
96     {
97         return I18N_ERROR_INVALID_PARAMETER;
98     }
99
100     UErrorCode icu_error = U_ZERO_ERROR;
101     *parsed_date = udat_parse((const UDateFormat *)format, text, text_length, parse_pos, &icu_error);
102     ERR("Error code : %d", icu_error);
103
104     i18n_error_code_e i18n_error;
105     ERR_MAPPING(icu_error, i18n_error);
106     return i18n_error;
107 }
108
109 int i18n_udate_parse_calendar (const i18n_udate_format_h format, i18n_ucalendar_h * calendar, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos )
110 {
111     if (format == NULL || calendar == NULL || text == NULL || text_length < -1)
112     {
113         return I18N_ERROR_INVALID_PARAMETER;
114     }
115
116     UErrorCode icu_error = U_ZERO_ERROR;
117     udat_parseCalendar((const UDateFormat *)format, calendar, text, text_length, parse_pos, &icu_error);
118     ERR("Error code : %d", icu_error);
119
120     i18n_error_code_e i18n_error;
121     ERR_MAPPING(icu_error, i18n_error);
122     return i18n_error;
123 }
124
125 i18n_ubool i18n_udate_is_lenient ( const i18n_udate_format_h format )
126 {
127     if (format == NULL)
128     {
129         set_last_result(I18N_ERROR_INVALID_PARAMETER);
130         return false;
131     }
132
133     set_last_result(I18N_ERROR_NONE);
134     return udat_isLenient((const UDateFormat *)format);
135 }
136
137 int i18n_udate_set_lenient ( i18n_udate_format_h format, i18n_ubool is_lenient )
138 {
139     if (format == NULL)
140     {
141         return I18N_ERROR_INVALID_PARAMETER;
142     }
143     udat_setLenient((UDateFormat *)format, is_lenient);
144     return I18N_ERROR_NONE;
145 }
146
147 int i18n_udate_get_calendar ( const i18n_udate_format_h format, i18n_ucalendar_h *calendar)
148 {
149     if (format == NULL || calendar == NULL)
150     {
151         return I18N_ERROR_INVALID_PARAMETER;
152     }
153     *calendar = (i18n_ucalendar_h)udat_getCalendar((const UDateFormat *)format);
154
155     return I18N_ERROR_NONE;
156 }
157
158 int i18n_udate_set_calendar ( i18n_udate_format_h format, const i18n_ucalendar_h calendar_to_set )
159 {
160     if (format == NULL || calendar_to_set == NULL)
161     {
162         return I18N_ERROR_INVALID_PARAMETER;
163     }
164     udat_setCalendar((UDateFormat *)format, (const UCalendar *)calendar_to_set);
165     return I18N_ERROR_NONE;
166 }
167
168 int i18n_udate_get_number_format ( const i18n_udate_format_h format, i18n_unumber_format_h *number_format )
169 {
170     if (format == NULL || number_format == NULL)
171     {
172         return I18N_ERROR_INVALID_PARAMETER;
173     }
174     set_last_result(I18N_ERROR_NONE);
175     *number_format = (i18n_unumber_format_h)udat_getNumberFormat((const UDateFormat *)format);
176     return I18N_ERROR_NONE;
177 }
178
179 int i18n_udate_set_number_format ( i18n_udate_format_h format, const i18n_unumber_format_h number_format_to_set )
180 {
181     if (format == NULL || number_format_to_set == NULL)
182     {
183         return I18N_ERROR_INVALID_PARAMETER;
184     }
185     udat_setNumberFormat((UDateFormat *)format, (const UNumberFormat *)number_format_to_set);
186     return I18N_ERROR_NONE;
187 }
188
189 const char *i18n_udate_get_available ( int32_t locale_index )
190 {
191     if(locale_index < 0) {
192         set_last_result(I18N_ERROR_INVALID_PARAMETER);
193         return NULL;
194     }
195     set_last_result(I18N_ERROR_NONE);
196     return udat_getAvailable(locale_index);
197 }
198
199 int32_t i18n_udate_count_available ( void )
200 {
201     set_last_result(I18N_ERROR_NONE);
202     return udat_countAvailable();
203 }
204
205 int i18n_udate_get_2digit_year_start ( const i18n_udate_format_h format, i18n_udate *year )
206 {
207     if (format == NULL || year == NULL)
208     {
209         return I18N_ERROR_INVALID_PARAMETER;
210     }
211
212     UErrorCode icu_error = U_ZERO_ERROR;
213     *year = (i18n_udate)udat_get2DigitYearStart((const UDateFormat *)format, &icu_error);
214     ERR("Error code : %d", icu_error);
215
216     i18n_error_code_e i18n_error;
217     ERR_MAPPING(icu_error, i18n_error);
218     return i18n_error;
219 }
220
221 int i18n_udate_set_2digit_year_start ( i18n_udate_format_h format, i18n_udate date )
222 {
223     if (format == NULL)
224     {
225         return I18N_ERROR_INVALID_PARAMETER;
226     }
227
228     UErrorCode icu_error = U_ZERO_ERROR;
229     udat_set2DigitYearStart((UDateFormat *)format, date, &icu_error);
230     ERR("Error code : %d", icu_error);
231
232     i18n_error_code_e i18n_error;
233     ERR_MAPPING(icu_error, i18n_error);
234     return i18n_error;
235 }
236
237 int32_t i18n_udate_to_pattern ( const i18n_udate_format_h format, i18n_ubool localized, i18n_uchar *result, int32_t result_length )
238 {
239     if (format == NULL)
240     {
241         set_last_result(I18N_ERROR_INVALID_PARAMETER);
242         return -1;
243     }
244
245     UErrorCode icu_error = U_ZERO_ERROR;
246     int32_t needed_buffer_size = udat_toPattern((const UDateFormat *)format, localized, result, result_length, &icu_error);
247     ERR("Error code : %d", icu_error);
248
249     i18n_error_code_e i18n_error;
250     ERR_MAPPING(icu_error, i18n_error);
251     set_last_result(i18n_error);
252     return needed_buffer_size;
253 }
254
255 int i18n_udate_apply_pattern ( i18n_udate_format_h format, i18n_ubool localized, const i18n_uchar *pattern, int32_t pattern_length )
256 {
257     if (format == NULL)
258     {
259         return I18N_ERROR_INVALID_PARAMETER;
260     }
261     udat_applyPattern((UDateFormat *)format, localized, pattern, pattern_length);
262     return I18N_ERROR_NONE;
263 }
264
265 int32_t i18n_udate_get_symbols ( const i18n_udate_format_h format, i18n_udate_format_symbol_type_e type, int32_t symbol_index, i18n_uchar *result, int32_t result_length )
266 {
267     if (format == NULL)
268     {
269         set_last_result(I18N_ERROR_INVALID_PARAMETER);
270         return -1;
271     }
272
273     UErrorCode icu_error = U_ZERO_ERROR;
274     int32_t needed_buffer_size = udat_getSymbols((const UDateFormat *)format, type, symbol_index, result, result_length, &icu_error);
275     ERR("Error code : %d", icu_error);
276
277     i18n_error_code_e i18n_error;
278     ERR_MAPPING(icu_error, i18n_error);
279     set_last_result(i18n_error);
280     return needed_buffer_size;
281 }
282
283 int32_t i18n_udate_count_symbols ( const i18n_udate_format_h format, i18n_udate_format_symbol_type_e type )
284 {
285     if (format == NULL)
286     {
287         set_last_result(I18N_ERROR_INVALID_PARAMETER);
288         return 0;
289     }
290
291     set_last_result(I18N_ERROR_NONE);
292     return udat_countSymbols((const UDateFormat *)format, type);
293 }
294
295 int i18n_udate_set_symbols ( i18n_udate_format_h format, i18n_udate_format_symbol_type_e type, int32_t symbol_index, i18n_uchar *value, int32_t value_length )
296 {
297     if (format == NULL || symbol_index < 0)
298     {
299         return I18N_ERROR_INVALID_PARAMETER;
300     }
301
302     UErrorCode icu_error = U_ZERO_ERROR;
303     udat_setSymbols((UDateFormat *)format, type, symbol_index, value, value_length, &icu_error);
304     ERR("Error code : %d", icu_error);
305
306     i18n_error_code_e i18n_error;
307     ERR_MAPPING(icu_error, i18n_error);
308     return i18n_error;
309 }
310
311 const char *i18n_udate_get_locale_by_type ( const i18n_udate_format_h format, i18n_ulocale_data_locale_type_e type )
312 {
313     if (format == NULL)
314     {
315         set_last_result(I18N_ERROR_INVALID_PARAMETER);
316         return NULL;
317     }
318
319     UErrorCode icu_error = U_ZERO_ERROR;
320     const char *locale_name = udat_getLocaleByType((const UDateFormat *)format, type, &icu_error);
321     ERR("Error code : %d", icu_error);
322
323     i18n_error_code_e i18n_error;
324     ERR_MAPPING(icu_error, i18n_error);
325     set_last_result(i18n_error);
326     return locale_name;
327 }
328
329 int i18n_udate_set_context ( i18n_udate_format_h format, i18n_udisplay_context_e value )
330 {
331     if (format == NULL)
332     {
333         return I18N_ERROR_INVALID_PARAMETER;
334     }
335
336     UErrorCode icu_error = U_ZERO_ERROR;
337     udat_setContext((UDateFormat *)format, value, &icu_error);
338     ERR("Error code : %d", icu_error);
339
340     i18n_error_code_e i18n_error;
341     ERR_MAPPING(icu_error, i18n_error);
342     return i18n_error;
343 }