User’s Guide to the Dates package^{∗}

Frank G. Bennett, Jr.^{†}

February 7, 1997

Abstract

This package provides two low-level macros for the parsing of text using L^{A}TEX 2ε.

Dates

1 Introduction

This package provides a macro, \dateread, that can be used to parse date strings, either individually or recursively. The macro expects a human-readable syntax, which it converts into an integer (a “long date”) that can be more easily used for some types of computation. A set of companion macros and token registers is also provided for converting long dates into human-readable form.

Dates may be specified either individually, or as recursive dates within a range. The human-readable syntax for two types of recursive date are built into the package, but an interface for writing specialized extensions in native L^{A}TEX is also provided. The handling of parsed dates is also within user control; it is defined in a single argument fed to \dateread when it is invoked in front of a list of dates.

This package was developed as one of the building blocks of the calendar package, but is released as a separate chunk of code because the date parsing task is a function discrete from the work of building a calendar from constituent blocks of information (which is the main task of the calendar package proper). If other applications are found for this package, that is all to the good. Put my name on it, let me know, and we’ll have a drink together sometime.

This user guide is brief, because the parser is flexible enough that a long catalogue of strictures is unnecessary. The next section describes the syntax for entering dates. It is followed by a brief description of how the macro can be called upon in another package, and notes on extending the parser to provide enhanced functionality. I have also provided guidance notes in the code itself.

∗This file is version number v1.1. It was last revised on 1996/12/27.

†The code contained in this file just provides a smart L^{A}TEX 2εfront end to the calendar macros for TEX that I found on CTAN. The TEX macros work quite well, but lack any documentation. Their provenance is also a mystery. If anyone can tell me who wrote them, I will gladly include an acknowledgement in this documentation.

2 Date Syntax

2.1 General Rules

The rules themselves are explained here first. If you want to cut to the chase, have a look at the examples and experiment a bit with the demo file. It’s all pretty straightforward.

The parser is indifferent to extra spaces and blank lines in dates. It will read a string word by word, and classify each word as a time, a day of the month, the name of a month, a year or a special function. With two exceptions, strings beginning in a number must not mix numbers and other characters — this will produce a low-level error from TEX, and at the moment I can’t think of an economical way of protecting against this.

Any string beginning in a number is interpreted as either a time, a day of the month or a year. A string containing a colon (the : character) is assumed to be a time of day. AM and PM are not recognized; you should use 24-hour format when specifying times. A numeric string containing a hyphen is assumed to be a range of times. In this instance, a colon is not necessary if the time is given in whole-hours. Pure numeric strings are construed as a day of the month if less than 32, or as a year if greater than 31.

Character strings are interpreted on the basis of their first three characters; the rest are ignored. Case is also irrelevant. Months and days of the week (used for defining recursive dates, described below) as well as certain special functions are specified as character strings. Any word that does not match an existing function will be ignored.

Dates are always terminated by a short description of the date, which must be in curly braces. The description should contain plain text only; do not put macros (like \LaTeX) here.

Dates will be processed until \dateread encounters \end{<⟨environment name⟩>}, or the string \relax{}. At the \end of an environment, the processing of the current date will be completed, and the \end will be executed. \relax{} will cause processing to end silently without doing anything on the last item (which is the \relax{} itself).

2.2 Specific Dates

Applying the above rules, the following is a valid list of dates. The use of \dateread is explained below.

      \begin{quote}
      \dateread{\message{\theshorttext}}
      Jan 1 1996{New Year's Day}
           1    January 1996 at 10:00
               {New Year Bash}"\\
      1996
          jan 1 8-20:30
          {New Year Cleaning. Rats.}"
      \end{quote}

Note that the parser is indifferent to word order, and that commas should not be used to separate words.^{1}

2.3 Recursive Dates

2.3.1 Ranges

Any recursive dates (such as Every Monday) must be preceded by one or more range specifications, which fix the time period or periods within which to recurse. A range is simply two specific dates separated by the word to. You may specify multiple ranges; effect will be given to each of them individually.

The following is a valid range specification:

         February  1 1996 {Start First Period}
      to April     1 1996 {End First Period}
         November  3 1996 {Start Second Period}
      to December 24 1996 {End Second Period}

2.3.2 Shorthand Dates

Two types of recursive date can be entered in a human-readable syntax. Entries of both types begin with the word every. To put an event on every occurence of a day of the week within a specified set of subranges, simply indicate the day of the week, followed by a short description in curly braces.

      Every Monday {Monday!}

To put an event at 14-day intervals, beginning with the first occurrence of a specified day of the week within each subrange, add the word other.

      Every other Monday 7:00 {Monday Morning Exercise}

To place an event on the Nth occurrence of a specified day of the week within each month, within subrange, add first, second or third.

      Every third Monday 8:00-9:00
        {Monday Morning Meeting}

2.3.3 Suppressing the Action

There are two ways of suppressing the user-defined action. To quickly comment out an entry, put the word rem in front of it. The date will be read, but the action in the argument to \dateread will not be performed. This can be used, for example, to suppress the appearance of a range in the output text.

The second method uses a toggle, and is meant for use by style authors. Setting:

1In fact, commas do no harm if they follow a string of three or more letters, but it is best to keep things simple and clean.

     \dates@suppress@globaltrue

will cause the user action to be suppressed throughout the invocation of \dateread. In order for the action to be performed on the next invocation, the toggle must be set to false in the external code that calls the parser.

2.3.4 Functions

It is possible to specify dates within the subranges using an arbitrary formula. Entries of this type begin with the word function. This is followed by two control strings, a block of L^{A}TEX code in curly braces, and the usual short description, also in curly braces.

The first control string is the name of a user-defined macro that will find the position of the first desired date after the start of the range. The second is the name of a macro to use in incrementing afterward. The block of L^{A}TEX code contains the definitions of these and any subsidiary macros.

The following will place an entry on every date within the specified subranges.

      Function \myinit \myincrement
       {\def\myinit{%
         \dates@date=\dates@subrange@start}
        \def\myincrement{%
         \advance\dates@date by 1\relax}
       }
       {A Day}

See the code commentary section of the Dates package for information on macros that can be used for manipulating dates.

3 Calling the Parser

As mentioned above, the short description in curly braces can only contain plain text. Printing of variable text (such as the text of the date) is done through the argument to the \dateread macro. This argument is an action that will be performed once after each date in the list is read. The date and the description text may be included by using the appropriate commands. These are:

\theyear  The year (four digits).
\themonth  The full name of the month.
\theshortmonth  The name of the month (3 characters).
\themonthnumber The number of the month.
\theday  The day of the month.
\thetimestart  Starting time for an item.
\thetimeend  Ending time for an item.
\theweekday  The name of the day of the week.
\theshortweekday Abbreviated day of the week (3 characters).
\theshorttext  The text describing the date.
\thelongtext  A longer description of the date.
\thelongdate  The long date (an integer).
\thedays  The maximum number of days this month.

You can also call up information using lower-level registers and macros that contain date-related information. Go to the code and commentary section for further details.

The demonstration file included in this package lists dates on the terminal using the \message command. In a more serious application, text can be printed, stored to registers or boxes, or sent in pre-processed form to an external file.

The following is a valid invocation of the macro:

      \dateread{%
        \message{%
          \theweekday, %
          \theday\space%
          \themonthname\space%
          \theyear}}
      %
      % The list of dates to be parsed
      %
      June 15 1997 {Summer Holiday Begins}
      September 25 1997 {Summer Holiday Ends}
      January 1 1997 {First day of January}
      February 18 1997 {Frank's Birthday}
      Every Monday at 2:00 {Go to the Beach}
      %
      % The end of the list.
      %
      \relax{}

A style package that uses the date parser should of course conceal stretches of code like that above from the end user. If lists of dates are maintained in separate files, you can do something like the following,

      \def\readinput{%
        \dateread{\message{\theweekday^^J}}}
      \def\inputandread{%
        \expandafter\readinput%
          \@@input \filename\relax{}
        }

where \filename contains the name of a file to be read. One would then confirm the existence of the file, then run \inputandread.

4 Extending the Parser

When the parser is used in a package, it may be necessary to modify its behaviour to perform special actions in the course of processing. For example, you may want to provide a facility for setting the range in files containing recursive dates from a single file so that, say, an academic schedule can be moved forward to the next year without a lot of typing. This kind of effect can be achieved by defining a word which diverts processing to a macro that performs the desired action before continuing to digest the list of dates.

The flow of processing can be diverted at two points:

1. Immediately after a word is read; or

2. Immediately after a date and its text have been digested. The parser can be re-entered in any of three states:

1. One that looks for the next word in a date string;

2. One that digests the description text; or

3. One that completes the processing of a date entry and its text. Each of the exit points and reentry states is associated with a macro name.

Diversion is accomplished by defining a word that \lets the exit macro name be the user’s own macro. Reentry is accomplished in the user’s macro by ending with the appropriate macro name. The names the macros to use as exit points are, in the order of the first list above:

1. \dates@@@read
2. \dates@read@exit

The names of macros to use for reentry states are:

1. \dates@@@read@default
2. \dates@@@@read@default
3. \dates@read@exit@default

An extension therefore consists of a user-defined macro, and a user-defined word that invokes \let as appropriate. Words should be padded to exactly three characters using X, and should be appended with the suffix mac. The following defines a word, if, which sets a toggle and continues parsing a date:

    \newif\ifusedif
    \def\myownmacro{%
      \usediftrue%
      \dates@@@read@default}
    \def\ifXmac{%
      \let\dates@@@read=\myownmacro}

This is the same method used in the package itself to provide recursive dates and the function entry type; see the code commentary section for less trivial examples.

5 The Macros

5.1 External Utilities

\@ifoverword A macro for breaking strings into words is provided by a package distributed separately.

1 ⟨∗style⟩

 2 \RequirePackage{overword}

5.2 The Engine Core

\dateread \date@read \date@@read \date@@@read \@gobble@enditalls The interface macro starts by setting \dates@action to be its first argument. It

then opens with the initialization of variables that are zeroed only at the start of a run of dates, and passes control to the next macro in the chain. At this point dates are unsorted, so an alternative format such as HTML should not be generated by the argument text, unless you have a filter for sorting the text after TEX exits. In normal use, events should be sorted and selected first, in the package that calls dates.sty. The argument can be used to choose between modes of L^{A}TEX handling, however. For example, you might want to export the text to a file, or store it directly into a list macro. By inserting a \let into the hook in \dates@analyze, you can put toggles into the event files themselves. This is used in calendar to provide for the selective “freezing” of events files in a pre-processed form.

 3 \long\gdef\dateread#1{%
 4 \csname dates@reset@longtext\endcsname%
 5 \let\dates@read\dates@read@default%
 6 \gdef\dates@action{#1}%
 7 \def\dates@subranges{}%
 8 \dates@subrange@start=0%
 9 \dates@subrange@end=0%
10 \dates@read}

The next macro continues with initialization, and makes uppercase characters active inside a group. Square braces are made grouping characters, so that when the next chained macro runs and grabs everything up to the next curly brace, we will have a means of grouping within the grabbed text. Square braces will be used for grouping in user-defined functions for the automatic generation of dates.

11 \long\gdef\dates@read@default{%
12 \dates@time@start=0%
13 \dates@time@end=0%
14 \setyear{1900}%
15 \dates@month=0%
16 \dates@day=0%
17 \dates@weekd@target=7%
18 \def\theshorttext{}%
19 \def\thelongtext{}%
20 \dates@suppressfalse%
21 \let\dates@@@read=\dates@@@read@default%
22 \let\dates@@@@read=\dates@@@@read@default%
23 \let\dates@read@exit=\dates@read@exit@default%
24 \bgroup%
25  \dates@capsdown%

26  \dates@@read}

Once we have the text with the catcodes set appropriately, we cut off the catcode change and run a macro in front of the gobbled text which will chew it up while looping. The \enditall terminator is a misnomer, really; it ends the parsing of a single date, but we will continue parsing until \end or \relax{} are encountered.

27 \long\def\dates@@read#1#{%
28 \egroup%
29 \dates@@@read #1 \enditall}

The following macro does the chewing. The macro \dates@analyze will alter the values of \dates@@@read and of \dates@read@exit to achieve special effects. Any hooks that redefine these functions should chain to their defaults, which end

in @default.
30 \def\dates@@@read@default{%
31  \@ifoverword\enditall%
32  {\expandafter\dates@analyze\@overword{}%
33  \@gobble@enditalls}%
34  {\expandafter\dates@analyze\@overword{}%
35  \dates@@@read}%
36  {\expandafter\dates@analyze\@overword{}%
37  \ifnum\dates@year>0%  If date exists, process
38  \let\dates@read\relax%       ... and expire
39  \dates@read@exit%   May be a range, so not default
40  \fi%
41  \expandafter\end\@gobbletwo}}%

Package extensions that import files on the fly may produce multiple \enditalls in the scan text for a date. This will chomp all of them. The change involves introducing the following macro via the second argument to \@ifoverword, above.

42 \def\@gobble@enditalls#1{%
43 \@ifnextnparchar\enditall%
44 {\let\next\@gobble@enditalls%
45  \next}%
46 {\dates@@@@read}%
47 {\message{This can't happen}}}

5.3 Selective Handling

5.3.1 Defaults

There are three points at which special handling can be spliced into the parsing routine. Below, the defaults are presented first, followed by alternatives that are built into the package. Other special handling routines can be spliced in, as discussed below under 5.4.

\dates@@@@read \dates@@@@@read The following will read arguments (one mandatory argument in curly braces, one optional one in square braces) and save them for use as text descriptions applying to the date. After the arguments are stored, we chain to the exit routine that will perform the action that the user gave to \dateread.

There are some dirty tricks here involving the square brace. We define the macro globally inside a group, so that our catcode change will not affect other code. Then we set the open brace to be a grouping character like {. We can’t use this inside the definition directly (because TEX will try to keep reading text until it finds the matching brace), so we \let a macro have this token’s value. The \let is global because the value of { is global. Then we use the funny brace macro inside the definition to look for a square brace coming up.

All of this jiggery-pokery is necessary because the \futurelet command (used by \@ifnextchar) will stamp a category code on the brace when it sees it. The end result, though, is that TEX will read all the way to the last closing square brace, not just to the next one it finds. Any square braces inside the text will disappear in printed text, but at least the user will not find the style blowing up in his face.

We also make capital letters active, with \dates@capsdown. This is done in a separately nested group, so that it can be turned off before reading any squarebraced text, without affecting the catcode of the braces.

48 \bgroup
49 \catcode`\[=1
50 \global\let\dates@funny@brace=[
51 \gdef\dates@@@@read@default#1{%
52 \def\theshorttext{#1}%
53 \bgroup%
54  \catcode`\[=1\catcode`\]=2\relax%
55  \bgroup%
56  \dates@capsdown%
57  \@ifnextnparchar\dates@funny@brace%
58  {\egroup%
59  \dates@@@@@read}%
60  {\egroup%
61  \egroup%
62  \dates@read@exit}%
63  {\egroup%
64  \egroup%
65  \dates@read@exit}}
66 \egroup

The optional argument uses bog-standard L^{A}TEX option syntax. The only wrinkle is in the catcode changes invoked within a group in the macro above. These are cut off here.

67 \def\dates@@@@@read#1{%
68 \egroup%
69 \iflongtext%
70  \def\thelongtext{#1}%
71 \fi%
72 \dates@read@exit}

The following is the default value of \dates@read@exit. It fixes the date and rationalizes the registers containing human-readable date information, and saves the date for future reference (i.e. in case we need it as the bottom end of a range). It then performs the action given by the user in the argument to \dates@read, and chains back to continue parsing within the presented list of dates.

73 \def\dates@read@exit@default{%
74 \dates@fix%
75 \caldate%

76 \dates@lastdate\dates@date%
77 \ifdates@suppress@global%
78 \else%
79  \ifdates@suppress%
80  \else%
81 %      \bgroup%
82 %      \dates@capsup%
83  \dates@action%
84 %      \egroup%
85  \fi%
86 \fi%
87 \dates@read}

5.3.2 Recursive Handling

\dates@read@exit@range This is meant to be spliced into the parser immediately before the macro:

     \dates@read@exit@default

It is invoked by the word to in the current date entry. Invoked after the text argument describing the current date has been grabbed, it begins by rationalizing the machine and human readable dates. It then expands the registers containing the starting and ending long dates describing a range into a list macro. The dates are stepped down one and up one day respectively, so that a simple > or < will tell us whether a given date is within a subrange. The list macro will later be used as a series of bounds in the generation of recursive dates and dates defined by a function.

88 \def\dates@read@exit@range{%
89 \dates@fix%
90 \caldate%
91 \dates@subrange@end\dates@date%
92 \dates@reg=\expandafter{\dates@subranges}%
93 \edef\dates@subranges{%
94  \the\dates@reg%
95  \noexpand\delim{\the\dates@subrange@start}%
96  {\the\dates@date}}%
97 \let\dates@read@exit\dates@read@exit@default%
98 \dates@read@exit}
\dates@read@exit@recurse \dates@read@exit@@recurse \dates@read@exit@@@recurse This is the generic routine for generating dates according to a recursively invoked

formula. It is also intended for splicing in in front of the macro:

     \dates@read@exit@default

The first macro in this chain checks to be sure that a list of ranges exists. It then defines the macro used to delimit the list to something that will recurse within a subrange. The list macro is then dumped into the document for complete expansion. When it finishes, control is passed back to the top, so that reading can continue through the list of dates.

99 \def\dates@read@exit@recurse{%
100 \expandafter\ifcat\expandafter$\dates@subranges$%
101  \PackageError{dates}{No range given}%

102  {You need to specify a range before %
103  recursive entries and formulae}%
104 \fi%
105 \let\delim\dates@read@exit@@recurse%
106 \dates@subranges%
107 \dates@read}

This is the macro to which \delim is set above. It sets up registers with the top and bottom boundaries of the range, as given in the list, then sets the current date to be the bottom of the range. It then executes an (arbitrary) function to find the first calculated date above the bottom of the range, rationalizes the humanreadable date registers, and performs the user’s requested action. It then chains to the macro that will loop until the range is exceeded.

108 \def\dates@read@exit@@recurse#1#2{%
109 \dates@subrange@start#1\relax%
110 \dates@subrange@end#2\relax%
111 \global\dates@date=\dates@subrange@start\relax%
112 \caldate%
113 \dates@init%
114 \caldate%
115 \ifdates@suppress@global%
116 \else%
117  \ifdates@suppress%
118  \else%
119 %      \bgroup%
120 %      \dates@capsup%
121  \dates@action%
122 %      \egroup%
123  \fi%
124 \fi%
125 \let\next\dates@read@exit@@@recurse%
126 \dates@read@exit@@@recurse}

This is the macro that does the actual recursion for recursive entries. It increments, checks the date, performs the action if appropriate, otherwise breaks the loop so that control can pass back to \dates@read@exit@recurse.

127 \def\dates@read@exit@@@recurse{%
128 \dates@increment\relax%
129 \ifnum\dates@date<\dates@subrange@end\relax%
130  \caldate%
131  \ifdates@suppress@global%
132  \else%
133  \ifdates@suppress%
134  \else%
135  \dates@action%
136  \fi%
137  \fi%
138 \else%
139  \let\next\relax%
140 \fi%
141 \next}

\dates@init@weekly \dates@increment@weekly The following is the initialization macro for weekly recursion. It moves to the first instance of the targetted day of the week above the bottom of the subrange.

142 \def\dates@init@weekly{%
143 \mod7\dates@date\dates@weekd%
144 \dates@three=\dates@weekd@target\relax%
145 \advance\dates@three by -\dates@weekd\relax%
146 \ifnum\dates@three<0\relax%
147  \advance\dates@three by7%
148 \fi%
149 \global\advance\dates@date by \dates@three\relax}

This is the increment macro used for weekly recursion. It just bumps up the date seven days each time it is invoked.

150 \def\dates@increment@weekly{%
151 \global\advance\dates@date by7\relax}

\dates@init@monthly \dates@increment@monthly The following is the general initialization macro for finding an instance of a day of the week within a month. It begins by setting the value of a macro with which it terminates to be \relax, in the hope that a match will be found the first time it is invoked. We then move the date forward, if necessary, to find the targetted day of the week. When this is found, we set the human-readable register information according to that long date (including the day of the month). Dividing and multiplying by 7 yields the largest multiple of 7 that is equal to or less than the day of the month. If this is 0, we’re in the first week, if 7 we’re in the second, and so forth. If the value of the current day is too low, we move up the date. If it is too high, we move to the first day of the next month, and request a loop back through the same routines.

152 \def\dates@init@monthly{%
153 \let\dates@init@monthly@recurse\relax%
154 \nextday\dates@weekd@target%
155 \caldate%
156 \dates@one\dates@day%
157 \divide\dates@one by 7\relax%
158 \multiply\dates@one by 7\relax%
159 \ifnum\dates@modulo>\dates@one\relax%
160  \dates@two\dates@modulo%
161  \advance\dates@two by -\dates@one%
162  \global\advance\dates@date by \dates@two%
163 \else%
164  \ifnum\dates@modulo<\dates@one\relax%
165  \dates@next@month%
166  \let\dates@init@monthly@recurse\dates@init@monthly%
167  \fi%
168 \fi%
169 \dates@init@monthly@recurse}

This is the general incrementing macro for monthly entries. It’s pretty simple; we just move to the first day of the next month, then run the initialization macro again to find the appropriate date within the month.

170 \def\dates@increment@monthly{%
171 \dates@next@month%
172 \dates@init@monthly}

\dates@@@@read@function \dates@@@@read@@function This is designed to be spliced into the parser immediately before the macro:

     \dates@@@@read@default.

It is invoked, like the other special handling routines, via the \dates@execute hook in the text analyzer (see 5.4 for details). It reads two arguments immediately following the word that triggers it. It stores these for reference. The third argument is read after checking to be sure that we have reached the end of the parsing string, and are looking at the delimiter that ends it. Then this is gobbled, and dumped into the document so that the definitions it contains will take effect. The jiggery-pokery with grouping limits the effect of \makeatletter to the scope of the braces delimiting the user’s function definition, while the macros he defines there can be defined locally.

This third argument should not produce any printed output, and should define, at minimum, two macros, the names of which are given as the first and second arguments following the invoking word in the parsed text. The first of these will initialize to the first date conforming to the formula, counting from the bottom of a range of dates, and should return a long date in \dates@date. The second should increment upward one date from a given date, also returning the result in \dates@date. The demonstration file contains a sample of code which calculates the phases of the moon.

173 \long\def\dates@@@@read@function#1#2{%
174 \def\dates@init{#1}%
175 \def\dates@increment{#2}%
176 \@ifnextnparchar\enditall%
177 {\bgroup%
178  \makeatletter%
179  \expandafter\dates@@@@read@@function\@gobble}%
180 {\PackageError%
181  {dates}%
182  {Extra text after function names}%
183  {The third argument must be in curly braces.}}%
184 {\relax}}
185 \long\def\dates@@@@read@@function#1{%
186  \long\gdef\dates@temp{#1}%
187 \egroup%
188 \dates@temp%
189 \let\dates@temp\relax%
190 \dates@@@@read@default}

5.4 The Analyzer

Human-readable dates are fetched word by word by the above routines. The \dates@@@read@default macro feeds individual words to the analyzer described here.

dates@analyze This is a pretty straightforward set of nested conditions that will uniquely identify an item as a month, a year, a day or a time, and respond accordingly. The macro is somewhat long, so comments are provided to indicate nesting levels and their functions.

191 \long\def\dates@analyze#1#2#{%

Level One Check to see if we’re dealing with a number. If so, define a macro containing only zero to use in a series of parsing checks.

192 \ifcat\noexpand#11%
193  \def\dates@zero{0}%

Level Two Check to see if this is is not a range of times.

194  \expandafter\dates@check@hyphen\@overword-{}%
195  \ifx\dates@arg@b\dates@zero%

Level Three It now looks like this either a lone time or an integer. Check to see if it’s not a time. (We use \@overword, but it will have the same content as

\dates@arg@a).
196  \expandafter\dates@check@colon\@overword:{}%
197  \ifx\dates@arg@b\dates@zero%

Level Four Alright, so it’s an integer. That means it’s either a year or a day of the month. The instructions require a 4-digit year, which is always larger than a day of the month.

198  \expandafter\ifnum\@overword>31%
199  \setyear{\dates@arg@a}%
200  \else%
201  \ifnum\dates@day>0%
202  \dates@dayerror%
203  \else%
204  \global\dates@day=\dates@arg@a%
205  \fi%
206  \fi%

Level Three (again) If the item was a lone time, we set variables accordingly.

207  \else%
208  \expandafter\dates@strip@colon\dates@arg@b%
209  \global\dates@time@start=\dates@arg@a%
210  \multiply\dates@time@start by 60%
211  \advance\dates@time@start by \dates@arg@b%
212  \global\dates@time@end=\dates@time@start%
213  \fi%

Level Two (again) If it was a range of times, we have the extremes of the range in \dates@arg@a and \dates@arg@b. We process each so that a colon delimiter is not required, but will be recognized if given. Note the 0 in the definition of \dates@check@colon et al., above; this produces a default of zero if this is a bare number with no colon delimiter.

We proceed by first setting aside the contents of \dates\arg@b so that they will not be overwritten. We then check for the existence of a colon and, if one is found, the trailing colon left by the test macro is stripped off. We then set the time.

214  \else%
215  \expandafter\dates@strip@hyphen\dates@arg@b%
216  \let\dates@temp\dates@arg@b%
217  \expandafter\dates@check@colon\dates@arg@a:{}%
218  \ifx\dates@arg@b\dates@zero%

219  \else%
220  \expandafter\dates@strip@colon\dates@arg@b%
221  \fi%
222  \dates@time@start=\dates@arg@a\relax%
223  \multiply\dates@time@start by 60\relax%
224  \global\advance\dates@time@start by \dates@arg@b\relax%

Level Two (continued) We then do exactly the same thing for the second time given.

225  \expandafter\dates@check@colon\dates@temp:{}%
226  \ifx\dates@arg@b\dates@zero%
227  \else%
228  \expandafter\dates@strip@colon\dates@arg@b%
229  \fi%
230  \dates@time@end=\dates@arg@a\relax%
231  \multiply\dates@time@end by 60\relax%
232  \global\advance\dates@time@end by \dates@arg@b%
233  \fi%

Level one (again) If \dates@word does not look like a number, then it is handled as text. We pass control to a macro that will simply execute a macro name derived from the first three characters of the word. If it’s unknown chaff, it will therefore effectively be ignored. This can be used as a hook for defining options not included in the dates package proper. The description of the hooks used by the vanilla version of dates is given below.

234 \else%
235  \dates@execute#1#2XXX{}%
236 \fi}

\dates@execute Used as the default action in the analyzer, this just blindly executes a macro that may match a day, month or option name. If the date string is limited by \relax{}, the macro name \relax will be read in as a token for interpretation. To prevent unexpected results, we therefore convert a leading control sequence to a string within \csname \endcsname. We must not give the same treatment to non-control-sequence items, however, because this would prevent the expansion of uppercase characters, which have been read in as active characters, to their lowercase counterparts. An \ifcat is used to choose which action is appropriate. The macros invoked by \dates@execute never take arguments, so they can be expanded within the scope of \ifcat.

237 \def\dates@execute#1#2#3#4#{%
238 \ifcat#1\relax%
239  \expandafter\csname%
240  \string#1#2#3mac\endcsname%
241 \else%
242  \expandafter\csname#1#2#3mac\endcsname%
243 \fi}%

5.5 Fodder for the Analyzer

As can be gleaned from an examination of \dates@execute, a word is read by executing a macro named using its first three letters. We provide definitions for a number of such macros here.

\evemac Every sets up for recursion, and for a default of incrementing over days of the week.

244 \def\evemac{%
245 \global\let\dates@read@exit=\dates@read@exit@recurse%
246 \global\let\dates@increment=\dates@increment@weekly%
247 \global\let\dates@init=\dates@init@weekly}

\othmac Other will modify the effect of every to choose every other date within the range. You can’t choose the starting dates within the subrange — but some automation is better than none.

248 \def\othmac{%
249 \global\let\dates@increment=\dates@increment@semiweekly}

\firmac \secmac \thimac First, second and third will set a counter than controls which instance of a day of the week within a month should be chosen, and modifies the effect of every to recurse on that instance within every month, within the subrange.

250 \def\firmac{%
251 \dates@modulo=0%
252 \global\let\dates@init=\dates@init@monthly%
253 \global\let\dates@increment=\dates@increment@monthly}%

254 %

255 \def\secmac{%
256 \dates@modulo=7%
257 \global\let\dates@init=\dates@init@monthly%
258 \global\let\dates@increment=\dates@increment@monthly}%

259 %

260 \def\thimac{%
261 \dates@modulo=14%
262 \global\let\dates@init=\dates@init@monthly%
263 \global\let\dates@increment=\dates@increment@monthly}%

\1stmac \2ndmac \3rdmac These are just aliases of the macros above.

264 \expandafter\let\csname1stmac\endcsname=\firmac%
265 \expandafter\let\csname2ndmac\endcsname=\secmac%
266 \expandafter\let\csname3rdmac\endcsname=\thimac%

\toXmac The following responds to to in the parsed text. If the year is zero, it sets the bottom of a range to the last date encountered, and lets the exit macro for processing a finished date be one which will set the top of the range before performing the action specified by the user. If the year is greater than zero, we grab the current date, reset everything to zero, and continue parsing. This permits “no frills” range designations, as in the calendar package.

The X is appended to fill out to three characters. For reasons that I do not fully understand, this was necessary to prevent the package from exiting within the scope of a group.

267 \def\toXmac{\relax%
268 \ifnum\dates@year=0%
269  \dates@subrange@start\dates@lastdate%
270 \else%
271  \dates@fix%
272  \dates@subrange@start\dates@date%
273  \dates@month=0%

274  \dates@day=0%
275  \setyear{1900}%
276 \fi%
277 \let\dates@read@exit\dates@read@exit@range}%

278

\funmac The following causes a function definition to be read immediately after the word function in the date text.

279 \def\funmac{%
280 \global\let\dates@read@exit=\dates@read@exit@recurse%
281 \global\let\dates@@@read=\dates@@@@read@function}

The following causes the suppression of any action in respect of the entry in which it is found.

282 \def\remmac{%
283 \global\dates@suppresstrue}

\janmac \jancheckmac The months are defined globally using a recursive engine that is discarded when the macros have been defined. Each month macro first sets a counter to the number of days in the month, so that a range check can be performed when the date is interpreted. It then checks to see if a month has already been selected, and if so it forces a fatal error; otherwise, the month counter is set to the appropriate value.

284 \bgroup
285 \def\monthdata#1#2#3#4#5\end{%
286  \expandafter\gdef\csname#2#3#4mac\endcsname{%
287  \ifnum\dates@month>0%
288  \dates@montherror%
289  \else%
290  \global\dates@month=#1%
291  \fi}%
292  \ifcat$#5$\let\next\@gobble\else\let\next\monthdata\fi%
293  \next#5\end}%

The following does the action generation of month macros.

294 \monthdata1jan2feb3mar4apr5may6jun%
295  7jul8aug9sep{10}oct{11}nov{12}dec\end
296 \egroup

\monmac Days of the week are specified in a similar fashion.

297 \bgroup
298 \def\weekdata#1#2#3#4#5\end{%
299  \expandafter\gdef\csname #2#3#4mac\endcsname{%
300  \ifnum\dates@weekd@target<7\relax%
301  \dates@weekerror%
302  \else%
303  \global\dates@weekd@target=#1\relax%
304  \fi}%
305  \ifcat$#5$\let\next\@gobble\else\let\next\weekdata\fi%
306  \next#5\end}

307

308 \weekdata0sun1mon2tue3wed4thu5fri6sat\end
309 \egroup

\dates@montherror \dates@weekerror \dates@dayerror These three macros are used in the above definitions to produce error messages if something is wrong with a date.

310 \def\dates@montherror{%
311 \PackageError{dates}{Month multiply defined}{%
312  You have set the month more than once here.}}

313 %

314 \def\dates@dayerror{%
315 \PackageError{dates}{Day multiply defined}{%
316  You have set the day more than once here.}}

317 %

318 \def\dates@weekerror{%
319 \PackageError{dates}{Week multiply defined}{%
320  You have set the day of the week more than once here.}}

5.6 Low-level Bits and Pieces

\dates@capsdown The \lowercase function provided in TEX does not operate in TEX’s mouth, and so cannot be used to create lowercase strings for export to a non-DVI file or for display on the terminal. We can get around this problem by making all upper-case characters active, and defining them to lowercase themselves. The operative definition is \dates@capsdown. The grouping culls the bootstrapping definitions used to create these macros from memory. A detailed explanation these macros is not given here, but they do work. Essentially, they function by looping over sets of three characters, two uppercase and one lowercase, building a macro (\dates@capsdown) that will make the uppercase characters in the list locally active, with the appropriate definition. Two uppercase characters are required in the list because on is read to identify the character so that it can be made active in order, on the second read, to take its active form and define it.

321 \bgroup
322 \newtoks\dates@reg
323 \dates@reg={}
324 \def\dates@capsdown{}
325 \def\dates@capsup{}
326 \def\dates@capsactive#1{%
327 \ifx#1\end%
328  \let\next\relax%
329 \else%
330  \catcode`#1=13%
331  \let\next\dates@@capsactive%
332 \fi%
333 \next}
334 \def\dates@@capsactive#1#2{%
335 \dates@reg=\expandafter{%
336  \dates@capsdown\catcode`#1=13\gdef#1{#2}}%
337 \xdef\dates@capsdown{\the\dates@reg}%
338 \dates@reg=\expandafter{%
339  \dates@capsup\edef#1{\string#1}}%
340 \xdef\dates@capsup{\the\dates@reg}%
341 \let\next\dates@capsactive%
342 \next}
343 \dates@capsactive%
344 AAaBBbCCcDDdEEeFFfGGgHHhIIiJJjKKk%

345 LLlMMmNNnOOoPPpQQqRRrSSsTTtUUuVVv%
346 WWwXXxYYyZZz\end
347 \egroup

For parsing strings, we use some little macros to identify strings that have a colon in them (which will be assumed to be a time) or a hyphen (assumed to be a range of times), and to strip those off where necessary.

348 \def\dates@check@colon#1:#2#{%
349 \def\dates@arg@a{#1}%
350 \def\dates@arg@b{0#2}}
351 \def\dates@strip@colon#1:{%
352 \def\dates@arg@b{#1}}
353 \def\dates@check@hyphen#1-#2#{%
354 \def\dates@arg@a{#1}%
355 \def\dates@arg@b{0#2}}
356 \def\dates@strip@hyphen#1-{%
357 \def\dates@arg@b{#1}}

The following macro is used to terminate processing.

358 \expandafter\expandafter\expandafter\def%
359 \expandafter\csname%
360  \string\relaxXXmac\endcsname{%
361  \let\dates@@@@read\relax}

5.7 Interface Macros for Date Info

\theyear \themonth \theshortmonth \themonthnumber \theday \theweekday \theshortweekday \thelongdate \themonthdays \theshorttext \thelongtext These macros can be used to include date information in the text fed to \dateread.

They do not contain unprintable operations, and so can be used to display text on the terminal as well as in a document. All are updated from the long date by the

\caldate command. The macros \theshorttext and \thelongtext are defined

elsewhere on the fly; token registers cannot be used for these because they must be allowed to expand fully on the terminal.

362 \def\theyear{\the\dates@year@full}
363 \def\themonth{\the\dates@monthname}
364 \def\theshortmonth{\the\dates@monthname}
365 \def\themonthnumber{\the\dates@month}
366 \def\theday{\the\dates@day}
367 \def\theweekday{\the\dates@dayname}
368 \def\theshortweekday{\the\dates@shortweekday}
369 \def\thelongdate{\the\dates@date}
370 \def\themonthdays{\the\dates@days}

5.8 Maths Functions for Dates

The following code is an almost direct rip-off of the calendar macros for plain TEX that are stored on CTAN. If anyone knows who the author is, I will be happy to include a credit here. I have made scant effort to document the code, but it works well.

\setyear Set the current year.

371 \def\setyear#1{\dates@year=#1%
372 \advance\dates@year by -1900\calculateyearbase}

\nextyear Advance by one year

373 \def\nextyear{\advance\dates@year by1%
374 \calculateyearbase}

\dates@next@month Advance by one month.

375 \def\dates@next@month{%
376 \ifnum\dates@month=12%
377  \advance\dates@year by 1\calculateyearbase%
378  \dates@month=1%
379  \dates@day=1%
380 \else%
381  \advance\dates@month by 1%
382  \dates@day=1%
383 \fi%
384 \dates@fix\caldate}

Set the current time zone.

\settimezone
385 \def\settimezone#1{\dates@timezone=#1%
386 \multiply\dates@timezone by 1000%
387 \divide\dates@timezone by24}
\calculateyearbase Set the \ifleapyear toggle. Set \dates@yearbase to the number of days passed

from 1900, Jan 0 to New year’s date in the target year.

388 \def\calculateyearbase{%
389 \dates@yearbase=-1%
390 {\count0=\dates@year\divide\count0 by4%
391 \multiply\count0 by4\relax%
392 \ifnum\dates@year=\count0%
393 \global\dates@catch=0%
394 \else%
395 \global\dates@catch=1%
396 \fi}%
397 \ifcase\dates@catch \leapyeartrue\or%
398 \leapyearfalse\fi%
399 {\count0=\dates@year\multiply\count0%
400 by1461\advance\count0 by3%
401 \divide\count0 by4%
402 \global\dates@catch=\count0}%
403 \global\advance\dates@yearbase by\dates@catch\relax%
404 \ifnum\dates@year=0\global\dates@yearbase=0\leapyearfalse\fi}

\monthdays Set the number of days passed at the end of each month in \dates@day.

405 \def\monthdays{%
406 \global\dates@catch\ifcase\dates@month%
407 0\or31\or%
408 59\or90\or120\or151\or181\or212\or%
409 243\or273\or304\or334\or365\fi%
410 {\ifleapyear\ifnum\dates@month>1%
411 \global\advance\dates@catch by1\fi\fi}%
412 \dates@day=\dates@catch}

\dates@fix Return the long date of \dates@month, \dates@day in the year \dates@year in

\dates@date after checking to be sure that the month and the day have been set, if confirmation has been toggled on, and that the number of days in the month conforms to the constraints of reality.

413 \def\dates@fix{%
414 \ifnum\dates@month=0%
415 \ifdates@requiremonth%
416  \PackageError{dates}{Missing month}{%
417  You need to specify a date for the month in this date entry.}%
418 \else%
419  \dates@month=1%
420 \fi%
421 \fi%
422 \ifnum\dates@day=0%
423 \ifdates@requireday%
424  \PackageError{dates}{Missing day of the month}{%
425  You need to specify a day of the month in this date entry.}%
426 \else%
427  \dates@day=1%
428 \fi%
429 \fi%
430 \dates@monthname@get%
431 \ifnum\dates@days<\dates@day%
432 \PackageError{dates}{Day of month out of range}{%
433  There are only \the\dates@days\space days in %
434  \themonth, but you have tried^^J%
435  to specify \the\dates@day.}%
436 \else%
437 \dates@date=\dates@day{\advance\dates@month by-1%
438  \monthdays%
439  \advance\dates@date by\dates@day%
440  \global\advance\dates@date by\dates@yearbase}%
441 \fi}

\mod Long date MOD 7 will give the week day. Sunday is 0 and Saturday is 6. The base number is returned in \dates@three.

442 \def\mod#1#2#3{\dates@three=#2\relax%
443 \dates@four=\dates@three%
444 \divide\dates@three by #1%
445 \multiply\dates@three by #1%
446 \advance\dates@four by -\dates@three%
447 #3=\dates@four}

448

449 \def\dates@year@get{%
450 \dates@year@full\dates@year%
451 \global\advance\dates@year@full by 1900\relax}

\theweekday Set the names of week day (Local).

452 \def\dates@dayname@get{%
453 \mod7\dates@date\dates@weekd%
454 \ifcase\dates@weekd%
455  \global\dates@dayname{Sunday}%
456  \global\dates@shortweekday{Sun}%

457  \or \global\dates@dayname{Monday}%
458  \global\dates@shortweekday{Mon}%
459  \or \global\dates@dayname{Tuesday}%
460  \global\dates@shortweekday{Tue}%
461  \or \global\dates@dayname{Wednesday}%
462  \global\dates@shortweekday{Wed}%
463  \or \global\dates@dayname{Thursday}%
464  \global\dates@shortweekday{Thu}%
465  \or \global\dates@dayname{Friday}%
466  \global\dates@shortweekday{Fri}%
467  \or \global\dates@dayname{Saturday}%
468  \global\dates@shortweekday{Sat}%
469 \fi}

\caldate Gives the usual calendar date for a long date in counter \dates@date. Returned

 in \dates@day, \dates@month and \dates@year and other registers. See the

interface macros above under 5.7.

470 \def\caldate{\dates@year=\dates@date%
471 \multiply\dates@year by4\divide\dates@year by1461%
472 \calculateyearbase%
473 {\advance\dates@date by-\dates@yearbase\dates@month=0%
474 \loop\monthdays\ifnum\dates@day<\dates@date%
475 \global\advance\dates@month by1\repeat%
476 {\advance\dates@month by-1\monthdays%
477 \advance\dates@date by-\dates@day%
478 \global\dates@day=\dates@date}}%
479 \dates@year@get%
480 \dates@monthname@get%
481 \dates@dayname@get}

\dates@monthname@get This stores the names of the months to registers. It is run by \caldate.

482 \def\dates@monthname@get{%
483 \ifcase\dates@month%
484  \or \global\dates@monthname{January}%
485  \global\dates@shortmonthname{Jan}%
486  \global\dates@days=31\relax%
487  \or \global\dates@monthname{February}%
488  \global\dates@shortmonthname{Feb}%
489  \ifleapyear%
490  \global\dates@days=29\relax%
491  \else%
492  \global\dates@days=28\relax%
493  \fi%
494  \or \global\dates@monthname{March}%
495  \global\dates@shortmonthname{Mar}%
496  \global\dates@days=31\relax%
497  \or \global\dates@monthname{April}%
498  \global\dates@shortmonthname{Apr}%
499  \global\dates@days=30\relax%
500  \or \global\dates@monthname{May}%
501  \global\dates@shortmonthname{May}%
502  \global\dates@days=31\relax%
503  \or \global\dates@monthname{June}%

504  \global\dates@shortmonthname{Jun}%
505  \global\dates@days=30\relax%
506  \or \global\dates@monthname{July}%
507  \global\dates@shortmonthname{Jul}%
508  \global\dates@days=31\relax%
509  \or \global\dates@monthname{August}%
510  \global\dates@shortmonthname{Aug}%
511  \global\dates@days=31\relax%
512  \or \global\dates@monthname{September}%
513  \global\dates@shortmonthname{Sep}%
514  \global\dates@days=30\relax%
515  \or \global\dates@monthname{October}%
516  \global\dates@shortmonthname{Oct}%
517  \global\dates@days=31\relax%
518  \or \global\dates@monthname{November}%
519  \global\dates@shortmonthname{Nov}%
520  \global\dates@days=30\relax%
521  \or \global\dates@monthname{December}%
522  \global\dates@shortmonthname{Dec}%
523  \global\dates@days=31\relax%
524 \fi}

\nextday Find the next (previous) day after (before) \dates@date with \mod7=#1.

525 \def\nextday#1{{\count0=#1\mod7\dates@date\dates@weekd%
526 \advance\count0 by-\dates@weekd\relax%
527 \ifnum\count0<0\advance\count0 by7\fi%
528 \global\advance\dates@date by\count0}}
\prevday
529 \def\prevday#1{\snextday#1%
530 \global\advance\dates@date by-7}
\snextday
531 \def\snextday#1{\global\advance\dates@date by1%
532 \nextday#1}
\sprevday
533 \def\sprevday#1{\global\advance\dates@date by-1%
534 \prevday#1}

Convert from Julian date and time in o long date (in local time)

535 \def\jdttol{\advance\dates@date by500%
536 \advance\dates@date by\dates@timezone\divide\dates@date by1000}

5.9 Trigonometric Functions

537 \def\sintable#1{\ifcase #1 0\or100\or199%
538 \or296\or389\or479\or565\or644\or717%
539 \or783\or841\or891\or932\or964\or985%
540 \or997\or1000\or992\or974\or946\or909%
541 \or863\or808\or746\or675\or598\or516%
542 \or427\or335\or239\or141\or42\or-58%
543 \or-158\fi}

Reduces modulo 2\pi (requires positive argument theta): theta := theta

 MOD 2\pi, where theta = count1*10^(-3)
544 \def\twopimod{\count2 =\count1%
545 \divide\count2 by6284 \count3 =1853%
546 \count4 =6283\multiply\count3  by\count2%
547 \multiply\count4  by\count2%
548 \divide\count3  by10000%
549 \advance\count3  by\count4%
550 \advance\count1  by-\count3}%
    v := sin(theta), where v = count4*10^(-3); theta = count1*10^(-3)

theta is reduced MOD 2\pi to be 0<=theta<2\pi by \TWOPIMOD, then linear interpolation is performed using \SINTABLE.

551 \def\sin{\relax%
552 \ifnum\count1<0 \signtrue%
553 \count1=-\count1\else \signfalse\fi%
554 \loop\ifnum\count1>6284\twopimod\repeat\relax%
555 \ifnum\count1>3142%
556 \advance\count1 by-3142%
557 \ifsign\signfalse\else\signtrue\fi\fi%
558 \multiply\count1  by10\count3=\count1%
559 \divide\count3  by1000\count2=\count3%
560 \multiply\count3  by1000%
561 \advance\count3  by-\count1  %
562 \count5 =\sintable{\count2 }%
563 \count4 =\count5\advance\count2  by1%
564 \advance\count4  by -\sintable{\count2}%
565 \multiply\count4  by\count3%
566 \divide\count4  by1000%
567 \advance\count4  by\count5%
568 \ifsign\count4 =-\count4\fi}%
    v := cos(theta), where v = count4*10^(-3); theta = count1*10^(-3).
 \SIN is evaluated on pi/2-theta.
569 \def\cos{\advance\count1  by-1571%
570 \multiply\count1  by-1\sin}%

Linear transformation of T giving theta := a*T+b, where T = count0*10^(-6);

 theta = count1*10^(-3); a = #1*10^3+#2+#3*10^(-3); b = #4*10^(-3)
571 \def\lin#1.#2.#3+#4.{\count1=#3%
572 \count2=#2\count3=#1%
573 \multiply\count1 by\count0%
574 \multiply\count2 by\count0\relax%
575 \multiply\count3 by\count0%
576 \divide\count1 by1000%
577 \advance\count1 by\count2%
578 \divide\count1 by1000%
579 \advance\count1 by\count3%
580 \advance\count1 by #4\relax}

Accumulate value returned by a trigonometric function, scaled by factor f,

 into count6: ac := ac+f*v, where ac = count6*10^(-7) v = value of SIN or
 COS =count4*10^(-3) f = #1*10^(-4)
581 \def\fac#1{\multiply\count4  by #1%
582 \advance\count6  by\count4}
583 \def\id{\count4=\count1}  % Identity

5.10 Initialization

This initializes \uselongtext to the value selected by the user in the package calling dates.

584 \def\dates@initialize@longtext{%
585 \ifuselongtext%
586  \def\dates@reset@longtext{%
587  \longtexttrue}%
588 \else%
589  \def\dates@reset@longtext{%
590  \longtextfalse}%
591 \fi}

Before anything further is executed, the following registers and macros need to be in place.

592 \newcount\dates@year
593 \newcount\dates@timezone
594 \dates@timezone=0
595 \newcount\dates@catch
596 \newcount\dates@day
597 \newcount\dates@month
598 \newcount\dates@date
599 \newcount\dates@one
600 \newcount\dates@two
601 \newcount\dates@three
602 \newcount\dates@four
603 \newcount\dates@weekd
604 \newcount\dates@time@start
605 \newcount\dates@time@end
606 \newcount\dates@subrange@start
607 \newcount\dates@subrange@end
608 \newcount\dates@days
609 \newcount\dates@lastdate
610 \newcount\dates@yearbase
611 \newcount\dates@weekd@target
612 \newcount\dates@modulo
613 \newcount\dates@year@full
614 \newcount\dates@extra@day
615 \newtoks\dates@reg
616 \newtoks\dates@dayname
617 \newtoks\dates@shortweekday
618 \newtoks\dates@monthname
619 \newtoks\dates@shortmonthname
620 \newif\iflongtext
621 \longtexttrue
622 \newif\ifdates@requiremonth
623 \newif\ifdates@requireday
624 \newif\ifdates@suppress
625 \newif\ifdates@suppress@global
626 \newif\ifleapyear
627 \let\delim\relax
628 \def\enditall{END IT ALL}
629 \def\dates@empty{}

630 ⟨/style⟩

6 The Demo File

631 ⟨∗demo⟩

632 \documentclass{minimal}
633 \usepackage{dates}
634 \begin{document}

635

636 \makeatletter

637

638 ⟨demo⟩%

639 ⟨demo⟩% We invoke the \dateread command.

640 ⟨demo⟩%

641 \dateread

642 ⟨demo⟩%

643 ⟨demo⟩% We tell it to report a few details to the terminal.

644 ⟨demo⟩%

645 {\message{\theshorttext}%
646 \message{\theday/\the\dates@month/\the\dates@year}%
647 \ifx\thelongtext\dates@empty%
648 \else%
649 \message{\thelongtext}%
650 \fi}

651 ⟨demo⟩%

652 ⟨demo⟩% The following are ranges.

653 ⟨demo⟩%

654 december 10 1995 {^^JStart of First}
655 to jan     10 1996 {End of First}
656 december 10 1996 {^^JStart of Second}
657 to jan     10 1997 {End of Second^^J}

658 ⟨demo⟩%

659 ⟨demo⟩% The following are specific dates (obviously)

660 ⟨demo⟩%

661 February 18 1997 {^^JFrank's Next Birthday}
662 [The best presents come in small packages.^^JIt
663 would be a terrific present for someone to make
664 this one smaller.]
665 February 18 1998 {^^JFrank's Next Birthday But One}

666 ⟨demo⟩%

667 ⟨demo⟩% These are a couple of recursive dates.

668 ⟨demo⟩%

669 Every Monday {^^JMonday Coffee}
670 Every Third Tuesday {^^JMeeting}

671 %

672 ⟨demo⟩% Here is a sample function definition, which gives
673 ⟨demo⟩% the phases of the moon within the ranges.  The code
674 ⟨demo⟩% was ripped off from the calendar package for plain

675 ⟨demo⟩% TeX 676 ⟨demo⟩%

677

678 function \moonsinit \moonsincrement

679

680 {

681 \newcount\moonno
682 \newif\ifsign

684 \def\firstmoon{\moonno=\dates@year
685 \multiply\moonno by123685
686 \divide\moonno by10000
687 \multiply\moonno by4\advance\moonno by-1
688 \loop\moondate\relax
689 \ifnum\dates@date<\dates@subrange@start
690 \advance\moonno by1\repeat}

691

692 ⟨demo⟩% Compute date for cycle quarter MOONNO

693

694 \def\moondate{{\count0=\moonno
695 \lin 202.126.369+0.\count0=\count1
696 \count6=0
697 \lin 0.2.319+2907.\sin\fac{3}
698 \divide\count6  by1000
699 \lin 365.249.86+7593.\id\fac{1}
700 \divide\count6 by10\count7 =\count6
701 \ifodd\moonno\quarters
702 \else\fullornew\fi\global\dates@date=\count7}
703 \jdttol}

704

705 ⟨demo⟩% Correction for full and new moon

706

707 \def\fullornew{\count6=0
708 \lin -393.0.0+0.
709 \divide\count1 by100000000
710 \advance\count6  by\count1
711 \lin 0.628.300+6269.\sin
712 \multiply\count6  by\count4
713 \lin -7.-700.-369+  928.\sin\fac{  -74}
714 \lin  0. 628. 300+ 6269.\sin\fac{ 1734}
715 \lin  1. 256. 600+12539.\sin\fac{   21}
716 \lin  8. 328. 670+ 5341.\sin\fac{-4068}
717 \lin  8. 538. 220+-4597.\sin\fac{   10}
718 \lin  8. 956. 970+11610.\sin\fac{  -51}
719 \lin 16. 238. 589+-5526.\sin\fac{   -4}
720 \lin 16. 657. 340+10682.\sin\fac{  161}
721 \lin 16. 866. 890+  743.\sin\fac{  104}
722 \lin 17. 285. 640+16951.\sin\fac{   50}
723 \lin 17. 495. 190+ 7013.\sin\fac{    4}
724 \lin 24. 986.  10+16023.\sin\fac{   -4}
725 \lin 25. 195. 560+ 6084.\sin\fac{   -6}
726 \divide\count6  by10000
727 \advance\count7 by\count6 }

728

729 ⟨demo⟩% Correction for quarters

730

731 \def\quarters{\lin -393.0.0+0.
732 \divide\count1  by100000000
733 \count6 =\count1  \lin 0.628.300+6269.
734 \sin\multiply\count6  by\count4
735 \lin -16. -29. -40+-4413.\sin\fac{   40}
736 \lin  -7.-700.-369+  928.\sin\fac{  -47}

737 \lin  -7. -72. -69+ 7198.\sin\fac{  -30}
738 \lin   0. 628. 300+ 6270.\sin\fac{ 1721}
739 \lin   1. 256. 600+12539.\sin\fac{   21}
740 \lin   8. 328. 670+ 5341.\sin\fac{-6280}
741 \lin   8. 538. 220+-4598.\sin\fac{   21}
742 \lin   8. 956. 970+11611.\sin\fac{ -119}
743 \lin  16. 238. 589+-5526.\sin\fac{   -4}
744 \lin  16. 657. 340+10682.\sin\fac{   89}
745 \lin  16. 866. 890+  743.\sin\fac{   79}
746 \lin  17. 285. 640+16952.\sin\fac{    3}
747 \lin  17. 495. 190+ 7013.\sin\fac{    3}
748 \lin  24. 986.  10+16023.\sin\fac{   -4}
749 \lin  25. 195. 560+ 6085.\sin\fac{   -6}
750 \count8=\count6\count6=28000
751 \lin 628. 300. 373+ 6270.\cos\fac{   -4}
752 \lin   8. 328. 670+ 5341.\cos\fac{    3}
753 \count2=\moonno
754 \advance\count2 by-1\divide\count2 by2
755 \ifodd\count2\multiply\count6  by-1 \fi
756 \advance\count6 by\count8
757 \divide\count6  by10000
758 \advance\count7  by\count6 }

759

760 ⟨demo⟩% Events for phases.
761 ⟨demo⟩% Uses PHASE (local).

762

763 \def\phase{{\count0=\moonno
764 \count1=\moonno
765 \divide\count0 by4\multiply\count0 by4
766 \advance\count1 by-\count0\relax
767 \global\edef\themoon{\ifcase\count1
768 New Moon\or First Quarter\or
769 Full Moon\or Last Quarter\fi}}}

770

771 \gdef\moonsinit{%
772 \firstmoon
773 \dates@month=12\dates@day=31\dates@fix\advance\dates@date by1
774 \count1=\dates@date
775 \moondate
776 \phase
777 \caldate}

778

779 \gdef\moonsincrement{%
780 \advance\moonno by 1
781 \moondate\phase
782 \caldate}

783

784 }

785 {\themoon}

786

787 \end{document}

788 ⟨/demo⟩