github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_datetime.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 // RuleDatetime implements `datetime` rule: 17 // Standard datetime, like: 2006-01-02 12:00:00. 18 // 19 // Format: datetime 20 type RuleDatetime struct{} 21 22 func init() { 23 Register(RuleDatetime{}) 24 } 25 26 func (r RuleDatetime) Name() string { 27 return "datetime" 28 } 29 30 func (r RuleDatetime) Message() string { 31 return "The {field} value `{value}` is not a valid datetime" 32 } 33 34 func (r RuleDatetime) 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(), `Y-m-d H:i:s`); err != nil { 47 return errors.New(in.Message) 48 } 49 return nil 50 }