datatypes

package module
v0.0.0-...-6a7eb5f Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 9 Imported by: 0

README

GORM Data Types Time Date

Date

import "github.com/XpamAmAdEuS/gormdatatypes"

type UserWithDate struct {
	gorm.Model
	Name string
	Date datatypes.Date
}

user := UserWithDate{Name: "jinzhu", Date: datatypes.Date(time.Now())}
DB.Create(&user)
// INSERT INTO `user_with_dates` (`name`,`date`) VALUES ("jinzhu","2020-07-17 00:00:00")

DB.First(&result, "name = ? AND date = ?", "jinzhu", datatypes.Date(curTime))
// SELECT * FROM user_with_dates WHERE name = "jinzhu" AND date = "2020-07-17 00:00:00" ORDER BY `user_with_dates`.`id` LIMIT 1

Time

MySQL, PostgreSQL, SQLite, SQLServer are supported.

Time with nanoseconds is supported for some databases which support for time with fractional second scale.

import "github.com/XpamAmAdEuS/gormdatatypes"

type UserWithTime struct {
    gorm.Model
    Name string
    Time datatypes.Time
}

user := UserWithTime{Name: "jinzhu", Time: datatypes.NewTime(1, 2, 3, 0)}
DB.Create(&user)
// INSERT INTO `user_with_times` (`name`,`time`) VALUES ("jinzhu","01:02:03")

DB.First(&result, "name = ? AND time = ?", "jinzhu", datatypes.NewTime(1, 2, 3, 0))
// SELECT * FROM user_with_times WHERE name = "jinzhu" AND time = "01:02:03" ORDER BY `user_with_times`.`id` LIMIT 1

NOTE: If the current using database is SQLite, the field column type is defined as TEXT type when GORM AutoMigrate because SQLite doesn't have time type.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date time.Time

func (*Date) GobDecode

func (date *Date) GobDecode(b []byte) error

func (Date) GobEncode

func (date Date) GobEncode() ([]byte, error)

func (Date) GormDataType

func (date Date) GormDataType() string

GormDataType gorm common data type

func (Date) MarshalJSON

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

func (*Date) Scan

func (date *Date) Scan(value interface{}) (err error)

func (*Date) UnmarshalJSON

func (date *Date) UnmarshalJSON(b []byte) error

func (Date) Value

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

type Time

type Time time.Duration

Time is time data type.

func NewTime

func NewTime(hour, min, sec, nsec int) Time

NewTime is a constructor for Time and returns new Time.

func (Time) Date

func (t Time) Date(value string) time.Time

func (Time) GormDBDataType

func (Time) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType returns gorm DB data type based on the current using database.

func (Time) GormDataType

func (Time) GormDataType() string

GormDataType returns gorm common data type. This type is used for the field's column type.

func (Time) Hour

func (t Time) Hour() int

func (Time) IsAfter

func (t Time) IsAfter(be Time) bool

func (Time) IsBefore

func (t Time) IsBefore(be Time) bool

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler to convert Time to json serialization.

func (Time) MinusMinutes

func (t Time) MinusMinutes(amount int) Time

func (Time) MinusSeconds

func (t Time) MinusSeconds(amount int) Time

func (Time) Minute

func (t Time) Minute() int

func (Time) Nanoseconds

func (t Time) Nanoseconds() int

func (Time) PlusMinutes

func (t Time) PlusMinutes(amount int) Time

func (Time) PlusSeconds

func (t Time) PlusSeconds(amount int) Time

func (*Time) Scan

func (t *Time) Scan(src interface{}) error

Scan implements sql.Scanner interface and scans value into Time,

func (Time) Seconds

func (t Time) Seconds() int

func (Time) String

func (t Time) String() string

String implements fmt.Stringer interface.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler to deserialize json data.

func (Time) Value

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

Value implements driver.Valuer interface and returns string format of Time.

Jump to

Keyboard shortcuts

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