Skip to content

Commit

Permalink
gorm: added ability to change the time.now format
Browse files Browse the repository at this point in the history
  • Loading branch information
Cihangir SAVAS committed Aug 23, 2014
1 parent 39ac95a commit 4e90fbf
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 19 deletions.
5 changes: 2 additions & 3 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gorm
import (
"fmt"
"strings"
"time"
)

func BeforeCreate(scope *Scope) {
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions callback_delete.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package gorm

import (
"fmt"
"time"
)
import "fmt"

func BeforeDelete(scope *Scope) {
scope.CallMethod("BeforeDelete")
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gorm
import (
"fmt"
"strings"
"time"
)

func AssignUpdateAttributes(scope *Scope) {
Expand Down Expand Up @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion doc/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
5 changes: 2 additions & 3 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"regexp"
"strconv"
"strings"
"time"
)

func (scope *Scope) primaryCondiation(value interface{}) string {
Expand Down Expand Up @@ -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...)
}
Expand Down

0 comments on commit 4e90fbf

Please sign in to comment.