The dttoasc() function

The dttoasc() function converts the field values of a datetime variable to an ASCII string that conforms to ANSI SQL standards.

For information about the ANSI SQL DATETIME standard, see ANSI SQL standards for DATETIME and INTERVAL values.

Syntax

mint dttoasc(dtvalue, outbuf)
   dtime_t *dtvalue;
   char *outbuf;
dtvalue
A pointer to the initialized datetime variable to convert.
outbuf
A pointer to the buffer that receives the ANSI-standard DATETIME string for the value in dtvalue.

Usage

The dttoasc() function converts the digits of the fields in the datetime variable to their character equivalents and copies them to the outbuf character string with delimiters (hyphen, space, colon, or period) between them. You must initialize the datetime variable in dtvalue with the qualifier that you want the character string to have.

The character string does not include the qualifier or the parentheses that SQL statements use to delimit a DATETIME literal. The outbuf string conforms to ANSI SQL standards. It includes one character for each delimiter, plus the fields, which are of the following sizes.
Field
Field size
Year
Four digits
Fraction of DATETIME
As specified by precision
All other fields
Two digits
A datetime value with the year to fraction(5) qualifier produces the maximum length of output. The string equivalent contains 19 digits, 6 delimiters, and the null terminator, for a total of 26 bytes:
YYYY-MM-DD HH:MM:SS.FFFFF

If you do not initialize the qualifier of the datetime variable, the dttoasc() function returns an unpredictable value, but this value does not exceed 26 bytes.

Return codes

0
The conversion was successful.
<0
The conversion failed.

Example

The demo directory contains this sample program in the file dttoasc.ec.
/*
   * dttoasc.ec *

   The following program illustrates the conversion of a datetime value
   into an ASCII string in ANSI SQL format
*/

#include <stdio.h>

EXEC SQL include datetime;

main()
{
    char out_str[16];

    EXEC SQL BEGIN DECLARE SECTION;  
      datetime year to hour dt1;
    EXEC SQL END DECLARE SECTION;

   printf("DTTOASC Sample ESQL Program running.\n\n");

    /* Initialize dt1 */
    dtcurrent(&dt1);

    /* Convert the internal format to ascii for displaying */
    dttoasc(&dt1, out_str);

    /* Print it out*/
    printf("\tToday's datetime (year to hour)value  is %s\n", out_str);

   printf("\nDTTOASC Sample Program over.\n\n");
}

Output

DTTOASC Sample ESQL Program running.

   Today's datetime (year to hour) value is 2007-09-19 08

DTTOASC Sample Program over.