openhours

package module
v0.0.0-...-51e93e3 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 6 Imported by: 0

README

openhours

A compromise of complexity of the "opening_hours".
Only the `day-day time-time" will work for now.

Online tools

https://openingh.openstreetmap.de/evaluation_tool/?setLng=en

Example

oh := openhours.New("Mo-Fr 09:00-17:00")
t := time.Date(2019, 3, 6, 10, 0, 0, 0, time.Now().Location())
// oh.Location = t.Location() //default to system but can be changed this way

fmt.Println("t =", t)

isOpen := oh.Match(t)
fmt.Println("Is it open at this date?", isOpen)

_, duration := oh.NextDur(t)
fmt.Println("For how long?", duration)

_, date := oh.NextDate(t)
fmt.Println("When will it close?", date)

fmt.Println(" +++++++++++++++++++++ ")

t = time.Date(2019, 3, 6, 18, 0, 0, 0, time.Now().Location())

fmt.Println("t =", t)

isOpen, date = oh.NextDate(t)
fmt.Println("Is it open?", isOpen, "Ok, when will it open then ?", date)

/* output
t = 2019-03-06 10:00:00 +0000 GMT
Is it open at this date? true
For how long? 7h0m0s
When will it close? 2019-03-06 17:00:00 +0000 GMT
 +++++++++++++++++++++
t = 2019-03-06 18:00:00 +0000 GMT
Is it open? false Ok, when will it open then ? 2019-03-07 09:00:00 +0000 GMT
*/

Documentation

Index

Constants

View Source
const (
	Monday int = iota + 1
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
	Sunday
)

Variables

View Source
var (

	// Errors
	ErrInvalidFormat error = errors.New("invalid format")
)

Functions

This section is empty.

Types

type OpenHours

type OpenHours []time.Time

OpenHours ...

func New

func New(str string, loc *time.Location) (OpenHours, error)

New returns a new instance of an openhours. If loc is nil, UTC is used.

func NewLocal

func NewLocal(str string) (OpenHours, error)

NewLocal returns a new instance of an openhours with local timezone

func NewMust

func NewMust(str string, loc *time.Location) OpenHours

NewMust returns a new instance of an openhours or panics on error If loc is nil, UTC is used.

func NewUTC

func NewUTC(str string) (OpenHours, error)

NewUTC returns a new instance of an openhours with UTC timezone

func (OpenHours) Add

func (o OpenHours) Add(from, to time.Time) OpenHours

func (OpenHours) Match

func (o OpenHours) Match(t time.Time) bool

Match returns true if the time t is in the open hours

func (OpenHours) NextDate

func (o OpenHours) NextDate(t time.Time) (bool, time.Time)

NextDate uses nextDur to gives the date of interest

func (OpenHours) NextDur

func (o OpenHours) NextDur(t time.Time) (bool, time.Duration)

NextDur returns true if t is in the open hours and the duration until it closes else it returns false if t is in the closed hours and the duration until it opens

func (OpenHours) String

func (o OpenHours) String() []string

func (OpenHours) When

func (o OpenHours) When(t time.Time, d time.Duration) *time.Time

When returns the date where the duration can be done in one go during open hours

Jump to

Keyboard shortcuts

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