Timestamp should always be saved in UTC.
Signed-off-by: Phan Le <ple@pivotallabs.com>
This commit is contained in:
parent
0156c26cfa
commit
9b869d7376
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
21
main_test.go
21
main_test.go
@ -574,9 +574,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
|
||||
@ -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) {
|
||||
if s.Code == "Invalid" {
|
||||
err = errors.New("invalid product")
|
||||
|
Loading…
x
Reference in New Issue
Block a user