Skip to content

Commit 71dc657

Browse files
committed
fix datetime
1 parent 74aa446 commit 71dc657

31 files changed

+126
-46
lines changed

modules/timeutil/datetime.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,54 @@ import (
77
"fmt"
88
"html"
99
"html/template"
10+
"time"
1011
)
1112

12-
// DateTime renders an absolute time HTML given a time as a string
13-
func DateTime(format, datetime, fallback string) template.HTML {
14-
datetimeEscaped := html.EscapeString(datetime)
15-
fallbackEscaped := html.EscapeString(fallback)
13+
// DateTime renders an absolute time HTML element by datetime.
14+
func DateTime(format string, datetime any) template.HTML {
15+
if p, ok := datetime.(*time.Time); ok {
16+
datetime = *p
17+
}
18+
if p, ok := datetime.(*TimeStamp); ok {
19+
datetime = *p
20+
}
21+
switch v := datetime.(type) {
22+
case TimeStamp:
23+
datetime = v.AsTime()
24+
case int:
25+
datetime = TimeStamp(v).AsTime()
26+
case int64:
27+
datetime = TimeStamp(v).AsTime()
28+
}
29+
30+
var datetimeEscaped, textEscaped string
31+
switch v := datetime.(type) {
32+
case nil:
33+
return "N/A"
34+
case string:
35+
datetimeEscaped = html.EscapeString(v)
36+
textEscaped = datetimeEscaped
37+
case time.Time:
38+
if v.IsZero() || v.Unix() == 0 {
39+
return "N/A"
40+
}
41+
datetimeEscaped = html.EscapeString(v.Format(time.RFC3339))
42+
if format == "full" {
43+
textEscaped = html.EscapeString(v.Format("2006-01-02 15:04:05 -07:00"))
44+
} else {
45+
textEscaped = html.EscapeString(v.Format("2006-01-02"))
46+
}
47+
default:
48+
panic(fmt.Sprintf("Unsupported time type %T", datetime))
49+
}
50+
1651
switch format {
1752
case "short":
18-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, fallbackEscaped))
53+
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
1954
case "long":
20-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="long" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, fallbackEscaped))
55+
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="long" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
2156
case "full":
22-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="%s">%s</relative-time>`, datetimeEscaped, fallbackEscaped))
57+
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
2358
}
24-
return template.HTML("error in DateTime")
59+
panic(fmt.Sprintf("Unsupported format %s", format))
2560
}

modules/timeutil/datetime_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package timeutil
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestDateTime(t *testing.T) {
16+
oldTz := setting.DefaultUILocation
17+
setting.DefaultUILocation, _ = time.LoadLocation("America/New_York")
18+
defer func() {
19+
setting.DefaultUILocation = oldTz
20+
}()
21+
22+
refTimeStr := "2018-01-01T00:00:00Z"
23+
refTime, _ := time.Parse(time.RFC3339, refTimeStr)
24+
refTimeStamp := TimeStamp(refTime.Unix())
25+
26+
assert.EqualValues(t, "N/A", DateTime("short", nil))
27+
assert.EqualValues(t, "N/A", DateTime("short", 0))
28+
assert.EqualValues(t, "N/A", DateTime("short", time.Time{}))
29+
assert.EqualValues(t, "N/A", DateTime("short", TimeStamp(0)))
30+
31+
actual := DateTime("short", "invalid")
32+
assert.EqualValues(t, `<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="invalid">invalid</relative-time>`, actual)
33+
34+
actual = DateTime("short", refTimeStr)
35+
assert.EqualValues(t, `<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="2018-01-01T00:00:00Z">2018-01-01T00:00:00Z</relative-time>`, actual)
36+
37+
actual = DateTime("short", refTime)
38+
assert.EqualValues(t, `<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="2018-01-01T00:00:00Z">2018-01-01</relative-time>`, actual)
39+
40+
actual = DateTime("short", refTimeStamp)
41+
assert.EqualValues(t, `<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="2017-12-31T19:00:00-05:00">2017-12-31</relative-time>`, actual)
42+
43+
actual = DateTime("full", refTimeStamp)
44+
assert.EqualValues(t, `<relative-time format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="2017-12-31T19:00:00-05:00">2017-12-31 19:00:00 -05:00</relative-time>`, actual)
45+
}

modules/timeutil/since.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string {
115115
}
116116

117117
func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
118-
friendlyText := then.Format("2006-01-02 15:04:05 +07:00")
118+
friendlyText := then.Format("2006-01-02 15:04:05 -07:00")
119119

120120
// document: https://github.com/github/relative-time-element
121121
attrs := `tense="past"`

templates/admin/auth/list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{.Name}}</a></td>
2727
<td>{{.TypeName}}</td>
2828
<td>{{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</td>
29-
<td>{{DateTime "short" .UpdatedUnix.FormatLong .UpdatedUnix.FormatShort}}</td>
30-
<td>{{DateTime "short" .CreatedUnix.FormatLong .CreatedUnix.FormatShort}}</td>
29+
<td>{{DateTime "short" .UpdatedUnix}}</td>
30+
<td>{{DateTime "short" .CreatedUnix}}</td>
3131
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{svg "octicon-pencil"}}</a></td>
3232
</tr>
3333
{{end}}

templates/admin/cron.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<td><button type="submit" class="ui green button" name="op" value="{{.Name}}" title="{{$.locale.Tr "admin.dashboard.operation_run"}}">{{svg "octicon-triangle-right"}}</button></td>
2222
<td>{{$.locale.Tr (printf "admin.dashboard.%s" .Name)}}</td>
2323
<td>{{.Spec}}</td>
24-
<td>{{DateTime "full" (DateFmtLong .Next) (DateFmtLong .Next)}}</td>
25-
<td>{{if gt .Prev.Year 1}}{{DateTime "full" (DateFmtLong .Prev) (DateFmtLong .Prev)}}{{else}}N/A{{end}}</td>
24+
<td>{{DateTime "full" (DateFmtLong .Next)}}</td>
25+
<td>{{if gt .Prev.Year 1}}{{DateTime "full" .Prev}}{{else}}N/A{{end}}</td>
2626
<td>{{.ExecTimes}}</td>
2727
<td {{if ne .Status ""}}data-tooltip-content="{{.FormatLastMessage $.locale}}"{{end}} >{{if eq .Status ""}}—{{else if eq .Status "finished"}}{{svg "octicon-check" 16}}{{else}}{{svg "octicon-x" 16}}{{end}}</td>
2828
</tr>

templates/admin/notice.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<td>{{.ID}}</td>
2727
<td>{{$.locale.Tr .TrStr}}</td>
2828
<td class="view-detail"><span class="notice-description text truncate">{{.Description}}</span></td>
29-
<td>{{DateTime "short" .CreatedUnix.FormatLong .CreatedUnix.FormatShort}}</td>
29+
<td>{{DateTime "short" .CreatedUnix}}</td>
3030
<td><a href="#">{{svg "octicon-note" 16 "view-detail"}}</a></td>
3131
</tr>
3232
{{end}}

templates/admin/org/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<td>{{.NumTeams}}</td>
4242
<td>{{.NumMembers}}</td>
4343
<td>{{.NumRepos}}</td>
44-
<td>{{DateTime "short" .CreatedUnix.FormatLong .CreatedUnix.FormatShort}}</td>
44+
<td>{{DateTime "short" .CreatedUnix}}</td>
4545
<td><a href="{{.OrganisationLink}}/settings">{{svg "octicon-pencil"}}</a></td>
4646
</tr>
4747
{{end}}

templates/admin/packages/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
{{end}}
6666
</td>
6767
<td>{{FileSize .CalculateBlobSize}}</td>
68-
<td>{{DateTime "short" .Version.CreatedUnix.FormatLong .Version.CreatedUnix.FormatShort}}</td>
68+
<td>{{DateTime "short" .Version.CreatedUnix}}</td>
6969
<td><a class="delete-button" href="" data-url="{{$.Link}}/delete?page={{$.Page.Paginater.Current}}&sort={{$.SortType}}" data-id="{{.Version.ID}}" data-name="{{.Package.Name}}" data-data-version="{{.Version.Version}}">{{svg "octicon-trash"}}</a></td>
7070
</tr>
7171
{{end}}

templates/admin/queue.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@
155155
{{range .Queue.Workers}}
156156
<tr>
157157
<td>{{.Workers}}{{if .IsFlusher}}<span title="{{$.locale.Tr "admin.monitor.queue.flush"}}">{{svg "octicon-sync"}}</span>{{end}}</td>
158-
<td>{{DateTime "full" (DateFmtLong .Start) (DateFmtLong .Start)}}</td>
159-
<td>{{if .HasTimeout}}{{DateTime "full" (DateFmtLong .Timeout) (DateFmtLong .Timeout)}}{{else}}-{{end}}</td>
158+
<td>{{DateTime "full" .Start}}</td>
159+
<td>{{if .HasTimeout}}{{DateTime "full" .Timeout}}{{else}}-{{end}}</td>
160160
<td>
161161
<a class="delete-button" href="" data-url="{{$.Link}}/cancel/{{.PID}}" data-id="{{.PID}}" data-name="{{.Workers}}" title="{{$.locale.Tr "remove"}}">{{svg "octicon-trash"}}</a>
162162
</td>

templates/admin/repo/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<td>{{.NumForks}}</td>
8181
<td>{{.NumIssues}}</td>
8282
<td>{{FileSize .Size}}</td>
83-
<td>{{DateTime "short" .CreatedUnix.FormatLong .CreatedUnix.FormatShort}}</td>
83+
<td>{{DateTime "short" .CreatedUnix}}</td>
8484
<td><a class="delete-button" href="" data-url="{{$.Link}}/delete?page={{$.Page.Paginater.Current}}&sort={{$.SortType}}" data-id="{{.ID}}" data-name="{{.Name}}">{{svg "octicon-trash"}}</a></td>
8585
</tr>
8686
{{end}}

0 commit comments

Comments
 (0)