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,不是机器的时区
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
