date

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2020 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Date Go Report Card GoDoc

Date is a small Go package that defines a date.Date struct, representing a date as in ISO 8601 (eg. 2006-01-02).

The package was created primarily to scan SQL DATE type and other date-only values.

Under the hood, each date is stored as time.Time instant with zero time part in UTC (eg. 2006-01-02 00:00:00:00 UTC). As a result, many of the date.Date methods are implemented using the corresponding methods of time.Time.

There is an active proposal for something similar to be implemented in the standard library - golang/go#19700

Installation

go get github.com/ganigeorgiev/date

Example usage:

import github.com/ganigeorgiev/date

type User struct {
    JoinDate date.Date
}

See the package documentation for more details and examples.

Documentation

Index

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

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

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

func Parse(s string) (Date, error)

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

func (d Date) After(d2 Date) bool

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

func (d Date) Before(d2 Date) bool

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

func (d Date) Day() int

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

func (d Date) Equal(d2 Date) bool

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

func (d Date) Format(layout string) string

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

func (d Date) IsZero() bool

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

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

MarshalText implements the encoding.TextMarshaler interface. The date is given in ISO 8601 extended format (e.g. "2006-01-02").

func (Date) Month

func (d Date) Month() time.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

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

Scan parses a value (usually from db). It implements sql.Scanner, https://golang.org/pkg/database/sql/#Scanner.

func (Date) String

func (d Date) String() string

String returns a string representing the date instant in ISO 8601 extended format (e.g. "2006-01-02").

func (Date) Sub

func (d Date) Sub(d2 Date) int

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

func (d *Date) UnmarshalText(data []byte) (err error)

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

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

Value converts the instant to a time.Time. It implements driver.Valuer, https://golang.org/pkg/database/sql/driver/#Valuer.

func (Date) Year

func (d Date) Year() int

Year returns the year in which d occurs.

Example
package main

import (
	"fmt"
	"time"

	"github.com/ganigeorgiev/date"
)

func main() {
	d := date.NewDate(2020, time.January, 1)

	fmt.Println(d.Year())
}
Output:

2020

Jump to

Keyboard shortcuts

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