Skip to content

Commit 1d300f8

Browse files
committed
display ui time with customize time location
1 parent 714dcf9 commit 1d300f8

File tree

6 files changed

+51
-18
lines changed

6 files changed

+51
-18
lines changed

custom/conf/app.ini.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ MAX_FILES = 5
547547
; Special supported values are ANSIC, UnixDate, RubyDate, RFC822, RFC822Z, RFC850, RFC1123, RFC1123Z, RFC3339, RFC3339Nano, Kitchen, Stamp, StampMilli, StampMicro and StampNano
548548
; For more information about the format see http://golang.org/pkg/time/#pkg-constants
549549
FORMAT =
550+
; Location the UI time display i.e. Asia/Shanghai
551+
; Empty means server's location setting
552+
UI_LOCATION =
550553

551554
[log]
552555
ROOT_PATH =

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,12 @@ Two special environment variables are passed to the render command:
502502
- `GITEA_PREFIX_SRC`, which contains the current URL prefix in the `src` path tree. To be used as prefix for links.
503503
- `GITEA_PREFIX_RAW`, which contains the current URL prefix in the `raw` path tree. To be used as prefix for image paths.
504504

505+
## Time (`time`)
506+
- `FORMAT`: Time format to diplay on UI. i.e. RFC1123 or 2006-01-02 15:04:05
507+
- `UI_LOCATION`: Location of time on the UI, so that we can display correct user's time on UI. i.e. Shanghai/Asia
508+
505509
## Other (`other`)
506510

507511
- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.
508512
- `SHOW_FOOTER_VERSION`: **true**: Show Gitea version information in the footer.
509-
- `SHOW_FOOTER_TEMPLATE_LOAD_TIME`: **true**: Show time of template execution in the footer.
513+
- `SHOW_FOOTER_TEMPLATE_LOAD_TIME`: **true**: Show time of template execution in the footer.

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ IS_INPUT_FILE = false
237237
- RENDER_COMMAND: 工具的命令行命令及参数。
238238
- IS_INPUT_FILE: 输入方式是最后一个参数为文件路径还是从标准输入读取。
239239

240-
240+
## Time (`time`)
241+
- `FORMAT`: 显示在界面上的时间格式。比如: RFC1123 或者 2006-01-02 15:04:05
242+
- `UI_LOCATION`: 显示在界面上的时区,默认为本地时区。比如: Asia/Shanghai
241243

242244
## Other (`other`)
243245

modules/base/tool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func TimeSince(then time.Time, lang string) template.HTML {
399399

400400
func htmlTimeSince(then, now time.Time, lang string) template.HTML {
401401
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
402-
then.Format(setting.TimeFormat),
402+
then.In(setting.UILocation).Format(setting.TimeFormat),
403403
timeSince(then, now, lang)))
404404
}
405405

@@ -410,7 +410,7 @@ func TimeSinceUnix(then util.TimeStamp, lang string) template.HTML {
410410

411411
func htmlTimeSinceUnix(then, now util.TimeStamp, lang string) template.HTML {
412412
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
413-
then.Format(setting.TimeFormat),
413+
then.FormatInLocation(setting.TimeFormat, setting.UILocation),
414414
timeSinceUnix(int64(then), int64(now), lang)))
415415
}
416416

modules/setting/setting.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ var (
278278

279279
// Time settings
280280
TimeFormat string
281+
// UILocation is the location on the UI, so that we can display the time on UI.
282+
UILocation = time.Local
281283

282284
CSRFCookieName = "_csrf"
283285
CSRFCookieHTTPOnly = true
@@ -356,10 +358,6 @@ var (
356358
HasRobotsTxt bool
357359
InternalToken string // internal access token
358360
IterateBufferSize int
359-
360-
// UILocation is the location on the UI, so that we can display the time on UI.
361-
// Currently only show the default time.Local, it could be added to app.ini after UI is ready
362-
UILocation = time.Local
363361
)
364362

365363
// DateLang transforms standard language locale name to corresponding value in datetime plugin.
@@ -792,7 +790,7 @@ func NewContext() {
792790
AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5)
793791
AttachmentEnabled = sec.Key("ENABLED").MustBool(true)
794792

795-
TimeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("RFC1123")
793+
timeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("RFC1123")
796794
TimeFormat = map[string]string{
797795
"ANSIC": time.ANSIC,
798796
"UnixDate": time.UnixDate,
@@ -809,17 +807,28 @@ func NewContext() {
809807
"StampMilli": time.StampMilli,
810808
"StampMicro": time.StampMicro,
811809
"StampNano": time.StampNano,
812-
}[TimeFormatKey]
810+
}[timeFormatKey]
813811
// When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05'
814812
if len(TimeFormat) == 0 {
815-
TimeFormat = TimeFormatKey
813+
TimeFormat = timeFormatKey
816814
TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat)
817815
if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" {
818816
log.Fatal("Can't create time properly, please check your time format has 2006, 01, 02, 15, 04 and 05")
819817
}
820818
log.Trace("Custom TimeFormat: %s", TimeFormat)
821819
}
822820

821+
zone := Cfg.Section("time").Key("UI_LOCATION").String()
822+
if zone != "" {
823+
UILocation, err = time.LoadLocation(zone)
824+
if err != nil {
825+
log.Fatal("Load time zone failed: %v", err)
826+
}
827+
}
828+
if UILocation == nil {
829+
UILocation = time.Local
830+
}
831+
823832
RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
824833
// Does not check run user when the install lock is off.
825834
if InstallLock {

modules/util/time_stamp.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,47 @@ func (ts TimeStamp) Year() int {
3535

3636
// AsTime convert timestamp as time.Time in Local locale
3737
func (ts TimeStamp) AsTime() (tm time.Time) {
38-
tm = time.Unix(int64(ts), 0).In(setting.UILocation)
38+
return ts.AsTimeInLocation(setting.UILocation)
39+
}
40+
41+
// AsTimeInLocation convert timestamp as time.Time in Local locale
42+
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) (tm time.Time) {
43+
tm = time.Unix(int64(ts), 0).In(loc)
3944
return
4045
}
4146

4247
// AsTimePtr convert timestamp as *time.Time in Local locale
4348
func (ts TimeStamp) AsTimePtr() *time.Time {
44-
tm := time.Unix(int64(ts), 0).In(setting.UILocation)
49+
return ts.AsTimePtrInLocation(setting.UILocation)
50+
}
51+
52+
// AsTimePtr convert timestamp as *time.Time in Local locale
53+
func (ts TimeStamp) AsTimePtrInLocation(loc *time.Location) *time.Time {
54+
tm := time.Unix(int64(ts), 0).In(loc)
4555
return &tm
4656
}
4757

48-
// Format formats timestamp as
58+
// Format formats timestamp as given format
4959
func (ts TimeStamp) Format(f string) string {
50-
return ts.AsTime().Format(f)
60+
return ts.FormatInLocation(f, setting.UILocation)
61+
}
62+
63+
// FormatInLocation formats timestamp as given format with spiecific location
64+
func (ts TimeStamp) FormatInLocation(f string, loc *time.Location) string {
65+
return ts.AsTimeInLocation(loc).Format(f)
5166
}
5267

5368
// FormatLong formats as RFC1123Z
54-
func (ts TimeStamp) FormatLong() string {
69+
/*func (ts TimeStamp) FormatLong() string {
5570
return ts.Format(time.RFC1123Z)
5671
}
5772
5873
// FormatShort formats as short
5974
func (ts TimeStamp) FormatShort() string {
6075
return ts.Format("Jan 02, 2006")
61-
}
76+
}*/
6277

6378
// IsZero is zero time
6479
func (ts TimeStamp) IsZero() bool {
65-
return ts.AsTime().IsZero()
80+
return ts.AsTimeInLocation(time.Local).IsZero()
6681
}

0 commit comments

Comments
 (0)