Merge 9b869d7376a9f2dbb3d9865b7eaaf790264bfa26 into dc2f27401eb44bb575423b467709f8f529dd9002

This commit is contained in:
phanle 2014-03-15 02:50:32 +00:00
commit 71f9b480da
4 changed files with 23 additions and 6 deletions

View File

@ -13,8 +13,8 @@ func BeforeCreate(scope *Scope) {
func UpdateTimeStampWhenCreate(scope *Scope) {
if !scope.HasError() {
scope.SetColumn("CreatedAt", time.Now())
scope.SetColumn("UpdatedAt", time.Now())
scope.SetColumn("CreatedAt", time.Now().UTC())
scope.SetColumn("UpdatedAt", time.Now().UTC())
}
}

View File

@ -15,7 +15,7 @@ func Delete(scope *Scope) {
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.TableName(),
scope.AddToVars(time.Now()),
scope.AddToVars(time.Now().UTC()),
scope.CombinedConditionSql(),
))
} else {

View File

@ -36,7 +36,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", time.Now().UTC())
}
}

View File

@ -575,9 +575,10 @@ func TestCount(t *testing.T) {
}
}
func TestCreatedAtAndUpdatedAt(t *testing.T) {
name := "check_created_at_and_updated_at"
func TestCreatedAtAndUpdatedAtUpdateCorrectly(t *testing.T) {
name := "check_created_at_and_updated_at_update_correctly"
u := User{Name: name, Age: 1}
db.Save(&u)
created_at := u.CreatedAt
updated_at := u.UpdatedAt
@ -603,6 +604,22 @@ func TestCreatedAtAndUpdatedAt(t *testing.T) {
}
}
func TestCreatedAtAndUpdatedAtAreSavedAsUTC(t *testing.T) {
name := "check_created_at_and_updated_at_saved_as_utc"
u := User{Name: name, Age: 1}
db.Save(&u)
created_at := u.CreatedAt
updated_at := u.UpdatedAt
if created_at.Location() != time.UTC {
t.Errorf("created_at should be UTC")
}
if updated_at.Location() != time.UTC {
t.Errorf("updated_at should be UTC")
}
}
func (s *Product) BeforeCreate() (err error) {
if s.Code == "Invalid" {
err = errors.New("invalid product")