Trac sqlite日期查询

时间:2012-10-23 20:45:20

标签: sqlite datetime timestamp trac

我有一段时间生成一份报告,其中包含我的门票创建日期。

这是我的sqlite查询(我直接查询数据库)

.header on
.mode csv
.output opentickets.csv
SELECT DISTINCT
   id AS ticket, summary, status, priority, t.type AS type, 
   owner, time as created

  FROM ticket t, ticket_custom q
  LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
  WHERE t.id = q.ticket AND status <> 'closed'
 ORDER BY  priority, time;

但是我在创作时获得的价值是垃圾: 我得到1341324096360000 什么时候得到 2012-07-03

我尝试过unix epoch时间转换... 查询:

.header on
.mode csv
.output opentickets.csv
SELECT DISTINCT
   id AS ticket, summary, status, priority, t.type AS type, 
   owner, datetime(time, 'unixepoch') as created

  FROM ticket t, ticket_custom q
  LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
  WHERE t.id = q.ticket AND status <> 'closed'
 ORDER BY  priority, time;

我得到:-1413-03-01 13:07:12

我错过了什么?

2 个答案:

答案 0 :(得分:5)

CL是正确的,时间以微秒为单位存储。您可以在schema中看到时间字段为int64,而release notes为.12,表示他们已将时间从“自纪元以来的秒数”更改为“自纪元以来的微秒”

至于我在Trac查询中显示时间的经验,我没有必要进行任何转换。这是一个查询,用于显示过去7天内所有已更改的票证。如您所见,我将票证更改时间显示为select和Trac正确格式化的一部分。 Trac会自动将创建,修改,日期,时间字段格式化为日期和/或时间。有关详细信息,请参阅此Trac wiki page

    SELECT 
tc.Ticket AS ticket, 
tc.Time as time, 
c.value as  __group__,
ifnull(tc.Newvalue,0) as total,
tc.Author  AS author,         
tc.Field as field     
from ticket_change tc 
LEFT JOIN ticket t on t.id = tc.ticket
LEFT JOIN ticket_custom c on c.ticket = tc.ticket and c.name= 'customer'
LEFT JOIN ticket_custom tt on tt.ticket = tc.ticket and tt.name = 'totalhours'
where field = 'hours' and c.Value is not null
and tc.Time > strftime('%s',CURRENT_DATE-7) +0
and tc.Time < strftime('%s',CURRENT_DATE) +0
and tc.Author = $USER
and tt.value is not null

如果您想要做一些特殊的装饰,请按时SQLite docs查看,但这是一个简单的例子

strftime('%m/%d/%Y', time / 1000000, 'unixepoch', 'localtime') 

我希望这会有所帮助。

答案 1 :(得分:2)

Unix时间戳以秒为单位,但看起来这些值是微秒:

> SELECT datetime(1341324096360000 / 1000000, 'unixepoch');
2012-07-03 14:01:36