mysqltime

package module
v0.0.0-...-32ec082 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 6 Imported by: 0

README

mysqltime

motivation

MySQL has the TIME type which supports storing time data.

According to its spec, it is said that

TIME values may range from '-838:59:59' to '838:59:59'.
The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours),
but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

Because of its spec, it is hard to unmarshal to primitive golang data structures.

https://github.com/go-sql-driver/mysql/issues/849

Therefore, it would be really great to introduce custom data type mysqltime.Time which automatically marshal/unmarshal data of mysql TIME type implementing mysql driver interface.

It can be either time of day or duration, but duration can cover all use case scenarioes.

interface

type Time interface {
    SetDuration(time.Duration) error
    GetDuration() (time.Duration, bool) // data type is nullable
}

expected use cases

assume some users' working hour data


type WorkHour struct {
    UserID        int64          `db:"user_id"`
    WorkHourStart mysqltime.Time `db:"work_hour_start"`
    WorkHourEnd   mysqltime.Time `db:"work_hour_end"`
}

...

// sqlx style
rows, err := db.NamedQuery(`SELECT * FROM work_hour WHERE user_id = ?`, userID)

references

https://github.com/jackc/pgtype

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Time

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

Time represents a custom type for handling MySQL TIME data.

func NewTime

func NewTime(d time.Duration) Time

NewTime creates a new mysqltime.Time from a time.Duration.

func (Time) GetDuration

func (t Time) GetDuration() (time.Duration, bool)

GetDuration returns the time.Duration and a boolean indicating if the value is valid.

func (Time) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface.

func (*Time) Scan

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

Scan implements the sql.Scanner interface for database deserialization.

func (*Time) SetDuration

func (t *Time) SetDuration(d time.Duration) error

SetDuration sets the Time based on a time.Duration.

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Time) Value

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

Value implements the driver.Valuer interface for database serialization.

Jump to

Keyboard shortcuts

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