date

package module
v0.0.0-...-555522f Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2020 License: MIT Imports: 6 Imported by: 0

README

date GoDoc

Golang's missing date package, including ranges

Date builds on Golang's time.Time package to provide a ISO 8601 Date type

Create a new Date:

import "github.com/aodin/date"

func main() {
    march1st := date.New(2015, 3, 1)
    fmt.Println(march1st) // 2015-03-01
}

Parse a date or build it from a time:

date.Parse("2015-03-01")
date.FromTime(time.Now())

Ranges, including Union and Intersection operations:

date.NewRange(date.Today(), date.Today().AddDays(7))
date.EntireYear(2014).Union(date.EntireYear(2015))

By default, the Date type uses the time.UTC location. It can be passed to functions requiring the time.Time type using the embedded Time field:

jan1 := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
jan1.Before(march1.Time)

Happy Hacking!

aodin, 2015-2016

Documentation

Index

Constants

View Source
const ISO8601Date = "2006-01-02"

ISO8601Date uses ISO 8601 as a default for parsing and rendering

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct{ time.Time }

func FromTime

func FromTime(t time.Time) Date

FromTime creates a Date from a time.Time

func New

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

New creates a new Date

func Parse

func Parse(value string) (Date, error)

Parse converts a ISO 8601 date string to a Date, possibly returning an error

func ParseUsingLayout

func ParseUsingLayout(format, value string) (Date, error)

ParseUsingLayout calls Parse with a different date layout

func Today

func Today() Date

Today converts the local time to a Date

func (Date) AddDate

func (date Date) AddDate(years, months, days int) Date

AddDate adds any number of years, months, and days to the date. It proxies to the embedded time.Time, but returns a Date

func (Date) AddDays

func (date Date) AddDays(days int) Date

AddDays adds the given number of days to the date

func (Date) After

func (date Date) After(other Date) bool

After returns true if the given date is after (and not equal) to the current date

func (Date) Before

func (date Date) Before(other Date) bool

Before returns true if the given date is before (and not equal) to the current date

func (Date) Equals

func (date Date) Equals(other Date) bool

Equals returns true if the dates are equal

func (Date) MarshalJSON

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

MarshalJSON returns the JSON output of a Date. Null will return a zero value date.

func (*Date) Scan

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

Scan converts an SQL value into a Date

func (Date) String

func (date Date) String() string

String returns the Date as a string

func (*Date) UnmarshalJSON

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

UnmarshalJSON converts a byte array into a Date

func (Date) Value

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

Value returns the date formatted for insert into PostgreSQL

func (Date) Within

func (date Date) Within(term Range) bool

Within returns true if the Date is within the Range - inclusive

type Range

type Range struct {
	Start Date `json:"start"`
	End   Date `json:"end"`
	// contains filtered or unexported fields
}

Range is a start and end date

func Empty

func Empty() (term Range)

Empty creates an empty Range

func EntireMonth

func EntireMonth(year int, month time.Month) (term Range)

EntireMonth creates a Range that includes the entire month

func EntireYear

func EntireYear(year int) (term Range)

EntireYear creates a Range that includes the entire year

func Forever

func Forever() (term Range)

Forever creates a Range without a start or end date

func Infinity

func Infinity() Range

Infinity is an alias for Forever

func Never

func Never() Range

Never is an alias for Empty

func NewRange

func NewRange(start, end Date) (term Range)

NewRange creates a Range with the given start and end dates

func OnlyToday

func OnlyToday() Range

func SingleDay

func SingleDay(date Date) Range

func StartBoundedRange

func StartBoundedRange(start Date) (term Range)

func (Range) Contains

func (term Range) Contains(other Range) bool

Contains returns true if the given range is entirely within the the range - inclusive

func (Range) Days

func (term Range) Days() int

Days returns the number of days in the range zero will be returned for infinity TODO or -1

func (Range) DoesNotContain

func (term Range) DoesNotContain(other Range) bool

func (Range) Equals

func (term Range) Equals(other Range) bool

Equals returns true if the term has equal start and end dates

func (Range) Error

func (term Range) Error() error

Error returns an error if there is both a start and end date and the given start date is not before the end date.

func (Range) Intersection

func (term Range) Intersection(other Range) (intersect Range)

Intersection returns a new range consisting of the days the given ranges have in common

func (Range) IsEmpty

func (term Range) IsEmpty() bool

IsEmpty returns true if the term is zero and has isEmpty = true

func (Range) IsInfinity

func (term Range) IsInfinity() bool

IsInfinity is an alias for IsZero

func (Range) IsZero

func (term Range) IsZero() bool

IsZero returns true if the start and end dates are both zero

func (Range) MarshalJSON

func (term Range) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON output of a Range. Empty ranges will return null

func (Range) Overlaps

func (term Range) Overlaps(other Range) bool

Overlaps returns true if the given range has at least one day in common with the range

func (*Range) Scan

func (term *Range) Scan(value interface{}) error

Scan converts the given database value to a Range, possibly returning an error if the conversion failed

func (Range) String

func (term Range) String() string

String returns a string representation of the date range

func (Range) Union

func (term Range) Union(other Range) (union Range)

Union creates the union of two Range types. If there is a gap between the two range it is included.

func (Range) Value

func (term Range) Value() (driver.Value, error)

Value prepares the nullable term for the database

Jump to

Keyboard shortcuts

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