From 1f67fcb0f9663abdb39de4700bebad239d4532f1 Mon Sep 17 00:00:00 2001 From: xiemengjun Date: Tue, 1 Jan 2013 21:42:16 +0800 Subject: [PATCH] http support pprof and template add date func like php's date function http support pprof PprofOn set to true you can profile the runtime data. template add date func like php's date function {{date "Y-m-d H:i:s"}} --- README.md | 10 ++++++++++ template.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.md b/README.md index f49d2e09..5d0f7295 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,16 @@ beego has many default variables, as follow is a list to show: - AppConfig *Config Appconfig is a result that parse file from conf/app.conf, if this file not exist then the variable is nil. if the file exist, then return the Config as follow. + +- PprofOn bool + + default is false. turn on pprof, if set to true. you can visit like this: + + /debug/pprof + /debug/pprof/cmdline + /debug/pprof/profile + /debug/pprof/symbol + this serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. For more information about pprof, see http://golang.org/pkg/net/http/pprof/ ## Config ============ diff --git a/template.go b/template.go index 2932efd0..4dc643e6 100644 --- a/template.go +++ b/template.go @@ -18,6 +18,7 @@ func init() { beegoTplFuncMap = make(template.FuncMap) beegoTplFuncMap["markdown"] = MarkDown beegoTplFuncMap["dateformat"] = DateFormat + beegoTplFuncMap["date"] = Date beegoTplFuncMap["compare"] = Compare } @@ -35,6 +36,45 @@ func DateFormat(t time.Time, layout string) (datestring string) { return } +// Date takes a PHP like date func to Go's time fomate +func Date(t time.Time, format string) (datestring string) { + patterns := []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 + } + replacer := strings.NewReplacer(patterns...) + format = replacer.Replace(format) + datestring = t.Format(format) + return +} + // Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. // Whitespace is trimmed. Used by the template parser as "eq" func Compare(a, b interface{}) (equal bool) {