jhol

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 5 Imported by: 0

README

jhol

test Go Reference Go Report Card

Go language library to check Japanese holidays using Google Calendar.

Preparation

  1. Enable Google Calendar API.
  2. Create an API Key.

Usage

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/winebarrel/jhol"
)

func parseDate(s string) time.Time {
	d, _ := time.ParseInLocation("2006-01-02", s, time.Local)
	return d
}

func main() {
	apiKey := os.Getenv("GOOGLE_API_KEY")
	client := jhol.NewClient(apiKey).WithoutContext()

	date := parseDate("2023-07-17")
	h, _ := client.Get(date)
	fmt.Println(h) //=> 2023-07-17	海の日

	yes, _ := client.IsHoliday(date)
	fmt.Println(yes) //=> true

	yes, _ = client.IsTodayHoliday()
	fmt.Println(yes)

	h, _ = client.Next(date)
	fmt.Println(h) //=> 2023-07-17	海の日

	holidays, _ := client.NextN(date, 3)
	fmt.Println(holidays) //=> [2023-07-17	海の日 2023-08-11	山の日 2023-09-18	敬老の日]

	holidays, _ = client.Between(date, parseDate("2023-08-11"))
	fmt.Println(holidays) //=> [2023-07-17	海の日 2023-08-11	山の日]
}

Limitation

Google Calendar events are limited in duration. For example, as of today (Jun 17th, 2023) you cannot get public holidays prior to 2022.

Check below command for details:

curl -s -H "X-goog-api-key: $GOOGLE_API_KEY" \
  'https://www.googleapis.com/calendar/v3/calendars/ja.japanese%23holiday%40group.v.calendar.google.com/events?showDeleted=false&singleEvents=true&orderBy=startTime&timeMin=2019-01-01T00:00:00Z&maxResults=100' \
  | jq -c '.items[] | {summary, start}'

CLI

Installation

brew install winebarrel/jhol/jhol

Usage

Usage: jhol --api-key=STRING <command>

Flags:
  -h, --help              Show context-sensitive help.
      --version
      --api-key=STRING    Google API Key ($GOOGLE_API_KEY)
      --lang="ja"         Calendar language (ja, en).

Commands:
  next --api-key=STRING [<n>]
    Show next holidays.

  is-holiday --api-key=STRING [<date>]
    Check whether the specified date is a holiday.

Run "jhol <command> --help" for more information on a command.
$ export GOOGLE_API_KEY=...

$ jhol next
2023-07-17	海の日
2023-08-11	山の日
2023-09-18	敬老の日

$ jhol is-holiday 2023-07-17
true

Documentation

Index

Constants

View Source
const (
	EnJapaneseHolidayCalendar = "en.japanese#holiday@group.v.calendar.google.com"
	JaJapaneseHolidayCalendar = "ja.japanese#holiday@group.v.calendar.google.com"
)

Variables

View Source
var (
	HolidayFormat           = "2006-01-02\t%s"
	JapaneseHolidayLocation *time.Location
)

Functions

This section is empty.

Types

type Client

type Client struct {
	APIKey     string
	CalendarID string
}

func NewClient

func NewClient(apiKey string) *Client

func NewClientWithCalendar

func NewClientWithCalendar(apiKey string, calendarID string) *Client

func (*Client) Between

func (client *Client) Between(ctx context.Context, from time.Time, to time.Time) ([]*Holiday, error)

func (*Client) Get

func (client *Client) Get(ctx context.Context, t time.Time) (*Holiday, error)

func (*Client) IsHoliday

func (client *Client) IsHoliday(ctx context.Context, t time.Time) (bool, error)

func (*Client) IsTodayHoliday added in v1.1.0

func (client *Client) IsTodayHoliday(ctx context.Context) (bool, error)

func (*Client) Next

func (client *Client) Next(ctx context.Context, t time.Time) (*Holiday, error)

func (*Client) NextN

func (client *Client) NextN(ctx context.Context, t time.Time, n int) ([]*Holiday, error)

func (*Client) WithoutContext

func (client *Client) WithoutContext() *ClientWithoutContext

type ClientWithoutContext

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

func (*ClientWithoutContext) Between

func (c *ClientWithoutContext) Between(from time.Time, to time.Time) ([]*Holiday, error)

func (*ClientWithoutContext) Get

func (c *ClientWithoutContext) Get(t time.Time) (*Holiday, error)

func (*ClientWithoutContext) IsHoliday

func (c *ClientWithoutContext) IsHoliday(t time.Time) (bool, error)

func (*ClientWithoutContext) IsTodayHoliday added in v1.1.0

func (c *ClientWithoutContext) IsTodayHoliday() (bool, error)

func (*ClientWithoutContext) Next

func (c *ClientWithoutContext) Next(t time.Time) (*Holiday, error)

func (*ClientWithoutContext) NextN

func (c *ClientWithoutContext) NextN(t time.Time, n int) ([]*Holiday, error)

type Holiday

type Holiday struct {
	Date time.Time
	Name string
}

func (*Holiday) String

func (h *Holiday) String() string

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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