简体   繁体   中英

How to return firebird timestamp from udf written in C under Linux

Could someone please show me how to return a firebird TIMESTAMP type from a udf written in C?

More specifically, is there a corresponding C type which maps to the TIMESTAMP type in Firebird? If yes, which header includes its definition?Thanks!

You need to use ISC_TIMESTAMP which is defined in ibase.h . You should be able to use isc_encode_timestamp to convert from the tm of time.h , but as I don't program in C or C++ myself, I don't have more detailed information or an example ready.

Using Mark's advice, here is an implementation:

#include <time.h>
#include <ibase.h>
#include <ib_util.h>


typedef long long int SINT64;


ISC_TIME encode_time(int hours, int minutes, int seconds, int fractions)
{

    return ((hours * 60 + minutes) * 60 + seconds) * ISC_TIME_SECONDS_PRECISION + fractions;
}

ISC_DATE encodeDate(const struct tm* t){
    // Convert a calendar date to a numeric day
    // (the number of days since the base date)

    const int day = t->tm_mday;
    int month = t->tm_mon + 1;
    int year = t->tm_year + 1900;

    if (month > 2)
        month -= 3;
    else
    {
        month += 9;
        year -= 1;
    }

    const int c = year / 100;
    const int ya = year - 100 * c;


    return (ISC_DATE) (((SINT64) 146097 * c) / 4 +
                   (1461 * ya) / 4 +
                   (153 * month + 2) / 5 + day + 1721119 - 2400001);
}



ISC_TIMESTAMP* UTCServerTime(){
    time_t t = time(NULL);

    struct tm utc;
    gmtime_r(&t, &utc);

    ISC_TIMESTAMP* ib_utc = (ISC_TIMESTAMP*)
        ib_util_malloc(sizeof(ISC_TIMESTAMP));

    ib_utc->timestamp_date = encodeDate(&utc);
    ib_utc->timestamp_time = encode_time(utc.tm_hour,utc.tm_min,utc.tm_sec, 0);

    return ib_utc;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM