┌──────────────────────────────────────────────────────────────────────┐ │Use XPG4 functions to format language/culture dependent data. │ └──────────────────────────────────────────────────────────────────────┘
Notations for presenting date, time, numeric and monetary value vary among countries or cultures. An application should format these values depending on the current locale. XPG4 provides locale sensitive formatting functions and locale querying functions.
The formatting functions are:
┌───────────────┬─────────────────────────────────────────────┐ │Function name │Descriptions │ ├───────────────┼─────────────────────────────────────────────┤ │strftime() │Converts time to a multibyte string. │ ├───────────────┼─────────────────────────────────────────────┤ │strptime() │Converts time to a multibyte string and │ │ │return the pointer. │ ├───────────────┼─────────────────────────────────────────────┤ │wcsftime() │Converts time to a wide string. │ ├───────────────┼─────────────────────────────────────────────┤ │strfmon() │Converts a monetary value to a formatted │ │ │string. │ └───────────────┴─────────────────────────────────────────────┘
The following example checks buffer overflow and tries to format the date
in the specified length.
Formatting date value (XPRMMAIN.C)
#include <time.h> &ellips. /**********************************************************************/ /* formatDate() */ /* Format date value of timeval to the format specified in the */ /* current locale. The formatted string is stored in the buffer */ /* pointed to by pszBuf. The format varies depending on the buffer */ /* size specified by len. */ /**********************************************************************/ BOOL formatDate( time_t timeval, char* pszBuf, size_t len ) { struct tm* timeptr; timeptr = localtime( &timeval ); if( strftime( pszBuf, --len, "%x", timeptr ) == 0 ) /* locale representation*/ { if( strftime( pszBuf, len, "%Ex", timeptr ) == 0 ) /* alternate representation*/ { if( strftime( pszBuf, len, "%D", timeptr ) == 0 ) /* force yy/mm/dd format*/ return FALSE; } } return TRUE; }
As mentioned in The Exception - LC_MONETARY, a monetary value must be formatted according to the original locale so that the original currency is respected. In such a case, the currency symbol would not be always available on the current locale. Use %i directive to format to the international currency format (e.g. in USA: USD 1,234.56).
Formatting functions of numeric values other than monetary values are not supported. If an application needs to insert thousand separators or radix character, an application must query the characters by the nl_langinfo() function.