The Udatepg module provides flexible generation of date format patterns, like "yy-MM-dd". The user can build up the generator by adding successive patterns.
The Udatepg module provides flexible generation of date format patterns, like "yy-MM-dd". The user can build up the generator by adding successive patterns. Once that is done, a query can be made using a "skeleton", which is a pattern that just includes the desired fields and lengths. The generator will return the "best fit" pattern corresponding to that skeleton.
The main method people will use is i18n_udatepg_get_best_pattern(), since normally i18n_udatepg_h is pre-built with data from a particular locale. However, generators can be built directly from other data as well.
Gets the best pattern according to a given locale and formats a current date and time using an i18n_udate_format_h
i18n_udatepg_h pattern_generator = NULL;
char *locale = I18N_ULOCALE_US;
dlog_print(DLOG_INFO, LOG_TAG, "pattern_generator\n");
if(!pattern_generator) {
i18n_udatepg_create(locale, &pattern_generator);
}
if(!pattern_generator) {
dlog_print(DLOG_INFO, LOG_TAG, "udatpg_open fail");
return ;
}
i18n_uchar bestPattern[64] = {0,};
char bestPatternString[64] = {0,};
int bestPatternLength, len;
const char *custom_format = "yyyy.MM.dd G 'at' HH:mm:ss zzz";
i18n_uchar uch_custom_format[64];
int ret = I18N_ERROR_NONE;
dlog_print(DLOG_INFO, LOG_TAG, "getBestPattern\n");
i18n_ustring_copy_ua(uch_custom_format, custom_format);
len = i18n_ustring_get_length(uch_custom_format);
i18n_udatepg_get_best_pattern(pattern_generator, uch_custom_format, len, bestPattern, 64, &bestPatternLength);
i18n_ustring_copy_au_n(bestPatternString, bestPattern, 64);
dlog_print(DLOG_INFO, LOG_TAG, "getBestPattern(char[]) : %s \n", bestPatternString);
i18n_udatepg_destroy(pattern_generator);
i18n_udate_format_h formatter_KR = NULL;
i18n_udate_format_h formatter_LA = NULL;
i18n_udate_format_h formatter_SaoPaulo = NULL;
i18n_uchar formatted[64] = {0,};
char result[64] = {0,};
int formattedLength;
i18n_udate date;
const char *timezone_KR = "GMT+9:00";
const char *timezone_LA = "America/Los_Angeles";
const char *timezone_SaoPaulo = "America/Sao_Paulo";
i18n_uchar utf16_timezone_KR[64] = {0,};
i18n_uchar utf16_timezone_LA[64] = {0,};
i18n_uchar utf16_timezone_SaoPaulo[64] = {0,};
i18n_ustring_copy_ua_n(utf16_timezone_KR, timezone_KR, strlen(timezone_KR));
i18n_ustring_copy_ua_n(utf16_timezone_LA, timezone_LA, strlen(timezone_LA));
i18n_ustring_copy_ua_n(utf16_timezone_SaoPaulo, timezone_SaoPaulo, strlen(timezone_SaoPaulo));
ret = i18n_udate_create(I18N_UDATE_FULL , I18N_UDATE_FULL , locale, utf16_timezone_KR, -1, bestPattern, -1, &formatter_KR);
if ( ret != I18N_ERROR_NONE ) {
dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_create is failed !!! \n");
}
if (!formatter_KR) {
dlog_print(DLOG_INFO, LOG_TAG, "formatter is NULL\n");
}
ret = i18n_udate_create(I18N_UDATE_FULL , I18N_UDATE_FULL , locale, utf16_timezone_LA, -1, bestPattern, -1, &formatter_LA);
if ( ret != I18N_ERROR_NONE ) {
dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_create is failed !!! \n");
}
if (!formatter_LA) {
dlog_print(DLOG_INFO, LOG_TAG, "formatter is NULL\n");
}
ret = i18n_udate_create(I18N_UDATE_PATTERN , I18N_UDATE_PATTERN , locale, utf16_timezone_SaoPaulo, -1, bestPattern, -1, &formatter_SaoPaulo);
if ( ret != I18N_ERROR_NONE ) {
dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_create is failed !!! \n");
}
if (!formatter_LA) {
dlog_print(DLOG_INFO, LOG_TAG, "formatter is NULL\n");
}
dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_format_date\n");
i18n_ucalendar_get_now(&date);
i18n_udate_format_date(formatter_KR, date, formatted, 64, NULL, &formattedLength);
i18n_ustring_copy_au_n(result, formatted, 64);
dlog_print(DLOG_INFO, LOG_TAG, "KOREA/Seoul - Current date : %s\n",result);
i18n_udate_format_date(formatter_LA, date, formatted, 64, NULL, &formattedLength);
i18n_ustring_copy_au_n(result, formatted, 64);
dlog_print(DLOG_INFO, LOG_TAG, "America/LOS Angeles - Current date : %s\n",result);
i18n_udate_format_date(formatter_SaoPaulo, date, formatted, 64, NULL, &formattedLength);
i18n_ustring_copy_au_n(result, formatted, 64);
dlog_print(DLOG_INFO, LOG_TAG, "Brazil/Sao Paulo - Current date : %s\n",result);
dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_destroy\n");
i18n_udate_destroy(formatter_KR);
i18n_udate_destroy(formatter_LA);
i18n_udate_destroy(formatter_SaoPaulo);