Skip to content

lovung/date

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Date Type Support in Golang

Go Goreport codecov

  • Date type in Golang
  • Unit testing
  • NullDate support
  • Some methods to working with Date type

Usage

const RFC3339Date = "2006-01-02"

RFC3339Date format

func RemoveTime

func RemoveTime(t time.Time) time.Time

RemoveTime removes the time values of time.Time at UTC Ex: 2020-01-01T15:30:30Z07:00 -> 2020-01-01T00:00:00Z

type Date

type Date time.Time

Date is the non-nullable type for Date only. Support UTC timezone only

func MustParse

func MustParse(s string) Date

MustParse creates a new Date from the RFC3339 Date - "2006-01-02" Panic if wrong format

func New

func New(t time.Time) Date

New creates a new Date

func NewDate

func NewDate(year int, month time.Month, day int) Date

NewDate from year, month and day

func NewFromStr

func NewFromStr(s string) (Date, error)

NewFromStr creates a new Date from the RFC3339 Date - "2006-01-02"

func NewZero

func NewZero() Date

NewZero creates the new zero (null) Date

func (Date) AddDate

func (d Date) AddDate(year, month, day int) Date

AddDate returns the Date corresponding to adding the given number of years, months, and days to d. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

func (Date) AddMonths

func (d Date) AddMonths(n int) Date

AddMonths return add the number of months. If the day at this month > maxDay (like 30 for Feb), it will be the last day of this month

func (Date) After

func (d Date) After(ref Date) bool

After compares date if it's after or not

func (Date) Before

func (d Date) Before(ref Date) bool

Before compares date if it's before or not

func (Date) Day

func (d Date) Day() int

Day returns the day of the NullDate

func (Date) DaysOfMonth

func (d Date) DaysOfMonth() int

DaysOfMonth returns total days in the month of the Date

func (Date) DiffMonths

func (d Date) DiffMonths(ref Date) int

DiffMonths to calculate the diff of months If date.After(ref) -> Return negative If date.Before(ref) -> Return positive Id date.Equal(ref) -> Return 1 a.DiffMonths(b) may != b.DiffMonths(a) because of the last day of month

func (Date) Equal

func (d Date) Equal(ref Date) bool

Equal to compare with another

func (Date) GormDataType

func (d Date) GormDataType() string

GormDataType gorm common data type

func (Date) IsZero

func (d Date) IsZero() bool

IsZero check if it is the zero value

func (Date) MarshalJSON

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

MarshalJSON marshal to the JSON

func (Date) Month

func (d Date) Month() time.Month

Month returns the month of the Date

func (*Date) Scan

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

Scan implements the Scanner interface

func (Date) String

func (d Date) String() string

Strong converts the Date to string as RFC3339 format

func (Date) Sub

func (d Date) Sub(ref Date) time.Duration

Sub return duration date - ref

func (Date) ToTime

func (d Date) ToTime() time.Time

ToTime converts the Date type to time.Time type

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(bs []byte) error

UnmarshalJSON to parse the JSON

func (Date) Value

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

Value implements the driver Valuer interface

func (Date) YMD

func (d Date) YMD() (year int, month time.Month, day int)

YMD returns year, month and day of the Date

func (Date) Year

func (d Date) Year() int

Year returns the year of the Date