Skip to content

Commit

Permalink
Remove nanoseconds when touching updated at (#415)
Browse files Browse the repository at this point in the history
* Remove nanoseconds when touching created and updated at

When you update a model, the `updated_at` field contains nanoseconds. But these nanoseconds are not stored in the database. So when you fetch the model again from the database does not contain the nanoseconds. This can lead to weird situations for clients.

For example, a iOS app sends a PUT request to update a certain model and stores the result. The `updated_at` field is `2019-07-20T10:36:19.819375Z`. Later, when re-fetching the result from the API it returns a different response (because the `updated_at` field is now `2019-07-20T10:36:19Z`). This leads to weird situations. Also, Swift doesn't understand the nanoseconds.

* Use time.Truncate instead of time.Unix trick
  • Loading branch information
ruudk authored and stanislas-m committed Aug 15, 2019
1 parent caa68d3 commit 77a6d94
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (m *Model) setID(i interface{}) {
func (m *Model) touchCreatedAt() {
fbn, err := m.fieldByName("CreatedAt")
if err == nil {
now := nowFunc()
now := nowFunc().Truncate(time.Microsecond)
v := fbn.Interface()
if !IsZeroOfUnderlyingType(v) {
// Do not override already set CreatedAt
Expand All @@ -162,7 +162,7 @@ func (m *Model) touchCreatedAt() {
func (m *Model) touchUpdatedAt() {
fbn, err := m.fieldByName("UpdatedAt")
if err == nil {
now := nowFunc()
now := nowFunc().Truncate(time.Microsecond)
v := fbn.Interface()
switch v.(type) {
case int, int64:
Expand Down

0 comments on commit 77a6d94

Please sign in to comment.