From 1030e6598ec907fa194bb5f1d3287921d906a0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CA=BFAhed=20=CA=BFid?= Date: Mon, 30 Jan 2017 14:40:02 +0200 Subject: [PATCH 1/2] 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 PS: First GO commit --- modules/setting/setting.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 0586bce94b6c8..ecf84e0d36c67 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -749,6 +749,11 @@ please consider changing to GITEA_CUSTOM`) "StampMicro": time.StampMicro, "StampNano": time.StampNano, }[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")] + // Determine When using custom time format like '2006-01-02 15:04:05' + if (TimeFormat == "") { + TimeFormat = Cfg.Section("time").Key("FORMAT").String() + log.Trace("Custom TimeFormat: %s", TimeFormat) + } RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername()) // Does not check run user when the install lock is off. From 80c62e8f4964bfb220a27ad5273538f748fea9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CA=BFAhed=20=CA=BFid?= Date: Fri, 3 Feb 2017 20:30:28 +0200 Subject: [PATCH 2/2] Refactor and validate TimeFormat (must have 2006, 01, 02, 15, 04 and 05) --- modules/setting/setting.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ecf84e0d36c67..a12ca705c64a8 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -732,6 +732,7 @@ please consider changing to GITEA_CUSTOM`) AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5) AttachmentEnabled = sec.Key("ENABLE").MustBool(true) + TimeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("RFC1123") TimeFormat = map[string]string{ "ANSIC": time.ANSIC, "UnixDate": time.UnixDate, @@ -748,10 +749,14 @@ please consider changing to GITEA_CUSTOM`) "StampMilli": time.StampMilli, "StampMicro": time.StampMicro, "StampNano": time.StampNano, - }[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")] - // Determine When using custom time format like '2006-01-02 15:04:05' - if (TimeFormat == "") { - TimeFormat = Cfg.Section("time").Key("FORMAT").String() + }[TimeFormatKey] + // When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05' + if len(TimeFormat) == 0 { + TimeFormat = TimeFormatKey + TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat) + if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" { + log.Fatal(4, "Can't create time properly, please check your time format has 2006, 01, 02, 15, 04 and 05") + } log.Trace("Custom TimeFormat: %s", TimeFormat) }