Skip to content

Commit

Permalink
add & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 3, 2024
1 parent 3cc2a71 commit ef9030d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewFuncMap() template.FuncMap {
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
},

// for backward compatibility only, do not use it anymore
// for backward compatibility only, do not use them anymore
"TimeSince": timeSinceLegacy,
"TimeSinceUnix": timeSinceLegacy,
"DateTime": dateTimeLegacy,
Expand Down
9 changes: 8 additions & 1 deletion modules/templates/util_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/translation"
)

type DateUtils struct{}
Expand Down Expand Up @@ -63,7 +64,7 @@ func dateTimeLegacy(format string, datetime any, _ ...string) template.HTML {
return dateTimeFormat(format, datetime)
}

func timeSinceLegacy(time any, _ ...any) template.HTML {
func timeSinceLegacy(time any, _ translation.Locale) template.HTML {
if !setting.IsProd || setting.IsInTesting {
panic("timeSinceLegacy is for backward compatibility only, do not use it in new code")
}
Expand All @@ -74,10 +75,16 @@ func anyToTime(any any) (t time.Time, isZero bool) {
switch v := any.(type) {
case nil:
// it is zero
case *time.Time:
if v != nil {
t = *v
}
case time.Time:
t = v
case timeutil.TimeStamp:
t = v.AsTime()
case timeutil.TimeStampNano:
t = v.AsTime()
case int:
t = timeutil.TimeStamp(v).AsTime()
case int64:
Expand Down
21 changes: 21 additions & 0 deletions modules/templates/util_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ func TestDateTime(t *testing.T) {
actual = du.FullTime(refTimeStamp)
assert.EqualValues(t, `<relative-time weekday="" year="numeric" format="datetime" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" data-tooltip-content data-tooltip-interactive="true" datetime="2017-12-31T19:00:00-05:00">2017-12-31 19:00:00 -05:00</relative-time>`, actual)
}

func TestTimeSince(t *testing.T) {
testTz, _ := time.LoadLocation("America/New_York")
defer test.MockVariableValue(&setting.DefaultUILocation, testTz)()
defer test.MockVariableValue(&setting.IsInTesting, false)()

du := NewDateUtils()
assert.EqualValues(t, "-", du.TimeSince(nil))

refTimeStr := "2018-01-01T00:00:00Z"
refTime, _ := time.Parse(time.RFC3339, refTimeStr)

actual := du.TimeSince(refTime)
assert.EqualValues(t, `<relative-time prefix="" tense="past" datetime="2018-01-01T00:00:00Z" data-tooltip-content data-tooltip-interactive="true">2018-01-01 00:00:00 +00:00</relative-time>`, actual)

actual = timeSinceTo(&refTime, time.Time{})
assert.EqualValues(t, `<relative-time prefix="" tense="future" datetime="2018-01-01T00:00:00Z" data-tooltip-content data-tooltip-interactive="true">2018-01-01 00:00:00 +00:00</relative-time>`, actual)

actual = timeSinceLegacy(timeutil.TimeStampNano(refTime.UnixNano()), nil)
assert.EqualValues(t, `<relative-time prefix="" tense="past" datetime="2017-12-31T19:00:00-05:00" data-tooltip-content data-tooltip-interactive="true">2017-12-31 19:00:00 -05:00</relative-time>`, actual)
}
2 changes: 1 addition & 1 deletion templates/package/shared/versionlist.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="flex-item-main">
<a class="flex-item-title" href="{{.VersionWebLink}}">{{.Version.LowerVersion}}</a>
<div class="flex-item-body">
{{ctx.Locale.Tr "packages.published_by" (TimeSinceUnix .Version.CreatedUnix ctx.Locale) .Creator.HomeLink .Creator.GetDisplayName}}
{{ctx.Locale.Tr "packages.published_by" (DateUtils.TimeSince .Version.CreatedUnix) .Creator.HomeLink .Creator.GetDisplayName}}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/milestones.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{{if .UpdatedUnix}}
<div class="flex-text-block">
{{svg "octicon-clock"}}
{{ctx.Locale.Tr "repo.milestones.update_ago" (TimeSinceUnix .UpdatedUnix ctx.Locale)}}
{{ctx.Locale.Tr "repo.milestones.update_ago" (DateUtils.TimeSince .UpdatedUnix)}}
</div>
{{end}}
<div class="flex-text-block">
Expand Down
2 changes: 1 addition & 1 deletion templates/shared/searchbottom.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</div>
<div class="tw-mr-4">
{{if not .result.UpdatedUnix.IsZero}}
<span class="ui grey text">{{ctx.Locale.Tr "explore.code_last_indexed_at" (TimeSinceUnix .result.UpdatedUnix ctx.Locale)}}</span>
<span class="ui grey text">{{ctx.Locale.Tr "explore.code_last_indexed_at" (DateUtils.TimeSince .result.UpdatedUnix)}}</span>
{{end}}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/user/dashboard/milestones.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
{{if .UpdatedUnix}}
<div class="flex-text-block">
{{svg "octicon-clock"}}
{{ctx.Locale.Tr "repo.milestones.update_ago" (TimeSinceUnix .UpdatedUnix ctx.Locale)}}
{{ctx.Locale.Tr "repo.milestones.update_ago" (DateUtils.TimeSince .UpdatedUnix)}}
</div>
{{end}}
<div class="flex-text-block">
Expand Down

0 comments on commit ef9030d

Please sign in to comment.