INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 6: Working with Time Data Types
Home Contents Index Master Index New Book

DATETIME and INTERVAL Library Functions

The following C functions are available in ESQL/C to handle datetime and interval host variables.

(1 of 2)

Function Name Description

dtaddinv()

Adds an interval value to a datetime value

dtcurrent()

Gets the current date and time

dtcvasc()

Converts an ANSI-compliant character string to a datetime value

dtcvfmtasc()

Converts a character string with a specified format to a datetime value

dtextend()

Changes the qualifier of a datetime value

dtsub()

Subtracts one datetime value from another

dtsubinv()

Subtracts an interval value from a datetime value

dttoasc()

Converts a datetime value to an ANSI-compliant character string

dttofmtasc()

Converts a datetime value to a character string with a specified format

incvasc()

Converts an ANSI-compliant character string to an interval value

incvfmtasc()

Converts a character string with a specified format to an interval value

intoasc()

Converts an interval value to an ANSI-compliant character string

intofmtasc()

Converts an interval value to a character string with a specified format

invdivdbl()

Divides an interval value by a numeric value

invdivinv()

Divides an interval value by another interval value

invextend()

Extends an interval value to a different interval qualifier

invmuldbl()

Multiplies an interval value by a numeric value

When you compile your ESQL/C program with the esql command, esql automatically links these functions into your program. The following pages describe each of these functions.

dtaddinv()

The dtaddinv() function adds an interval value to a datetime value. The result is a datetime value.

Syntax

Usage

The dtaddinv() function adds the interval value in inv to the datetime value in dt and stores the datetime value in res. This result inherits the qualifier of dt.

The interval value must be in either the year to month or day to fraction(5) ranges.

The datetime value must include all the fields present in the interval value.

If you do not initialize the variables dt and inv, the function might return an unpredictable result.

Return Codes

0

The addition was successful.

<0

Error in addition.

Example

The demo directory contains this sample program in the dtaddinv.ec file.

Example Output

dtcurrent()

The dtcurrent() function assigns the current date and time to a datetime variable.

Syntax

Usage

When the variable qualifier is set to zero (or any invalid qualifier), the dtcurrent() function initializes it with the year to fraction(3) qualifier.

When the variable contains a valid qualifier, the dtcurrent() function extends the current date and time to agree with the qualifier.

Example Calls

The following statements set the host variable timewarp to the current date:

The following statements set the variable now to the current time, to the nearest millisecond:

Example

The demo directory contains this sample program in the dtcurrent.ec file.

Example Output

dtcvasc()

The dtcvasc() function converts a string that conforms to ANSI SQL standard for a DATETIME value to a datetime value. For information about the ANSI SQL DATETIME standard, see page 6-36.

Syntax

Usage

You must initialize the datetime variable in dtvalue with the qualifier that you want this variable to have.

The character string in inbuf must have values that conform to the year to second qualifier in the ANSI SQL format. The inbuf string can have leading and trailing spaces. However, from the first significant digit to the last, inbuf can only contain characters that are digits and delimiters that conform to the ANSI SQL standard for DATETIME values.

If you specify a year value as one or two digits, the dtcvasc() function assumes that the year is in the 20th century (it adds 1900 to the year value). You can set the DBCENTURY environment variable to determine which century dtcvasc() uses when you omit a century from the date.

If the character string is an empty string, the dtcvasc() function sets to null the value to which dtvalue points. If the character string is acceptable, the function sets the value in the datetime variable and returns zero. Otherwise, the function leaves the variable unchanged and returns a negative error code.

Return Codes

0

Conversion was successful.

-1260

It is not possible to convert between the specified types.

-1261

Too many digits in the first field of datetime or interval.

-1262

Non-numeric character in datetime or interval.

-1263

A field in a datetime or interval value is out of range or incorrect.

-1264

Extra characters exist at the end of a datetime or interval.

-1265

Overflow occurred on a datetime or interval operation.

-1266

A datetime or interval value is incompatible with the operation.

-1267

The result of a datetime computation is out of range.

-1268

A parameter contains an invalid datetime qualifier.

Example Call

The following example initializes the host variable leap_day to the extra day of leap year 1996:

Example

The demo directory contains this sample program in the dtcvasc.ec file.

Example Output

dtcvfmtasc()

The dtcvfmtasc() function uses a formatting mask to convert a character string to a datetime value.

Syntax

Usage

You must initialize the datetime variable in dtvalue with the qualifier that you want this variable to have. The datetime variable does not need to specify the same qualifier that the formatting mask implies. When the datetime qualifier is different from the implied formatting-mask qualifier, dtcvfmtasc() extends the datetime value (as if it had called the dtextend() function).

All qualifier fields in the character string in inbuf must be contiguous. In other words, if the qualifier is hour to second, you must specify all values for hour, minute, and second somewhere in the string, or the dtcvfmtasc() function returns an error.

The inbuf character string can have leading and trailing spaces. However, from the first significant digit to the last, inbuf can contain only digits and delimiters that are appropriate for the qualifier fields that the formatting mask implies. For more information on acceptable digits and delimiters for a DATETIME value, see the "ANSI SQL Standards for DATETIME and INTERVAL Values".

The dtcvfmtasc() function returns an error if the formatting mask, fmtstring, is an empty string. If fmtstring is a null pointer, the dtcvfmtasc() function must determine the format to use when it reads the character string in inbuf. When you use the default locale, the function uses the following precedence:

1. The format that the DBTIME environment variable specifies (if DBTIME is set). For more information on DBTIME, refer to the Informix Guide to SQL: Reference.

    2. The format that the GL_DATETIME environment variable specifies (if GL_DATETIME is set). For more information on GL_DATETIME, refer to the Informix Guide to GLS Functionality.

    3. The default date format conforms to the standard ANSI SQL format: %iY-%m-%d %H:%M:%S

The ANSI SQL format specifies a qualifier of year to second for the output. You can express the year as four digits (1995) or as two digits (95). When you use a two-digit year (%y) in a formatting mask, the dtcvfmtasc() function uses the value of the DBCENTURY environment variable to determine which century to use. If you do not set DBCENTURY, dtcvfmtasc() assumes the 20th century for two-digit years. For information on how to set DBCENTURY, see the Informix Guide to SQL: Reference.

GLS
When you use a nondefault locale (one other than U.S. English) and do not set the DBTIME or GL_DATETIME environment variables, dtcvfmtasc() uses the default DATETIME format that the locale defines. For more information, see the
Informix Guide to GLS Functionality.

When the character string and the formatting mask are acceptable, the dtcvfmtasc() function sets the datetime variable in dtvalue and returns zero. Otherwise, it returns an error code and the datetime variable contains an unpredictable value.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The demo directory contains this sample program in the file dtcvfmtasc.ec. The code initializes the variable birthday to a fictitious birthday.

Example Output

dtextend()

The dtextend() function extends a datetime value to a different qualifier. Extending is the operation of adding or dropping fields of a DATETIME value to make it match a given qualifier.

Syntax

Usage

The dtextend() function copies the qualifier-field digits of the in_dt datetime variable to the out_dt datetime variable. The qualifier of the out_dt variable controls the copy.

The function discards any fields in in_dt that the out_dt variable does not include. The function fills in any fields in out_dt that are not present in in_dt, as follows:

Return Codes

0

The operation was successful.

-1268

A parameter contains an invalid datetime qualifier.

Example Call

In these statements, a variable fiscal_start is set up with the first day of a fiscal year that begins on June 1. The dtextend() function generates the current year.

Example

The demo directory contains this sample program in the file dtextend.ec.

Example Output

dtsub()

The dtsub() function subtracts one datetime value from another. The result is an interval value.

Syntax

Usage

The dtsub() function subtracts the datetime value d2 from d1 and stores the interval result in inv. The result can be either a positive or a negative value. If necessary, the function extends d2 to match the qualifier for d1, before the subtraction.

Initialize the qualifier for inv with a value in either the year to month or day to fraction(5) classes. When d1 contains fields in the day to fraction class, the interval qualifier must also be in the day to fraction class.

Return Codes

0

The subtraction was successful.

<0

An error occurred while performing the subtraction.

Example

The demo directory contains this sample program in the file dtsub.ec. The program performs datetime subtraction that returns equivalent interval results in the range of year to month and month to month and attempts to return an interval result in the range day to hour.

Example Output

dtsubinv()

The dtsubinv() function subtracts an interval value from a datetime value. The result is a datetime value.

Syntax

Usage

The dtsubinv() function subtracts the interval value in inv from the datetime value in dt and stores the datetime value in res. This result inherits the qualifier of dt.

The datetime value must include all the fields present in the interval value. When you do not initialize the variables dt and inv, the function might return an unpredictable result.

Return Codes

0

The subtraction was successful.

<0

An error occurred while performing the subtraction.

Example

The demo directory contains this sample program in the file dtsubinv.ec.

Example Output

dttoasc()

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 page 6-36.

Syntax

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:

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.

Example Output

dttofmtasc()

The dttofmtasc() function uses a formatting mask to convert a datetime variable to a character string.

Syntax

Usage

You must initialize the datetime variable in dtvalue with the qualifier that you want the character string to have. If you do not initialize the datetime variable, the function returns an unpredictable value. The character string in outbuf does not include the qualifier or the parentheses that SQL statements use to delimit a DATETIME literal.

The formatting mask, fmtstring, does not need to imply the same qualifiers as the datetime variable. When the implied formatting-mask qualifier is different from the datetime qualifier, dttofmtasc() extends the datetime value (as if it called the dtextend() function).

If the formatting mask is an empty string, the function sets character string, outbuf, to an empty string. If fmtstring is a null pointer, the dttofmtasc() function must determine the format to use for the character string in outbuf. When you use the default locale, the function uses the following precedence:

1. The format that the DBTIME environment variable specifies (if DBTIME is set). For more information on DBTIME, refer to the Informix Guide to SQL: Reference.

    2. The format that the GL_DATETIME environment variable specifies (if GL_DATETIME is set). For more information on GL_DATETIME, refer to the Informix Guide to GLS Functionality.

    3. The default date format that conforms to the standard ANSI SQL
    format:
    %iY-%m-%d %H:%M:%S

When you use a two-digit year (%y) in a formatting mask, the dttofmtasc() function uses the value of the DBCENTURY environment variable to determine which century to use. If you do not set DBCENTURY, dttofmtasc() assumes the 20th century for two-digit years. For information on how to set DBCENTURY, see the Informix Guide to SQL: Reference.

GLS
When you use a nondefault locale (one other than U.S. English) and do not set the DBTIME or GL_DATETIME environment variables, dttofmtasc() uses the default DATETIME format that the client locale defines. For more information, see the
Informix Guide to GLS Functionality.

Return Codes

0

The conversion was successful.

<0

The conversion failed. Check the text of the error message.

Example

The demo directory contains this sample program in the file dttofmtasc.ec.

Example Output

incvasc()

The incvasc() function converts a string that conforms to the ANSI SQL standard for an INTERVAL value to an interval value. For information about the ANSI SQL interval standard, see page 6-36.

Syntax

Usage

You must initialize the interval variable in invvalue with the qualifier that you want this variable to have.

The character string in inbuf can have leading and trailing spaces. However, from the first significant digit to the last, inbuf can only contain characters that are digits and delimiters that are appropriate to the qualifier fields of the interval variable.

If the character string is an empty string, the incvasc() function sets the value in invvalue to null. If the character string is acceptable, the function sets the value in the interval variable and returns zero. Otherwise, the function sets the value in the interval value to null.

Return Codes

0

The conversion was successful.

-1260

It is not possible to convert between the specified types.

-1261

Too many digits in the first field of datetime or interval.

-1262

Non-numeric character in datetime or interval.

-1263

A field in a datetime or interval value is out of range or incorrect.

-1264

Extra characters at the end of a datetime or interval value.

-1265

Overflow occurred on a datetime or interval operation.

-1266

A datetime or interval value is incompatible with the operation.

-1267

The result of a datetime computation is out of range.

-1268

A parameter contains an invalid datetime or interval qualifier.

Example

The demo directory contains this sample program in the file incvasc.ec.

Example Output

incvfmtasc()

The incvfmtasc() function uses a formatting mask to convert a character string to an interval value.

Syntax

Usage

You must initialize the interval variable in invvalue with the qualifier you want this variable to have. The interval variable does not need to specify the same qualifier as the formatting mask. When the interval qualifier is different from the implied formatting-mask qualifier, incvfmtasc() converts the result to appropriate units as necessary. However, both qualifiers must belong to the same interval class: either the year to month class or the day to fraction class.

All fields in the character string in inbuf must be contiguous. In other words, if the qualifier is hour to second, you must specify all values for hour, minute, and second somewhere in the string, or incvfmtasc() returns an error.

The inbuf character string can have leading and trailing spaces. However, from the first significant digit to the last, inbuf can contain only digits and delimiters that are appropriate for the qualifier fields that the formatting mask implies. For more information on acceptable digits and delimiters for an INTERVAL value, see "ANSI SQL Standards for DATETIME and INTERVAL Values".

If the character string is acceptable, the incvfmtasc() function sets the interval value in invvalue and returns zero. Otherwise, the function returns an error code and the interval variable contains an unpredictable value.

The formatting directives %B, %b, and %p, which the DBTIME environment variable accepts, are not applicable in fmtstring because month name and A.M./P.M. information is not relevant for intervals of time. Use the %Y directive if the interval is more than 99 years (%y can handle only two digits). For hours, use %H (not %I, because %I can represent only 12 hours). If fmtstring is an empty string, the function returns an error.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The demo directory contains this sample program in the file incvfmtasc.ec.

Example Output

intoasc()

The intoasc() function converts the field values of an interval variable to an ASCII string that conforms to the ANSI SQL standard. For information about the ANSI SQL DATETIME standard, see page 6-36.

Syntax

Usage

The intoasc() function converts the digits of the fields in the interval 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 interval variable in invvalue 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 an INTERVAL literal. The outbuf string conforms to ANSI SQL standards. It includes one character for each delimiter (hyphen, space, colon, or period) plus fields with the following sizes.

Field Field Size

Leading field

As specified by precision

Fraction

As specified by precision

All other fields

Two digits

An interval value with the day(5) to fraction(5) qualifier produces the maximum length of output. The string equivalent contains 16 digits, 4 delimiters, and the null terminator, for a total of 21 bytes:

If you do not initialize the qualifier of the interval variable, the intoasc() function returns an unpredictable value, but this value does not exceed 21 bytes.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The demo directory contains this sample program in the file intoasc.ec.

Example Output

intofmtasc()

The intofmtasc() function uses a formatting mask to convert an interval variable to a character string.

Syntax

Usage

You must initialize the interval variable in invvalue with the qualifier that you want the character string to have. If you do not initialize the interval variable, the function returns an unpredictable value. The character string in outbuf does not include the qualifier or the parentheses that SQL statements use to delimit an INTERVAL literal.

The formatting mask, fmtstring, does not need to imply the same qualifiers as the interval variable. When the implied formatting-mask qualifier is different from the interval qualifier, intofmtasc() converts the result to appropriate units, as necessary (as if it called the invextend() function). However, both qualifiers must belong to the same class: either the year to month class or the day to fraction class.

If fmtstring is an empty string, the intofmtasc() function sets outbuf to an empty string.

The formatting directives %B, %b, and %p, which the DBTIME environment variable accepts, are not applicable in fmtstring because month name and A.M./P.M. information is not relevant for intervals of time. Use the %Y directive if the interval is more than 99 years (%y can handle only two digits). For hours, use %H (not %I, because%I can represent only 12 hours). If fmtstring is an empty string, the function returns an error.

If the character string and the formatting mask are acceptable, the incvfmtasc() function sets the interval value in invvalue and returns zero. Otherwise, the function returns an error code and the interval variable contains an unpredictable value.

Return Codes

0

The conversion was successful.

<0

The conversion failed.

Example

The demo directory contains this sample program in the file intofmtasc.ec.

Example Output

invdivdbl()

The invdivdbl() function divides an interval value by a numeric value.

Syntax

Usage

The input and output qualifiers must both belong to the same interval class: either the year to month class or the day to fraction(5) class. If the qualifier for ov is different from the qualifier for iv (within the same class), the invdivdbl() function extends the result (as the invextend() function defines).

The invdivdbl() function divides the interval value in iv by num and stores the result in ov.

The value in num can be either a positive or a negative value.

Return Codes

0

The division was successful.

<0

The division failed.

-1200

A numeric value is too large (in magnitude).

-1201

A numeric value is too small (in magnitude).

-1202

The num parameter is zero (0).

-1265

Overflow occurred on an interval operation.

-1266

An interval value is incompatible with the operation.

-1268

A parameter contains an invalid interval qualifier.

Example

The demo directory contains this sample program in the file invdivdbl.ec.

Example Output

invdivinv()

The invdivinv() function divides an interval value by another interval value.

Syntax

Usage

The invdivinv() function divides the interval value in i1 by i2, and stores the result in num. The result can be either positive or negative.

Both the input and output qualifiers must belong to the same interval class: either the year to month class or the day to fraction(5) class. If necessary, the invdivinv() function extends the interval value in i2 to match the qualifier for i1 before the division.

Return Codes

0

The division was successful.

<0

The division failed.

-1200

A numeric value is too large (in magnitude).

-1201

A numeric value is too small (in magnitude).

-1266

An interval value is incompatible with the operation.

-1268

A parameter contains an invalid interval qualifier.

Example

The demo directory contains this sample program in the file invdivinv.ec.

Example Output

invextend()

The invextend() function copies an interval value under a different qualifier. Extending is the operation of adding or dropping fields of an INTERVAL value to make it match a given qualifier. For INTERVAL values, both qualifiers must belong to the same interval class: either the year to month class or the day to fraction(5) class.

Syntax

Usage

The invextend() function copies the qualifier-field digits of in_inv interval variable to the out_inv interval variable. The qualifier of the out_inv variable controls the copy.

The function discards any fields in in_inv that are to the right of the least-significant field in out_inv. The function fills in any fields in out_inv that are not present in in_inv as follows:

Return Codes

0

The conversion was successful.

<0

The conversion failed.

-1266

An interval value is incompatible with the operation.

-1268

A parameter contains an invalid interval qualifier.

Example

The demo directory contains this sample program in the file invextend.ec. The example illustrates interval extension. In the second result, the output contains zeros in the seconds field, and the days field has been set to 3.

Example Output

invmuldbl()

The invmuldbl() function multiplies an interval value by a numeric value.

Syntax

Usage

The invmuldbl() function multiplies the interval value in iv by num and stores the result in ov. The value in num can be either positive or negative.

Both the input and output qualifiers must belong to the same interval class: either the year to month class or the day to fraction(5) class. If the qualifier for ov is different from the qualifier for iv (but of the same class), the invmuldbl() function extends the result (as the invextend() function defines).

Return Codes

0

The multiplication was successful.

<0

The multiplication failed.

-1200

A numeric value is too large (in magnitude).

-1201

A numeric value is too small (in magnitude).

-1266

An interval value is incompatible with the operation.

-1268

A parameter contains an invalid interval qualifier.

Example

The demo directory contains this sample program in the file invmuldbl.ec. The example illustrates how to multiply an interval value by a numeric value. The second multiplication illustrates the result of interval multiplication when the input and output qualifiers are different.

Example Output




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.