recurrence

package module
v0.0.0-...-9c1ad7f Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2017 License: MIT Imports: 4 Imported by: 0

README

Recurrence

Calendar recurrence rules in Go.

Basically, implementing the strategy described in Martin Fowler's paper. Read it. It's fun.

Check out the full docs on godoc.org

Schedule

The Schedule interface is the foundation of the recurrence package. By using and combining schedules, we can represent all kinds of recurrence rules.

schedule.IsOccurring(time.Time) bool - does this time.Time occur in this schedule? schedule.Occurrences(TimeRange) chan time.Time - generate time.Times occuring with the passed TimeRange

Day

Integer day of the month, 1 through 31, or the constant Last.

first := recurrence.Day(recurrence.First)
last := recurrence.Day(recurrence.Last)
twentieth := recurrence.Day(20)

Week

Integer week of the month, 1 through 5, or the constant Last.

first := recurrence.Week(recurrence.First)
last := recurrence.Week(recurrence.Last)
third := recurrence.Week(recurrence.Third)

Weekday

Day of the week, Sunday through Monday. Constants are defined so you can use them with ease.

recurrence.Sunday.IsOccurring(time.Now())

Month

A month of the year. Constants are defined to be used with ease.

recurrence.January.IsOccurring(time.Now())

Year

A year.

the_future := recurrence.Year(2525)

TimeRange

A range of time. Primarily used as an argument to schedule.Occurrences(t recurrence.TimeRange) chan time.Time

It can also act as a schedule. Any day within the time range is considered as occurring.

forty_days := recurrence.TimeRange{time.Now(), time.Now().AddDate(0, 0, 40)}

Some shortcuts are provided for common time ranges.

recurrence.YearRange(2525)
recurrence.MonthRange(time.January, 2525)

Set Operations

Intersection

Intersection is a slice of Schedules. IsOccurring is only satisfied if all members of the slice are true. (Set intersection).

american_thanksgiving := recurrence.Intersection{recurrence.Week(4), recurrence.Thursday, recurrence.November}
Union

Union is a slice of Schedules. IsOccurring is satisfied if any member of the slice is occurring. (Set union).

weekends := recurrence.Union{recurrence.Saturday, recurrence.Sunday}

Documentation

Overview

Package recurrence providers calendar recurrence rules in Go.

Basically, implementing the strategy described in Martin Fowler's paper at http://martinfowler.com/apsupp/recurring.pdf. Read it. It's fun.

Index

Constants

View Source
const (
	Last int = -1 + iota
	Never
	First
	Second
	Third
	Fourth
	Fifth
)

Integers representing the natural language expression of dates. (i.e. "First Sunday")

Variables

This section is empty.

Functions

This section is empty.

Types

type AnySchedule

type AnySchedule struct {
	Schedule Schedule
}

AnySchedule acts as a wrapper around...any schedule. Why does this exists? Since the tree of Schedules is an arbitrary relationship of interfaces, we need a struct to *easily* marshal/unmarshal json of the entire schedule hierarchy.

func (AnySchedule) IsOccurring

func (a AnySchedule) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (AnySchedule) MarshalJSON

func (a AnySchedule) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (AnySchedule) Occurrences

func (a AnySchedule) Occurrences(t TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*AnySchedule) UnmarshalJSON

func (a *AnySchedule) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Date

type Date time.Time

Date is a specific day. Shorthand for Intersection{Year, Month, Day}.

func NewDate

func NewDate(s string) Date

NewDate let's you create a new Date from the time format "2006-01-02"

func (Date) IsOccurring

func (d Date) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Date) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (Date) Occurrences

func (d Date) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Day

type Day int

A Day specifies a day of the month. (1, 2, 3, ...31)

func (Day) IsOccurring

func (d Day) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Day) MarshalJSON

func (d Day) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Day) Occurrences

func (d Day) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*Day) UnmarshalJSON

func (d *Day) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Intersection

type Intersection []Schedule

Intersection computes the set intersection of a slice of Schedules.

func OrdinalWeekday

func OrdinalWeekday(i int, w Weekday) Intersection

OrdinalWeekday generates Schedules for natural recurrence patterns such as the "Last Wednesday" or "Second Sunday".

func (Intersection) IsOccurring

func (i Intersection) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Intersection) MarshalJSON

func (i Intersection) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Intersection) Occurrences

func (i Intersection) Occurrences(t TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*Intersection) UnmarshalJSON

func (i *Intersection) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Month

type Month time.Month

A Month represents a month of the year. Just like time.Month.

const (
	January Month = 1 + iota
	February
	March
	April
	May
	June
	July
	August
	September
	October
	November
	December
)

The months of the year

func (Month) IsOccurring

func (m Month) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Month) MarshalJSON

func (m Month) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Month) Occurrences

func (m Month) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (Month) String

func (m Month) String() string

String implements the Stringer interface.

func (*Month) UnmarshalJSON

func (m *Month) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Schedule

type Schedule interface {
	IsOccurring(time.Time) bool
	Occurrences(TimeRange) chan time.Time
}

The Schedule interface is the foundation of the recurrence package. Types satisfying the Schedule interface can be used to determine if a time.Time occurs in the Schedule, and generate time.Times satisfying the Schedule.

func ScheduleUnmarshalJSON

func ScheduleUnmarshalJSON(b []byte) (schedule Schedule, err error)

ScheduleUnmarshalJSON unmarshals bytes representing any arbitrary relationship of schedules.

type TimeRange

type TimeRange struct {
	Start time.Time
	End   time.Time
}

A TimeRange represents a range of time, with a start and an end.

func MonthRange

func MonthRange(month interface{}, year int) TimeRange

MonthRange generates a TimeRange representing a specific month.

func NewTimeRange

func NewTimeRange(start, end string) TimeRange

NewTimeRange let's you create a new TimeRange from the time format "2006-01-02"

func YearRange

func YearRange(y int) TimeRange

YearRange generates a TimeRange representing the entire year.

func (TimeRange) IsOccurring

func (tr TimeRange) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (TimeRange) Occurrences

func (tr TimeRange) Occurrences(other TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*TimeRange) UnmarshalJSON

func (tr *TimeRange) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Union

type Union []Schedule

Union computes the set union of a slice of Schedules.

func (Union) IsOccurring

func (u Union) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Union) MarshalJSON

func (u Union) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Union) Occurrences

func (u Union) Occurrences(t TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (*Union) UnmarshalJSON

func (u *Union) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Week

type Week int

A Week represents a week of the month. This is most useful in combination with other entities satisfying the Schedule interface.

func (Week) IsOccurring

func (w Week) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Week) MarshalJSON

func (w Week) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Week) Occurrences

func (w Week) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (Week) String

func (w Week) String() string

implements the Stringer interface.

func (*Week) UnmarshalJSON

func (w *Week) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Weekday

type Weekday time.Weekday

A Weekday represents a day of the week. (Sunday, Monday, ...Saturday)

const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

The days of the week

func (Weekday) IsOccurring

func (w Weekday) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Weekday) MarshalJSON

func (w Weekday) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Weekday) Occurrences

func (w Weekday) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (Weekday) String

func (w Weekday) String() string

implements the Stringer interface.

func (*Weekday) UnmarshalJSON

func (w *Weekday) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Year

type Year int

Year represents a year.

func (Year) IsOccurring

func (y Year) IsOccurring(t time.Time) bool

IsOccurring implements the Schedule interface.

func (Year) MarshalJSON

func (y Year) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Year) Occurrences

func (y Year) Occurrences(tr TimeRange) chan time.Time

Occurrences implements the Schedule interface.

func (Year) String

func (y Year) String() string

implements the Stringer interface.

Jump to

Keyboard shortcuts

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