diff --git a/callback_create.go b/callback_create.go index d5e56267..17592bbc 100644 --- a/callback_create.go +++ b/callback_create.go @@ -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()) } } diff --git a/callback_delete.go b/callback_delete.go index 3bf5b7b0..c1e69dd3 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -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 { diff --git a/callback_update.go b/callback_update.go index 095bbf48..4bacaf04 100644 --- a/callback_update.go +++ b/callback_update.go @@ -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()) } } diff --git a/main_test.go b/main_test.go index 1df4558a..16d135de 100644 --- a/main_test.go +++ b/main_test.go @@ -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")