You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
杨赟 8e7a9e733d
init
2 years ago
.gitignore init 2 years ago
LICENSE init 2 years ago
README.cn.md init 2 years ago
README.md init 2 years ago
customize.go init 2 years ago
customize_test.go init 2 years ago
date.go init 2 years ago
hour.go init 2 years ago
minute.go init 2 years ago
second.go init 2 years ago
timestamp.go init 2 years ago
timestamp_test.go init 2 years ago
type.go init 2 years ago

README.md

GoDoc

formatime

中文

Customize the format of the time in golang, can be used in conjunction with the gorm, no need to use sql.nullTime, and can return the specified time format in the json serialization of the struct

Installation

go get github.com/golangkit/formatime

Usage


type foo struct {
    ID        int64              `gorm:"column:id"`
	CreatedAt formatime.Date `gorm:"column:created_at" json:"created_at"`
	UpdatedAt formatime.Timestamp `gorm:"column:updated_at" json:"updated_at"`
	DeletedAt formatime.CustomTime `gorm:"column:deleted_at" json:"deleted_at"`
}

func Test_json(t *testing.T) {
	b := foo{
		CreatedAt: formatime.NewDateNow(),
		UpdatedAt: formatime.NewTimestampNow(),
		DeletedAt: formatime.NewCustomTimeNow("Jan _2 15:04:05"),
	}

	text, err := json.Marshal(&b)
	if err != nil {
		t.Fatal(err)
	} else {
		log.Print(string(text))
	}
}

func Test_gorm(t *testing.T) {
	b := &foo{}
    	gorm.DB.First(b)
	text, err := json.Marshal(&b)
	if err != nil {
		t.Fatal(err)
	} else {
		log.Print(string(text))
	}
}
// log 
{
    "created_at":"2019-10-29",
    "updated_at":"1572312916",
    "deleted_at":"Oct 29 09:35:16"
}