openinghours

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// AddToSchema adds the opening hour definition/config spec to the
	// provided cofig schema.
	AddToSchema = configBuilder.AddToSchema
)
View Source
var Spec = conf.SectionSpec{
	{
		Name:        "OnWeekday",
		Description: "A list of days (Mo, Tue, Wed, Thu, Fri, Sat, Sun) at which this section takes effect",
		Type:        conf.StringSliceType,
		Annotations: new(conf.Annotation).With(
			runtime.OneOf(
				runtime.PossibleValue{
					Value:   "Mon",
					Display: "Monday",
				},
				runtime.PossibleValue{
					Value:   "Tue",
					Display: "Tuesday",
				},
				runtime.PossibleValue{
					Value:   "Wed",
					Display: "Wednesday",
				},
				runtime.PossibleValue{
					Value:   "Thu",
					Display: "Thursday",
				},
				runtime.PossibleValue{
					Value:   "Fri",
					Display: "Friday",
				},
				runtime.PossibleValue{
					Value:   "Sat",
					Display: "Saturday",
				},
				runtime.PossibleValue{
					Value:   "Sun",
					Display: "Sunday",
				},
			),
		),
	},
	{
		Name:        "UseAtDate",
		Description: "A list of dates at which this section takes effect. Format is defined MM/DD",
		Type:        conf.StringSliceType,
	},
	{
		Name:        "OpenBefore",
		Type:        conf.DurationType,
		Description: "Defines how long before the opening hour the entry door to should get unlocked.",
	},
	{
		Name:        "CloseAfter",
		Type:        conf.DurationType,
		Description: "Defines how long after the opening hour the entry door should get locked.",
	},
	{
		Name:        "TimeRanges",
		Type:        conf.StringSliceType,
		Description: "A list of office/opening hour time ranges (HH:MM - HH:MM).",
		Required:    true,
	},
	{
		Name:        "Holiday",
		Type:        conf.StringType,
		Description: "Whether or not this opening hour counts on holidays. Possible values are 'yes' (normal and holidays), 'no' (normal only) or 'only' (holiday only).",
		Annotations: new(conf.Annotation).With(
			runtime.OneOf(
				runtime.PossibleValue{
					Value:   "yes",
					Display: "Yes",
				},
				runtime.PossibleValue{
					Value:   "no",
					Display: "No",
				},
				runtime.PossibleValue{
					Value:   "only",
					Display: "Only",
				},
			),
		),
	},
}

Spec describes the different configuration stanzas for the Definition struct.

Functions

func ParseDay

func ParseDay(day string) (time.Weekday, bool)

ParseDay parses the weekday specified in day. For strict parsing, day should be validated using ValidDay before using ParseDay.

func ValidDay

func ValidDay(day string) error

ValidDay checks if day represents a valid weekday.

Types

type ChangeNotifyFunc

type ChangeNotifyFunc func()

ChangeNotifyFunc can be registered at the Controller to get notified when new opening hours have been configured.

type Controller

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

Controller keeps track of opening hours.

func New

func New(ctx context.Context, cfg cfgspec.Config, globalSchema *runtime.ConfigSchema, holidays HolidayGetter) (*Controller, error)

New returns a new opening hour controller.

func (*Controller) AddOpeningHours

func (ctrl *Controller) AddOpeningHours(ctx context.Context, timeRanges ...Definition) error

func (*Controller) Country

func (ctrl *Controller) Country() string

Country returns the name of the country the controller is configured for. The country is important to detect public holidays.

func (*Controller) ForDate

func (ctrl *Controller) ForDate(ctx context.Context, date time.Time) []OpeningHour

func (*Controller) Location

func (ctrl *Controller) Location() *time.Location

Location returns the location the controller is configured for.

func (*Controller) NotifyChange

func (ctrl *Controller) NotifyChange(ctx context.Context, changeType string, id string, sec *conf.Section) error

func (*Controller) OnChange

func (ctrl *Controller) OnChange(fn ChangeNotifyFunc)

OnChange registers fn to be called whenever the configured opening hours change.

func (*Controller) UpcomingFrames

func (ctrl *Controller) UpcomingFrames(ctx context.Context, dateTime time.Time, limit int) []daytime.TimeRange

func (*Controller) Validate

func (ctrl *Controller) Validate(ctx context.Context, sec runtime.Section) error

type Definition

type Definition struct {

	// OnWeekday is a list of days (Mo, Tue, ...) on which this opening
	// hours take effect.
	OnWeekday []string

	// UseAtDate is a list of dates on which this opening hours take effect.
	// UseAtDate should have the format MM/DD and are year independent.
	UseAtDate []string

	// OpenBefore describes the amount of time the entry door
	// should open before the specified time.
	OpenBefore time.Duration

	// CloseAfter describes the amount of time the entry door
	// should close after the specified time.
	CloseAfter time.Duration

	// TimeRanges describe the opening hours on the specified days
	// in the format of HH:MM - HH:MM.
	TimeRanges []string

	Holiday string
	// contains filtered or unexported fields
}

Definition is used to describe the opening and office hours.

func (*Definition) Validate

func (opt *Definition) Validate() error

Validate validates the opening hours defined in opt.

type HolidayCache

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

HolidayCache can load holidays for countries and supports caching the results.

func NewHolidayCache

func NewHolidayCache() *HolidayCache

NewHolidayCache returns a new holiday cache.

func (*HolidayCache) Get

func (cache *HolidayCache) Get(ctx context.Context, country string, year int) ([]PublicHoliday, error)

Get returns a list of public holidays for the given two-letter ISO country code in the given year. If the holidays have already been loaded they are served from cache.

func (*HolidayCache) IsHoliday

func (cache *HolidayCache) IsHoliday(ctx context.Context, country string, d time.Time) (bool, error)

IsHoliday returns true if d is a public holiday in country.

type HolidayGetter

type HolidayGetter interface {
	// Get returns a list of public holidays for the given
	// country and year.
	Get(ctx context.Context, country string, year int) ([]PublicHoliday, error)

	// IsHoliday returns true if d is a public holiday in
	// country.
	IsHoliday(ctx context.Context, country string, d time.Time) (bool, error)
}

HolidayGetter allows to retrieve holidays.

type OpeningHour

type OpeningHour struct {
	daytime.Range

	ID         string        `json:"id"`
	Holiday    bool          `json:"holiday"`
	OpenBefore time.Duration `json:"closeBefore"`
	CloseAfter time.Duration `json:"closeAfter"`
}

OpeningHour describes a single business-open time range with an additional OpenBefore and CloseAfter threshold for the entry door.

func (OpeningHour) EffectiveClose

func (oh OpeningHour) EffectiveClose() time.Duration

EffectiveClose returns the duration from midnight at which the door should close.

func (OpeningHour) EffectiveOpen

func (oh OpeningHour) EffectiveOpen() time.Duration

EffectiveOpen returns the duration from midnight at which the door should open.

func (OpeningHour) String

func (oh OpeningHour) String() string

type OpeningHourSlice

type OpeningHourSlice []OpeningHour

OpeningHourSlice is a slice of opening hours used for sorting.

func (OpeningHourSlice) Len

func (os OpeningHourSlice) Len() int

func (OpeningHourSlice) Less

func (os OpeningHourSlice) Less(i, j int) bool

func (OpeningHourSlice) Swap

func (os OpeningHourSlice) Swap(i, j int)

type PublicHoliday

type PublicHoliday struct {
	Date        string `json:"date"`
	LocalName   string `json:"localName"`
	Name        string `json:"name"`
	CountryCode string `json:"countryCode"`
	Fixed       bool   `json:"fixed"`
	Global      bool   `json:"global"`

	// Type may be  Public, Bank, School, Authorities, Optional, Observance
	Type string `json:"type"`
}

PublicHoliday represents a public holiday record returned by date.nager.at.

func LoadHolidays

func LoadHolidays(ctx context.Context, country string, year int) ([]PublicHoliday, error)

LoadHolidays loads all public holidays for the given country and year from the Nager Holiday API. Users should cache the response as it won't change for the given year anyway.

func (*PublicHoliday) Is

func (p *PublicHoliday) Is(d time.Time) bool

Is checks if p is on d.

Jump to

Keyboard shortcuts

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