add func to use in callbacks: NowFuncString() *string

This commit is contained in:
kmayer 2018-01-15 14:51:50 +05:00
parent 0a51f6cdc5
commit aefc97d8a9
4 changed files with 8 additions and 3 deletions

View File

@ -31,7 +31,7 @@ func beforeCreateCallback(scope *Scope) {
// updateTimeStampForCreateCallback will set `CreatedAt`, `UpdatedAt` when creating
func updateTimeStampForCreateCallback(scope *Scope) {
if !scope.HasError() {
now := NowFunc()
now := NowFuncString()
if createdAtField, ok := scope.FieldByName("CreatedAt"); ok {
if createdAtField.IsBlank {

View File

@ -40,7 +40,7 @@ func deleteCallback(scope *Scope) {
"UPDATE %v SET %v=%v%v%v",
scope.QuotedTableName(),
scope.Quote(deletedAtField.DBName),
scope.AddToVars(NowFunc()),
scope.AddToVars(NowFuncString()),
addExtraSpaceIfExist(scope.CombinedConditionSql()),
addExtraSpaceIfExist(extraOption),
)).Exec()

View File

@ -49,7 +49,7 @@ func beforeUpdateCallback(scope *Scope) {
// updateTimeStampForUpdateCallback will set `UpdatedAt` when updating
func updateTimeStampForUpdateCallback(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok {
scope.SetColumn("UpdatedAt", NowFunc())
scope.SetColumn("UpdatedAt", NowFuncString())
}
}

View File

@ -22,6 +22,11 @@ var NowFunc = func() time.Time {
return time.Now()
}
var NowFuncString = func() *string {
t := time.Now().UTC().Format(time.RFC3339)
return &t
}
// Copied from golint
var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
var commonInitialismsReplacer *strings.Replacer