SQLITE:以毫秒获取当前时间并适合32位int

时间:2015-07-28 12:03:51

标签: sqlite

我需要32位时间标识符。目前,sqlite strftime('%s','now')正在触发器中用于设置32位时间标识符。原来我们的应用程序没有一秒的分辨率没有削减它,所以我想移动到毫秒并切断左侧的位以保持总共32位。我不需要能够将结果作为日期时间使用,因为它将被连接成标识符。字符串不需要是32位,但它代表的数字必须是。

2 个答案:

答案 0 :(得分:0)

With 32 bits, you get 232 = 4294967296 distinct values.

4294967296 milliseconds is just 49.7 days.

If your application blows up after 49 days, you're not alone, but in any case it's not a good idea.

Even using seconds for the time since 1970 is going to run out soon with 32 bits. You can use a different epoch (e.g., 2015-01-01, or even 2020-01-01 and start with negative numbers), but you need to reduce the precision to be able to get a usable range out of 32 bits.

答案 1 :(得分:0)

在64位系统上使用bitshift的解决方案:

以毫秒计算时间

cast(julianday("now")*100000000 as int)

然后将这个数字移位32次然后右移相同的次数。这会丢掉前导位。然后取绝对值。

abs((cast(julianday("now")*100000000 as int) << 32) >> 32)

瞧!