date

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2018 License: MIT Imports: 5 Imported by: 0

README

GoDoc Build Status Go Report Card

Date it easy

Small, user-friendly library to help you work with dates and dates ranges in Go. A time-zone-independent representation of time used that follows the rules of the proleptic Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second minutes.

Date supports SQL scan and JSON marshalling/unmarshalling out of the box.

Date can be parsed from string start, err := date.Parse("2018-10-15") or created manually d := date.Date{2018, 10, 1}.

Two Dates define Range date.Range{date.Date{2018, 10, 1}, date.Date{2018, 10, 5}}.

RangeSet can be used to work with Ranges:


ranges = date.RangeSet(ranges).
	Filter(func(dr date.Range) bool {
		return !dr.Empty()
	}).
	TrimEnd().
	Sub(date.Range{date.Date{2018, 10, 1}, date.Date{2018, 10, 5}}).
	ShiftEnd(5).
	List()

Install

go get github.com/furdarius/date
Adding as dependency by "go dep"
$ dep ensure -add github.com/furdarius/date

Usage


start, _ := date.Parse("2018-10-15")
end, _ := date.Parse("2018-10-20")

interval1 := date.Range{start, end}
interval2 := date.Range{start, end.AddDays(-1)}

base := []date.Range{
	date.Range{date.Date{2018, 10, 1}, date.Date{2018, 10, 5}},
	date.Range{date.Date{2018, 10, 8}, date.Date{2018, 10, 22}},
}

ranges := date.RangeSet(base).Sub(interval).Impose(interval2).ExtendEnd().List()

Contributing

Pull requests are very much welcomed. Make sure a test or example is included that covers your change and your commits represent coherent changes that include a reason for the change.

Use gometalinter to check code with linters:

gometalinter -t --vendor

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RangeNotEmpty

func RangeNotEmpty(dr Range) bool

RangeNotEmpty tests that Range is not empty.

Types

type ByAsc

type ByAsc []Date

ByAsc sorting type - used to sort Date slice from the earliest to the latest

func (ByAsc) Len

func (a ByAsc) Len() int

Len returns length of underlying array.

func (ByAsc) Less

func (a ByAsc) Less(i, j int) bool

Less compare i and j elements and returns true if element i less than element j.

func (ByAsc) Swap

func (a ByAsc) Swap(i, j int)

Swap swaps i and j elements.

type Date

type Date struct {
	Year  int        // Year (e.g., 2014).
	Month time.Month // Month of the year (January = 1, ...).
	Day   int        // Day of the month, starting at 1.
}

A Date represents a date (year, month, day).

This type does not include location information, and therefore does not describe a unique 24-hour timespan.

func FromTime

func FromTime(t time.Time) Date

FromTime returns the Date in which a time occurs in that time's location.

func Parse

func Parse(s string) (Date, error)

Parse parses a string in RFC3339 full-date format and returns the date value it represents.

func (Date) AddDays

func (d Date) AddDays(n int) Date

AddDays returns the date that is n days in the future. n can also be negative to go into the past.

func (Date) AddDuration added in v0.0.4

func (d Date) AddDuration(dr time.Duration) Date

AddDuration returns the date by appending dr to d. dr can also be negative to go into the past.

func (Date) AddMonths added in v0.0.4

func (d Date) AddMonths(n int) Date

AddMonths returns the date that is n months in the future. n can also be negative to go into the past.

func (Date) After

func (d Date) After(p Date) bool

After reports whether d1 occurs after p.

func (Date) Before

func (d Date) Before(p Date) bool

Before reports whether d occurs before p.

func (Date) DaysSince

func (d Date) DaysSince(s Date) (days int)

DaysSince returns the signed number of days between the date and s, not including the end day. This is the inverse operation to AddDays.

func (Date) Equal

func (d Date) Equal(d2 Date) bool

Equal returns true if d equal d2.

func (Date) FirstDayOfMonth added in v0.0.4

func (d Date) FirstDayOfMonth() Date

FirstDayOfMonth returns the first day of month for d.

func (Date) In

func (d Date) In(loc *time.Location) time.Time

In returns the time corresponding to time 00:00:00 of the date in the location.

In is always consistent with time.Date, even when time.Date returns a time on a different day. For example, if loc is America/Indiana/Vincennes, then both

time.Date(1955, time.May, 1, 0, 0, 0, 0, loc)

and

date.Date{Year: 1955, Month: time.May, Day: 1}.In(loc)

return 23:00:00 on April 30, 1955.

In panics if loc is nil.

func (Date) IsValid

func (d Date) IsValid() bool

IsValid reports whether the date is valid.

func (Date) LastDayOfMonth added in v0.0.4

func (d Date) LastDayOfMonth() Date

LastDayOfMonth returns the last day of month for d.

func (Date) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface. The output is the result of d.String().

func (*Date) Scan

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

Scan implements the database/sql Scanner interface.

func (Date) String

func (d Date) String() string

String returns the date in RFC3339 full-date format.

func (*Date) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. The date is expected to be a string in a format accepted by ParseDate.

func (Date) Value

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

Value implements database/sql Valuer.

type Range

type Range struct {
	Start Date `json:"start"`
	End   Date `json:"end"`
}

Range represents range between two dates.

func (Range) Contains

func (r Range) Contains(d Date) bool

Contains reports whether d is within r.

func (Range) Empty

func (r Range) Empty() bool

Empty returns true if Start equal End.

func (Range) Encloses

func (r Range) Encloses(dr Range) bool

Encloses returns true if the bounds of the inner range do not extend outside the bounds of the outer range.

func (Range) Intersects added in v0.0.3

func (r Range) Intersects(dr Range) bool

Intersects returns true if r/dr contains any of dr/r bounds.

func (Range) IsValid

func (r Range) IsValid() bool

IsValid reports if r is valid range.

type RangeSet

type RangeSet []Range

RangeSet used to works with date ranges list.

func (RangeSet) ExtendEnd

func (a RangeSet) ExtendEnd() RangeSet

ExtendEnd shifts end date a day later.

func (RangeSet) Filter

func (a RangeSet) Filter(test RangeTest) RangeSet

Filter returns RangeSet with all elements that pass the test.

func (RangeSet) FilterEmpty

func (a RangeSet) FilterEmpty() RangeSet

FilterEmpty returns RangeSet with not empty Ranges from a.

func (RangeSet) Impose

func (a RangeSet) Impose(dr ...Range) RangeSet

Impose returns RangeSet after dr imposition. Range intersections will be merged: [10, 15] impose with [13, 22] got [10, 22] [10, 15] impose with [20, 25] got [10, 15], [20, 25]

func (RangeSet) ImposeSet

func (a RangeSet) ImposeSet(set RangeSet) RangeSet

ImposeSet returns ImposeSet list after set imposition.

func (RangeSet) List

func (a RangeSet) List() []Range

List returns list of Ranges in set.

func (RangeSet) ShiftEnd

func (a RangeSet) ShiftEnd(n int) RangeSet

ShiftEnd shifts end date to n days. n can also be negative to go into the past.

func (RangeSet) Sub

func (a RangeSet) Sub(dr ...Range) RangeSet

Sub returns RangeSet after dr subtraction.

func (RangeSet) SubSet

func (a RangeSet) SubSet(set RangeSet) RangeSet

SubSet returns RangeSet list after set subtraction.

func (RangeSet) TrimEnd

func (a RangeSet) TrimEnd() RangeSet

TrimEnd shifts end date a day earlier.

type RangeTest

type RangeTest func(Range) bool

RangeTest used to filter RangeSet.

Jump to

Keyboard shortcuts

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