rrule

package module
v0.0.0-...-d0c966a Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2018 License: MIT Imports: 7 Imported by: 0

README

rrule-go

Go library for working with recurrence rules for calendar dates.

Build Status Coverage Status License GoDoc

The rrule module offers a complete implementation of the recurrence rules documented in the iCalendar RFC. It is a partial port of the rrule module from the excellent python-dateutil library.

Demo

rrule.RRule
package main

import (
  "fmt"
  "time"

  "github.com/teambition/rrule-go"
)

func exampleRRule() {
  fmt.Println("Daily, for 10 occurrences.")
  r, _ := rrule.NewRRule(rrule.ROption{
    Freq:    rrule.DAILY,
    Count:   10,
    Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.Local)})
  fmt.Println(r.All())
  // [1997-09-02 09:00:00 +0800 CST
  // 1997-09-03 09:00:00 +0800 CST
  // ...
  // 1997-09-10 09:00:00 +0800 CST
  // 1997-09-11 09:00:00 +0800 CST]

  fmt.Println(r.Between(
    time.Date(1997, 9, 6, 0, 0, 0, 0, time.Local),
    time.Date(1997, 9, 8, 0, 0, 0, 0, time.Local), true))
  // [1997-09-06 09:00:00 +0800
  // CST 1997-09-07 09:00:00 +0800 CST]

  fmt.Println(r)
  // FREQ=DAILY;DTSTART=19970902T010000Z;COUNT=10
}
rrule.Set
package main

import (
  "fmt"
  "time"

  "github.com/teambition/rrule-go"
)

func exampleRRuleSet() {
  fmt.Println("\nDaily, for 7 days, jumping Saturday and Sunday occurrences.")
  set := rrule.Set{}
  r, _ := rrule.NewRRule(rrule.ROption{
    Freq:    rrule.DAILY,
    Count:   7,
    Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.Local)})
  set.RRule(r)
  r, _ = rrule.NewRRule(rrule.ROption{
    Freq:      rrule.YEARLY,
    Byweekday: []rrule.Weekday{rrule.SA, rrule.SU},
    Dtstart:   time.Date(1997, 9, 2, 9, 0, 0, 0, time.Local)})
  set.ExRule(r)
  fmt.Println(set.All())
  // [1997-09-02 09:00:00 +0800 CST
  // 1997-09-03 09:00:00 +0800 CST
  // 1997-09-04 09:00:00 +0800 CST
  // 1997-09-05 09:00:00 +0800 CST
  // 1997-09-08 09:00:00 +0800 CST]

  fmt.Println("\nWeekly, for 4 weeks, plus one time on day 7, and not on day 16.")
  set = rrule.Set{}
  r, _ = rrule.NewRRule(rrule.ROption{
    Freq:    rrule.WEEKLY,
    Count:   4,
    Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.Local)})
  set.RRule(r)
  set.RDate(time.Date(1997, 9, 7, 9, 0, 0, 0, time.Local))
  set.ExDate(time.Date(1997, 9, 16, 9, 0, 0, 0, time.Local))
  fmt.Println(set.All())
  // [1997-09-02 09:00:00 +0800 CST
  // 1997-09-07 09:00:00 +0800 CST
  // 1997-09-09 09:00:00 +0800 CST
  // 1997-09-23 09:00:00 +0800 CST]
}
rrule.StrToRRule
func exampleStr() {
  fmt.Println()
  r, _ := rrule.StrToRRule("FREQ=DAILY;INTERVAL=10;COUNT=5")
  fmt.Println(r.All())
  // [2017-03-15 14:12:02 +0800 CST
  // 2017-03-25 14:12:02 +0800 CST
  // 2017-04-04 14:12:02 +0800 CST
  // 2017-04-14 14:12:02 +0800 CST
  // 2017-04-24 14:12:02 +0800 CST]
}

For more examples see python-dateutil documentation.

Documentation

Index

Constants

View Source
const (
	MAXYEAR = 9999
)

MAXYEAR

Variables

View Source
var (
	M366MASK     []int
	M365MASK     []int
	MDAY366MASK  []int
	MDAY365MASK  []int
	NMDAY366MASK []int
	NMDAY365MASK []int
	WDAYMASK     []int
	M366RANGE    = []int{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
	M365RANGE    = []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
)

Every mask is 7 days longer to handle cross-year weekly periods.

View Source
var (
	MO = Weekday{/* contains filtered or unexported fields */}
	TU = Weekday{/* contains filtered or unexported fields */}
	WE = Weekday{/* contains filtered or unexported fields */}
	TH = Weekday{/* contains filtered or unexported fields */}
	FR = Weekday{/* contains filtered or unexported fields */}
	SA = Weekday{/* contains filtered or unexported fields */}
	SU = Weekday{/* contains filtered or unexported fields */}
)

Weekdays

Functions

This section is empty.

Types

type Frequency

type Frequency int

Frequency denotes the period on which the rule is evaluated.

const (
	YEARLY Frequency = iota
	MONTHLY
	WEEKLY
	DAILY
	HOURLY
	MINUTELY
	SECONDLY
)

Constants

func (Frequency) String

func (f Frequency) String() string

type Next

type Next func() (value time.Time, ok bool)

Next is a generator of time.Time. It returns false of Ok if there is no value to generate.

type ROption

type ROption struct {
	Freq       Frequency
	Dtstart    time.Time
	Interval   int
	Wkst       Weekday
	Count      int
	Until      time.Time
	Bysetpos   []int
	Bymonth    []int
	Bymonthday []int
	Byyearday  []int
	Byweekno   []int
	Byweekday  []Weekday
	Byhour     []int
	Byminute   []int
	Bysecond   []int
	Byeaster   []int
}

ROption offers options to construct a RRule instance

func StrToROption

func StrToROption(rfcString string) (*ROption, error)

StrToROption converts string to ROption

func (*ROption) String

func (option *ROption) String() string

type RRule

type RRule struct {
	OrigOptions ROption
	// contains filtered or unexported fields
}

RRule offers a small, complete, and very fast, implementation of the recurrence rules documented in the iCalendar RFC, including support for caching of results.

func NewRRule

func NewRRule(arg ROption) (*RRule, error)

NewRRule construct a new RRule instance

func StrToRRule

func StrToRRule(rfcString string) (*RRule, error)

StrToRRule converts string to RRule

func (*RRule) After

func (r *RRule) After(dt time.Time, inc bool) time.Time

After returns the first recurrence after the given datetime instance, or time.Time's zero value if no recurrence match. The inc keyword defines what happens if dt is an occurrence. With inc == True, if dt itself is an occurrence, it will be returned.

func (*RRule) All

func (r *RRule) All() []time.Time

All returns all occurrences of the RRule.

func (*RRule) Before

func (r *RRule) Before(dt time.Time, inc bool) time.Time

Before returns the last recurrence before the given datetime instance, or time.Time's zero value if no recurrence match. The inc keyword defines what happens if dt is an occurrence. With inc == True, if dt itself is an occurrence, it will be returned.

func (*RRule) Between

func (r *RRule) Between(after, before time.Time, inc bool) []time.Time

Between returns all the occurrences of the RRule between after and before. The inc keyword defines what happens if after and/or before are themselves occurrences. With inc == True, they will be included in the list, if they are found in the recurrence set.

func (*RRule) Iterator

func (r *RRule) Iterator() Next

Iterator return an iterator for RRule

func (*RRule) String

func (r *RRule) String() string

type Set

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

Set allows more complex recurrence setups, mixing multiple rules, dates, exclusion rules, and exclusion dates

func StrToRRuleSet

func StrToRRuleSet(s string) (*Set, error)

StrToRRuleSet converts string to RRuleSet

func (*Set) After

func (set *Set) After(dt time.Time, inc bool) time.Time

After returns the first recurrence after the given datetime instance, or time.Time's zero value if no recurrence match. The inc keyword defines what happens if dt is an occurrence. With inc == True, if dt itself is an occurrence, it will be returned.

func (*Set) All

func (set *Set) All() []time.Time

All returns all occurrences of the rrule.Set.

func (*Set) Before

func (set *Set) Before(dt time.Time, inc bool) time.Time

Before Returns the last recurrence before the given datetime instance, or time.Time's zero value if no recurrence match. The inc keyword defines what happens if dt is an occurrence. With inc == True, if dt itself is an occurrence, it will be returned.

func (*Set) Between

func (set *Set) Between(after, before time.Time, inc bool) []time.Time

Between returns all the occurrences of the rrule between after and before. The inc keyword defines what happens if after and/or before are themselves occurrences. With inc == True, they will be included in the list, if they are found in the recurrence set.

func (*Set) ExDate

func (set *Set) ExDate(exdate time.Time)

ExDate include the given datetime instance in the recurrence set exclusion list. Dates included that way will not be generated, even if some inclusive rrule or rdate matches them.

func (*Set) ExRule

func (set *Set) ExRule(exrule *RRule)

ExRule include the given rrule instance in the recurrence set exclusion list. Dates which are part of the given recurrence rules will not be generated, even if some inclusive rrule or rdate matches them.

func (*Set) GetRRule

func (set *Set) GetRRule() []*RRule

GetRRule return the rrules in the set

func (*Set) Iterator

func (set *Set) Iterator() (next func() (time.Time, bool))

Iterator returns an iterator for rrule.Set

func (*Set) RDate

func (set *Set) RDate(rdate time.Time)

RDate include the given datetime instance in the recurrence set generation.

func (*Set) RRule

func (set *Set) RRule(rrule *RRule)

RRule include the given rrule instance in the recurrence set generation.

type Weekday

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

Weekday specifying the nth weekday. Field N could be positive or negative (like MO(+2) or MO(-3). Not specifying N (0) is the same as specifying +1.

func (*Weekday) Nth

func (wday *Weekday) Nth(n int) Weekday

Nth return the nth weekday __call__ - Cannot call the object directly, do it through e.g. TH.nth(-1) instead,

func (Weekday) String

func (wday Weekday) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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