formatime

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2022 License: MIT Imports: 5 Imported by: 0

README

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"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomTime

type CustomTime struct {
	Format string
	// contains filtered or unexported fields
}

CustomTime is a nullable time.Time. It supports SQL, JSON serialization AND custom format. It will marshal to null if null.

func NewCustomTime

func NewCustomTime(s int64, format string) CustomTime

new custom format of time, s is timestamp examples: ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000"

func NewCustomTimeFrom

func NewCustomTimeFrom(t time.Time, format string) CustomTime

new custom format of time from time.Time

func NewCustomTimeNow

func NewCustomTimeNow(format string) CustomTime

new custom format of time right now

func (CustomTime) IsZero

func (t CustomTime) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (CustomTime) MarshalJSON

func (t CustomTime) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (CustomTime) MarshalText

func (t CustomTime) MarshalText() ([]byte, error)

Marshal

func (*CustomTime) Scan

func (t *CustomTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*CustomTime) UnmarshalJSON

func (t *CustomTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*CustomTime) UnmarshalText

func (t *CustomTime) UnmarshalText(text []byte) error

func (CustomTime) Value

func (t CustomTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Date

type Date struct {
	// contains filtered or unexported fields
}

Date is a nullable time.Time. It supports SQL, JSON serialization AND datetime format(2006-01-02). It will marshal to null if null.

func NewDate

func NewDate(s int64) Date

new Date format of time, s is timestamp

func NewDateFrom

func NewDateFrom(t time.Time) Date

new Date format of time from time.Time

func NewDateNow

func NewDateNow() Date

new Date format of time right now

func (Date) IsZero

func (t Date) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Date) MarshalJSON

func (t Date) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Date) MarshalText

func (t Date) MarshalText() ([]byte, error)

Marshal

func (*Date) Scan

func (t *Date) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Date) UnmarshalJSON

func (t *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Date) UnmarshalText

func (t *Date) UnmarshalText(text []byte) error

func (Date) Value

func (t Date) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Hour

type Hour struct {
	// contains filtered or unexported fields
}

Hour is a nullable time.Time. It supports SQL, JSON serialization AND datetime format(2006-01-02 15). It will marshal to null if null.

func NewHour

func NewHour(s int64) Hour

new Hour format of time, s is timestamp

func NewHourFrom

func NewHourFrom(t time.Time) Hour

new Hour format of time from time.Time

func NewHourNow

func NewHourNow() Hour

new Hour format of time right now

func (Hour) IsZero

func (t Hour) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Hour) MarshalJSON

func (t Hour) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Hour) MarshalText

func (t Hour) MarshalText() ([]byte, error)

Marshal

func (*Hour) Scan

func (t *Hour) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Hour) UnmarshalJSON

func (t *Hour) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Hour) UnmarshalText

func (t *Hour) UnmarshalText(text []byte) error

func (Hour) Value

func (t Hour) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Minute

type Minute struct {
	// contains filtered or unexported fields
}

Minute is a nullable time.Time. It supports SQL, JSON serialization AND datetime format(2006-01-02 15). It will marshal to null if null.

func NewMinute

func NewMinute(s int64) Minute

new Minute format of time, s is timestamp

func NewMinuteFrom

func NewMinuteFrom(t time.Time) Minute

new Minute format of time from time.Time

func NewMinuteNow

func NewMinuteNow() Minute

new Minute format of time right now

func (Minute) IsZero

func (t Minute) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Minute) MarshalJSON

func (t Minute) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Minute) MarshalText

func (t Minute) MarshalText() ([]byte, error)

Marshal

func (*Minute) Scan

func (t *Minute) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Minute) UnmarshalJSON

func (t *Minute) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Minute) UnmarshalText

func (t *Minute) UnmarshalText(text []byte) error

func (Minute) Value

func (t Minute) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Second

type Second struct {
	// contains filtered or unexported fields
}

Second is a nullable time.Time. It supports SQL, JSON serialization AND datetime format(2006-01-02 15:04:05). It will marshal to null if null.

func NewSecond

func NewSecond(s int64) Second

new Second format of time, s is timestamp

func NewSecondFrom

func NewSecondFrom(t time.Time) Second

new Second format of time from time.Time

func NewSecondNow

func NewSecondNow() Second

new Second format of time right now

func (Second) IsZero

func (t Second) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Second) MarshalJSON

func (t Second) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Second) MarshalText

func (t Second) MarshalText() ([]byte, error)

Marshal

func (*Second) Scan

func (t *Second) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Second) UnmarshalJSON

func (t *Second) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Second) UnmarshalText

func (t *Second) UnmarshalText(text []byte) error

func (Second) Value

func (t Second) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Timestamp

type Timestamp struct {
	// contains filtered or unexported fields
}

Timestamp is a nullable time.Time. It supports SQL, JSON serialization AND timestamp format. It will marshal to null if null.

func NewTimestamp

func NewTimestamp(s int64) Timestamp

new timestamp format of time

func NewTimestampFrom

func NewTimestampFrom(t time.Time) Timestamp

new timestamp format of time from time.Time

func NewTimestampNow

func NewTimestampNow() Timestamp

new timestamp format of time right now

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Timestamp) MarshalText

func (t Timestamp) MarshalText() ([]byte, error)

Marshal

func (*Timestamp) Scan

func (t *Timestamp) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Timestamp) UnmarshalText

func (t *Timestamp) UnmarshalText(text []byte) error

func (Timestamp) Value

func (t Timestamp) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL