Debian Codegraph @

texmf-dist/tex/generic/tikz-ext/pgfcalendar-ext.code.tex

raw
1 % Copyright 2022 by Qrrbrbirlbel
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Free Documentation License.
7 %
8
9 \unless\ifcsname tikzextset\endcsname
10 \input tikzext-util.tex
11 \fi
12
13 \pgfqkeys{/pgf/calendar/ext}{
14 style/.style={#1},
15 yesterday/.value required,
16 yesterday/.code={%
17 \begingroup
18 \pgfutil@tempcnta=\pgfcalendarifdatejulian\relax
19 \advance\pgfutil@tempcnta by -1\relax
20 \pgfcalendarjuliantodate{\pgfutil@tempcnta}{\pgfcalendarifdateyear}{\pgfcalendarifdatemonth}{\pgfcalendarifdateday}%
21 \edef\pgfcalendarifdatejulian{\the\pgfutil@tempcnta}%
22 \pgfcalendarjuliantoweekday\pgfutil@tempcnta\pgfutil@tempcntb
23 \edef\pgfcalendarifdateweekday{\the\pgfutil@tempcntb}%
24 \pgfcalendarjulianyeartoweek\pgfutil@tempcnta\pgfcalendarifdateyear\pgfutil@tempcntb
25 \edef\pgfcalendarifdateweek{\the\pgfutil@tempcntb}%
26 \pgfcalendar@launch@ifdate{#1}{%
27 \def\pgf@cal@temp{\pgfcalendarmatchestrue}%
28 }{%
29 \let\pgf@cal@temp\pgfutil@empty
30 }%
31 \expandafter
32 \endgroup\pgf@cal@temp
33 },
34 relative/.code 2 args={%
35 \begingroup
36 \pgfutil@tempcnta=\pgfcalendarifdatejulian\relax
37 \advance\pgfutil@tempcnta by #1\relax
38 \pgfcalendarjuliantodate{\pgfutil@tempcnta}{\pgfcalendarifdateyear}{\pgfcalendarifdatemonth}{\pgfcalendarifdateday}%
39 \edef\pgfcalendarifdatejulian{\the\pgfutil@tempcnta}%
40 \pgfcalendarjuliantoweekday\pgfutil@tempcnta\pgfutil@tempcntb
41 \edef\pgfcalendarifdateweekday{\the\pgfutil@tempcntb}%
42 \pgfcalendarjulianyeartoweek\pgfutil@tempcnta\pgfcalendarifdateyear\pgfutil@tempcntb
43 \edef\pgfcalendarifdateweek{\the\pgfutil@tempcntb}%
44 \pgfcalendar@launch@ifdate{#2}{%
45 \def\pgf@cal@temp{\pgfcalendarmatchestrue}%
46 }{%
47 \let\pgf@cal@temp\pgfutil@empty
48 }%
49 \expandafter
50 \endgroup\pgf@cal@temp
51 },
52 Jan/.code={\ifnum\pgfcalendarifdatemonth=1 \expandafter\pgfcalendarmatchestrue\fi},Jan/.value forbidden,
53 Feb/.code={\ifnum\pgfcalendarifdatemonth=2 \expandafter\pgfcalendarmatchestrue\fi},Feb/.value forbidden,
54 Mar/.code={\ifnum\pgfcalendarifdatemonth=3 \expandafter\pgfcalendarmatchestrue\fi},Mar/.value forbidden,
55 Apr/.code={\ifnum\pgfcalendarifdatemonth=4 \expandafter\pgfcalendarmatchestrue\fi},Apr/.value forbidden,
56 May/.code={\ifnum\pgfcalendarifdatemonth=5 \expandafter\pgfcalendarmatchestrue\fi},May/.value forbidden,
57 Jun/.code={\ifnum\pgfcalendarifdatemonth=6 \expandafter\pgfcalendarmatchestrue\fi},Jun/.value forbidden,
58 Jul/.code={\ifnum\pgfcalendarifdatemonth=7 \expandafter\pgfcalendarmatchestrue\fi},Jul/.value forbidden,
59 Aug/.code={\ifnum\pgfcalendarifdatemonth=8 \expandafter\pgfcalendarmatchestrue\fi},Aug/.value forbidden,
60 Sep/.code={\ifnum\pgfcalendarifdatemonth=9 \expandafter\pgfcalendarmatchestrue\fi},Sep/.value forbidden,
61 Oct/.code={\ifnum\pgfcalendarifdatemonth=10 \expandafter\pgfcalendarmatchestrue\fi},Oct/.value forbidden,
62 Nov/.code={\ifnum\pgfcalendarifdatemonth=11 \expandafter\pgfcalendarmatchestrue\fi},Nov/.value forbidden,
63 Dec/.code={\ifnum\pgfcalendarifdatemonth=12 \expandafter\pgfcalendarmatchestrue\fi},Dec/.value forbidden,
64 %
65 leap year/.code={%
66 \pgfutil@tempcnta=#1\relax
67 \divide\pgfutil@tempcnta4
68 \multiply\pgfutil@tempcnta4
69 \ifnum\pgfutil@tempcnta=#1\relax
70 \divide\pgfutil@tempcnta100
71 \multiply\pgfutil@tempcnta100
72 \ifnum\pgfutil@tempcnta=#1\relax
73 \divide\pgfutil@tempcnta400
74 \multiply\pgfutil@tempcnta400
75 \ifnum\pgfutil@tempcnta=#1\relax
76 \pgfcalendarmatchestrue
77 \fi
78 \else
79 \pgfcalendarmatchestrue
80 \fi
81 \fi},
82 leap year/.default=\pgfcalendarifdateyear,
83 between days/.value required,
84 between days/.code args={#1and#2}{%
85 \pgfutil@tempcnta=#1\relax
86 \ifnum\pgfcalendarifdateday<\pgfutil@tempcnta\else
87 \pgfutil@tempcnta=#2\relax
88 \ifnum\pgfcalendarifdateday>\pgfutil@tempcnta\else
89 \pgfcalendarmatchestrue\fi\fi},
90 week of month/.code={%
91 \pgfutil@tempcnta=#1\relax
92 \multiply\pgfutil@tempcnta7
93 \ifnum\pgfcalendarifdateday>\pgfutil@tempcnta\else
94 \advance\pgfutil@tempcnta-7
95 \ifnum\pgfcalendarifdateday>\pgfutil@tempcnta
96 \pgfcalendarmatchestrue
97 \fi
98 \fi},
99 week of month'/.code={%
100 \pgfextcalendar@getlastYMX\pgfcalendarifdateyear\pgfcalendarifdatemonth\pgfutil@tempcnta
101 \advance\pgfutil@tempcnta1
102 \pgfutil@tempcntb=#1\relax
103 \multiply\pgfutil@tempcntb7
104 \advance\pgfutil@tempcnta-\pgfutil@tempcntb
105 \ifnum\pgfcalendarifdateday<\pgfutil@tempcnta\else
106 \advance\pgfutil@tempcnta+7
107 \ifnum\pgfcalendarifdateday<\pgfutil@tempcnta
108 \pgfcalendarmatchestrue
109 \fi
110 \fi},
111 first/.code={\pgfutil@in@:{#1}\ifpgfutil@in@
112 \pgfextcalendar@ifdate@first{}#1\pgf@stop\else
113 \pgfextcalendar@ifdate@first{}1:#1\pgf@stop\fi},
114 last/.code={\pgfutil@in@:{#1}\ifpgfutil@in@
115 \pgfextcalendar@ifdate@first'#1\pgf@stop\else
116 \pgfextcalendar@ifdate@first'1:#1\pgf@stop\fi},
117 % logic
118 not/.value required,
119 not/.code=%
120 \begingroup
121 \let\pgf@cal@tempa\pgfutil@empty
122 \pgfcalendar@launch@ifdate{#1}{}{\def\pgf@cal@tempa{\pgfcalendarmatchestrue}}%
123 \expandafter\endgroup\pgf@cal@tempa,
124 and/.value required,
125 and/.code=% and = {<cond 1>, <cond 2>, <cond 3>, …}
126 \begingroup
127 \pgfcalendarmatchestrue
128 \pgfqkeys{/pgf/calendar/ext/and}{#1}%
129 \ifpgfcalendarmatches % is it still true?
130 \expandafter\pgfutil@firstoftwo
131 \else
132 \expandafter\pgfutil@secondoftwo
133 \fi
134 {\def\pgf@cal@temp{\pgfcalendarmatchestrue}}%
135 {\let\pgf@cal@temp\pgfutil@empty}%
136 \expandafter\endgroup\pgf@cal@temp,
137 and/.unknown/.code=% only inside the group of and/.code
138 \ifpgfcalendarmatches
139 \expandafter\pgfutil@firstofone
140 \else
141 \expandafter\pgfutil@gobble
142 \fi
143 {%
144 \begingroup
145 \pgfcalendar@launch@ifdate{\pgfkeyscurrentname={#1}}%
146 {\let\pgf@cal@temp\pgfutil@empty}{\def\pgf@cal@temp{\pgfcalendarmatchesfalse}}
147 \expandafter\endgroup\pgf@cal@temp
148 },%
149 calendar week of month/.default=1,
150 calendar week of month'/.default=1,
151 calendar week of month/.code={%
152 \begingroup
153 \pgfutil@tempcnta=\pgfcalendarifdateday\relax
154 \advance\pgfutil@tempcnta by 5
155 \advance\pgfutil@tempcnta by -\pgfcalendarifdateweekday\relax
156 \divide\pgfutil@tempcnta by 7
157 \advance\pgfutil@tempcnta by -#1\relax
158 \expandafter\endgroup\expandafter
159 \ifnum\the\pgfutil@tempcnta=-1
160 \pgfcalendarmatchestrue
161 \fi},
162 calendar week of month'/.code={%
163 \begingroup
164 \pgfextcalendar@getlastYMX\pgfcalendarifdateyear\pgfcalendarifdatemonth\pgfutil@tempcnta
165 \advance\pgfutil@tempcnta by -\pgfcalendarifdateday\relax
166 \edef\pgfcalendarifdateday{\the\pgfutil@tempcnta}%
167 \pgfutil@tempcnta=-\pgfcalendarifdateweekday\relax
168 \advance\pgfutil@tempcnta by 5
169 \edef\pgfcalendarifdateweekday{\the\pgfutil@tempcnta}%
170 \let\pgf@cal@temp\pgfutil@empty
171 \pgfcalendar@launch@ifdate{ext/calendar week of month={#1}}{\def\pgf@cal@temp{\pgfcalendarmatchestrue}}{}%
172 \expandafter\endgroup\pgf@cal@temp}}
173
174 \def\pgfextcalendar@ifdate@first#1#2:#3\pgf@stop{%
175 \pgfqkeys{/pgf/calendar/ext}{and={#3, week of month#1={#2}}}}
176 \def\pgfextcalendar@getlastYMX#1#2#3{% #1 = year, #2 = month, #3 := last day
177 \begingroup
178 \ifnum#2=2 % stupid February
179 \pgfcalendarmatchesfalse
180 \pgfqkeys{/pgf/calendar/ext}{leap year={#1}}%
181 \ifpgfcalendarmatches
182 #3=29
183 \else
184 #3=28
185 \fi
186 \else
187 #3=\ifcase#2\relax\or
188 31\or\or31\or30\or31\or30\or31\or31\or30\or31\or30\or31\fi
189 \fi
190 \edef\pgf@cal@temp{#3=\the#3\relax}
191 \expandafter
192 \endgroup\pgf@cal@temp}
193
194 %
195 % weeks
196 %
197 \def\pgfextcalendar@week@setup#1{%
198 \pgfutil@IfUndefined{pgfextcalendar@week@#1}{%
199 \begingroup
200 \pgfcalendardatetojulian{#1-01-01}\pgfutil@tempcnta
201 \pgfcalendarjuliantoweekday\pgfutil@tempcnta\pgfutil@tempcntb
202 %
203 % tempcnta holds the julian number for first day of the current year
204 % tempcntb holds the weekday for the first day of the current year
205 %
206 % set tempcnta to the Monday of the week with first day of current year
207 \advance\pgfutil@tempcnta by -\pgfutil@tempcntb
208 %
209 % if the first week starts at Fri, Sat or Sun, next week is the 1st week
210 \ifnum\pgfutil@tempcntb>3\relax
211 \advance\pgfutil@tempcnta by 7\relax
212 \fi
213 % setup macro for year with {Julian number for day of first week}{weekday of -01-01}
214 \expandafter\xdef\csname pgfextcalendar@week@#1\endcsname{{\the\pgfutil@tempcnta}{\the\pgfutil@tempcntb}}%
215 \endgroup
216 }{}%
217 }
218
219 \def\pgfextcalendarjulianyeartoweek#1#2#3{\pgfextcalendarjulianyeartoweek@{#1}{#2}{#3}{\iftrue}}
220 \def\pgfextcalendarjulianyeartoweek@#1#2#3#4{%
221 % #1 = julian date (count)
222 % #2 = year
223 % #3 = count that holds the week at the end
224 % #4 = \iftrue or \iffalse: whether week 53 needs to be checked (\iffalse when determining week from next year)
225 \begingroup
226 \pgfextcalendar@week@setup{#2}%
227 #3=#1\relax
228 %
229 % calculate difference of days between current date and start of week 1
230 %
231 \advance#3 by -\expandafter\expandafter\expandafter\pgfutil@firstoftwo\csname pgfextcalendar@week@#2\endcsname\relax
232 \ifnum#3<0\relax % whoops, we are in the week of the previous year
233 \expandafter\pgfutil@firstoftwo
234 \else
235 \expandafter\pgfutil@secondoftwo
236 \fi
237 {% if first day of the year is Fri, Sat or Sun
238 \ifnum\expandafter\expandafter\expandafter\pgfutil@secondoftwo\csname pgfextcalendar@week@#2\endcsname>3\relax
239 \expandafter\pgfutil@firstoftwo
240 \else
241 \expandafter\pgfutil@secondoftwo
242 \fi
243 {% we need to check the week of the previous year
244 #3=#2\relax
245 \advance#3 by -1
246 \edef\pgf@cal@temp{\noexpand\pgfextcalendarjulianyeartoweek@{#1}{\the#3}{#3}\noexpand\iffalse}%
247 \pgf@cal@temp
248 }{% yeah, it's weird
249 \divide#3 by 7
250 \advance#3 by 1
251 }%
252 }{%
253 \divide#3 by 7
254 \advance#3 by 1
255 #4%
256 \expandafter\pgfutil@firstofone
257 \else
258 \expandafter\pgfutil@gobble
259 \fi
260 {%
261 \ifnum#3=53\relax % whoops, we are possibly in the first week of the next year
262 \expandafter\pgfutil@firstofone
263 \else
264 \expandafter\pgfutil@gobble
265 \fi
266 {%
267 \begingroup
268 % check whether we're already in week 1 of the next year
269 #3=#2\relax
270 \advance#3 by 1
271 \expandafter\pgfextcalendar@week@setup\expandafter{\the#3}%
272 \ifnum#1<\expandafter\expandafter\expandafter\pgfutil@firstoftwo\csname pgfextcalendar@week@\the#3\endcsname\relax
273 #3=53
274 \else
275 #3=1
276 \fi
277 \expandafter
278 \endgroup\expandafter#3\the#3\relax
279 }%
280 }%
281 }%
282 \expandafter
283 \endgroup\expandafter
284 #3\the#3\relax
285 }
286
287 %
288 % shorthands for weeks (n)
289 %
290 % n-: shortest
291 % n=: shortest but prepends whitespace
292 % n0: leading zero
293 %
294 \expandafter\def\csname pgfcalendar@shorthand@n-\endcsname{%
295 \if0\pgfextcalendarcurrentweek\else\pgfextcalendarcurrentweek\fi}
296 \expandafter\def\csname pgfcalendar@shorthand@n=\endcsname{%
297 {\pgfutil@tempcnta=\pgfextcalendarcurrentweek\relax\ifnum\pgfutil@tempcnta<10\relax\setbox0=\hbox{1}\kern\wd0\relax\fi\the\pgfutil@tempcnta}}
298 \expandafter\def\csname pgfcalendar@shorthand@n0\endcsname{%
299 \pgfextcalendarcurrentweek}
300
301 \let\tikzextorig@pgfcalendar\pgfcalendar
302 \long\def\pgfcalendar#1#2#3#4{%
303 \tikzextorig@pgfcalendar{#1}{#2}{#3}{%
304 \pgfextcalendarjulianyeartoweek{\pgfcalendarcurrentjulian}{\pgfcalendarcurrentyear}{\pgfutil@tempcntb}%
305 \edef\pgfextcalendarcurrentweek{\ifnum\pgfutil@tempcntb<10 0\fi\the\pgfutil@tempcntb}%
306 #4%
307 }%
308 }
309
310 %
311 % Patching original \pgfcalendar@local@ifdate
312 %
313 \tikzextutil@prefixtomacro\pgfcalendar@local@ifdate{%
314 \let\pgfextcalendarifdateweekday=\pgfextcalendarcurrentweekday
315 }
316 %
317 % Overwriting original \pgfcalendarifdate
318 %
319
320 \let\tikzextorig@pgfcalendar@launch@ifdate\pgfcalendar@launch@ifdate
321 \def\pgfcalendar@launch@ifdate{%
322 \pgfextcalendarjulianyeartoweek{\pgfcalendarifdatejulian}{\pgfcalendarifdateyear}{\pgfutil@tempcntb}%
323 \edef\pgfextcalendarifdateweek{\ifnum\pgfutil@tempcntb<10 0\fi\the\pgfutil@tempcntb}%
324 %
325 \tikzextorig@pgfcalendar@launch@ifdate
326 }
327
328 \pgfqkeys{/pgf/calendar/ext/week}{.value required,.code={\ifnum#1=\pgfcalendarifdateweek\relax\expandafter\pgfcalendarmatchestrue\fi}}
329
330 % Overwriting shorthands of pgfcalendar
331 \expandafter\def\csname pgfcalendar@shorthand@d-\endcsname{%
332 \if0\pgfcalendarcurrentday\else\pgfcalendarcurrentday\fi}
333 \expandafter\def\csname pgfcalendar@shorthand@m-\endcsname{%
334 \if0\pgfcalendarcurrentmonth\else\pgfcalendarcurrentmonth\fi}
335
336 %%% Compatibility
337 \tikzextset{
338 /tikz-ext/compat/add library={pgfcalendar-ext}{pre 0.6},
339 compat/pgfcalendar-ext/warn/.append code={%
340 \pgfutil@for\tikzext@temp:=Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,%
341 leap year,and,not,week of month,week of month',calendar week of month,%
342 calendar week of month',yesterday,week\do{%
343 \pgfkeys@expanded{\noexpand\tikzextutil@compatwarning@key
344 {pgfcalendar-ext}{pre 0.6}{/pgf/calendar/\tikzext@temp}{/pgf/calendar/ext/\tikzext@temp}%
345 }%
346 }%
347 \pgfkeyssetvalue{/pgf/calendar/leap year/.@def}{\pgfcalendarifdateyear}%
348 \pgfkeyssetvalue{/pgf/calendar/calendar week of month/.@def}{1}%
349 \pgfkeyssetvalue{/pgf/calendar/calendar week of month'/.@def}{1}%
350 \tikzextutil@compatwarning@cmd{pgfcalendar-ext}{pre 0.6}
351 {pgfcalendarjulianyeartoweek}\pgfextcalendarjulianyeartoweek\tikzextutil@gobblethree
352 \def\pgfcalendarcurrentweek{\pgfextcalendarcurrentweek}% no warning possible
353 \def\pgfcalendarifdateweek{\pgfextcalendarifdateweek}% no warning possible
354 },
355 compat/pgfcalendar-ext/pre 0.6/.append code={%
356 \pgfutil@for\tikzext@temp:=Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,%
357 leap year,and,not,week of month,week of month',calendar week of month,%
358 calendar week of month',yesterday,week\do{%
359 \pgfkeysedef{/pgf/calendar/\tikzext@temp}{%
360 \noexpand\pgfkeysvalueof{/pgf/calendar/ext/\tikzext@temp/.@cmd}##1\noexpand\pgfeov
361 }%
362 \let\pgfcalendarjulianyeartoweek\pgfextcalendarjulianyeartoweek
363 \def\pgfcalendarcurrentweek{\pgfextcalendarcurrentweek}%
364 \def\pgfcalendarifdateweek{\pgfextcalendarifdateweek}%
365 }%
366 },
367 compat/pgfcalendar-ext/\pgfkeysvalueof{/tikz/ext/compat/pgfcalendar-ext}%
368 }
369 \endinput