Categorygithub.com/rickar/cal/v2
modulepackage
2.1.21
Repository: https://github.com/rickar/cal.git
Documentation: pkg.go.dev

# README

cal/v2: Go (golang) calendar library for dealing with holidays and work days

This library augments the Go time package to provide easy handling of holidays and work days (business days).

Holiday instances are calculated from either builtin or user-created functions to support exact days, floating days such as the 3rd Monday of the month, yearly offsets such as the 100th day of the year, or more complex rules such as offsets from Easter. Holidays may provide separate actual and observed dates for cases where holidays are celebrated on an alternate day if they fall on a specific day of the week (usually weekends).

The Calendar type provides the basic functionality for creating a yearly calendar with holidays.

BusinessCalendar adds additional functionality for calculating workdays and hours worked.

Differences from v1

For v2, much of the functionality of this library was rewritten to address shortcomings of the v1 releases. This version provides the following benefits over v1:

  • Holidays
    • Observation rules are tied to individual holidays rather than a per-calendar setting
    • Name, description, and observance type fields added
    • Starting, ending, and exception year options
    • Holiday definitions are separated into subpackages by ISO code (no longer necessary to bundle all holidays in the final binary)
  • Calendar
    • Separation of business specific functionality into BusinessCalendar
    • Name and description fields added
    • Support for time.Location matching to ease use of multiple Calendars
  • BusinessCalendar
    • Full support for working hours and related calculations
    • Work days and work start and end times can be provided by custom functions

Example

Here is a simple usage example of a cron job that runs once per day:

package main

import (
	"time"

	"github.com/rickar/cal/v2"
	"github.com/rickar/cal/v2/us"
)

func main() {
	c := cal.NewBusinessCalendar()
	c.Name = "Bigco, Inc."
	c.Description = "Default company calendar"

	// add holidays that the business observes
	c.AddHoliday(
		us.NewYear,
		us.MemorialDay,
		us.IndependenceDay,
		us.LaborDay,
		us.ThanksgivingDay,
		us.ChristmasDay,
	)

	// change the default of a Mon - Fri, 9am-5pm work week
	c.SetWorkday(time.Saturday, true)
	c.SetWorkHours(8*time.Hour, 18*time.Hour+30*time.Minute)

	t := time.Now()

	// run different batch processing jobs based on the day

	if c.IsWorkday(t) {
		// create daily activity reports
	}

	if cal.IsWeekend(t) {
		// validate employee time submissions
	}

	if c.WorkdaysRemain(t) == 10 {
		// 10 business days before the end of month
		// send account statements to customers
	}

	if c.WorkdaysRemain(t) == 0 {
		// last business day of the month
		// execute auto billing transfers
	}

	// determine the number of working hours left in the current month
	nextMonth := cal.DayStart(cal.MonthStart(t.AddDate(0, 1, 0)))
	hoursLeft := c.WorkHoursInRange(t, nextMonth)

	// check if there are any tasks for this month that are in danger of missing their deadline
	pendingTasks := []struct{ pendingHours time.Duration }{{pendingHours: 32}} // assumed to be fetched from a DB or API
	for _, task := range pendingTasks {
		if hoursLeft < task.pendingHours {
			// send alert to management
		}
	}
}

# Packages

Package aa provides common holiday definitions that are frequently used.
Package at provides holiday definitions for Austria.
Package au provides holiday definitions for Australia.
Package be provides holiday definitions for Belgium.
Package ie provides holiday definitions for Brazil.
Package ca provides holiday definitions for Canada.
Package ch provides holiday definitions for Switzerland.
Package cy provides holiday definitions for Cyprus.
Package cz provides holiday definitions for the Czech Republic.
Package de provides holiday definitions for Germany.
Package dk provides holiday definitions for Denmark.
Package ecb provides holiday definitions for the European Central Bank.
Package ee provides holiday definitions for Estonia.
Package es provides holiday definitions for Spain.
Package fi provides holiday definitions for Finland.
Package fr provides holiday definitions for France.
Package gb provides holiday definitions for the United Kingdom.
Package hr provides definitions for Croatian holidays.
Package hu provides holiday definitions for Hungary.
Package ie provides holiday definitions for the Republic Of Ireland.
Package it provides holiday definitions for Italy.
Package jp provides holiday definitions for Japan.
Package ke provides holiday definitions for Kenya.
Package be provides holiday definitions for Luxembourg.
Package mt provides holiday definitions for Malta.
Package mw provides holiday definitions for the Republic of Malawi.
Package mx provides holiday definitions for Mexico.
Package nl provides holiday definitions for the Netherlands.
Package no provides holiday definitions for Norway.
Package nz provides holiday definitions for New Zealand.
Package pl provides holiday definitions for Poland.
Package pt provides holiday definitions for Portugal.
Package ro provides holiday definitions for Romania.
Package rs provides holiday definitions for Serbia.
Package ru provides holiday definitions for Russia.
Package se provides holiday definitions for Sweden.
Package si provides holiday definitions for Slovenia.
Package sk provides holiday definitions for Slovakia.
Package th provides holiday definition for Thailand.
Package ua provides holiday definitions for Ukraine.
Package us provides holiday definitions for the United States of America.
Package za provides holiday definitions for South Africa.

# Functions

CalcDayOfMonth calculates the occurrence of a holiday that is always a specific day of the month such as the 5th of November.
CalcEasterOffset calculates the occurrence of a holiday that is determined by its relation to the Easter holiday.
CalcWeekdayFrom calculates the occurrence of a holiday that falls on a specific day of the week following a starting date.
CalcWeekdayOffset calculates the occurrence of a holiday that falls on the nth occurrence of a weekday in a month, such as the third wednesday of July.
DayEnd reports the end of the day in t (sets time fields to maximum).
DayStart reports the start of the day in t (sets time fields zero).
IsWeekdayN reports whether the given date is the nth occurrence of the day in the month.
IsWeekend reports whether the given time falls on a weekend.
JulianDate reports the Julian Date (which includes time as a fraction) for t.
JulianDayNumber reports the Julian Day Number for t.
MaxTime reports the maximum time in the given list.
MinTime reports the minimum time in the given list.
ModifiedJulianDate reports the modified Julian Date Number for t.
ModifiedJulianDayNumber reports the modified Julian Day Number for t.
MonthEnd reports the ending day of the month in t.
MonthStart reports the starting day of the month in t.
NewBusinessCalendar creates a new BusinessCalendar with no holidays defined and work days of Monday through Friday from 9am-5pm.
ReplaceLocation returns a copy of the given time in the given location without changing the time of day.
WeekdayN reports the nth occurrence of a weekday starting in the given year and month.
WeekdayNFrom reports the nth occurrence of a weekday starting from the given time.

# Constants

bank holiday.
all other holidays (school, work, etc.).
public / national / regional holiday.
religious holiday.
value not set or not applicable.

# Variables

CacheEvictSize is the number of items to evict from cache when it is full.
CacheMaxSize is the maximum number of items that can be stored in the cache.
DefaultLoc is the default time.Location to use in functions that do not require a full time.Time value.

# Structs

AltDay represents an alternative day to observe a holiday.
BusinessCalendar represents a calendar used for business purposes.
Calendar represents a basic yearly calendar with a list of holidays.
Holiday holds information about the type and occurrence of a holiday.

# Type aliases

HolidayFn calculates the expected occurrence of a holiday for the given year.
ObservanceType represents the type of holiday or special day being observed.
WorkdayEndFn reports the end of the workday for the given date.
WorkdayFn reports whether the given date is a workday.
WorkdayStartFn reports the start of the workday for the given date.