1024programmer Java Detailed explanation of formatting time problem in golanggorm

Detailed explanation of formatting time problem in golanggorm

Foreword

I recently encountered a problem while developing a project,

Although gorm can automatically help you maintain key time fields such as created_at, updated_at, and deleted_at. But its principles and drawbacks need to be understood.

1. How to use

Control the time format by customizing a localtime structure

 package utils
 import (
  "time"
  //"strconv"
  "fmt"
  "database/sql/driver"
  "strconv"
 )
 typeLocalTime struct {
  time.Time
 }
 func (t LocalTime) MarshalJSON() ([]byte, error) {
  //Format seconds
  seconds := t.Unix()
  return []byte(strconv.FormatInt(seconds, 10)), nil
 }
 func (t LocalTime) Value() (driver.Value, error) {
  var zeroTime time.Time
  if t.Time.UnixNano() == zeroTime.UnixNano() {
  return nil, nil
  }
  return t.Time, nil
 }
 func (t *LocalTime) Scan(v interface{}) error {
  value, ok := v.(time.Time)
  if ok {
  *t = LocalTime{Time: value}
  return nil
  }
  return fmt.Errorf("can not convert %v to timestamp", v)
 }

The field structure of dao at this time is

 type TestDao struct{
    Id uint `gorm:"primary_key,AUTO_INCREMENT" json:"id"`
  CreatedAt LocalTime `json:"-"`
  UpdatedAt LocalTime `json:"update_at"`
  DeletedAt *LocalTime `json:"-"`
 }

2. Implementation principle

The way it is implemented is to define it as the current time through the callback function when the save changes. For articles, please refer to Portal

This way you can control the time format through custom LocalTime.

3. Disadvantages and Suggestions

Because when the program is running, the type of fields such as createAt is still LocalTime, so if you want to copy it yourself, it is not easy to do it.

For example, you want to change the createAt time while the program is running. You can’t! Because its type is LocalTime and your time is either a timestamp or a string, the types don’t match. . . Isn’t it embarrassing? ? ?

So it is recommended that such reserved fields should not be modified while the program is running. Just use it as a record or identification. If you really need to change the time, maintain the content of the field yourself. For example, use int to store timestamps or string to store strings. Then every time it changes, modify its value.

Of course, you can also encapsulate this work into a callback function, so that you can control this field at will. Please refer to the content in the portal above.

So, what I want to complain about is that gorm’s implementation of time formatting is too inhumane!

Summary

The above is the entire content of this article. I hope that the content of this article has certain reference and learning value for everyone’s study or work. Thank you for your support.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/784273

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索