2012-10-23 85 views
2

我有一段時間生成一個報告,其中包含我的門票的創建日期。Trac sqlite日期查詢

,這裏是我的SQLite查詢(我直接查詢DB)

.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紀元時間轉換... 查詢:

.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

我錯過了什麼?

回答

1

的Unix時間戳以秒,但它似乎這些值是微秒:

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

CL是正確的,因爲時間存儲在微秒。你可以在schema這裏看到,時間字段是一個int64,而在release notes中的這個意思是說,他們把時間從「epoch以來的秒數」改變爲「epoch以來的微秒數」。

至於我在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 

如果你正在尋找做一些特殊的fomatting,看看SQLite docs時間,但這裏是一個簡單的例子

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

我希望這有助於。