Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

udat.h File Reference

C API: DateFormat. More...

#include "unicode/utypes.h"
#include "unicode/ucal.h"
#include "unicode/unum.h"

Go to the source code of this file.

Typedefs

typedef void * UDateFormat
 A date formatter. More...

typedef enum UDateFormatStyle UDateFormatStyle
typedef enum UDateFormatSymbolType UDateFormatSymbolType
typedef UDateFormatSymbols UDateFormatSymbols

Enumerations

enum  UDateFormatStyle {
  UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT,
  UDAT_DEFAULT = UDAT_MEDIUM, UDAT_NONE = -1, UDAT_IGNORE = -2
}
 The possible date/time format styles. More...

enum  UDateFormatSymbolType {
  UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS, UDAT_WEEKDAYS,
  UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, UDAT_LOCALIZED_CHARS
}
 The possible types of date format symbols. More...


Functions

U_CAPI UDateFormat *U_EXPORT2 udat_open (UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const char *locale, const UChar *tzID, int32_t tzIDLength, const UChar *pattern, int32_t patternLength, UErrorCode *status)
 Open a new UDateFormat for formatting and parsing dates and times. More...

U_CAPI void U_EXPORT2 udat_close (UDateFormat *format)
 Close a UDateFormat. More...

U_CAPI UDateFormat *U_EXPORT2 udat_clone (const UDateFormat *fmt, UErrorCode *status)
 Open a copy of a UDateFormat. More...

U_CAPI int32_t U_EXPORT2 udat_format (const UDateFormat *format, UDate dateToFormat, UChar *result, int32_t resultLength, UFieldPosition *position, UErrorCode *status)
 Format a date using an UDateFormat. More...

U_CAPI UDate U_EXPORT2 udat_parse (const UDateFormat *format, const UChar *text, int32_t textLength, int32_t *parsePos, UErrorCode *status)
 Parse a string into an date/time using a UDateFormat. More...

U_CAPI UBool U_EXPORT2 udat_isLenient (const UDateFormat *fmt)
 Determine if an UDateFormat will perform lenient parsing. More...

U_CAPI void U_EXPORT2 udat_setLenient (UDateFormat *fmt, UBool isLenient)
 Specify whether an UDateFormat will perform lenient parsing. More...

U_CAPI const UCalendar *U_EXPORT2 udat_getCalendar (const UDateFormat *fmt)
 Get the UCalendar associated with an UDateFormat. More...

U_CAPI void U_EXPORT2 udat_setCalendar (UDateFormat *fmt, const UCalendar *calendarToSet)
 Set the UCalendar associated with an UDateFormat. More...

U_CAPI const UNumberFormat
*U_EXPORT2 
udat_getNumberFormat (const UDateFormat *fmt)
 Get the UNumberFormat associated with an UDateFormat. More...

U_CAPI void U_EXPORT2 udat_setNumberFormat (UDateFormat *fmt, const UNumberFormat *numberFormatToSet)
 Set the UNumberFormat associated with an UDateFormat. More...

U_CAPI const char *U_EXPORT2 udat_getAvailable (int32_t index)
 Get a locale for which date/time formatting patterns are available. More...

U_CAPI int32_t U_EXPORT2 udat_countAvailable (void)
 Determine how many locales have date/time formatting patterns available. More...

U_CAPI UDate U_EXPORT2 udat_get2DigitYearStart (const UDateFormat *fmt, UErrorCode *status)
 Get the year relative to which all 2-digit years are interpreted. More...

U_CAPI void U_EXPORT2 udat_set2DigitYearStart (UDateFormat *fmt, UDate d, UErrorCode *status)
 Set the year relative to which all 2-digit years will be interpreted. More...

U_CAPI int32_t U_EXPORT2 udat_toPattern (const UDateFormat *fmt, UBool localized, UChar *result, int32_t resultLength, UErrorCode *status)
 Extract the pattern from a UDateFormat. More...

U_CAPI void U_EXPORT2 udat_applyPattern (UDateFormat *format, UBool localized, const UChar *pattern, int32_t patternLength)
 Set the pattern used by an UDateFormat. More...

U_CAPI int32_t U_EXPORT2 udat_getSymbols (const UDateFormat *fmt, UDateFormatSymbolType type, int32_t index, UChar *result, int32_t resultLength, UErrorCode *status)
 Get the symbols associated with an UDateFormat. More...

U_CAPI int32_t U_EXPORT2 udat_countSymbols (const UDateFormat *fmt, UDateFormatSymbolType type)
 Count the number of particular symbols for an UDateFormat. More...

U_CAPI void U_EXPORT2 udat_setSymbols (UDateFormat *format, UDateFormatSymbolType type, int32_t index, UChar *value, int32_t valueLength, UErrorCode *status)
 Set the symbols associated with an UDateFormat. More...


Detailed Description

C API: DateFormat.

Date Format C API

Date Format C API consists of functions that convert dates and times 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 millis is known as "parsing." We currently define only one concrete structure UDateFormat, which can handle pretty much all normal date formatting and parsing actions.

Date Format helps you to 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.

To format a date for the current Locale with default time and date style, use one of the static factory methods:

 
  UErrorCode status = U_ZERO_ERROR;
  UChar *myString;
  int32_t myStrlen = 0;
  UDateFormat* dfmt = udat_open(UCAL_DEFAULT, UCAL_DEFAULT, NULL, "PST", &status);
  myStrlen = udat_format(dfmt, myDate, NULL, myStrlen, NULL, &status);
  if (status==U_BUFFER_OVERFLOW_ERROR){
      status=U_ZERO_ERROR;
      myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
      udat_format(dfmt, myDate, myString, myStrlen+1, NULL, &status);
  }
If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.
 
  UErrorCode status = U_ZERO_ERROR;
  int32_t i, myStrlen = 0;
  UChar* myString;
  char buffer[1024];
  UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values
  UDateFormat* df = udat_open(UCAL_DEFAULT, UCAL_DEFAULT, NULL, "GMT", &status);
  for (i = 0; i < 3; i++) {
      myStrlen = udat_format(df, myDateArr[i], NULL, myStrlen, NULL, &status);
      if(status == U_BUFFER_OVERFLOW_ERROR){
          status = U_ZERO_ERROR;
          myString = (UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
          udat_format(df, myDateArr[i], myString, myStrlen+1, NULL, &status);
          printf("%s\n", u_austrcpy(buffer, myString) );
          free(myString);
      }
  }
To get specific fields of a date, you can use UFieldPosition to get specific fields.
 
  UErrorCode status = U_ZERO_ERROR;
  UFieldPosition pos;
  UChar *myString;
  int32_t myStrlen = 0;
  char buffer[1024];

  pos.field = 1;  // Same as the DateFormat::EField enum
  UDateFormat* dfmt = udat_open(UCAL_DEFAULT, UCAL_DEFAULT, NULL, "PST", &status);
  myStrlen = udat_format(dfmt, myDate, NULL, myStrlen, &pos, &status);
  if (status==U_BUFFER_OVERFLOW_ERROR){
      status=U_ZERO_ERROR;
      myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
      udat_format(dfmt, myDate, myString, myStrlen+1, &pos, &status);
  }
  printf("date format: %s\n", u_austrcpy(buffer, myString));
  buffer[pos.endIndex] = 0;   // NULL terminate the string.
  printf("UFieldPosition position equals %s\n", &buffer[pos.beginIndex]);
To format a date for a different Locale, specify it in the call to udat_open()
 
        UDateFormat* df = udat_open(UDAT_SHORT, UDAT_SHORT, "fr_FR", "GMT", &status);
You can use a DateFormat API udat_parse() to parse.
 
  UErrorCode status = U_ZERO_ERROR;
  int32_t parsepos=0;
  UDate myDate = udat_parse(df, myString, u_strlen(myString), &parsepos, &status);
You can pass in different options for the arguments for date and time style to control the length of the result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the locale, but generally: see UDateFormatStyle for more details You can also set the time zone on the format if you wish.

You can also use forms of the parse and format methods with Parse Position and UFieldPosition to allow you to


Typedef Documentation

typedef void* UDateFormat
 

A date formatter.

For usage in C programs.


Enumeration Type Documentation

enum UDateFormatStyle
 

The possible date/time format styles.

Enumeration values:
UDAT_FULL  Full style.
UDAT_LONG  Long style.
UDAT_MEDIUM  Medium style.
UDAT_SHORT  Short style.
UDAT_DEFAULT  Default style.
UDAT_NONE  No style.
UDAT_IGNORE  for internal API use only.

enum UDateFormatSymbolType
 

The possible types of date format symbols.

Enumeration values:
UDAT_ERAS  The era names, for example AD.
UDAT_MONTHS  The month names, for example February.
UDAT_SHORT_MONTHS  The short month names, for example Feb.
UDAT_WEEKDAYS  The weekday names, for example Monday.
UDAT_SHORT_WEEKDAYS  The short weekday names, for example Mon.
UDAT_AM_PMS  The AM/PM names, for example AM.
UDAT_LOCALIZED_CHARS  The localized characters.


Function Documentation

U_CAPI void U_EXPORT2 udat_applyPattern UDateFormat   format,
UBool    localized,
const UChar   pattern,
int32_t    patternLength
 

Set the pattern used by an UDateFormat.

The pattern should follow the pattern syntax rules.

Parameters:
fmt  The formatter to set.
localized  TRUE if the pattern is localized, FALSE otherwise.
pattern  The new pattern
patternLength  The length of pattern, or -1 if null-terminated.
See also:
udat_toPattern @draft

U_CAPI UDateFormat* U_EXPORT2 udat_clone const UDateFormat   fmt,
UErrorCode   status
 

Open a copy of a UDateFormat.

This function performs a deep copy.

Parameters:
fmt  The format to copy
status  A pointer to an UErrorCode to receive any errors.
Returns:
A pointer to a UDateFormat identical to fmt. @stable

U_CAPI void U_EXPORT2 udat_close UDateFormat   format
 

Close a UDateFormat.

Once closed, a UDateFormat may no longer be used.

Parameters:
fmt  The formatter to close. @stable

U_CAPI int32_t U_EXPORT2 udat_countAvailable void   
 

Determine how many locales have date/time formatting patterns available.

This function is most useful as determining the loop ending condition for calls to \Ref{udat_getAvailable}.

Returns:
The number of locales for which date/time formatting patterns are available.
See also:
udat_getAvailable @stable

U_CAPI int32_t U_EXPORT2 udat_countSymbols const UDateFormat   fmt,
UDateFormatSymbolType    type
 

Count the number of particular symbols for an UDateFormat.

This function is most useful as for detemining the loop termination condition for calls to \Ref{udat_getSymbols}.

Parameters:
fmt  The formatter to query.
type  The type of symbols to count. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS, UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
Returns:
The number of symbols of type type.
See also:
udat_getSymbols , udat_setSymbols @stable

U_CAPI int32_t U_EXPORT2 udat_format const UDateFormat   format,
UDate    dateToFormat,
UChar   result,
int32_t    resultLength,
UFieldPosition   position,
UErrorCode   status
 

Format a date using an UDateFormat.

The date will be formatted using the conventions specified in \Ref{udat_open} or \Ref{udat_openPattern}

Parameters:
format  The formatter to use
dateToFormat  The date to format
result  A pointer to a buffer to receive the formatted number.
resultLength  The maximum size of result.
position  A pointer to a UFieldPosition. 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.
status  A pointer to an UErrorCode to receive any errors
Returns:
The total buffer size needed; if greater than resultLength, the output was truncated.
See also:
udat_parse , UFieldPosition @stable

U_CAPI UDate U_EXPORT2 udat_get2DigitYearStart const UDateFormat   fmt,
UErrorCode   status
 

Get the year relative to which all 2-digit years are interpreted.

For example, if the 2-digit start year is 2100, the year 99 will be interpreted as 2199.

Parameters:
fmt  The formatter to query.
status  A pointer to an UErrorCode to receive any errors
Returns:
The year relative to which all 2-digit years are interpreted.
See also:
udat_Set2DigitYearStart @stable

U_CAPI const char* U_EXPORT2 udat_getAvailable int32_t    index
 

Get a locale for which date/time formatting patterns are available.

A UDateFormat in a locale returned by this function will perform the correct formatting and parsing for the locale.

Parameters:
index  The index of the desired locale.
Returns:
A locale for which date/time formatting patterns are available, or 0 if none.
See also:
udat_countAvailable @stable

U_CAPI const UCalendar* U_EXPORT2 udat_getCalendar const UDateFormat   fmt
 

Get the UCalendar associated with an UDateFormat.

A UDateFormat uses a UCalendar to convert a raw value to, for example, the day of the week.

Parameters:
fmt  The formatter to query.
Returns:
A pointer to the UCalendar used by fmt.
See also:
udat_setCalendar @stable

U_CAPI const UNumberFormat* U_EXPORT2 udat_getNumberFormat const UDateFormat   fmt
 

Get the UNumberFormat associated with an UDateFormat.

A UDateFormat uses a UNumberFormat to format numbers within a date, for example the day number.

Parameters:
fmt  The formatter to query.
Returns:
A pointer to the UNumberFormat used by fmt to format numbers.
See also:
udat_setNumberFormat @stable

U_CAPI int32_t U_EXPORT2 udat_getSymbols const UDateFormat   fmt,
UDateFormatSymbolType    type,
int32_t    index,
UChar   result,
int32_t    resultLength,
UErrorCode   status
 

Get the symbols associated with an UDateFormat.

The symbols are what a UDateFormat uses to represent locale-specific data, for example month or day names.

Parameters:
fmt  The formatter to query.
type  The type of symbols to get. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS, UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
index  The desired symbol of type type.
result  A pointer to a buffer to receive the pattern.
resultLength  The maximum size of result.
status  A pointer to an UErrorCode to receive any errors
Returns:
The total buffer size needed; if greater than resultLength, the output was truncated.
See also:
udat_countSymbols , udat_setSymbols @stable

U_CAPI UBool U_EXPORT2 udat_isLenient const UDateFormat   fmt
 

Determine if an UDateFormat will perform lenient parsing.

With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match the pattern. With strict parsing, inputs must match the pattern.

Parameters:
fmt  The formatter to query
Returns:
TRUE if fmt is set to perform lenient parsing, FALSE otherwise.
See also:
udat_setLenient @stable

U_CAPI UDateFormat* U_EXPORT2 udat_open UDateFormatStyle    timeStyle,
UDateFormatStyle    dateStyle,
const char *    locale,
const UChar   tzID,
int32_t    tzIDLength,
const UChar   pattern,
int32_t    patternLength,
UErrorCode   status
 

Open a new UDateFormat for formatting and parsing dates and times.

A UDateFormat may be used to format dates in calls to \Ref{udat_format}, and to parse dates in calls to \Ref{udat_parse}.

Parameters:
timeStyle  The style used to format times; one of UDAT_FULL_STYLE, UDAT_LONG_STYLE, UDAT_MEDIUM_STYLE, UDAT_SHORT_STYLE, or UDAT_DEFAULT_STYLE
dateStyle  The style used to format dates; one of UDAT_FULL_STYLE, UDAT_LONG_STYLE, UDAT_MEDIUM_STYLE, UDAT_SHORT_STYLE, or UDAT_DEFAULT_STYLE
locale  The locale specifying the formatting conventions
tzID  A timezone ID specifying the timezone to use. If 0, use the default timezone.
tzIDLength  The length of tzID, or -1 if null-terminated.
status  A pointer to an UErrorCode to receive any errors
pattern  A pattern specifying the format to use.
patternLength  The number of characters in the pattern, or -1 if null-terminated.
locale  The locale specifying the formatting conventions
status  A pointer to an UErrorCode to receive any errors
Returns:
A pointer to a UDateFormat to use for formatting dates and times, or 0 if an error occurred. @stable

U_CAPI UDate U_EXPORT2 udat_parse const UDateFormat   format,
const UChar   text,
int32_t    textLength,
int32_t *    parsePos,
UErrorCode   status
 

Parse a string into an date/time using a UDateFormat.

The date will be parsed using the conventions specified in \Ref{udat_open} or \Ref{udat_openPattern}

Parameters:
fmt  The formatter to use.
text  The text to parse.
textLength  The length of text, or -1 if null-terminated.
parsePos  If not 0, on input a pointer to an integer specifying the offset at which to begin parsing. If not 0, on output the offset at which parsing ended.
status  A pointer to an UErrorCode to receive any errors
Returns:
The value of the parsed date/time
See also:
udat_format @stable

U_CAPI void U_EXPORT2 udat_set2DigitYearStart UDateFormat   fmt,
UDate    d,
UErrorCode   status
 

Set the year relative to which all 2-digit years will be interpreted.

For example, if the 2-digit start year is 2100, the year 99 will be interpreted as 2199.

Parameters:
fmt  The formatter to set.
d  The year relative to which all 2-digit years will be interpreted.
status  A pointer to an UErrorCode to receive any errors
See also:
udat_Set2DigitYearStart @stable

U_CAPI void U_EXPORT2 udat_setCalendar UDateFormat   fmt,
const UCalendar   calendarToSet
 

Set the UCalendar associated with an UDateFormat.

A UDateFormat uses a UCalendar to convert a raw value to, for example, the day of the week.

Parameters:
fmt  The formatter to set.
calendarToSet  A pointer to an UCalendar to be used by fmt.
See also:
udat_setCalendar @stable

U_CAPI void U_EXPORT2 udat_setLenient UDateFormat   fmt,
UBool    isLenient
 

Specify whether an UDateFormat will perform lenient parsing.

With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match the pattern. With strict parsing, inputs must match the pattern.

Parameters:
fmt  The formatter to set
isLenient  TRUE if fmt should perform lenient parsing, FALSE otherwise.
See also:
dat_isLenient @stable

U_CAPI void U_EXPORT2 udat_setNumberFormat UDateFormat   fmt,
const UNumberFormat   numberFormatToSet
 

Set the UNumberFormat associated with an UDateFormat.

A UDateFormat uses a UNumberFormat to format numbers within a date, for example the day number.

Parameters:
fmt  The formatter to set.
numberFormatToSet  A pointer to the UNumberFormat to be used by fmt to format numbers.
See also:
udat_getNumberFormat @stable

U_CAPI void U_EXPORT2 udat_setSymbols UDateFormat   format,
UDateFormatSymbolType    type,
int32_t    index,
UChar   value,
int32_t    valueLength,
UErrorCode   status
 

Set the symbols associated with an UDateFormat.

The symbols are what a UDateFormat uses to represent locale-specific data, for example month or day names.

Parameters:
fmt  The formatter to set
type  The type of symbols to set. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS, UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
index  The index of the symbol to set of type type.
value  The new value
valueLength  The length of value, or -1 if null-terminated
status  A pointer to an UErrorCode to receive any errors
Returns:
A pointer to result.
See also:
udat_getSymbols , udat_countSymbols @stable

U_CAPI int32_t U_EXPORT2 udat_toPattern const UDateFormat   fmt,
UBool    localized,
UChar   result,
int32_t    resultLength,
UErrorCode   status
 

Extract the pattern from a UDateFormat.

The pattern will follow the pattern syntax rules.

Parameters:
fmt  The formatter to query.
localized  TRUE if the pattern should be localized, FALSE otherwise.
result  A pointer to a buffer to receive the pattern.
resultLength  The maximum size of result.
status  A pointer to an UErrorCode to receive any errors
Returns:
The total buffer size needed; if greater than resultLength, the output was truncated.
See also:
udat_applyPattern @stable


Generated on Mon Mar 4 20:05:53 2002 for ICU 2.0 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002