Skip to content

Commit 49fa03b

Browse files
Ahed91lunny
authored andcommitted
Allow using custom time format (#798)
* Allow using custom time format I need to use custom time format in `conf/app.ini' like FORMAT = 2006-01-02 15:04:05 so that Gitea will display '2017-01-30 08:41:49' check this answer for more constants to format date <http://stackoverflow.com/a/20234207/2570425> PS: First GO commit * Refactor and validate TimeFormat (must have 2006, 01, 02, 15, 04 and 05)
1 parent d0960b8 commit 49fa03b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

modules/setting/setting.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ please consider changing to GITEA_CUSTOM`)
732732
AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5)
733733
AttachmentEnabled = sec.Key("ENABLE").MustBool(true)
734734

735+
TimeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("RFC1123")
735736
TimeFormat = map[string]string{
736737
"ANSIC": time.ANSIC,
737738
"UnixDate": time.UnixDate,
@@ -748,7 +749,16 @@ please consider changing to GITEA_CUSTOM`)
748749
"StampMilli": time.StampMilli,
749750
"StampMicro": time.StampMicro,
750751
"StampNano": time.StampNano,
751-
}[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")]
752+
}[TimeFormatKey]
753+
// When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05'
754+
if len(TimeFormat) == 0 {
755+
TimeFormat = TimeFormatKey
756+
TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat)
757+
if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" {
758+
log.Fatal(4, "Can't create time properly, please check your time format has 2006, 01, 02, 15, 04 and 05")
759+
}
760+
log.Trace("Custom TimeFormat: %s", TimeFormat)
761+
}
752762

753763
RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
754764
// Does not check run user when the install lock is off.

0 commit comments

Comments
 (0)