在 Firebird 中创建一个整数时间戳

Create an integer timestamp in Firebird

我需要一个整数形式的时间戳(请不要问我为什么,我完全被这个困扰了)。定义是 01.01.1970 之后的秒数。

我的初始值是一个格式为

的字符串

YYYYMMDDHHMMSS(例如 20140108154821)

我已经能够建立一个正常的时间戳

CAST(
(SUBSTRING ( t.thetime FROM 5 FOR 2 )||'/'||SUBSTRING ( t.thetime FROM 7 FOR 2 )||'/'||SUBSTRING ( t.thetime FROM 1 FOR 4 )||' ' ||SUBSTRING ( t.thetime FROM 9 FOR 2 )||':'||SUBSTRING ( t.thetime FROM 11 FOR 2 )||':'||SUBSTRING ( t.thetime FROM 13 FOR 2 )||'.00'
) AS TIMESTAMP)

然后我尝试使用这种技术将其转换为 int

CAST(cast(
(SUBSTRING ( t.thetime FROM 5 FOR 2 )||'/'||SUBSTRING ( t.thetime FROM 7 FOR 2 )||'/'||SUBSTRING ( t.thetime FROM 1 FOR 4 )||' '|| SUBSTRING ( t.thetime FROM 9 FOR 2 )||':'||SUBSTRING ( t.thetime FROM 11 FOR 2 )||':'||SUBSTRING ( t.thetime FROM 13 FOR 2 )||'.00'
) AS TIMESTAMP)
-cast('01/01/1970 00:00:00' as timestamp) as BIGINT) as TIMESTAMP_INT

我确实得到了一个整数,但看起来返回值好像是天数之类的。

例如 08.01.2015,16:33:01.114 变为 16444

对于为什么会发生这种情况或者是否有其他方法可以提出任何建议?

我使用的是 Firebird 2.5 版

提前致谢

附言:

SELECT 
CAST(cast(
    (SUBSTRING ( '20140108154821' FROM 5 FOR 2 )||'/'||SUBSTRING ( '20140108154821' FROM 7 FOR 2 )||'/'||SUBSTRING ( '20140108154821' FROM 1 FOR 4 )||' '
    ||
    SUBSTRING ( '20140108154821' FROM 9 FOR 2 )||':'||SUBSTRING ( '20140108154821' FROM 11 FOR 2 )||':'||SUBSTRING ( '20140108154821' FROM 13 FOR 2 )||'.00'
    ) AS TIMESTAMP)
    -cast('01/01/1970 00:00:00' as timestamp) as BIGINT) 
    from sometable

;

是的,当您减去两个时间戳时,答案就是它们之间的天数。所以你要做的就是用你的时间戳减去"big bang"的时间戳,然后乘以一天的秒数,即

SELECT floor((yourTIMESTAMP - cast('1970-01-01' as TIMESTAMP)) * 86400)
FROM rdb$database

yourTIMESTAMP 替换为将字符串转换为时间戳的代码,常量 86400 是每天的秒数。

一种方法是使用 DATEDIFF:

SELECT DATEDIFF(second, TIMESTAMP'1970-01-01 00:00', CURRENT_TIMESTAMP) 
FROM RDB$DATABASE

这假设时间是 UTC,否则您可能需要通过时区偏移来补偿(例如,对于 CET (UTC+1),您将使用 TIMESTAMP'1970-01-01 01:00' 代替)。