Informix的时间与数值秒转换

sysmaster库中的sysptnhdr表中created字段是表的建立时间,其值是自1970-01-01 00:00:00 开始的秒数。
若我们需要获取表建立的时间,则需要进行转换,比如 to_date('1970-01-01 00:00:00','%Y-%m-%d %H:%M:%S') + created units second,但尝试执行这个时,会返回错误。

 1263: A field in a datetime or interval value is incorrect or an illegal operation
 specified on datetime field.
Error in line 1
Near character position 113

其中的返回错误表明,interval 的值存在问题,查出created的值为:1,340,483,148,这个原因明了:interval的值不能超过9位,而created的值现在为10位,超出了!!

那我们该将created的秒数转换成时间呢?需要使interval的值小于10位即可。

首先,获取到2000-01-01 00:00:00的秒数

select substr((to_date('2000-01-01 00:00:00','%Y-%m-%d %H:%M:%S') - to_date('1970-01-01 00:00:00','%Y-%m-%d %H:%M:%S')) * 24 * 3600,1,10) 
from sysmaster:sysdual;
 946684800

然后,使用 to_date('2000-01-01 00:00:00','%Y-%m-%d %H:%M:%S') + (created - 946684800) units second 即为created的时间了。

我们可以用个存储过程来实现这个功能(注:10亿秒约31.7年)。

CREATE FUNCTION intsec_time(intsec int)
RETURNING datetime year to second;
    
    RETURN to_date('2000-01-01 00:00:00','%Y-%m-%d %H:%M:%S') + (intsec - 946684800) units second;

END FUNCTION;

然后再使用查询时,使用intsec_time可以容易的进行时间转换。

select first 1 created, intsec_time(created) from sysmaster:sysptnhdr;

    created (expression)        

 1340483148 2012-06-23 20:25:48

1 row(s) retrieved.

评论:
informixer 2014-11-27 11:17

dbinfo('UTC_TO_DATETIME',ti.ti_created)

标签: none

添加新评论

Free Web Hosting