SQL 如何在 SQLite 中转换 Unix 纪元时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14629347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Unix epoch time in SQLite
提问by Phong Tran
Could you help me convert UNIX epoch time into format yyyy-mm-dd hh:mm:ss(24h) in SQLite? (GMT+7 would be appreciated).
你能帮我yyyy-mm-dd hh:mm:ss在 SQLite 中将UNIX 纪元时间转换为格式(24 小时)吗?(GMT+7 将不胜感激)。
Example: from 1319017136629to Wednesday, October 19, 2011 4:38:56 PM GMT+7.
示例:从1319017136629到Wednesday, October 19, 2011 4:38:56 PM GMT+7。
p/s: Just looked around and found a solution:
p/s:只是环顾四周,找到了解决方案:
SELECT datetime(1319017136629, 'unixepoch', 'localtime');
But i am still looking for a way to batch convert UNIX epoch time in SQLite.
但我仍在寻找一种在 SQLite 中批量转换 UNIX 纪元时间的方法。
回答by CL.
To change the values in the database, use the UPDATEcommand:
要更改数据库中的值,请使用以下UPDATE命令:
UPDATE MyTable SET MyColumn = datetime(MyColumn, 'unixepoch', 'localtime')
回答by Martin Zeitler
1319017136629is a value in milliseconds, which is not UNIXepoch time;
therefore it has to be divided by 1000and rounded to integer seconds; only then DATE()and DATETIME()will convert.
1319017136629是 中的一个值milliseconds,它不是UNIX纪元时间;因此它必须被除以1000并四舍五入为整数seconds;只有这样DATE(),DATETIME()才会转换。
SELECT DATETIME(ROUND(dateColumn / 1000), 'unixepoch') AS isodate FROM tableName
Converting database values to localtimeis unfortunate; conversion on output is less problematic.
localtime不幸的是,将数据库值转换为 输出转换问题较少。
The Y2038 problemmight already be worth a consideration.
该Y2038问题可能已经是值得考虑的。

