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.

type User struct {
    Id int64
    Name string
    CreatedAt time.Time `xorm:"created"`
}

Or

type JSONTime time.Time
func (j JSONTime) MarshalJSON() ([]byte, error) {
    return []byte(`"`+time.Time(j).Format("2006-01-02 15:04:05")+`"`), nil
}

type User struct {
    Id int64
    Name string
    CreatedAt JSONTime `xorm:"created"`
}

Or

type User struct {
    Id int64
    Name string
    CreatedAt int64 `xorm:"created"`
}

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

var user User
engine.Insert(&user)
// 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,

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