Documentation ¶
Index ¶
- type Date
- func (d Date) After(d2 Date) bool
- func (d Date) Before(d2 Date) bool
- func (d Date) Day() int
- func (d Date) Equal(d2 Date) bool
- func (d Date) Format(layout string) string
- func (d Date) IsZero() bool
- func (d Date) MarshalText() ([]byte, error)
- func (d Date) Month() time.Month
- func (d *Date) Scan(v interface{}) (err error)
- func (d Date) String() string
- func (d Date) Sub(d2 Date) int
- func (d *Date) UnmarshalText(data []byte) (err error)
- func (d Date) Value() (driver.Value, error)
- func (d Date) Year() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Date ¶
type Date struct {
// contains filtered or unexported fields
}
Date specifies a date without time (only year, month and day). Internally it represents a time.Time instant with zero UTC time parts. The zero value of the struct is represented as "0001-01-01".
func NewDate ¶
NewDate creates a new date instance from the provided year, month and day.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d1 := date.NewDate(2020, time.January, 15) d2 := date.NewDate(2020, time.January, 32) // auto normalized fmt.Println(d1) fmt.Println(d2) }
Output: 2020-01-15 2020-02-01
func Parse ¶
Parse parses a formatted string and returns the date value it represents. The string must be in in ISO 8601 extended format (e.g. "2006-01-02").
Example ¶
package main import ( "fmt" "github.com/ganigeorgiev/date" ) func main() { d, _ := date.Parse("2020-01-01") fmt.Println(d) }
Output: 2020-01-01
func (Date) After ¶
After reports whether the date instant d is after d2.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d1 := date.NewDate(2020, time.January, 1) d2 := date.NewDate(2020, time.February, 1) fmt.Println(d2.After(d1)) }
Output: true
func (Date) Before ¶
Before reports whether the date instant d is before d2.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d1 := date.NewDate(2020, time.January, 1) d2 := date.NewDate(2020, time.February, 1) fmt.Println(d1.Before(d2)) }
Output: true
func (Date) Day ¶
Day returns the day of the month specified by d.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d := date.NewDate(2020, time.January, 1) fmt.Println(d.Day()) }
Output: 1
func (Date) Equal ¶
Equal reports whether the date instant d is equal to d2.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d1 := date.NewDate(2020, time.February, 1) d2 := date.NewDate(2020, time.January, 32) fmt.Println(d1.Equal(d2)) }
Output: true
func (Date) Format ¶ added in v1.1.0
Format returns a textual representation of the date value formatted according to the specified layout (the same as in time.Time.Format).
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d := date.NewDate(2020, time.December, 1) s := d.Format("02 Jan 2006, Monday") fmt.Println(s) }
Output: 01 Dec 2020, Tuesday
func (Date) IsZero ¶
IsZero reports whether d represents the zero date instant, year 1, January 1 ("0001-01-01").
Example ¶
package main import ( "fmt" "github.com/ganigeorgiev/date" ) func main() { d1 := date.Date{} d2, _ := date.Parse("0001-01-01") d3, _ := date.Parse("2020-01-01") fmt.Println(d1.IsZero()) fmt.Println(d2.IsZero()) fmt.Println(d3.IsZero()) }
Output: true true false
func (Date) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The date is given in ISO 8601 extended format (e.g. "2006-01-02").
func (Date) Month ¶
Month returns the month of the year specified by t.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d := date.NewDate(2020, time.January, 1) fmt.Println(d.Month()) }
Output: January
func (*Date) Scan ¶
Scan parses a value (usually from db). It implements sql.Scanner, https://golang.org/pkg/database/sql/#Scanner.
func (Date) String ¶
String returns a string representing the date instant in ISO 8601 extended format (e.g. "2006-01-02").
func (Date) Sub ¶
Sub returns the duration d-d2 in days.
Example ¶
package main import ( "fmt" "time" "github.com/ganigeorgiev/date" ) func main() { d1 := date.NewDate(2020, time.January, 1) d2 := date.NewDate(2020, time.January, 15) fmt.Println(d2.Sub(d1)) }
Output: 14
func (*Date) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The date is expected to be in ISO 8601 extended format (e.g. "2006-01-02").
func (Date) Value ¶
Value converts the instant to a time.Time. It implements driver.Valuer, https://golang.org/pkg/database/sql/driver/#Valuer.