Oninit® SQLMorph — C to UDR: a real date library
mydates is an existing C library that converts between Informix DATE and DATETIME, UNIX
time_t, and three formats of its own (a YYYYMMDD number, a month/day/year structure, and a
date-with-microseconds structure). Its header declares 27 functions; c2udr converts all 27 with one map file.
c2udr is a work in progress. These pages show real output of the current version, run against an Informix 15.0 test engine; they are examples, not a released product.
Teaching c2udr the library's types
A header that uses Idate_t or My_Datetime_t tells c2udr nothing about what those are in SQL. A map file does. The first part describes the date types; the second part, after the types behind the seven functions, describes the structs that first stopped seven functions from converting:
# Informix DATE (days since 1899-12-31) is a 4-byte value; the library's Idate_t is a long.
[type Idate_t]
sql = DATE
wtype = mi_date
in = {l} = (Idate_t){a};
out = {r} = (mi_date){l};
# YYYYMMDD in a long: an INTEGER.
[type Mydate4_t]
sql = INTEGER
wtype = mi_integer
in = {l} = (Mydate4_t){a};
out = {r} = (mi_integer){l};
# month/day/year shorts: a DATE through the library's own converters.
[type Mydate6_t]
sql = DATE
wtype = mi_date
in = idatetoMydate6(&{l}, (Idate_t){a});
out = {r} = (mi_date)Mydate6toidate({l});
# Library date-time (date, seconds, microseconds): DATETIME YEAR TO FRACTION(5)
# (Informix keeps at most 5 fraction digits; the 6th microsecond digit is dropped).
[type My_Datetime_t]
sql = DATETIME YEAR TO FRACTION(5)
wtype = mi_datetime *
byref = yes
in = idttomydt(*{a}, &{l});
# mydttoidt sets dec_ndgts to 8, enough for YEAR TO FRACTION(2): FRACTION(5) needs 10 digits
out = mydttoidt({l}, {r}, TU_DTENCODE(TU_YEAR, TU_F5)); {r}->dt_dec.dec_ndgts = 10;
# the Informix DATE argument of idatetostr is declared long in the header
[func idatetostr]
arg.idate = Idate_t
# ---- the types behind the seven functions that first did not convert -------------------
[prelude]
code = /* DATETIME <-> gdt_t, through the library's own converters */
static void c2u_dt_to_gdt(const mi_datetime *dt, gdt_t *g)
{
My_Datetime_t m;
struct tm t;
idttomydt(*dt, &m);
memset(g, 0, sizeof *g);
memset(&t, 0, sizeof t);
t.tm_year = (int)(m.MY_Date / 10000) - 1900;
t.tm_mon = (int)((m.MY_Date % 10000) / 100) - 1;
t.tm_mday = (int)(m.MY_Date % 100);
t.tm_hour = (int)(m.MY_Secs / 3600);
t.tm_min = (int)((m.MY_Secs % 3600) / 60);
t.tm_sec = (int)(m.MY_Secs % 60);
{ struct tm w = t; timegm(&w); t.tm_wday = w.tm_wday; t.tm_yday = w.tm_yday; }
g->tm = t;
g->nsecs = m.MY_USec * 1000;
}
static void c2u_gdt_to_dt(const gdt_t *g, mi_datetime *dt)
{
My_Datetime_t m;
m.MY_Date = (Mydate4_t)((g->tm.tm_year + 1900) * 10000 + (g->tm.tm_mon + 1) * 100 + g->tm.tm_mday);
m.MY_Secs = g->tm.tm_hour * 3600 + g->tm.tm_min * 60 + g->tm.tm_sec;
m.MY_USec = g->nsecs / 1000;
mydttoidt(m, dt, TU_DTENCODE(TU_YEAR, TU_F5));
dt->dt_dec.dec_ndgts = 10;
}
/* ivltomydt reads the interval's decimal digits by fixed position and ignores dec_exp,
while the engine drops leading and trailing zero digits (digit i has the base-100 exponent
dec_exp - i). Put every digit where it expects it: the leading field first, T more after it. */
static void c2u_ivl_norm(struct intrvl *iv)
{
int s = TU_START(iv->in_qual), e = TU_END(iv->in_qual);
int t = ((e - s) + (e % 2)) >> 1, i, j;
int f = (e > TU_SECOND) ? ((e - TU_SECOND) + 1) / 2 : 0; /* base-100 fraction digits */
int top = t + 1 - f; /* exponent of the first digit */
char n[DECSIZE];
memset(n, 0, sizeof n);
for (i = 0; i < iv->in_dec.dec_ndgts; i++) {
j = top - iv->in_dec.dec_exp + i;
if (j >= 0 && j <= t)
n[j] = iv->in_dec.dec_dgts[i];
}
memcpy(iv->in_dec.dec_dgts, n, sizeof n);
iv->in_dec.dec_exp = top;
iv->in_dec.dec_ndgts = t + 1;
}
# struct timeval: in as two arguments (seconds, microseconds); out as one BIGINT of
# microseconds since the epoch (a ROW of two values is T008).
[struct timeval]
fields = tv_sec:BIGINT, tv_usec:INTEGER
result.sql = BIGINT
result.wtype = mi_bigint *
result.byref = yes
result.out = *{r} = (mi_bigint){l}.tv_sec * 1000000 + (mi_bigint){l}.tv_usec;
# Informix INTERVAL: the engine hands the library its own struct intrvl. ivltomydt reads the
# digits assuming the default leading precision (DAY(2)); a wider DAY(9) crashes it.
[struct intrvl]
sql = INTERVAL DAY TO FRACTION(5)
wtype = mi_interval *
byref = yes
local = struct intrvl
in = {l} = *{a}; c2u_ivl_norm(&{l});
# gdt_t and struct tm are a DATETIME in the SQL world. The time zone is not part of it:
# a DATETIME in, tz = 0 (UTC); a gdt_t out, the date and time as written, tz dropped.
[type gdt_t]
sql = DATETIME YEAR TO FRACTION(5)
wtype = mi_datetime *
byref = yes
in = c2u_dt_to_gdt({a}, &{l});
out = c2u_gdt_to_dt(&{l}, {r});
[type struct tm]
sql = DATETIME YEAR TO FRACTION(5)
wtype = mi_datetime *
byref = yes
in = { gdt_t g_; c2u_dt_to_gdt({a}, &g_); {l} = g_.tm; }
# gdttomydt copies nsecs (nanoseconds) into MY_USec (microseconds). A gdt_t that holds
# microseconds in nsecs makes it right; a wrong value there crashes mydttoidt.
[type gdt_us_t]
sql = DATETIME YEAR TO FRACTION(5)
wtype = mi_datetime *
byref = yes
local = gdt_t
in = c2u_dt_to_gdt({a}, &{l}); {l}.nsecs /= 1000;
# The My_Datetime_t that ivltomydt fills is an interval (year 0, month 0, the day count in
# MY_Date), not a date: as a DATETIME it would be invalid. SQL gets total microseconds.
[type My_Interval_t]
sql = BIGINT
wtype = mi_bigint *
byref = yes
local = My_Datetime_t
# the library drops the sign of the interval; l0 is the INTERVAL argument of ivltomydt
out = *{r} = ((((mi_bigint){l}.MY_Date * 86400) + {l}.MY_Secs) * 1000000 + {l}.MY_USec) * (l0.in_dec.dec_pos == DECPOSNEG ? -1 : 1);
[func ivltomydt]
arg.mydt = My_Interval_t *
[func gdttomydt]
role.date = in
arg.date = gdt_us_t *
[func gdt_Validate_Tm]
role.tm = in
# returns result: the library function writes into a caller buffer of unstated size
[func gdt_format]
ret = void
buf.result = 128
# the parse status is negative when the text is not a date
[func get_date_and_time]
null_if = {rv} < 0
Each type states its SQL type, the C type the wrapper receives, and the C statements that convert between that and the
library's own type — here the library's own converters, idttomydt and mydttoidt. Informix keeps at
most five fraction digits in a DATETIME, so the library's microsecond digit six is dropped.
The command
c2udr.py -m mydates.map -s mydates.c -s getdateandtime.tab.c mydates.h getdateandtime.h make -f mydates.mk INFORMIXDIR=/opt/informix
Registered functions
| SQL signature | Returns | Code |
|---|---|---|
gdttomydt(DATETIME YEAR TO FRACTION(5)) | DATETIME YEAR TO FRACTION(5) | |
idatetostr(DATE) | LVARCHAR | |
strtodate6(LVARCHAR) | DATE | |
strtodate4(LVARCHAR) | INTEGER | |
Mydate4to6(INTEGER) | DATE | |
Mydate4to6x(INTEGER) | DATE | |
Mydate6to4(DATE) | INTEGER | |
Mydate6toidate(DATE) | DATE | |
Mydate4toidate(INTEGER) | DATE | |
idatetoMydate6(DATE) | DATE | |
idatetoMydate4(DATE) | INTEGER | |
strtoidate(LVARCHAR) | DATE | |
idatetotime(DATE) | BIGINT | |
timetoidate(BIGINT) | DATE | |
idttomydt(DATETIME YEAR TO FRACTION(5)) | DATETIME YEAR TO FRACTION(5) | |
ivltomydt(INTERVAL DAY TO FRACTION(5)) | BIGINT | |
mydttoidt(DATETIME YEAR TO FRACTION(5), INTEGER) | DATETIME YEAR TO FRACTION(5) | |
timetomydt(BIGINT) | DATETIME YEAR TO FRACTION(5) | |
timevaltomydt(BIGINT, INTEGER) | DATETIME YEAR TO FRACTION(5) | |
mydttotime(DATETIME YEAR TO FRACTION(5)) | BIGINT | |
mydttotimeval(DATETIME YEAR TO FRACTION(5)) | BIGINT | |
strtomydt(LVARCHAR) | DATETIME YEAR TO FRACTION(5) | |
mydttostr(DATETIME YEAR TO FRACTION(5)) | LVARCHAR | |
mydttostrlcl(DATETIME YEAR TO FRACTION(5), BIGINT) | LVARCHAR | |
get_date_and_time(LVARCHAR) | DATETIME YEAR TO FRACTION(5) | |
gdt_format(DATETIME YEAR TO FRACTION(5), INTEGER) | LVARCHAR | |
gdt_Validate_Tm(DATETIME YEAR TO FRACTION(5)) | BIGINT |
The show code links open the generated C wrapper and the registration SQL for that function.
C wrapper
/* gdttomydt: Convert gdt_t to MyDatetime_t */
mi_datetime *udr_gdttomydt(mi_datetime * a0, MI_FPARAM *fp)
{
gdt_t l0;
My_Datetime_t l1;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
c2u_dt_to_gdt(a0, &l0); l0.nsecs /= 1000;
memset(&l1, 0, sizeof l1);
gdttomydt(&l0, &l1);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
mydttoidt(l1, r, TU_DTENCODE(TU_YEAR, TU_F5)); r->dt_dec.dec_ndgts = 10;
return r;
}Registration SQL
-- gdttomydt: Convert gdt_t to MyDatetime_t
CREATE FUNCTION gdttomydt(DATETIME YEAR TO FRACTION(5))
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_gdttomydt)'
LANGUAGE C;
C wrapper
/* idatetostr: Convert an Informix DATE Type to an ANSI format date string - YYYY-MM-DD */
mi_lvarchar *udr_idatetostr(mi_date a0, MI_FPARAM *fp)
{
Idate_t l0;
char l1[512];
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = (Idate_t)a0;
l1[0] = '\0';
idatetostr(l0, l1, (size_t)512);
r = mi_string_to_lvarchar(l1);
return r;
}Registration SQL
-- idatetostr: Convert an Informix DATE Type to an ANSI format date string - YYYY-MM-DD
CREATE FUNCTION idatetostr(DATE)
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_idatetostr)'
LANGUAGE C;
C wrapper
/* strtodate6: Convert character string to a Mydate6_t date. */
mi_date udr_strtodate6(mi_lvarchar *a0, MI_FPARAM *fp)
{
Mydate6_t l0;
char *l1;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l1 = mi_lvarchar_to_string(a0);
memset(&l0, 0, sizeof l0);
strtodate6(&l0, l1);
r = (mi_date)Mydate6toidate(l0);
return r;
}Registration SQL
-- strtodate6: Convert character string to a Mydate6_t date.
CREATE FUNCTION strtodate6(LVARCHAR)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_strtodate6)'
LANGUAGE C;
C wrapper
/* strtodate4: Convert character string to a Mydate4_t date. */
mi_integer udr_strtodate4(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
Mydate4_t rv;
mi_integer r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l0 = mi_lvarchar_to_string(a0);
rv = strtodate4(l0);
r = (mi_integer)rv;
return r;
}Registration SQL
-- strtodate4: Convert character string to a Mydate4_t date.
CREATE FUNCTION strtodate4(LVARCHAR)
RETURNING INTEGER
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_strtodate4)'
LANGUAGE C;
C wrapper
/* Mydate4to6: Convert Mydate4_t date to Mydate6_t date. */
mi_date udr_Mydate4to6(mi_integer a0, MI_FPARAM *fp)
{
Mydate6_t l0;
Mydate4_t l1;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l1 = (Mydate4_t)a0;
memset(&l0, 0, sizeof l0);
Mydate4to6(&l0, l1);
r = (mi_date)Mydate6toidate(l0);
return r;
}Registration SQL
-- Mydate4to6: Convert Mydate4_t date to Mydate6_t date.
CREATE FUNCTION Mydate4to6(INTEGER)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_Mydate4to6)'
LANGUAGE C;
C wrapper
/* Mydate4to6x: Convert Mydate4_t date to Mydate6_t date dropping century if present. */
mi_date udr_Mydate4to6x(mi_integer a0, MI_FPARAM *fp)
{
Mydate6_t l0;
Mydate4_t l1;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l1 = (Mydate4_t)a0;
memset(&l0, 0, sizeof l0);
Mydate4to6x(&l0, l1);
r = (mi_date)Mydate6toidate(l0);
return r;
}Registration SQL
-- Mydate4to6x: Convert Mydate4_t date to Mydate6_t date dropping century if present.
CREATE FUNCTION Mydate4to6x(INTEGER)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_Mydate4to6x)'
LANGUAGE C;
C wrapper
/* Mydate6to4: Convert Mydate6_t date to Mydate4_t date with century */
mi_integer udr_Mydate6to4(mi_date a0, MI_FPARAM *fp)
{
Mydate6_t l0;
Mydate4_t rv;
mi_integer r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
idatetoMydate6(&l0, (Idate_t)a0);
rv = Mydate6to4(l0);
r = (mi_integer)rv;
return r;
}Registration SQL
-- Mydate6to4: Convert Mydate6_t date to Mydate4_t date with century
CREATE FUNCTION Mydate6to4(DATE)
RETURNING INTEGER
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_Mydate6to4)'
LANGUAGE C;
C wrapper
/* Mydate6toidate: Convert Mydate6_t date to Informix Date type */
mi_date udr_Mydate6toidate(mi_date a0, MI_FPARAM *fp)
{
Mydate6_t l0;
Idate_t rv;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
idatetoMydate6(&l0, (Idate_t)a0);
rv = Mydate6toidate(l0);
r = (mi_date)rv;
return r;
}Registration SQL
-- Mydate6toidate: Convert Mydate6_t date to Informix Date type
CREATE FUNCTION Mydate6toidate(DATE)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_Mydate6toidate)'
LANGUAGE C;
C wrapper
/* Mydate4toidate: Convert Mydate4_t date to Informix Date type */
mi_date udr_Mydate4toidate(mi_integer a0, MI_FPARAM *fp)
{
Mydate4_t l0;
Idate_t rv;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l0 = (Mydate4_t)a0;
rv = Mydate4toidate(l0);
r = (mi_date)rv;
return r;
}Registration SQL
-- Mydate4toidate: Convert Mydate4_t date to Informix Date type
CREATE FUNCTION Mydate4toidate(INTEGER)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_Mydate4toidate)'
LANGUAGE C;
C wrapper
/* idatetoMydate6: Convert Informix Date type to Mydate6_t date */
mi_date udr_idatetoMydate6(mi_date a0, MI_FPARAM *fp)
{
Mydate6_t l0;
Idate_t l1;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l1 = (Idate_t)a0;
memset(&l0, 0, sizeof l0);
idatetoMydate6(&l0, l1);
r = (mi_date)Mydate6toidate(l0);
return r;
}Registration SQL
-- idatetoMydate6: Convert Informix Date type to Mydate6_t date
CREATE FUNCTION idatetoMydate6(DATE)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_idatetoMydate6)'
LANGUAGE C;
C wrapper
/* idatetoMydate4: Convert Informix Date type to Mydate4_t date */
mi_integer udr_idatetoMydate4(mi_date a0, MI_FPARAM *fp)
{
Idate_t l0;
Mydate4_t rv;
mi_integer r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l0 = (Idate_t)a0;
rv = idatetoMydate4(l0);
r = (mi_integer)rv;
return r;
}Registration SQL
-- idatetoMydate4: Convert Informix Date type to Mydate4_t date
CREATE FUNCTION idatetoMydate4(DATE)
RETURNING INTEGER
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_idatetoMydate4)'
LANGUAGE C;
C wrapper
/* strtoidate: Convert string in format: to Informix Date type */
mi_date udr_strtoidate(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
Idate_t rv;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l0 = mi_lvarchar_to_string(a0);
rv = strtoidate(l0);
r = (mi_date)rv;
return r;
}Registration SQL
-- strtoidate: Convert string in format: to Informix Date type
CREATE FUNCTION strtoidate(LVARCHAR)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_strtoidate)'
LANGUAGE C;
C wrapper
/* idatetotime: Convert Informix Date to UNIX time_t */
mi_bigint *udr_idatetotime(mi_date a0, MI_FPARAM *fp)
{
Idate_t l0;
time_t rv;
mi_bigint * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = (Idate_t)a0;
rv = idatetotime(l0);
r = (mi_bigint *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = (mi_bigint)rv;
return r;
}Registration SQL
-- idatetotime: Convert Informix Date to UNIX time_t
CREATE FUNCTION idatetotime(DATE)
RETURNING BIGINT
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_idatetotime)'
LANGUAGE C;
C wrapper
/* timetoidate: Convert Informix UNIX time_t to Date */
mi_date udr_timetoidate(mi_bigint * a0, MI_FPARAM *fp)
{
time_t l0;
Idate_t rv;
mi_date r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return 0;
}
l0 = (time_t)*a0;
rv = timetoidate(l0);
r = (mi_date)rv;
return r;
}Registration SQL
-- timetoidate: Convert Informix UNIX time_t to Date
CREATE FUNCTION timetoidate(BIGINT)
RETURNING DATE
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_timetoidate)'
LANGUAGE C;
C wrapper
/* idttomydt: Convert Informix DateTime type to My_DateTime_t structure. */
mi_datetime *udr_idttomydt(mi_datetime * a0, MI_FPARAM *fp)
{
struct dtime l0;
My_Datetime_t l1;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = (struct dtime)*a0;
memset(&l1, 0, sizeof l1);
idttomydt(l0, &l1);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
mydttoidt(l1, r, TU_DTENCODE(TU_YEAR, TU_F5)); r->dt_dec.dec_ndgts = 10;
return r;
}Registration SQL
-- idttomydt: Convert Informix DateTime type to My_DateTime_t structure.
CREATE FUNCTION idttomydt(DATETIME YEAR TO FRACTION(5))
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_idttomydt)'
LANGUAGE C;
C wrapper
/* ivltomydt: Convert Informix Interval type to My_DateTime_t structure. */
mi_bigint *udr_ivltomydt(mi_interval * a0, MI_FPARAM *fp)
{
struct intrvl l0;
My_Datetime_t l1;
mi_bigint * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = *a0; c2u_ivl_norm(&l0);
memset(&l1, 0, sizeof l1);
ivltomydt(l0, &l1);
r = (mi_bigint *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = ((((mi_bigint)l1.MY_Date * 86400) + l1.MY_Secs) * 1000000 + l1.MY_USec) * (l0.in_dec.dec_pos == DECPOSNEG ? -1 : 1);
return r;
}Registration SQL
-- ivltomydt: Convert Informix Interval type to My_DateTime_t structure.
CREATE FUNCTION ivltomydt(INTERVAL DAY TO FRACTION(5))
RETURNING BIGINT
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_ivltomydt)'
LANGUAGE C;
C wrapper
/* mydttoidt: Convert My_DateTime_t structure to Informix DateTime type of precision prec. */
mi_datetime *udr_mydttoidt(mi_datetime * a0, mi_integer a1, MI_FPARAM *fp)
{
My_Datetime_t l0;
struct dtime l1;
int l2;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0) || mi_fp_argisnull(fp, 1)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
idttomydt(*a0, &l0);
l2 = (int)a1;
memset(&l1, 0, sizeof l1); l1.dt_qual = TU_DTENCODE(TU_YEAR, TU_F5);
mydttoidt(l0, &l1, l2);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = (mi_datetime)l1;
return r;
}Registration SQL
-- mydttoidt: Convert My_DateTime_t structure to Informix DateTime type of precision prec.
CREATE FUNCTION mydttoidt(DATETIME YEAR TO FRACTION(5), INTEGER)
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_mydttoidt)'
LANGUAGE C;
C wrapper
mi_datetime *udr_timetomydt(mi_bigint * a0, MI_FPARAM *fp)
{
My_Datetime_t l0;
time_t l1;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l1 = (time_t)*a0;
memset(&l0, 0, sizeof l0);
timetomydt(&l0, l1);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
mydttoidt(l0, r, TU_DTENCODE(TU_YEAR, TU_F5)); r->dt_dec.dec_ndgts = 10;
return r;
}Registration SQL
CREATE FUNCTION timetomydt(BIGINT)
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_timetomydt)'
LANGUAGE C;
C wrapper
/* timevaltomydt: Convert UNIX timeval structure to My_DateTime_t structure. */
mi_datetime *udr_timevaltomydt(mi_bigint * a0, mi_integer a1, MI_FPARAM *fp)
{
My_Datetime_t l0;
struct timeval l1;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0) || mi_fp_argisnull(fp, 1)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
memset(&l1, 0, sizeof l1);
l1.tv_sec = (__typeof__(l1.tv_sec))*a0;
l1.tv_usec = (__typeof__(l1.tv_usec))a1;
memset(&l0, 0, sizeof l0);
timevaltomydt(&l0, l1);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
mydttoidt(l0, r, TU_DTENCODE(TU_YEAR, TU_F5)); r->dt_dec.dec_ndgts = 10;
return r;
}Registration SQL
-- timevaltomydt: Convert UNIX timeval structure to My_DateTime_t structure.
CREATE FUNCTION timevaltomydt(BIGINT, INTEGER)
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_timevaltomydt)'
LANGUAGE C;
C wrapper
/* mydttotime: Convert My_DateTime_t structure to UNIX time_t. */
mi_bigint *udr_mydttotime(mi_datetime * a0, MI_FPARAM *fp)
{
time_t l0;
My_Datetime_t l1;
mi_bigint * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
idttomydt(*a0, &l1);
memset(&l0, 0, sizeof l0);
mydttotime(&l0, l1);
r = (mi_bigint *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = (mi_bigint)l0;
return r;
}Registration SQL
-- mydttotime: Convert My_DateTime_t structure to UNIX time_t.
CREATE FUNCTION mydttotime(DATETIME YEAR TO FRACTION(5))
RETURNING BIGINT
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_mydttotime)'
LANGUAGE C;
C wrapper
/* mydttotimeval: Convert My_DateTime_t structure to UNIX timeval structure. */
mi_bigint *udr_mydttotimeval(mi_datetime * a0, MI_FPARAM *fp)
{
struct timeval l0;
My_Datetime_t l1;
mi_bigint * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
idttomydt(*a0, &l1);
memset(&l0, 0, sizeof l0);
mydttotimeval(&l0, l1);
r = (mi_bigint *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = (mi_bigint)l0.tv_sec * 1000000 + (mi_bigint)l0.tv_usec;
return r;
}Registration SQL
-- mydttotimeval: Convert My_DateTime_t structure to UNIX timeval structure.
CREATE FUNCTION mydttotimeval(DATETIME YEAR TO FRACTION(5))
RETURNING BIGINT
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_mydttotimeval)'
LANGUAGE C;
C wrapper
/* strtomydt: Convert string to My_DateTime_t structure. */
mi_datetime *udr_strtomydt(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
My_Datetime_t l1;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = mi_lvarchar_to_string(a0);
memset(&l1, 0, sizeof l1);
strtomydt(l0, &l1);
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
mydttoidt(l1, r, TU_DTENCODE(TU_YEAR, TU_F5)); r->dt_dec.dec_ndgts = 10;
return r;
}Registration SQL
-- strtomydt: Convert string to My_DateTime_t structure.
CREATE FUNCTION strtomydt(LVARCHAR)
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_strtomydt)'
LANGUAGE C;
C wrapper
/* mydttostr: Convert My_DateTime_t structure to a String assuming local time. */
mi_lvarchar *udr_mydttostr(mi_datetime * a0, MI_FPARAM *fp)
{
My_Datetime_t l0;
char l1[512];
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
idttomydt(*a0, &l0);
l1[0] = '\0';
mydttostr(l0, l1, (size_t)512);
r = mi_string_to_lvarchar(l1);
return r;
}Registration SQL
-- mydttostr: Convert My_DateTime_t structure to a String assuming local time.
CREATE FUNCTION mydttostr(DATETIME YEAR TO FRACTION(5))
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_mydttostr)'
LANGUAGE C;
C wrapper
/* mydttostrlcl: Convert My_DateTime_t structure to local time String assuming My_Datetime contains UCT. */
mi_lvarchar *udr_mydttostrlcl(mi_datetime * a0, mi_bigint * a1, MI_FPARAM *fp)
{
My_Datetime_t l0;
char l1[512];
long l2;
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0) || mi_fp_argisnull(fp, 1)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
idttomydt(*a0, &l0);
l2 = (long)*a1;
l1[0] = '\0';
mydttostrlcl(l0, l1, (size_t)512, l2);
r = mi_string_to_lvarchar(l1);
return r;
}Registration SQL
-- mydttostrlcl: Convert My_DateTime_t structure to local time String assuming My_Datetime contains UCT.
CREATE FUNCTION mydttostrlcl(DATETIME YEAR TO FRACTION(5), BIGINT)
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_mydttostrlcl)'
LANGUAGE C;
C wrapper
mi_datetime *udr_get_date_and_time(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
gdt_t *l1;
long st;
mi_datetime * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = mi_lvarchar_to_string(a0);
l1 = NULL;
st = get_date_and_time(l0, &l1);
if (st < 0) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
if (!l1) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
r = (mi_datetime *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
c2u_gdt_to_dt(&(*l1), r);
return r;
}Registration SQL
CREATE FUNCTION get_date_and_time(LVARCHAR)
RETURNING DATETIME YEAR TO FRACTION(5)
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_get_date_and_time)'
LANGUAGE C;
C wrapper
mi_lvarchar *udr_gdt_format(mi_datetime * a0, mi_integer a1, MI_FPARAM *fp)
{
gdt_t l0;
char l1[128];
int l2;
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0) || mi_fp_argisnull(fp, 1)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
c2u_dt_to_gdt(a0, &l0);
l2 = (int)a1;
l1[0] = '\0';
gdt_format(l0, l1, l2);
r = mi_string_to_lvarchar(l1);
return r;
}Registration SQL
CREATE FUNCTION gdt_format(DATETIME YEAR TO FRACTION(5), INTEGER)
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_gdt_format)'
LANGUAGE C;
C wrapper
mi_bigint *udr_gdt_Validate_Tm(mi_datetime * a0, MI_FPARAM *fp)
{
struct tm l0;
long rv;
mi_bigint * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
{ gdt_t g_; c2u_dt_to_gdt(a0, &g_); l0 = g_.tm; }
rv = gdt_Validate_Tm(&l0);
r = (mi_bigint *)mi_alloc(sizeof *r);
if (!r) {
mi_db_error_raise(NULL, MI_EXCEPTION, "c2udr: out of memory");
return NULL;
}
*r = (mi_bigint)rv;
return r;
}Registration SQL
CREATE FUNCTION gdt_Validate_Tm(DATETIME YEAR TO FRACTION(5))
RETURNING BIGINT
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mydates/mydates.bld(udr_gdt_Validate_Tm)'
LANGUAGE C;
Calls run in the engine
| SQL | Result |
|---|---|
idatetostr(MDY(11, 19, 2026)) | 2026-11-19 |
strtoidate('2026-11-19') = MDY(11, 19, 2026) | t |
strtodate4('2026-11-19') | 20261119 |
Mydate6to4(MDY(11, 19, 2026)) | 20261119 |
idatetoMydate4(MDY(2, 29, 2000)) | 20000229 |
strtomydt('1998-05-19 3:15:52.582347') | 1998-05-19 03:15:52.58234 |
idttomydt('2026-11-19 10:20:30.12345'::DATETIME YEAR TO FRACTION(5)) | 2026-11-19 10:20:30.12345 |
mydttotime(strtomydt('2000-01-01 00:00:00')) | 946684800 |
mydttostr(strtomydt('2026-11-19 10:20:30')) | 2026-11-19 10:20:30.000000 |
idatetostr(NULL) IS NULL | t |
timevaltomydt(946684800::INT8, 123456) | 1999-12-31 00:00:00.12345 |
mydttotimeval(strtomydt('2000-01-01 00:00:00.5')) | 946684800500000 |
ivltomydt('3 04:05:06.12345'::INTERVAL DAY TO FRACTION(5)) | 273906123450 |
ivltomydt('-1 00:00:00'::INTERVAL DAY TO SECOND) | -86400000000 |
gdttomydt('2026-11-19 10:20:30.12345'::DATETIME YEAR TO FRACTION(5)) | 2026-11-19 10:20:30.12345 |
gdt_format('2026-11-19 10:20:30.12345'::DATETIME YEAR TO FRACTION(5), 0) | Thu Nov 19 10:20:30.123450000 UTC 2026 |
gdt_Validate_Tm('2026-11-19 10:20:30.12345'::DATETIME YEAR TO FRACTION(5)) | 0 |
get_date_and_time('2026-11-19 10:20:30') | 2026-11-19 10:20:30.00000 |
get_date_and_time('not a date at all') IS NULL | t |
What the conversion showed up in the library
Calling the functions from SQL exercised them more than the original tests had. Behaviours of the library that came out:
mydttoidtsets the number of stored digits to 8, which holds aDATETIMEonly up toFRACTION(2). The map above corrects the count forFRACTION(5); the library is unchanged.idatetotime,timetoidateandtimetomydtuse 25567 days for the 1970 epoch. 1970-01-01 is day 25568 as an InformixDATE, so the UNIX times are one day early;timevaltomydt(946684800, ...)gives 1999-12-31.gdttomydtcopiesgdt_t.nsecs(nanoseconds) into the microsecond field. Passed a real nanosecond count the result is 1000 times too large and crashesmydttoidtin the engine. The map hands it microseconds in that field, which makes the result right.ivltomydtreads an interval's digits by fixed position, ignores the digit exponent and drops the sign. The engine drops leading and trailing zero digits, so an interval such as0 00:00:01gives a wrong value or reads past the digits and crashes. A helper in the map puts every digit where the function expects it, and the sign is kept in the result.
See also a small library and the type mapping.