Created

Created

Created tag will automatically inserted current time when you insert one record. To use this feature, you should use created tag at the related field.The field type could be time.Time, int, int64 and etc.

  1. type User struct {
  2. Id int64
  3. Name string
  4. CreatedAt time.Time `xorm:"created"`
  5. }

Or

  1. type JSONTime time.Time
  2. func (j JSONTime) MarshalJSON() ([]byte, error) {
  3. return []byte(`"`+time.Time(j).Format("2006-01-02 15:04:05")+`"`), nil
  4. }
  5. type User struct {
  6. Id int64
  7. Name string
  8. CreatedAt JSONTime `xorm:"created"`
  9. }

Or

  1. type User struct {
  2. Id int64
  3. Name string
  4. CreatedAt int64 `xorm:"created"`
  5. }

When Insert() or InsertOne() be called, user.CreatedAt will be filled with time.Now() or time.Now().Unix(). For example,

  1. var user User
  2. engine.Insert(&user)
  3. // INSERT user (created...) VALUES (?...)

The last question is the time zone. Defaultly, xorm use Local time zone. so when time.Now() will be filled into record. The time will be convert local time zone first. So if you want to change the time zone of xorm, you can,

  1. engine.TZLocation, _ = time.LoadLocation("Asia/Shanghai")