Judgment of holidays including holidays with bash

Purpose: Use bash to determine holidays, including holidays

If you want to operate the shell etc. only on weekdays, or only on holidays including holidays Holidays need to be considered as well as Saturdays and Sundays. This article explains how to implement that case.

environment

Get a list of holidays

The list of holidays is published by the Cabinet Office.

https://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html

A csv file that describes a list of holidays is open to the public, As of February 29, 2020, it is only listed until 2021.

Therefore, use the following:

holiday-jp's repository https://github.com/holiday-jp/holiday_jp/blob/master/holidays.yml

... 2019-11-03: Culture Day 2019-11-04: Culture Day Substitute Holiday 2019-11-23: Labor Thanksgiving Day 2020-01-01: New Year's Day 2020-01-13: Coming of Age Day 2020-02-11: National Foundation Day 2020-02-23: The Emperor's Birthday 2020-02-24: Emperor's Birthday Substitute Holiday ...

Here, I used the above yml format file, but since there are modules for other programming languages, If you want to use it in other environments, please look for the above repository.

When updating holidays as needed to determine holidays

Holidays are subject to change (recently, changes to the Emperor's Birthday holidays, etc.) By executing the following curl command regularly with crontab etc., it will be downloaded automatically.

# "token"Enter OAuthToken in the place of
curl -H 'Authorization:token token' -H 'Accept: application/vnd.github.v3.raw' -L https://api.github.com/repos/holiday-jp/holiday_jp/contents/holidays.yml > holidays.yml

In addition, it is assumed that the following is satisfied.

--The environment for judging holidays must be able to connect to the Internet --The GitHub repository has been updated to the latest holiday list --You have created a GitHub account and issued an OAuth Token.

OAuthToken is

  1. Go to Personal Access Tokens
  2. Click Generate new token
  3. Enter any character string in Note
  4. Check public_repo
  5. Click Generate token You can create it with.

When determining a holiday without updating the holiday at any time

Download the holiday list file (holidays.yml) from holiday-jp's repository and use it. It doesn't matter if the environment for judging holidays is not connected to the Internet.

important point If there are any changes to the holidays, you will need to download the holiday list file again.

Determine holidays

Here, the date argument is taken when the shell is executed, and the holiday judgment result (and the holiday name in the case of a holiday) is returned.

example.sh


#!/bin/bash

#A file that describes a list of holidays
PUBLIC_HOLIDAY_LIST='./holidays.yml'
#Dates for determining holidays and holidays
target_date=$(/bin/date +"%Y-%m-%d" -d $1)

#For holidays
if [ -n "$(/bin/grep $target_date $PUBLIC_HOLIDAY_LIST)" ] ; then
	#Get the holiday name
	public_holiday_name=$(/bin/grep $target_date $PUBLIC_HOLIDAY_LIST | /bin/awk -F ':' '{print $2}')
	echo "$target_date is a holiday:$public_holiday_is name."
#holiday(Saturday,Sunday)in the case of
elif [ -n "$(/bin/date -d $target_date | /bin/grep -E "[Saturday and Sunday]Day of the week")" ] ; then
	#Get the day of the week
	day_of_the_week=$(/bin/date -d $target_date | /bin/awk '{print $4}')
	echo "$target_date is$day_of_the_week."
#On weekdays
else
	echo "$target_date is a weekday."
fi

result


$ ./holiday.sh 2020-01-13
2020-01-13 is a holiday:Coming of age day.
$ ./holiday.sh 20200113
2020-01-13 is a holiday:Coming of age day.
$ ./holiday.sh 20200114
2020-01-14 is a weekday.
$ ./holiday.sh 20200112
2020-01-12 is sunday.
$ ./holiday.sh 20200211
2020-02-11 is a holiday:National Foundation Day.

Bonus: If you want to determine your own holiday

Separately from the general holiday list file, describe in the following format and change the if statement OK

2020-01-14: Important Anniversary 2020-02-05: Important Anniversary

original.sh


#A file that describes a list of holidays
PUBLIC_HOLIDAY_LIST='./public_holidays.yml'
#A file that describes your own holidays
ORIGINAL_HOLIDAY_LIST='./original_holidays.yml'
#Dates for determining holidays and holidays
target_date=$(/bin/date +"%Y-%m-%d" -d $1)

#For holidays
#if [ -n "$(/bin/grep $target_date $PUBLIC_HOLIDAY_LIST)" ] ; then
if [ -n "$(/bin/grep $target_date $PUBLIC_HOLIDAY_LIST)" ] || [ -n "$(/bin/grep $target_date $ORIGINAL_HOLIDAY_LIST)" ] ; then
# ...
fi

Recommended Posts

Judgment of holidays including holidays with bash
Color page judgment of scanned image with python
Call bash with golang
[Personal memo] Auto-completion of bash
Equation of motion with sympy
Handling of quotes in [bash]
Parallel processing with Parallel of scikit-learn
Prediction of Nikkei 225 with Pytorch 2
Memories of fighting with Selenium
Prediction of Nikkei 225 with Pytorch
Variable parameter expansion of bash
Create a compatibility judgment program with the random module of python.