8

Is there a package, or macro in an extant package which will calculate the next business day or nth business day (taking a list of Holidays into account) from today or an arbitrary date? (If there isn't, I guess I'll finally have something to code up and post on CTAN)

1 Answer 1

3

I don't know of a package that does it all, but the pgfcalendar package is pretty powerful. The following MWE handles the next business day (assuming that there is not a full week of holidays)

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{pgfcalendar}
\usepackage{etoolbox}
\newif\ifholiday
\def\holidays{2016-09-12, 2016-09-16, 2016-09-20, 2016-09-21, 2016-09-23, 2016-09-26}
\newcount\nextjulianday
\newcount\julianholiday
\newcommand{\isholiday}[2]{%
    \holidayfalse%
    \def\do##1{%
        \pgfcalendardatetojulian{##1}{\julianholiday}%
        \ifnumequal{\the\julianholiday}{\the\nextjulianday}{\holidaytrue}{}%
    }%
    \docsvlist{#1}%
}
\newcommand{\nextbusinessday}[1]{\begingroup%
    \pgfcalendardatetojulian{#1}{\nextjulianday}%
    \advance\nextjulianday 1\relax%
    \loop
        \expandafter\isholiday\expandafter{\holidays}{\the\nextjulianday}%
        \ifholiday%
            \advance\nextjulianday 1\relax%
            \repeat%
    \pgfcalendarjuliantodate{\the\nextjulianday}{\theyear}{\themonth}{\theday}%
    \pgfcalendarifdate{\theyear-\themonth-\theday}{Saturday}{%
        \advance\nextjulianday 1\relax%
        \pgfcalendarjuliantodate{\the\nextjulianday}{\theyear}{\themonth}{\theday}%
    }{}%
    \pgfcalendarifdate{\theyear-\themonth-\theday}{Sunday}{%
        \advance\nextjulianday 1\relax%
        \pgfcalendarjuliantodate{\the\nextjulianday}{\theyear}{\themonth}{\theday}%
    }{}%
    \loop
        \expandafter\isholiday\expandafter{\holidays}{\the\nextjulianday}%
        \ifholiday%
            \advance\nextjulianday 1\relax%
            \repeat%
    \pgfcalendarjuliantodate{\the\nextjulianday}{\theyear}{\themonth}{\theday}%
    \theyear-\themonth-\theday
\endgroup}
\begin{document}
Holidays:

\def\do#1{#1\par}
\expandafter\docsvlist\expandafter{\holidays}

\def\do#1{2016-09-#1 -- \nextbusinessday{2016-09-#1}\par}
\docsvlist{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}
\end{document}

It should be possible to expand the approach to handle the nth business day.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .