% Copyright 2019 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgfcalendar.code.tex}

\input pgfint.code.tex

% Translation stuff

\ifx\translate\@undefined
  \def\translate#1{#1}
\fi

% Load month dictionary, if possible

\ifx\usedictionary\@undefined
\else
  \usedictionary{translator-months-dictionary}
\fi




% Typeset a calendar
%
% #1 = prefix for the calendar nodes
% #2 = begin date in ISO format
% #3 = end date in ISO format
% #4 = date rendering code
%
% Description:
%
% First note that this macro need not be called inside a pgfpicture
% (even though it often will be). You can use it to typeset
% calendars also using packages other than pgf. You do not even need
% to load pgf to use this file.
%
% A calendar is typeset as follows: You specify a range of dates (by
% providing a begin and an end date). Then, for each date the code #4
% is executed. When the code is called, lots of information about the
% current date (like its day of week) is available. The job of
% #4 is to render the date. Typically, this is done by placing nodes
% somewhere, but the function could do other things as well.
% It is also the job of #4 to position the calendar correctly.
%
% The different calls of #4 are not surrounded by TeX groups (though
% you can do so yourself, of course). This means that settings can
% accumulate between different calls, which is often desirable and
% useful.
%
% Inside #4 code, different macros can be access:
%
% \pgfcalendarprefix
% The parameter #1. This prefix is recommended for nodes inside the
% calendar, but you have to use it yourself explicitly.
%
% \pgfcalendarbeginiso
% Start date of range being typeset in ISO format (like 2006-01-10)
%
% \pgfcalendarbeginjulian
% Julian day number of start date begin typeset
%
% \pgfcalendarendiso
% End date of range begin typeset
%
% \pgfcalendarendjulian
% Same as Julian day number
%
% \pgfcalendarcurrentjulian (a TeX-count)
% Julian day number of day to be rendered/considered
%
% \pgfcalendarcurrentweekday
% Weekday (0 = Monday) of day to be checked
%
% \pgfcalendarcurrentyear
% Year of day to be checked
%
% \pgfcalendarcurrentmonth
% Month of day to be checked
%
% \pgfcalendarcurrentday
% Day of month of day to be checked
%
%
% Inside the \pgfcalendar macro the macro \ifdate is available, which
% has the same effect as \pgfcalendarifdate{\pgfcalendarcurrentjulian}
% (only faster). This means that you
% can write, say, \ifdate{Monday}{current date is a Monday}{Not a Monday}.
%
% Example:
%
% \begin{tikzpicture}
%   \pgfcalendar{cal}{2007-01-20}{2007-02-10}
%     {\node[anchor=base] {\pgfcalendaronday}; \pgftransformyshift{-1.5em}}
% \end{tikzpicture}
%
% \pgfcalendar{cal}{2007-01-20}{2007-02-10}{ \pgfcalendaronday\ }

\long\def\pgfcalendar#1#2#3#4{%
  \begingroup%
    % Setup local \ifdate
    \let\ifdate=\pgfcalendar@local@ifdate%
    % Let's start with computing start and end dates...
    \def\pgfcalendarprefix{#1}%
    \pgfcalendardatetojulian{#2}{\pgfcalendarcurrentjulian}%
    \edef\pgfcalendarbeginjulian{\the\pgfcalendarcurrentjulian}%
    \edef\pgfcalendarbeginiso{#2}%
    \pgfcalendardatetojulian{#3}{\pgfutil@tempcnta}%
    \edef\pgfcalendarendjulian{\the\pgfutil@tempcnta}%
    \advance\pgfutil@tempcnta by1\relax%
    \edef\pgfcalendarendjulianplus{\the\pgfutil@tempcnta}%
    \edef\pgfcalendarendiso{#3}%
    %
    % Start main loop
    %
    \loop%
    \ifnum\pgfcalendarcurrentjulian<\pgfcalendarendjulianplus\relax%
      % Setup information about current date
      \pgfcalendarjuliantodate{\pgfcalendarcurrentjulian}%
      {\pgfcalendarcurrentyear}{\pgfcalendarcurrentmonth}{\pgfcalendarcurrentday}%
      \pgfcalendarjuliantoweekday{\pgfcalendarcurrentjulian}{\pgfutil@tempcntb}%
      \edef\pgfcalendarcurrentweekday{\the\pgfutil@tempcntb}%
      % Render:
      #4%
      % Advance day:
      \advance\pgfcalendarcurrentjulian by1\relax%
    \repeat%
  \endgroup%
}

\newcount\pgfcalendarcurrentjulian

\def\pgfcalendar@local@ifdate{%
  \let\pgfcalendarifdatejulian=\pgfcalendarcurrentjulian%
  \let\pgfcalendarifdateyear=\pgfcalendarcurrentyear%
  \let\pgfcalendarifdatemonth=\pgfcalendarcurrentmonth%
  \let\pgfcalendarifdateday=\pgfcalendarcurrentday%
  \let\pgfcalendarifdateweekday=\pgfcalendarcurrentweekday%
  \pgfcalendar@launch@ifdate%
}



% Execute on specific kinds of dates
%
% #1 = ISO-format date
% #2 = date specification
% #3 = code
%
% Description:
%
% The given code #3 is execute on every day that matches the date
% specification #2. This specification is a comma-separated list of
% key-value pairs with the path /pgf/calendar. The code for a
% key-value should check whether the given day matches its
% specification. If so, it should set \pgfcalendarmatches to true.
%
% When the key-value code is executed, the following macros will have
% been setup:
%
% \pgfcalendarifdatejulian
% Julian day number of day to be checked
%
% \pgfcalendarifdateweekday
% Weekday (0 = Monday) of day to be checked
%
% \pgfcalendarifdateyear
% Year of day to be checked
%
% \pgfcalendarifdatemonth
% Month of day to be checked
%
% \pgfcalendarifdateday
% Day of month of day to be checked
%
% The setting of these macros will persist after the call, so a
% side-effect of calling this macro is to setup these macros.
%
% By setting \ifpgfcalendar@do@not@recompute to false, the computation
% of the year, month etc. is suppressed.
%
% Examples:
%
% \pgfcalendarifdate{2007-01-10}{all}{}
% % Simply setup the macros
%
% \pgfcalendarifdate{2007-01-10}{mondays}{Is a monday}
%
% \pgfcalendarifdate{\mydate}{date=05-01}{\mydate is a Worker's Day}

\def\pgfcalendarifdate#1#2#3#4{%
  \pgfcalendardatetojulian{#1}{\pgfutil@tempcnta}%
  \pgfcalendarjuliantodate{\pgfutil@tempcnta}
    {\pgfcalendarifdateyear}{\pgfcalendarifdatemonth}{\pgfcalendarifdateday}%
  \edef\pgfcalendarifdatejulian{\the\pgfutil@tempcnta}%
  % Compute info about date
  \pgfcalendarjuliantoweekday{\pgfutil@tempcnta}{\pgfutil@tempcntb}%
  \edef\pgfcalendarifdateweekday{\the\pgfutil@tempcntb}%
  %
  \pgfcalendar@launch@ifdate{#2}{#3}{#4}%
}


\long\def\pgfcalendar@launch@ifdate#1#2#3{%
  % When this macro is called, the pgfcalendarifdatexxxx macros must
  % be setup correctly
  %
  % Set match to false
  \pgfcalendarmatchesfalse%
  \pgfqkeys{/pgf/calendar}{#1}%
  \ifpgfcalendarmatches%
    #2%
  \else%
    #3%
  \fi%
}

\newif\ifpgfcalendarmatches


% Keys for matching

\pgfkeys{/pgf/calendar/all/.code=\pgfcalendarmatchestrue}

\pgfkeys{/pgf/calendar/Monday/.code={\ifnum\pgfcalendarifdateweekday=0\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Tuesday/.code={\ifnum\pgfcalendarifdateweekday=1\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Wednesday/.code={\ifnum\pgfcalendarifdateweekday=2\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Thursday/.code={\ifnum\pgfcalendarifdateweekday=3\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Friday/.code={\ifnum\pgfcalendarifdateweekday=4\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Saturday/.code={\ifnum\pgfcalendarifdateweekday=5\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/Sunday/.code={\ifnum\pgfcalendarifdateweekday=6\relax\pgfcalendarmatchestrue\fi}}

\pgfkeys{/pgf/calendar/workday/.code={\ifnum\pgfcalendarifdateweekday<5\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/weekend/.code={\ifnum\pgfcalendarifdateweekday>4\relax\pgfcalendarmatchestrue\fi}}

\pgfkeys{/pgf/calendar/equals/.cd,.value required,.code={%
  \pgfcalendar@special@datetojulian{#1}%
  \ifnum\pgfcalendarifdatejulian=\pgfutil@tempcnta\relax%
    \pgfcalendarmatchestrue%
  \fi}%
}

\pgfkeys{/pgf/calendar/day of month/.cd,.value required,.code={\ifnum#1=\pgfcalendarifdateday\relax\pgfcalendarmatchestrue\fi}}
\pgfkeys{/pgf/calendar/end of month/.cd,.default=1,.code={%
  % Tricky and expensive ... do only if necessary
  \ifpgfcalendarmatches%
  \else% sigh
    {%
      \pgfutil@tempcnta=\pgfcalendarifdatejulian\relax%
      \advance\pgfutil@tempcnta by#1\relax%
      \pgfcalendarjuliantodate{\pgfutil@tempcnta}{\pgf@dummy}{\pgf@dummy}{\pgf@cal@temp}%
      \global\let\pgf@cal@temp=\pgf@cal@temp%
    }%
    \ifnum\pgf@cal@temp=1\relax\pgfcalendarmatchestrue\fi%
  \fi}%
}

\pgfkeys{/pgf/calendar/at least/.cd,.value required,.code={%
  \pgfcalendar@special@datetojulian{#1}%
  \ifnum\pgfcalendarifdatejulian<\pgfutil@tempcnta\relax%
  \else%
    \pgfcalendarmatchestrue%
  \fi%
}}

\pgfkeys{/pgf/calendar/at most/.cd,.value required,.code={%
  \pgfcalendar@special@datetojulian{#1}%
  \ifnum\pgfcalendarifdatejulian>\pgfutil@tempcnta\relax%
  \else%
    \pgfcalendarmatchestrue%
  \fi%
}}

\pgfkeys{/pgf/calendar/between/.cd,.value required,.code args={#1and#2}{%
  \pgfcalendar@special@datetojulian{#1}%
  \ifnum\pgfcalendarifdatejulian<\pgfutil@tempcnta\relax%
  \else%
    \pgfcalendar@special@datetojulian{#2}%
    \ifnum\pgfcalendarifdatejulian>\pgfutil@tempcnta\relax%
    \else%
      \pgfcalendarmatchestrue%
    \fi%
  \fi%
}}

\def\pgfcalendar@special@datetojulian#1{%
  \edef\pgf@temp{#1}%
  \expandafter\pgfcalendar@special@split@test\pgf@temp-\relax{#1}%
}

\def\pgfcalendar@special@split@test#1-#2-#3\relax#4{%
  \def\pgf@temp{#3}%
  \ifx\pgf@temp\pgfutil@empty%
    \def\pgf@temp{\pgfcalendarifdateyear-#1-#2}%
  \else%
    \def\pgf@temp{#4}%
  \fi%
  \pgfcalendardatetojulian{\pgf@temp}{\pgfutil@tempcnta}%
}


% Suggested name for nodes inside a calendar
%
% Description:
%
% It is suggested that (main) nodes (if present) in a calendar should
% get this name.
%
% Example:
%
% \pgfcalendar{cal}{2007-01-20}{2007-02-10}{}
% { \node[anchor=base] (\pgfcalendarsuggestedname) {\pgfcalendarifdateday};
%   \pgftransformyshift{-1.5em} }
%
% \draw (cal-2007-01-30) -- (cal-2007-02-08);

\def\pgfcalendarsuggestedname{%
  \ifx\pgfcalendarprefix\pgfutil@empty\else\pgfcalendarprefix-%
  \pgfcalendarcurrentyear-\pgfcalendarcurrentmonth-\pgfcalendarcurrentday\fi%
}



%
%
% Date conversion functions
%
%



% Convert a date to the Julian day number (number of days since
% January 1st, -4712)
%
% #1 = date specification
% #2 = counter that should be set to the number of days
%
% Description:
%
% The conversion is taken from the Wikipedia entry on Julian days.
%
% The date specification works as follows:
%
% - It starts with a year. Use \year for the current year (the year
%   the file is being typeset).
% - Next comes a hyphen.
% - Next comes a month (number). Use \month for the current month.
% - Next comes a hyphen.
% - Next comes either a day of month (number, use \day for current
%   day) or "last". The keyword "last" refers the last day of the
%   month. Thus, 2006-01-last is the same as 2006-01-31 and
%   2000-02-last is the same as 2002-02-29.
% - Next may come a plus sign, followed by a number (positive or
%   negative). This will adjust the date by the given number of
%   days. Thus, 2006-01-31+1 is the same as 2006-02-01 and
%   2006-02-01+-2 is the same as 2006-01-30. And \year-\month-day+1 is
%   tomorrow. You may add a space before the +.
%
% Example:
%
% \pgfcalendardatetojulian{2006-01-10}{\mycount}

\def\pgfcalendardatetojulian#1#2{\edef\pgf@temp{#1}\expandafter\pgfcalendar@datetojulian\pgf@temp+/{#2}}
\def\pgfcalendar@datetojulian#1-#2-#3+#4/#5{%
  {%
    %
    % Store year, month and days.
    %
    \count1=#1\relax%
    \count2=#2\relax%
    % If #3=="last", then calculate first day of next month
    \edef\pgf@temp{#3}%
    \ifx\pgf@temp\pgfcalendar@last@text%
      \advance\count2 by1\relax%
      \count3=1\relax%
    \else%
      \count3=#3\relax%
    \fi%
    %
    % Store offset
    %
    \edef\pgf@@temp{#4}%
    \ifx\pgf@@temp\pgfutil@empty%
      \count8=0\relax%
    \else%
      \pgfcalendar@unpack#4%
    \fi%
    %
    %
    % 4) a = \lfloor (14-month) /12 \rfloor
    %
    \ifnum\count2<3\relax%
      \count4=1\relax%
    \else%
      \count4=0\relax%
    \fi%
    %
    % 5) y = year + 4800 - a
    %
    \count5=\count1\relax%
    \advance\count5 by 4800\relax%
    \advance\count5 by-\count4\relax%
    %
    % 6) m = month + 12a - 3
    %
    \count6=\count2\relax%
    \count0=\count4\relax%
    \multiply\count0 by12\relax
    \advance\count6 by\count0\relax%
    \advance\count6 by-3\relax%
    %
    % 7) jdn = day + \floor{(153 m+2)/5} + 365y + \floor{y/4} -
    % \floor{y/100} + \florr{y/400} - 32045
    %
    \count7=\count3\relax%
    % + \floor{(153 m+2)/5} :
    \count0=\count6\relax%
    \multiply\count0 by 153\relax%
    \advance\count0 by 2\relax%
    \divide\count0 by 5\relax%
    \advance\count7 by \count0\relax%
    % + 365y :
    \count0=\count5%
    \multiply\count0by365\relax%
    \advance\count7 by\count0\relax%
    % + \floor{y/4}
    \count0=\count5\relax%
    \divide\count0 by 4\relax%
    \advance\count7 by\count0%
    % - \floor{y/100}
    \count0=\count5\relax%
    \divide\count0 by 100\relax%
    \advance\count7 by-\count0\relax%
    % + \floor{y/400}
    \count0=\count5\relax%
    \divide\count0 by 400\relax%
    \advance\count7 by\count0%
    %  - 32045
    \advance\count7 by-32045\relax%
    %
    % Undo "last" advance
    \ifx\pgf@temp\pgfcalendar@last@text%
      \advance\count7by-1\relax%
    \fi%
    % Add offset
    \advance\count7by\count8\relax%
    \expandafter%
  }%
  \expandafter#5\the\count7\relax%
}
\def\pgfcalendar@last@text{last}
\def\pgfcalendar@unpack#1+{%
  \count8=#1\relax%
}

% Convert Julian day number date.
%
% #1 = the number of Julian days
% #2 = a macro in which the year should be stored.
% #3 = a macro in which the month should be stored.
% #4 = a macro in which the day should be stored.
%
% Formula used:
%
% 1) J = Julian day number
% 2) j = J + 32044
% 3) g = j div 146097
% 4) dg = j mod 146097
% 5) c = (dg div 36524 + 1) * 3 div 4
% 6) dc = dg - c * 36524
% 7) b = dc div 1461
% 8) db = dc mod 1461
% 9) a = (db div 365 + 1) * 3 div 4
% 10)da = db - a * 365
% 11)y = g * 400 + c * 100 + b * 4 + a
% 12)m = (da * 5 + 308) div 153 - 2
% 13)d = da - (m + 4) * 153 div 5 + 122
% 14)Y = y - 4800 + (m + 2) div 12
% 15)M = (m + 2) mod 12 + 1
% 16)D = d + 1
%
% Example
%
% \pgfcalendarjuliantodate{\mynumber}{\myyear}{\mymonth}{\myday}

\def\pgfcalendarjuliantodate#1#2#3#4{%
  {%
    % 1) J = Julian day number
    \count1=#1\relax%
    % 2) j = J + 32044
    \count2=\count1\relax%
    \advance\count2 by 32044\relax%
    % 3) g = j div 146097
    \count3=\count2\relax%
    \divide\count3 by 146097\relax%
    % 4) dg = j mod 146097
    \count4=\count3\relax%
    \multiply\count4 by-146097\relax%
    \advance\count4 by\count2\relax%
    % 5) c = (dg div 36524 + 1) * 3 div 4
    \count5=\count4\relax%
    \divide\count5 by36524\relax%
    \advance\count5 by1\relax%
    \multiply\count5 by3\relax%
    \divide\count5 by4\relax%
    % 6) dc = dg - c * 36524
    \count6=\count4\relax%
    \count0=\count5\relax%
    \multiply\count0 by-36524\relax%
    \advance\count6 by\count0\relax%
    % 7) b = dc div 1461
    \count7=\count6\relax%
    \divide\count7 by1461\relax%
    % 8) db = dc mod 1461
    \count8=\count7\relax%
    \multiply\count8 by-1461\relax%
    \advance\count8 by\count6\relax%
    % 9) a = (db div 365 + 1) * 3 div 4
    \count9=\count8\relax%
    \divide\count9 by 365\relax%
    \advance\count9 by 1\relax%
    \multiply\count9 by3\relax%
    \divide\count9 by4\relax%
    % 10)da = db - a * 365
    \count10=\count8\relax%
    \count0=\count9\relax%
    \multiply\count0 by-365\relax%
    \advance\count10 by\count0\relax%
    % 11)y = g * 400 + c * 100 + b * 4 + a
    \count11=\count3\relax%
    \multiply\count11 by400\relax%
    \count0=\count5\relax%
    \multiply\count0 by100\relax%
    \advance\count11 by\count0\relax%
    \count0=\count7\relax%
    \multiply\count0 by4\relax%
    \advance\count11 by\count0\relax%
    \advance\count11 by\count9\relax%
    % 12)m = (da * 5 + 308) div 153 - 2
    \count12=\count10\relax%
    \multiply\count12 by5\relax%
    \advance\count12 by 308\relax%
    \divide\count12 by 153\relax%
    \advance\count12 by -2\relax%
    % 13)d = - (m + 4) * 153 div 5 + 122 + da
    \count13=\count12\relax%
    \advance\count13 by 4\relax%
    \multiply\count13 by153\relax%
    \divide\count13 by5\relax%
    \count13=-\count13\relax%
    \advance\count13 by 122\relax%
    \advance\count13 by \count10\relax%
    % 14)Y =  (m + 2) div 12 + y - 4800
    \count14=\count12\relax%
    \advance\count14 by 2\relax%
    \divide\count14 by12\relax%
    \advance\count14 by\count11\relax%
    \advance\count14 by-4800\relax%
    % 15)M = (m + 2) mod 12 + 1
    \count15=\count12\relax%
    \advance\count15 by2\relax%
    \count0=\count15\relax%
    \divide\count0 by12\relax%
    \multiply\count0 by12\relax%
    \advance\count15 by-\count0\relax%
    \advance\count15 by1\relax%
    % 16)D = d + 1
    \count16=\count13%
    \advance\count16by 1\relax%
    %
    \xdef\pgf@temp@year{\the\count14}%
    \xdef\pgf@temp@month{\ifnum\count15<10 0\fi\the\count15}%
    \xdef\pgf@temp@day{\ifnum\count16<10 0\fi\the\count16}%
  }%
  \let#2=\pgf@temp@year%
  \let#3=\pgf@temp@month%
  \let#4=\pgf@temp@day%
}



% Returns the day of week as a number between 0 = Monday and 6 =
% Sunday
%
% #1 = a Julian day number
% #2 = a counter into which the weekday should be put.
%
% Example:
%
% \pgfcalendardatetojulian{2006-01-10}{\mycount}
% \pgfcalendarjuliantoweekday{\mycount}{\myweekday}

\def\pgfcalendarjuliantoweekday#1#2{%
  {%
    \pgfutil@tempcnta=#1\relax%
    #2=\pgfutil@tempcnta%
    \divide#2by7\relax%
    \multiply#2by-7\relax%
    \advance#2by\pgfutil@tempcnta\relax%
    \expandafter%
  }%
  \expandafter#2\the#2\relax%
}



% Converts a day of week into a weekday name (long or short)
%
% #1 = a number representing a weekday (0=Monday)
%
% Example:
%
% \pgfcalendardatetojulian{2006-01-10}{\mycount}
% \pgfcalendarjuliantoweekday{\mycount}{\myweekday}
% October 1st, 2006 was a \pgfcalendarweekdayname{\myweekday}

\def\pgfcalendarweekdayname#1{%
  \pgfutil@translate{\ifcase#1Monday\or Tuesday\or Wednesday\or Thursday\or Friday\or Saturday\or Sunday\fi}%
}

\def\pgfcalendarweekdayshortname#1{%
  \pgfutil@translate{\ifcase#1Mon\or Tue\or Wed\or Thu\or Fri\or Sat\or Sun\fi}%
}


% Converts a month of year number into a month name (long or short)
%
% #1 = a number representing a month (1=January)
%
% Example:
%
% \pgfcalendarmonthname

\def\pgfcalendarmonthname#1{%
  \pgfutil@translate{\ifcase#1\or January\or February\or March\or April\or
    May\or June\or July\or August\or September\or October\or
    November\or December\fi}%
}

\def\pgfcalendarmonthshortname#1{%
  \pgfutil@translate{\ifcase#1\or Jan\or Feb\or Mar\or Apr\or
    May\or Jun\or Jul\or Aug\or Sep\or Oct\or
    Nov\or Dec\fi}%
}






% Macro for easy typesetting of days, etc.
%
% #1 = kind selection
% #2 = length and representation selection
%
% Description:
%
% Replaces the shorthand according to the following rules: The
% first letter of the shorthand describes the kind of
% shorthand. Possible kinds are:
%
% d = day of current date (in an invocation of \pgfcalendar)
% m = month of current date
% y = year of current date
% w = week day of current date
%
% The second parameter determines how the kind is represented:
%
% - = shortest possible numerical way (allowed only for d, m, y)
% = = same, but always of the same length (padded with blanks as
%     needed, allowed only for d, m, y)
% 0 = numerical representation for d and m padded with leading zeros.
% t = textual representation (allowed only for d, m, w)
% . = abbreviated textual representation (allowed only for d, m, w)
%
% It is advised that you say, for example,
% \let\%=\pgfcalendarshorthand.
%
% With this setting, you can typeset an ISO-date by saying \%y0-\%m0-\%d0.
% For another example, on 2007-02-09, which is a
% Friday, you can write "\%wt, \%mt \%d-, \%y0" to get "Friday, February 9, 2007"

\def\pgfcalendarshorthand#1#2{\csname pgfcalendar@shorthand@#1#2\endcsname}
\expandafter\def\csname pgfcalendar@shorthand@d-\endcsname{%
  {\pgfutil@tempcnta=\pgfcalendarcurrentday\relax\the\pgfutil@tempcnta}}
\expandafter\def\csname pgfcalendar@shorthand@d=\endcsname{%
  {\pgfutil@tempcnta=\pgfcalendarcurrentday\relax\ifnum\pgfutil@tempcnta<10\relax\setbox0=\hbox{1}\kern\wd0\relax\fi\the\pgfutil@tempcnta}}
\expandafter\def\csname pgfcalendar@shorthand@d0\endcsname{%
  \pgfcalendarcurrentday}
\expandafter\def\csname pgfcalendar@shorthand@m-\endcsname{%
  {\pgfutil@tempcnta=\pgfcalendarcurrentmonth\relax\the\pgfutil@tempcnta}}
\expandafter\def\csname pgfcalendar@shorthand@m=\endcsname{%
  {\pgfutil@tempcnta=\pgfcalendarcurrentmonth\relax\ifnum\pgfutil@tempcnta<10\relax\setbox0=\hbox{1}\kern\wd0\relax\fi\the\pgfutil@tempcnta}}
\expandafter\def\csname pgfcalendar@shorthand@m0\endcsname{%
  \pgfcalendarcurrentmonth}
\expandafter\def\csname pgfcalendar@shorthand@y-\endcsname{%
  \pgfcalendarcurrentyear}
\expandafter\def\csname pgfcalendar@shorthand@y=\endcsname{%
  \pgfcalendarcurrentyear}
\expandafter\def\csname pgfcalendar@shorthand@y0\endcsname{%
  \pgfcalendarcurrentyear}
\expandafter\def\csname pgfcalendar@shorthand@w.\endcsname{%
  \pgfcalendarweekdayshortname{\pgfcalendarcurrentweekday}}
\expandafter\def\csname pgfcalendar@shorthand@wt\endcsname{%
  \pgfcalendarweekdayname{\pgfcalendarcurrentweekday}}
\expandafter\def\csname pgfcalendar@shorthand@m.\endcsname{%
  \pgfcalendarmonthshortname{\pgfcalendarcurrentmonth}}
\expandafter\def\csname pgfcalendar@shorthand@mt\endcsname{%
  \pgfcalendarmonthname{\pgfcalendarcurrentmonth}}

% Calculate the date of Easter

% Credits to Håkon Malmedal on TeX.SX https://tex.stackexchange.com/a/57243
% and Donald Knuth, TAoCP, Vol 1, 3rd Ed, Section 1.3.2, Algorithm E, p. 160.

\newcount\pgf@cal@easter@Y
\newcount\pgf@cal@easter@G
\newcount\pgf@cal@easter@C
\newcount\pgf@cal@easter@X
\newcount\pgf@cal@easter@Z
\newcount\pgf@cal@easter@D
\newcount\pgf@cal@easter@E
\newcount\pgf@cal@easter@N
\newcount\pgf@cal@easter@M
\newcount\pgf@cal@easter@julianday

\pgfutil@protected\def\pgfcalendareastersunday#1#2{%
  \pgfintset\pgf@cal@easter@Y{#1}%
  % E1. Golden number
  \pgfintset\pgf@cal@easter@G{%
    \pgfintmod{\pgf@cal@easter@Y}{19}+1
  }%
  % E2. Century
  \pgfintset\pgf@cal@easter@C{%
    \pgfintdivfloor{\pgf@cal@easter@Y}{100}+1
  }%
  % E3. Corrections
  \pgfintset\pgf@cal@easter@X{%
    \pgfintdivfloor{3*\pgf@cal@easter@C}{4}-12
  }%
  \pgfintset\pgf@cal@easter@Z{%
    \pgfintdivfloor{8*\pgf@cal@easter@C+5}{25}-5
  }%
  % E4. Find Sunday
  \pgfintset\pgf@cal@easter@D{%
    \pgfintdivfloor{5*\pgf@cal@easter@Y}{4}-\pgf@cal@easter@X-10
  }%
  % E5. Epact (full moon)
  \pgfintset\pgf@cal@easter@E{%
    \pgfintmod{11*\pgf@cal@easter@G+20+\pgf@cal@easter@Z-\pgf@cal@easter@X}{30}
  }%
  \ifnum\pgf@cal@easter@E<0
    \pgfintset\pgf@cal@easter@E{\pgf@cal@easter@E+30}%
  \fi
  \ifnum\pgf@cal@easter@E=25
    \ifnum\pgf@cal@easter@G>11
      \pgfintset\pgf@cal@easter@E{\pgf@cal@easter@E+1}%
    \fi
  \else
    \ifnum\pgf@cal@easter@E=24
      \pgfintset\pgf@cal@easter@E{\pgf@cal@easter@E+1}%
    \fi
  \fi
  % E6. Find full moon
  \pgfintset\pgf@cal@easter@N{44-\pgf@cal@easter@E}%
  \ifnum\pgf@cal@easter@N<21
    \pgfintset\pgf@cal@easter@N{\pgf@cal@easter@N+30}%
  \fi
  % E7. Advance to Sunday
  \pgfintset\pgf@cal@easter@N{%
    \pgf@cal@easter@N+7-\pgfintmod{\pgf@cal@easter@D+\pgf@cal@easter@N}{7}
  }%
  % E8. Get month
  \ifnum\pgf@cal@easter@N>31
    \pgfintset\pgf@cal@easter@N{\pgf@cal@easter@N-31}%
    \pgfintset\pgf@cal@easter@M{4}% April
  \else
    \pgfintset\pgf@cal@easter@M{3}% March
  \fi
  \pgfcalendardatetojulian
    {\pgf@cal@easter@Y-\pgf@cal@easter@M-\pgf@cal@easter@N}%
    {#2}%
}

\pgfkeys{/pgf/calendar/Easter/.cd,.default=0,.code={%
  \pgfcalendareastersunday{\pgfcalendarifdateyear}{\pgf@cal@easter@julianday}%
  \ifnum\pgfcalendarifdatejulian=\pgfinteval{\pgf@cal@easter@julianday+#1}
    \expandafter\pgfcalendarmatchestrue
  \fi}%
}


\endinput
