From 7759b9ee6efb069a1f846b43bd6bc981f998e55a Mon Sep 17 00:00:00 2001 From: Peter Smit Date: Mon, 16 Feb 2015 14:44:27 +0200 Subject: [PATCH] Remove the "PHP" style formatting function The "PHP" formatting function doesn't add anything, except an undocumented date format. All usages in the templates have been replaced with DateFmtShort and DateFmtLong for convenience. --- modules/base/template.go | 9 ++- modules/base/tool.go | 67 ++--------------------- templates/admin/auth/list.tmpl | 4 +- templates/admin/org/list.tmpl | 2 +- templates/admin/repo/list.tmpl | 2 +- templates/admin/user/list.tmpl | 2 +- templates/user/profile.tmpl | 2 +- templates/user/settings/applications.tmpl | 2 +- templates/user/settings/social.tmpl | 2 +- templates/user/settings/sshkeys.tmpl | 2 +- 10 files changed, 22 insertions(+), 72 deletions(-) diff --git a/modules/base/template.go b/modules/base/template.go index f3fa138578..0fd519e6c5 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -126,8 +126,13 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ return a + b }, "ActionIcon": ActionIcon, - "DateFormat": DateFormat, - "List": List, + "DateFmtLong": func(t time.Time) string { + return t.Format(time.RFC1123Z) + }, + "DateFmtShort": func(t time.Time) string { + return t.Format("Jan 02, 2006") + }, + "List": List, "Mail2Domain": func(mail string) string { if !strings.Contains(mail, "@") { return "try.gogs.io" diff --git a/modules/base/tool.go b/modules/base/tool.go index 5043364cec..55e6dffd96 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -126,7 +126,7 @@ func VerifyTimeLimitCode(data string, minutes int, code string) bool { retCode := CreateTimeLimitCode(data, minutes, start) if retCode == code && minutes > 0 { // check time is expired or not - before, _ := DateParse(start, "YmdHi") + before, _ := time.ParseInLocation("200601021504", start, time.Local) now := time.Now() if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() { return true @@ -141,7 +141,7 @@ const TimeLimitCodeLength = 12 + 6 + 40 // create a time limit code // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string { - format := "YmdHi" + format := "200601021504" var start, end time.Time var startStr, endStr string @@ -149,16 +149,16 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string if startInf == nil { // Use now time create code start = time.Now() - startStr = DateFormat(start, format) + startStr = start.Format(format) } else { // use start string create code startStr = startInf.(string) - start, _ = DateParse(startStr, format) - startStr = DateFormat(start, format) + start, _ = time.ParseInLocation(format, startStr, time.Local) + startStr = start.Format(format) } end = start.Add(time.Minute * time.Duration(minutes)) - endStr = DateFormat(end, format) + endStr = end.Format(format) // create sha1 encode string sh := sha1.New() @@ -420,58 +420,3 @@ func Subtract(left interface{}, right interface{}) interface{} { return fleft + float64(rleft) - (fright + float64(rright)) } } - -// DateFormat pattern rules. -var datePatterns = []string{ - // year - "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 - "y", "06", //A two digit representation of a year Examples: 99 or 03 - - // month - "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 - "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 - "M", "Jan", // A short textual representation of a month, three letters Jan through Dec - "F", "January", // A full textual representation of a month, such as January or March January through December - - // day - "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 - "j", "2", // Day of the month without leading zeros 1 to 31 - - // week - "D", "Mon", // A textual representation of a day, three letters Mon through Sun - "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday - - // time - "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 - "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 - "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 - "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 - - "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm - "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM - - "i", "04", // Minutes with leading zeros 00 to 59 - "s", "05", // Seconds, with leading zeros 00 through 59 - - // time zone - "T", "MST", - "P", "-07:00", - "O", "-0700", - - // RFC 2822 - "r", time.RFC1123Z, -} - -// Parse Date use PHP time format. -func DateParse(dateString, format string) (time.Time, error) { - replacer := strings.NewReplacer(datePatterns...) - format = replacer.Replace(format) - return time.ParseInLocation(format, dateString, time.Local) -} - -// Date takes a PHP like date func to Go's time format. -func DateFormat(t time.Time, format string) string { - replacer := strings.NewReplacer(datePatterns...) - format = replacer.Replace(format) - return t.Format(format) -} diff --git a/templates/admin/auth/list.tmpl b/templates/admin/auth/list.tmpl index aba516b8eb..ec701a8f8c 100644 --- a/templates/admin/auth/list.tmpl +++ b/templates/admin/auth/list.tmpl @@ -34,8 +34,8 @@ {{.Name}} {{.TypeString}} - {{DateFormat .Updated "M d, Y"}} - {{DateFormat .Created "M d, Y"}} + {{DateFmtShort .Updated}} + {{DateFmtShort .Created}} {{end}} diff --git a/templates/admin/org/list.tmpl b/templates/admin/org/list.tmpl index b522dc08a3..ce5083a0ae 100644 --- a/templates/admin/org/list.tmpl +++ b/templates/admin/org/list.tmpl @@ -35,7 +35,7 @@ {{.NumTeams}} {{.NumMembers}} {{.NumRepos}} - {{DateFormat .Created "M d, Y"}} + {{DateFmtShort .Created}} {{end}} diff --git a/templates/admin/repo/list.tmpl b/templates/admin/repo/list.tmpl index 88e16a4337..981e2ef72b 100644 --- a/templates/admin/repo/list.tmpl +++ b/templates/admin/repo/list.tmpl @@ -37,7 +37,7 @@ {{.NumWatches}} {{.NumStars}} {{.NumIssues}} - {{DateFormat .Created "M d, Y"}} + {{DateFmtShort .Created}} {{end}} diff --git a/templates/admin/user/list.tmpl b/templates/admin/user/list.tmpl index d42d52910e..1dd5553eb5 100644 --- a/templates/admin/user/list.tmpl +++ b/templates/admin/user/list.tmpl @@ -37,7 +37,7 @@ {{.NumRepos}} - {{DateFormat .Created "M d, Y"}} + {{DateFmtShort .Created }} {{end}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 44c2212383..a18a8b50d6 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -28,7 +28,7 @@ {{if .Owner.Website}}
  •   {{.Owner.Website}}
  • {{end}} -
  •   {{.i18n.Tr "user.join_on"}} {{DateFormat .Owner.Created "M d, Y"}}
  • +
  •   {{.i18n.Tr "user.join_on"}} {{DateFmtShort .Owner.Created}}