Tizen Native API
Functions | Typedefs
Udate
i18n

Functions

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, int pattern_len, i18n_udate_format_h *format)
 Creates a new i18n_udate_format_h for formatting and parsing dates and times.
int i18n_udate_destroy (i18n_udate_format_h format)
 Destroys an i18n_udate_format_h.
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)
 Formats a date using an i18n_udate_format_h.

Typedefs

typedef double i18n_udate
 Date and Time data type.
This is a primitive data type that holds the date and time as the number of milliseconds since 1970-jan-01, 00:00 UTC. UTC leap seconds are ignored.

The Udate module consists of functions that convert dates and time from their internal representations to textual form and back again in a language-independent manner.

Required Header

#include <utils_i18n.h>

Overview

The Udate module consists of functions that convert dates and time from their internal representations to textual form and back again in a language-independent manner. Converting from the internal representation (milliseconds since midnight, January 1, 1970) to text is known as "formatting," and converting from text to milliseconds is known as "parsing". We currently define only one concrete handle i18n_udate_format_h, which can handle pretty much all normal date formatting and parsing actions.
The Udate module helps you format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.

Sample Code 1

Gets the best pattern according to a given locale and formats a current date and time using a locale_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) {
        // open a pattern generator according to a given locale
        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);

    // gets the best pattern that matches the given custom_format
    i18n_udatepg_get_best_pattern(pattern_generator, uch_custom_format, len, bestPattern, 64, &bestPatternLength);

    i18n_ustring_copy_au_n(bestPatternString, bestPattern, 64);
    // gets "MM/dd/yyyy G h:mm:ss a zzz" as the best pattern
    dlog_print(DLOG_INFO, LOG_TAG, "getBestPattern(char[]) : %s \n", bestPatternString);

    // closes a generator
    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";   // TimeZone for Korea/Seoul
    const char *timezone_LA = "America/Los_Angeles";
    const char *timezone_SaoPaulo = "America/Sao_Paulo";    // Brazil/East
    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));

    // creates new i18n_udate_format_h to format dates and times
    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");

    // gets the current date and time
    i18n_ucalendar_get_now(&date);
    
    // formats a date using i18n_udate_format_h
    i18n_udate_format_date(formatter_KR, date, formatted, 64, NULL, &formattedLength);
    i18n_ustring_copy_au_n(result, formatted, 64);
    //ex) KOREA/Seoul - Current date : Wednesday, June 18, 2014 1:34:54 PM GMT+09:00
    dlog_print(DLOG_INFO, LOG_TAG, "KOREA/Seoul - Current date : %s\n",result);
    
    // formats a date using i18n_udate_format
    i18n_udate_format_date(formatter_LA, date, formatted, 64, NULL, &formattedLength);
    i18n_ustring_copy_au_n(result, formatted, 64);
    //ex) America/LOS Angeles - Current date : Tuesday, June 17, 2014 9:34:54 PM Pacific Daylight Time
    dlog_print(DLOG_INFO, LOG_TAG, "America/LOS Angeles - Current date : %s\n",result);

    // formats a date using i18n_udate_format_h
    i18n_udate_format_date(formatter_SaoPaulo, date, formatted, 64, NULL, &formattedLength);
    i18n_ustring_copy_au_n(result, formatted, 64);
    //ex) Brazil/Sao Paulo - Current date : 6 18, 2014 AD, 1:34:54 PM GMT-2
    dlog_print(DLOG_INFO, LOG_TAG, "Brazil/Sao Paulo - Current date : %s\n",result);

    dlog_print(DLOG_INFO, LOG_TAG, "i18n_udate_destroy\n"); 
    // destroy a i18n_udate_format_h
    i18n_udate_destroy(formatter_KR);
    i18n_udate_destroy(formatter_LA);
    i18n_udate_destroy(formatter_SaoPaulo);

Enumeration Type Documentation

Enumeration for the possible date/time format styles.

Enumerator:
I18N_UDATE_FULL 

Full style

I18N_UDATE_LONG 

Long style

I18N_UDATE_MEDIUM 

Medium style

I18N_UDATE_SHORT 

Short style

I18N_UDATE_DEFAULT 

Default style

I18N_UDATE_RELATIVE 

Bitfield for relative date

I18N_UDATE_FULL_RELATIVE 

I18N_UDATE_FULL | I18N_UDATE_RELATIVE

I18N_UDATE_LONG_RELATIVE 

I18N_UDATE_LONG | I18N_UDATE_RELATIVE

I18N_UDATE_MEDIUM_RELATIVE 

I18N_UDATE_MEDIUM | I18N_UDATE_RELATIVE

I18N_UDATE_SHORT_RELATIVE 

I18N_UDATE_SHORT | I18N_UDATE_RELATIVE

I18N_UDATE_NONE 

No style

I18N_UDATE_PATTERN 

Use the pattern given in the parameter to i18n_udate_create().


Function Documentation

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,
int  pattern_len,
i18n_udate_format_h *  format 
)

Creates a new i18n_udate_format_h for formatting and parsing dates and times.

A i18n_udate_format_h may be used to format dates in calls to i18n_udate_create().

Since :
2.3
Remarks:
Must release format using i18n_udate_destroy().
Parameters:
[in]time_styleThe style used to format times
One of I18N_UDATE_FULL, I18N_UDATE_LONG, I18N_UDATE_MEDIUM, I18N_UDATE_SHORT, I18N_UDATE_DEFAULT, or I18N_UDATE_NONE (relative time styles are not currently supported).
[in]date_styleThe style used to format dates
One of I18N_UDATE_FULL, I18N_UDATE_LONG, I18N_UDATE_MEDIUM, I18N_UDATE_SHORT, I18N_UDATE_DEFAULT, I18N_UDATE_RELATIVE, I18N_UDATE_LONG_RELATIVE, I18N_UDATE_MEDIUM_RELATIVE, I18N_UDATE_SHORT_RELATIVE, or I18N_UDATE_NONE
[in]localeThe locale specifying the formatting conventions
[in]tz_idA timezone ID specifying the timezone to use
If 0, use the default timezone
[in]tz_id_lenThe length of tz_id, otherwise -1 if null-terminated
[in]patternA pattern specifying the format to use. The pattern is generated by Udatepg module
[in]pattern_lenThe number of characters in the pattern, or otherwise -1 if null-terminated
[out]formatA pointer to an i18n_udate_format_h to use for formatting dates and times, otherwise 0 if an error occurs
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
int i18n_udate_destroy ( i18n_udate_format_h  format)

Destroys an i18n_udate_format_h.

Once destroyed, an i18n_udate_format_h may no longer be used.

Since :
2.3
Parameters:
[in]formatThe formatter to close.
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter
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 
)

Formats a date using an i18n_udate_format_h.

The date will be formatted using the conventions specified in i18n_udate_create()

Since :
2.3
Parameters:
[in]formatThe formatter to use
[in]date_to_formatThe date to format
[out]resultA pointer to a buffer to receive the formatted number
[in]result_lenThe maximum size of the result
[in]posA pointer to an i18n_ufield_position
On input, position->field is read
On output, position->beginIndex and position->endIndex indicate the beginning and ending indices of field number position->field, if such a field exists
This parameter may be NULL, in which case no field position data is returned.
[out]buf_size_neededThe total buffer size needed
If greater than result_len, the output was truncated.
Return values:
I18N_ERROR_NONESuccessful
I18N_ERROR_INVALID_PARAMETERInvalid function parameter

Except as noted, this content - excluding the Code Examples - is licensed under Creative Commons Attribution 3.0 and all of the Code Examples contained herein are licensed under BSD-3-Clause.
For details, see the Content License