Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5 from marcw/time_format_filter

Adds time_format filter. Contributed by marcw, thanks!
  • Loading branch information...
commit 3cbd7243abef3f307c4735343fa998f800d29a00 2 parents 32724a1 + 3ce8a35
@flosch authored
Showing with 31 additions and 10 deletions.
  1. +31 −10 filters.go
View
41 filters.go
@@ -9,6 +9,7 @@ import (
"reflect"
"regexp"
"strings"
+ "time"
)
type FilterFunc func(interface{}, []interface{}, *FilterChainContext) (interface{}, error)
@@ -35,16 +36,17 @@ func (ctx *FilterChainContext) visitFilter(name string) {
}
var Filters = map[string]FilterFunc{
- "safe": filterSafe,
- "unsafe": nil, // It will not be called, just added to visited filters (applied_filters)
- "lower": filterLower,
- "upper": filterUpper,
- "capitalize": filterCapitalize,
- "default": filterDefault,
- "trim": filterTrim,
- "length": filterLength,
- "join": filterJoin,
- "striptags": filterStriptags,
+ "safe": filterSafe,
+ "unsafe": nil, // It will not be called, just added to visited filters (applied_filters)
+ "lower": filterLower,
+ "upper": filterUpper,
+ "capitalize": filterCapitalize,
+ "default": filterDefault,
+ "trim": filterTrim,
+ "length": filterLength,
+ "join": filterJoin,
+ "striptags": filterStriptags,
+ "time_format": filterTimeFormat,
/* TODO:
- verbatim
@@ -86,6 +88,25 @@ func filterLower(value interface{}, args []interface{}, ctx *FilterChainContext)
return strings.ToLower(str), nil
}
+func filterTimeFormat(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
+ t, is_time := value.(time.Time)
+ if !is_time {
+ return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
+ }
+
+ arg := args[0]
+ if arg == nil {
+ return nil, errors.New("time_format requires you pass a format.")
+ }
+
+ format, is_string := arg.(string)
+ if !is_string {
+ return nil, errors.New(fmt.Sprintf("time_format's format must be a string. %v (%T) passed.", format, format))
+ }
+
+ return t.Format(format), nil
+}
+
func filterUpper(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
Please sign in to comment.
Something went wrong with that request. Please try again.