dateseq

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2018 License: MIT Imports: 2 Imported by: 0

README

Software License Travis Go Doc Coverage Status Go Report Card

dateseq

A package to create a sequence series of daily dates, written in golang.

Sequence is an immutable struct, each operations returns a new Sequence and leaves the original untouched.

seq := dateseq.New()
seq = seq.Steps(10)

for k, v := range seq.Sequence() {
    fmt.Printf("%v. %v\n", k, v.Format("2006-01-02 Mon"))
}

Returns a sequence of dates from the current date counted backwards (assuming, you start this this programm on 2017-10-09):

0. 2017-10-09 Mon
1. 2017-10-08 Sun
2. 2017-10-07 Sat
3. 2017-10-06 Fri
4. 2017-10-05 Thu
5. 2017-10-04 Wed
6. 2017-10-03 Tue
7. 2017-10-02 Mon
8. 2017-10-01 Sun
9. 2017-09-30 Sat

Weekends can be excluded with ExcludeWeekends() from the sequence series. IncludeWeekends() will include them again, which is also the default behavior.

seq := dateseq.New()
seq = seq.ExcludeWeekends().Steps(10)

for k, v := range seq.Sequence() {
    fmt.Printf("%v. %v\n", k, v.Format("2006-01-02 Mon"))
}

Returns:

0. 2017-10-09 Mon
1. 2017-10-06 Fri
2. 2017-10-05 Thu
3. 2017-10-04 Wed
4. 2017-10-03 Tue
5. 2017-10-02 Mon
6. 2017-09-29 Fri
7. 2017-09-28 Thu
8. 2017-09-27 Wed
9. 2017-09-26 Tue

Exclude some specific dates with Exclude(list []string).

seq := dateseq.New()
exclude := []string{
    "2017-12-25",
    "2017-12-26",
}

seq = seq.Steps(10).Exclude(exclude)

The sequence methods are chainable, which allows creation and retrieving of the sequence slice in one go.

seq := dateseq.New().Steps(10).Sequence()

Need a slice with only the string representations of the dates?

seq := dateseq.New().Steps(10).String()
fmt.Println(seq)

Returns the dates in format YYYY-MM-DD:

[2017-10-09 2017-10-08 2017-10-07 2017-10-06 2017-10-05]

For a custom output format use the Format(layout string) method, which returns the string in a layout defined by the time package.

seq := dateseq.New().Steps(10).Format("Jan 01. 2006")

Documentation

Overview

Package dateseq creates a sequence series of daily dates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sequence

type Sequence struct {
	// contains filtered or unexported fields
}

Sequence represents a slice of dates.

func New

func New() Sequence

New returns a Sequence ready for use.

func (Sequence) Exclude added in v0.1.5

func (s Sequence) Exclude(list []string) Sequence

Exclude excludes a list of dates from the sequence. This shortens the sequence by the number of excluded dates. If steps is set, the sequence fill be filled up by the number of excluded dates.

func (Sequence) ExcludeWeekends added in v0.1.5

func (s Sequence) ExcludeWeekends() Sequence

ExcludeWeekends excludes Saturday and Sunday from the sequence. Weekends are included by default.

func (Sequence) Format added in v0.1.3

func (s Sequence) Format(layout string) []string

Format returns the sequence as a slice with a string repesentation of the date in the specified layout.

func (Sequence) FromDate added in v0.1.5

func (s Sequence) FromDate(date string) Sequence

FromDate starts the sequence from a given date. If the sequence is alredy generated by Steps(), it will truncate the sequence from that date. The direction back or forward in time will be determined by the sign of steps. If the sequence is not already generated, this methof will generate it.

func (Sequence) IncludeWeekends added in v0.1.5

func (s Sequence) IncludeWeekends() Sequence

IncludeWeekends includes Saturday and Sunday back into the sequence.

func (Sequence) Sequence added in v0.1.5

func (s Sequence) Sequence() []time.Time

Sequence returns the sequence slice.

func (Sequence) SortAsc added in v0.1.5

func (s Sequence) SortAsc() Sequence

SortAsc sorts a slice of dates in ascending order, i.e. 2006-01-02 comes before 2006-01-03

func (Sequence) SortDesc added in v0.1.5

func (s Sequence) SortDesc() Sequence

SortDesc sorts a slice of dates in descending order, i.e. 2006-01-01 comes after 2006-01-02

func (Sequence) Steps added in v0.1.4

func (s Sequence) Steps(i int) Sequence

Steps creates a date sequence with the specified length of days ending now. The signing of steps will determine, if the sequence goes back of forward in time. Calls to Steps will reset the sequence slice to nil before generating an new sequence.

func (Sequence) String added in v0.1.2

func (s Sequence) String() []string

String returns the sequence as a slice with a simple string repesentation of the dates in the format YYYY-MM-DD.

func (Sequence) ToDate added in v0.1.5

func (s Sequence) ToDate(date string) Sequence

ToDate starts the sequence that it ends at a given date.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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