Sqlite: CURRENT_TIMESTAMP is in GMT, not the timezone of the machine(Sqlite:CURRENT_TIMESTAMP 是 GMT,不是机器的时区)
问题描述
我有一个带有此列定义的 sqlite (v3) 表:
I have a sqlite (v3) table with this column definition:
"timestamp" DATETIME DEFAULT CURRENT_TIMESTAMP
此数据库所在的服务器位于 CST 时区.当我在不包含时间戳列的情况下插入表时,sqlite 会自动使用 GMT 中的当前时间戳填充该字段,而不是 CST.
The server that this database lives on is in the CST time zone. When I insert into my table without including the timestamp column, sqlite automatically populates that field with the current timestamp in GMT, not CST.
有没有办法修改我的插入语句以强制存储的时间戳在 CST 中?另一方面,最好将它存储在 GMT 中(例如,以防数据库移动到不同的时区),所以有没有办法可以修改我的选择 SQL 以将存储的时间戳转换为 CST从表中提取它?
Is there a way to modify my insert statement to force the stored timestamp to be in CST? On the other hand, it is probably better to store it in GMT (in case the database gets moved to a different timezone, for example), so is there a way I can modify my select SQL to convert the stored timestamp to CST when I extract it from the table?
推荐答案
我在 sqlite 文档上找到了 (https://www.sqlite.org/lang_datefunc.html) 这段文字:
I found on the sqlite documentation (https://www.sqlite.org/lang_datefunc.html) this text:
计算给定 unix 的日期和时间时间戳1092941466,并进行补偿为您当地的时区.
Compute the date and time given a unix timestamp 1092941466, and compensate for your local timezone.
SELECT datetime(1092941466, 'unixepoch', 'localtime');
这看起来不符合我的需要,所以我尝试稍微更改日期时间"功能,结果如下:
That didn't look like it fit my needs, so I tried changing the "datetime" function around a bit, and wound up with this:
select datetime(timestamp, 'localtime')
这似乎可行 - 这是为您的时区转换的正确方法,还是有更好的方法来做到这一点?
That seems to work - is that the correct way to convert for your timezone, or is there a better way to do this?
这篇关于Sqlite:CURRENT_TIMESTAMP 是 GMT,不是机器的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sqlite:CURRENT_TIMESTAMP 是 GMT,不是机器的时区
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
