Updated on 2024-05-07 GMT+08:00

Date Type

Table 1 lists the common date type APIs provided by ecpg.

Table 1 Common date type APIs

API

Description

Remarks

date* PGTYPESdate_new(void)

Returns a pointer to an allocated date variable.

This function creates a date variable on the heap. The return value is of the date* type.

void PGTYPESdate_free(date*)

Releases the memory that has been allocated to a date variable.

Frees a date* variable created using the PGTYPESdate_free function.

date PGTYPESdate_from_asc(char* str, char** endptr)

Parses a date from its textual representation.

This function accepts a C string str and a pointer to a C string endptr. ecpg converts the date expressed in text into a character string. Currently, ECPG does not support storing the address of the first invalid character in *endptr. However, endptr can be set to null.

Note that the function always assumes MDY-formatted dates and there is currently no variable to change that within ecpg.

char* PGTYPESdate_to_asc(date dDate)

Returns the textual representation of a date variable.

This function accepts the date dDate as its unique parameter. It will output the date in the YYYY-MM-DD format.

date PGTYPESdate_from_timestamp(timestamp dt)

Extracts the date part from a timestamp.

This function accepts a timestamp as its unique parameter and returns the extracted date part from the timestamp.

void PGTYPESdate_julmdy(date jd, int* mdy)

Extracts the values of day, month, and year from a variable of the date type.

This function accepts the date jd and a pointer to an array of three integer values mdy. The variable name indicates the sequence. mdy[0] indicates the month, mdy[1] indicates the day, and mdy[2] indicates the year.

void PGTYPESdate_mdyjul(int* mdy, date* jdate)

Creates a date using the specified integer value.

This function accepts an array consisting of three integers (mdy) as its first parameter. The three integers are used to indicate the day, month, and year, respectively. The second parameter is a pointer to a variable of the date type, which is used to store the result of the operation.

int PGTYPESdate_dayofweek(date d)

Returns a number indicating the day of a week for a date value.

This function accepts the date variable d as its unique parameter and returns an integer indicating the day of the week.

void PGTYPESdate_today(date* d)

Returns the current date.

This function accepts a pointer to a date variable d and sets the parameter to the current date.
  • 0: Sunday
  • 1: Monday
  • 2: Tuesday
  • 3: Wednesday
  • 4: Thursday
  • 5: Friday
  • 6: Saturday

int PGTYPESdate_defmt_asc(date* d, const char* fmt, char* str)

Converts a C char* string to a value of the date type using a format mask.

This function accepts a pointer d pointing to a date value for storing the operation result, a format mask fmt for parsing the date, and a C char* string str containing the textual representation of the date. The textual representation is expected to match the format mask. However, you do not need to have a 1:1 mapping of the string to the format mask. The function only analyzes the sequential order and looks for the literals "yy" or "yyyy" that indicate the position of the year, "mm" indicating the position of the month and "dd" indicating the position of the day.

Example of valid input (fmt, str):

yy/mm/dd 1994, February 3rd

int PGTYPESdate_fmt_asc(dat dDate, const char* fmtstring, char* outbuf)

Converts a variable of the date type into its textual representation using a format mask.

This function accepts the date dDate to be converted, the format mask fmtstring, and the string outbuf of the textual representation of the date to be saved.

The function returns 0 on success and a negative value in case of error.

Example of valid input:
112359            //mmddyy
59/11/23          //yy/mm/dd
Nov.23,1959       //mmm.dd,yyyy
Mon,Nov.23,1959   //ddd,mmm.dd,yyyy
Format:
  • dd: indicates the day of a month.
  • mm: indicates the month of a year.
  • yy: indicates a two-digit year.
  • yyyy: indicates a four-digit year.
  • ddd: indicates the day of a week.
  • mmm: indicates the month.

Examples

For details, see Examples.