hrrule

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: Apache-2.0 Imports: 11 Imported by: 2

README

hrrule-go 🧐👀⚛️📜

made with Go License GitHub issues

Library for make human readable recurrence rules from iCalendar RRULE (RFC5545) in Golang
It supports serialization of recurrence rules to natural language, with internationalisation!

It is a partial port of the rrule module from rrule.js library.

Using

package main

import (
	"fmt"
	"log"

	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"

	"github.com/skillcoder/hrrule-go"
)

func main() {
	hRule, err := hrrule.New(i18n.NewBundle(language.AmericanEnglish))
	if err != nil {
		log.Fatalf("filed to init rrule humanizer: %v", err)
	}

	rOption, err := hrrule.StrToROption("FREQ=MONTHLY;INTERVAL=1;BYDAY=-1FR;UNTIL=20241229T155400Z")
	if err != nil {
		log.Fatalf("rrule string to option: %v", err)
	}

	nlString, err := hRule.Humanize(rOption, "en-US")
	if err != nil {
		log.Fatalf("humanize rrule to string: %v", err)
	}
	fmt.Println(nlString)
}

Translation to new language

See docs in https://github.com/nicksnyder/go-i18n

  1. touch l10n/translate.ru.toml
  2. goi18n merge l10n/active.en-US.toml translate.ru.toml
  3. After translate.ru.toml has been translated, move it to l10n/active.ru-RU.toml.

🚧 It is necessary to agreement on the declension, cases and kind in the languages in which they exist 🚧

TODO

  • Day of the week translation support
  • Months translation support

Documentation

Index

Constants

View Source
const (
	UNKNOWN = "UNKNOWN"
)

Variables

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

func NewI18NBundle

func NewI18NBundle(path string) (*i18n.Bundle, error)

Types

type DateFormatter

type DateFormatter interface {
	Format(year int, month time.Month, day int) string
	MonthName(month time.Month) string
	Nth(i int) string
	WeekdayName(wDay Weekday) string
}

func NewDateFormatterSimple

func NewDateFormatterSimple(loc *i18n.Localizer) DateFormatter

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

type HRRule

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

func (*HRRule) Humanize

func (imp *HRRule) Humanize(rule *ROption, lang string) (string, error)

type Humanizer

type Humanizer interface {
	Humanize(rule *ROption, lang string) (string, error)
}

func New

func New(bundle *i18n.Bundle) (Humanizer, error)

type ListMode

type ListMode int
const (
	IsINT ListMode = iota
	IsNTH
	IsMONTH
)

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
}

func FromROption

func FromROption(o *rrule.ROption) *ROption

FromRRule covert teambition/rrule-go to ROption

func StrToROption

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

StrToROption parse rrule string to ROption

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 FromWeekdaySlice

func FromWeekdaySlice(weekdays []rrule.Weekday) []Weekday

FromWeekdaySlice covert from teambition/rrule-go slice Weekday slice

func NewWeekday

func NewWeekday(weekday int, n int) Weekday

func (*Weekday) Day

func (wday *Weekday) Day() int

Day returns index of the day in a week (0 for MO, 6 for SU)

func (*Weekday) N

func (wday *Weekday) N() int

N returns index of the week, e.g. for 3MO, N() will return 3

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

String convert struct to human readable string String returns the English rrule name of the Weekday with number

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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