goholiday

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: MIT Imports: 2 Imported by: 2

README

goholiday

v1.0.5 Test coverage Go Report Card
GoDoc license

Calculating (holiday, business) days support for golang (Go language).

Provide functions to calculate and judge the business days and holidays in each country.

Currently, we only handle holidays for a few countries, but we plan to handle holidays for other countries in the future.
Please Contribute!

Install

$ go install github.com/yut-kt/goholiday

Suggested golang environment

go version >= 1.16.15

Usage

Supported ccTLD

support 2022, 2023
|- am (Armenia)
|- do (Dominican Republic)
|- gr (Greece)
|- jp (Japan)
|- sg (Singapore)
|- uk (United Kingdom)
    |- England
    |- NorthernIreland
    |- Scotland
    |- Wales

Contribution

CONTRIBUTING.md

License

goholiday is released under the MIT License.

Documentation

Overview

Package goholiday provides Package ome simple calculation functions. These are functions to calculate business days.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BusinessDaysAfter deprecated

func BusinessDaysAfter(t time.Time, bds int) time.Time

Deprecated: Moved to Goholiday instance (goholiday.go). BusinessDaysAfter is a function that calculates bds business days after given t

func BusinessDaysBefore deprecated

func BusinessDaysBefore(t time.Time, bds int) time.Time

Deprecated: Moved to Goholiday instance (goholiday.go). BusinessDaysBefore is a function that calculates bds business days before given t

func IsBusinessDay deprecated

func IsBusinessDay(t time.Time) bool

Deprecated: Moved to Goholiday instance (goholiday.go). IsBusinessDay is a function to decide whether t given business day.

func IsHoliday deprecated

func IsHoliday(t time.Time) bool

Deprecated: Moved to Goholiday instance (goholiday.go). IsHoliday is a function to decide whether t given holiday.

func IsNationalHoliday deprecated

func IsNationalHoliday(t time.Time) bool

Deprecated: Moved to Goholiday instance (goholiday.go). IsNationalHoliday is a function to decide whether t given national holiday.

func SetUniqueHolidays deprecated

func SetUniqueHolidays(ts []time.Time)

Deprecated: Moved to Goholiday instance (goholiday.go). SetUniqueHolidays is a function to set unique holidays.

Types

type Goholiday added in v0.1.8

type Goholiday struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday/nholidays"

	"github.com/yut-kt/goholiday/nholidays/uk"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	// Japan Schedule
	ghj := goholiday.New(jp.New())
	fmt.Println(ghj.IsNationalHoliday(time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local)))

	// England Schedule
	ghe := goholiday.New(uk.NewEngland())
	fmt.Println(ghe.IsNationalHoliday(time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local)))

	// Original Schedule
	mySchedule := nholidays.New(
		map[time.Weekday]struct{}{},
		map[string]string{"2022-05-01": "my holiday"},
	)
	ghMine := goholiday.New(mySchedule)
	fmt.Println(ghMine.IsNationalHoliday(time.Date(2022, 5, 1, 0, 0, 0, 0, time.Local)))

}
Output:

true
true
true

func New added in v0.1.8

func New(schedule Schedule) *Goholiday
Example
package main

import (
	"fmt"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	gh := goholiday.New(jp.New())
	fmt.Printf("%T", gh)
}
Output:

*goholiday.Goholiday

func (*Goholiday) BusinessDaysAfter added in v0.1.8

func (g *Goholiday) BusinessDaysAfter(t time.Time, bds int) time.Time
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	cases := []struct {
		date   time.Time
		days   int
		expect time.Time
	}{
		{
			date:   time.Date(2017, 10, 5, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 5, 0, 0, 0, 0, time.Local),
			days:   2,
			expect: time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
			days:   2,
			expect: time.Date(2017, 10, 11, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 7, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 9, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		},
	}

	gh := goholiday.New(jp.New())
	for _, c := range cases {
		fmt.Println(gh.BusinessDaysAfter(c.date, c.days).Format("2006-01-02") == c.expect.Format("2006-01-02"))
	}
}
Output:

true
true
true
true
true
true

func (*Goholiday) BusinessDaysBefore added in v0.1.8

func (g *Goholiday) BusinessDaysBefore(t time.Time, bds int) time.Time
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	cases := []struct {
		date   time.Time
		days   int
		expect time.Time
	}{
		{
			date:   time.Date(2017, 10, 11, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 11, 0, 0, 0, 0, time.Local),
			days:   2,
			expect: time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
			days:   2,
			expect: time.Date(2017, 10, 5, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 9, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
		},
		{
			date:   time.Date(2017, 10, 8, 0, 0, 0, 0, time.Local),
			days:   1,
			expect: time.Date(2017, 10, 6, 0, 0, 0, 0, time.Local),
		},
	}

	gh := goholiday.New(jp.New())
	for _, c := range cases {
		fmt.Println(gh.BusinessDaysBefore(c.date, c.days).Format("2006-01-02") == c.expect.Format("2006-01-02"))
	}
}
Output:

true
true
true
true
true
true

func (*Goholiday) IsBusinessDay added in v0.1.8

func (g *Goholiday) IsBusinessDay(t time.Time) bool
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	targets := []time.Time{
		// national holiday of weekday"
		time.Date(2017, 10, 9, 0, 0, 0, 0, time.Local),
		// national holiday of holiday
		time.Date(2017, 9, 23, 0, 0, 0, 0, time.Local),
		// business day
		time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		// Sunday
		time.Date(2017, 9, 24, 0, 0, 0, 0, time.Local),
	}
	gh := goholiday.New(jp.New())
	for _, target := range targets {
		fmt.Println(gh.IsBusinessDay(target))
	}
}
Output:

false
false
true
false

func (*Goholiday) IsHoliday added in v0.1.8

func (g *Goholiday) IsHoliday(t time.Time) bool
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	targets := []time.Time{
		// national holiday of weekday"
		time.Date(2017, 10, 9, 0, 0, 0, 0, time.Local),
		// national holiday of holiday
		time.Date(2017, 9, 23, 0, 0, 0, 0, time.Local),
		// business day
		time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		// Sunday
		time.Date(2017, 9, 24, 0, 0, 0, 0, time.Local),
	}
	gh := goholiday.New(jp.New())
	for _, target := range targets {
		fmt.Println(gh.IsHoliday(target))
	}
}
Output:

true
true
false
true

func (*Goholiday) IsNationalHoliday added in v0.1.8

func (g *Goholiday) IsNationalHoliday(t time.Time) bool
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	targets := []time.Time{
		// national holiday of weekday"
		time.Date(2017, 10, 9, 0, 0, 0, 0, time.Local),
		// national holiday of holiday
		time.Date(2017, 9, 23, 0, 0, 0, 0, time.Local),
		// business day
		time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local),
		// Sunday
		time.Date(2017, 9, 24, 0, 0, 0, 0, time.Local),
	}

	gh := goholiday.New(jp.New())
	for _, target := range targets {
		fmt.Println(gh.IsNationalHoliday(target))
	}
}
Output:

true
true
false
false

func (*Goholiday) SetUniqueHolidays added in v0.1.8

func (g *Goholiday) SetUniqueHolidays(ts []time.Time)
Example
package main

import (
	"fmt"
	"time"

	"github.com/yut-kt/goholiday"
	"github.com/yut-kt/goholiday/nholidays/jp"
)

func main() {
	gh := goholiday.New(jp.New())
	gh.SetUniqueHolidays([]time.Time{time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local)})
	date := time.Date(2017, 10, 10, 0, 0, 0, 0, time.Local)
	fmt.Println(gh.IsHoliday(date))
	fmt.Println(gh.IsBusinessDay(date))
	fmt.Println(gh.IsNationalHoliday(date))
}
Output:

true
false
false

type Schedule added in v0.1.8

type Schedule interface {
	GetNationalHolidays() map[string]string
	GetWeekdayHolidays() map[time.Weekday]struct{}
}

Directories

Path Synopsis
am
do
gr
jp
sg
uk

Jump to

Keyboard shortcuts

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