github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_date_format.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package builtin 8 9 import ( 10 "errors" 11 "time" 12 13 "github.com/wangyougui/gf/v2/os/gtime" 14 ) 15 16 // RuleDateFormat implements `date-format` rule: 17 // Custom date format. 18 // 19 // Format: date-format:format 20 type RuleDateFormat struct{} 21 22 func init() { 23 Register(RuleDateFormat{}) 24 } 25 26 func (r RuleDateFormat) Name() string { 27 return "date-format" 28 } 29 30 func (r RuleDateFormat) Message() string { 31 return "The {field} value `{value}` does not match the format: {pattern}" 32 } 33 34 func (r RuleDateFormat) Run(in RunInput) error { 35 type iTime interface { 36 Date() (year int, month time.Month, day int) 37 IsZero() bool 38 } 39 // support for time value, eg: gtime.Time/*gtime.Time, time.Time/*time.Time. 40 if obj, ok := in.Value.Val().(iTime); ok { 41 if obj.IsZero() { 42 return errors.New(in.Message) 43 } 44 return nil 45 } 46 if _, err := gtime.StrToTimeFormat(in.Value.String(), in.RulePattern); err != nil { 47 return errors.New(in.Message) 48 } 49 return nil 50 }