Newsgroups: comp.databases.informix
From: shemminga@cix.compulink.co.uk (Stuart Hemming)
Subject: unix date -> DATETIME -- A sum
Reply-To: shemminga@cix.compulink.co.uk
Date: Mon, 28 Sep 1992 11:59:00 +0000


I recently asked for advice on how to convert unix 4-byte dates to
Informix DATETIME format. Here is a summary of the advice I received
and a sample of the program that I wrote.

The suggested answer was to call a user written function with the
unix date as a parameter with wout return a sequence of 6 int values
from the unix structure tm representing the year, month, day, hour,
minute and second of the passed unix date. EG.

        DEFINE yy INT,
                mm INT,
                dd INT,
                hh INT,
                min INT,
                sec INT,
                dummy CHAR(20),
                dtime DATETIME YEAR TO SECOND,
                unixdate INT

        LET unixdate = 717070899 -- For example
        CALL timeconv(unixdate) RETURNING yy, mm, dd, hh, min, sec
        LET yy = yy + 1900 -- 'cos unix returns no of year since 1900
        LET dummy = yy using "&&&&", "-",
                mm using "&&", "-",
                dd using "&&", " ",
                hh using "&&", ":",
                min using "&&", ":"
                sec using "&&"
        LET dtime = dummy
        ...

I took this one step futher and had to function return a string in
the correct format. The following are samples/examples of the
fgiusr.c, usr_func.c, makefile and *4gl files I subsequently created:

----------SAMPLE FGIUSR.C ----------------------------
#include "fgicfunc.h"

int timeconv();

cfunc_t usrcfuncs[] = 
    {
    "timeconv", timeconv, -1,
    0, 0, 0
    };

----------SAMPLE USR_FUNCS.C -------------------------
#include <sys/types.h>
#include <time.h>

int timeconv()
{
time_t *clock;
char *buf[20];

        poplong(clock);
        (void) cftime(buf, "%Y-%m-%d %T", clock);
        retquote(buf);
        return(1);
}

----------SAMPLE MAKEFILE ----------------------------
CFLAGS=-O -I${INFORMIXDIR}/incl

all: ufglgo1 ufgldb1

ufglgo1: fgiusr.o usr_funcs.o
        cfglgo fgiusr.o usr_funcs.o -o ufglgo1 -s

ufgldb1: fgiusr.o usr_funcs.o
        cfgldb fgiusr.o usr_funcs.o -o ufgldb1 -s

----------SAMPLE .4GL --------------------------------
database tudor

main
        define dummy char(20),
                rightnow datetime year to second,

        call timeconv(717070899) returning dummy
        let rightnow = dummy
        let to_day = extend(rightnow, year to day)
        display rightnow
end main

Thanks to all who replied especially Dvae Snyder for the help and the
examples in db4glgen in which I found lots of examples on user
written C functions.

--------------------------------------------------------------
| Who cares whos opinions these are?!                        |
| Send me mail and I'll love you forever (Maybe)             |
|------------------------------------------------------------|     
| Stuart Hemming       | shemminga@cix.compulink.co.uk       |
| Tudor Labels Ltd     | uunet!cix.compulink.co.uk!shemminga |
| Roman Bank Bourne    | Tel :  (+44) 778 426444             |
| Lincs PE10 9LQ. UK.  | Fax :  (+44) 778 421862             |
--------------------------------------------------------------


Stuart
|8-)