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 1319017136629
to 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 UPDATE
command:
要更改数据库中的值,请使用以下UPDATE
命令:
UPDATE MyTable SET MyColumn = datetime(MyColumn, 'unixepoch', 'localtime')
回答by Martin Zeitler
1319017136629
is a value in milliseconds
, which is not UNIX
epoch time;
therefore it has to be divided by 1000
and 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 localtime
is unfortunate; conversion on output is less problematic.
localtime
不幸的是,将数据库值转换为 输出转换问题较少。
The Y2038 problemmight already be worth a consideration.
该Y2038问题可能已经是值得考虑的。