Timestamp should always be saved in UTC.

Signed-off-by: Phan Le <ple@pivotallabs.com>
This commit is contained in:
Ted Young 2014-02-21 14:20:43 -08:00 committed by Phan Le
parent 0156c26cfa
commit 9b869d7376
4 changed files with 23 additions and 6 deletions

View File

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

View File

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

View File

@ -36,7 +36,7 @@ func BeforeUpdate(scope *Scope) {
func UpdateTimeStampWhenUpdate(scope *Scope) { func UpdateTimeStampWhenUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column") _, ok := scope.Get("gorm:update_column")
if !ok { if !ok {
scope.SetColumn("UpdatedAt", time.Now()) scope.SetColumn("UpdatedAt", time.Now().UTC())
} }
} }

View File

@ -574,9 +574,10 @@ func TestCount(t *testing.T) {
} }
} }
func TestCreatedAtAndUpdatedAt(t *testing.T) { func TestCreatedAtAndUpdatedAtUpdateCorrectly(t *testing.T) {
name := "check_created_at_and_updated_at" name := "check_created_at_and_updated_at_update_correctly"
u := User{Name: name, Age: 1} u := User{Name: name, Age: 1}
db.Save(&u) db.Save(&u)
created_at := u.CreatedAt created_at := u.CreatedAt
updated_at := u.UpdatedAt updated_at := u.UpdatedAt
@ -602,6 +603,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) { func (s *Product) BeforeCreate() (err error) {
if s.Code == "Invalid" { if s.Code == "Invalid" {
err = errors.New("invalid product") err = errors.New("invalid product")