繁体   English   中英

如何在Linux下从C语言编写的udf返回firebird时间戳

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

有人可以告诉我如何从用C编写的udf返回firebird TIMESTAMP类型吗?

更具体地说,是否有对应的C类型映射到Firebird中的TIMESTAMP类型? 如果是,则哪个标头包含其定义?

您需要使用ISC_TIMESTAMP中定义的ibase.h 您应该可以使用isc_encode_timestamptime.htm进行转换,但是由于我自己没有使用C或C ++进行编程,因此我没有更详细的信息或示例。

根据马克的建议,这是一个实现:

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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