From 4e90fbf4e87407299b036afeb072db88c0b8edd3 Mon Sep 17 00:00:00 2001 From: Cihangir SAVAS Date: Sat, 23 Aug 2014 01:00:04 -0700 Subject: [PATCH] gorm: added ability to change the time.now format --- callback_create.go | 5 ++--- callback_delete.go | 7 ++----- callback_query.go | 3 +-- callback_update.go | 3 +-- doc/development.md | 2 +- logger.go | 2 +- main.go | 11 +++++++++++ main_private.go | 2 +- scope.go | 2 +- scope_private.go | 5 ++--- 10 files changed, 23 insertions(+), 19 deletions(-) diff --git a/callback_create.go b/callback_create.go index 47b6ef96..cef035d6 100644 --- a/callback_create.go +++ b/callback_create.go @@ -3,7 +3,6 @@ package gorm import ( "fmt" "strings" - "time" ) func BeforeCreate(scope *Scope) { @@ -13,14 +12,14 @@ func BeforeCreate(scope *Scope) { func UpdateTimeStampWhenCreate(scope *Scope) { if !scope.HasError() { - now := time.Now() + now := NowFunc() scope.SetColumn("CreatedAt", now) scope.SetColumn("UpdatedAt", now) } } func Create(scope *Scope) { - defer scope.Trace(time.Now()) + defer scope.Trace(NowFunc()) if !scope.HasError() { // set create sql diff --git a/callback_delete.go b/callback_delete.go index 32d88630..c58a21fa 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -1,9 +1,6 @@ package gorm -import ( - "fmt" - "time" -) +import "fmt" func BeforeDelete(scope *Scope) { scope.CallMethod("BeforeDelete") @@ -15,7 +12,7 @@ func Delete(scope *Scope) { scope.Raw( fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.QuotedTableName(), - scope.AddToVars(time.Now()), + scope.AddToVars(NowFunc()), scope.CombinedConditionSql(), )) } else { diff --git a/callback_query.go b/callback_query.go index 439e3f96..b921248b 100644 --- a/callback_query.go +++ b/callback_query.go @@ -3,11 +3,10 @@ package gorm import ( "reflect" "strings" - "time" ) func Query(scope *Scope) { - defer scope.Trace(time.Now()) + defer scope.Trace(NowFunc()) var ( isSlice bool diff --git a/callback_update.go b/callback_update.go index a286fb25..eb65e2e0 100644 --- a/callback_update.go +++ b/callback_update.go @@ -3,7 +3,6 @@ package gorm import ( "fmt" "strings" - "time" ) func AssignUpdateAttributes(scope *Scope) { @@ -36,7 +35,7 @@ func BeforeUpdate(scope *Scope) { func UpdateTimeStampWhenUpdate(scope *Scope) { _, ok := scope.Get("gorm:update_column") if !ok { - scope.SetColumn("UpdatedAt", time.Now()) + scope.SetColumn("UpdatedAt", NowFunc()) } } diff --git a/doc/development.md b/doc/development.md index 03d66aa1..de5bb9aa 100644 --- a/doc/development.md +++ b/doc/development.md @@ -27,7 +27,7 @@ There are four kinds of callbacks corresponds to sql's CURD: create callbacks, u func updateCreated(scope *Scope) { if scope.HasColumn("Created") { - scope.SetColumn("Created", time.Now()) + scope.SetColumn("Created", NowFunc()) } } diff --git a/logger.go b/logger.go index 2c76987e..001b2165 100644 --- a/logger.go +++ b/logger.go @@ -24,7 +24,7 @@ var sqlRegexp = regexp.MustCompile(`(\$\d+)|\?`) func (logger Logger) Print(v ...interface{}) { if len(v) > 1 { level := v[0] - currentTime := "\n\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m" + currentTime := "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m" source := fmt.Sprintf("\033[35m(%v)\033[0m", v[1]) messages := []interface{}{source, currentTime} diff --git a/main.go b/main.go index 514c5fa8..959367c6 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,19 @@ import ( "errors" "fmt" "reflect" + "time" ) +// NowFunc returns current time, this function is exported in order to be able +// to give the flexiblity to the developer to costumize it accoring to their +// needs +// +// e.g: return time.Now().UTC() +// +var NowFunc = func() time.Time { + return time.Now() +} + type DB struct { Value interface{} Error error diff --git a/main_private.go b/main_private.go index b9326360..2353b43f 100644 --- a/main_private.go +++ b/main_private.go @@ -56,6 +56,6 @@ func (s *DB) log(v ...interface{}) { func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { if s.logMode == 2 { - s.print("sql", fileWithLineNum(), time.Now().Sub(t), sql, vars) + s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars) } } diff --git a/scope.go b/scope.go index bf17e704..cbbbeba9 100644 --- a/scope.go +++ b/scope.go @@ -349,7 +349,7 @@ func (scope *Scope) Raw(sql string) *Scope { // Exec invoke sql func (scope *Scope) Exec() *Scope { - defer scope.Trace(time.Now()) + defer scope.Trace(NowFunc()) if !scope.HasError() { result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...) diff --git a/scope_private.go b/scope_private.go index 493742da..aa75346f 100644 --- a/scope_private.go +++ b/scope_private.go @@ -9,7 +9,6 @@ import ( "regexp" "strconv" "strings" - "time" ) func (scope *Scope) primaryCondiation(value interface{}) string { @@ -368,13 +367,13 @@ func (scope *Scope) sqlTagForField(field *Field) (typ string) { } func (scope *Scope) row() *sql.Row { - defer scope.Trace(time.Now()) + defer scope.Trace(NowFunc()) scope.prepareQuerySql() return scope.DB().QueryRow(scope.Sql, scope.SqlVars...) } func (scope *Scope) rows() (*sql.Rows, error) { - defer scope.Trace(time.Now()) + defer scope.Trace(NowFunc()) scope.prepareQuerySql() return scope.DB().Query(scope.Sql, scope.SqlVars...) }