PGF/TikZ Manual | PGFplots | TikZ Editor

TikZ and PGF Manual

Utilities

89 Date and Calendar Utility Macros

This section describes the package pgfcalendar.

89.1 Handling Dates
89.1.1 Conversions Between Date Types
  • \pgfcalendardatetojulian{date}{counter}

  • This macro converts a date in a format to be described in a moment to the Julian day number in the Gregorian calendar. The date should expand to a string of the following form:

    • 1. It should start with a number representing the year. Use \year for the current year, that is, the year the file is being typeset.

    • 2. The year must be followed by a hyphen.

    • 3. Next should come a number representing the month. Use \month for the current month. You can, but need not, use leading zeros. For example, 02 represents February, just like 2.

    • 4. The month must also be followed by a hyphen.

    • 5. Next you must either provide a day of month (again, a number and, again, \day yields the current day of month) or the keyword last. This keyword refers to the last day of the month, which is automatically computed (and which is a bit tricky to compute, especially for February).

    • 6. Optionally, you can next provide a plus sign followed by positive or negative number. This number of days will be added to the computed date.

    Here are some examples:

    • 2006-01-01 refers to the first day of 2006.

    • 2006-02-last refers to February 28, 2006.

    • \year-\month-\day refers to today.

    • 2006-01-01+2 refers to January 3, 2006.

    • \year-\month-\day+1 refers to tomorrow.

    • \year-\month-\day+-1 refers to yesterday.

    The conversion method is taken from the English Wikipedia entry on Julian days.

    Example: \pgfcalendardatetojulian{2007-01-14}{\mycount} sets \mycount to 2454115.

  • \pgfcalendarjuliantodate{Julian day}{year macro}{month macro}{day macro}

  • This command converts a Julian day number to an ISO-date. The Julian day must be a number or counter, the year macro, month macro and day macro must be macro names. They will be set to numbers representing the year, month, and day of the given Julian day in the Gregorian calendar.

    The year macro will be assigned the year without leading zeros. Note that this macro will produce year 0 (as opposed to other calendars, where year 0 does not exist). However, if you really need calendars for before the year 1, it is expected that you know what you are doing anyway.

    The month macro gets assigned a two-digit number representing the month (with a leading zero, if necessary). Thus, the macro is set to 01 for January.

    The day macro gets assigned a two-digit number representing the day of the month (again, possibly with a leading zero).

    To convert a Julian day number to an ISO-date you use code like the following:

    \pgfcalendarjuliantodate{2454115}{\myyear}{\mymonth}{\myday}
    \edef\isodate{\myyear-\mymonth-\myday}
    

    The above code sets \isodate to 2007-01-14.

89.1.2 Checking Dates
  • \pgfcalendarifdate{date}{tests}{code}{else code}

  • This command is used to execute code based on properties of date. The date must be a date in ISO-format. For this date, the tests are checked (to be detailed later) and if one of the tests succeeds, the code is executed. If none of the tests succeeds, the else code is executed.

    Example: \pgfcalendarifdate{2007-02-07}{Wednesday}{Is a Wednesday}{Is not a Wednesday} yields Is a Wednesday.

    The tests is a comma-separated list of key–value pairs. The following are defined by default:

    • all This test is passed by all dates.

    • Monday This test is passed by all dates that are Mondays.

    • Tuesday as above.

    • Wednesday as above.

    • Thursday as above.

    • Friday as above.

    • Saturday as above.

    • Sunday as above.

    • workday Passed by Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays.

    • weekend Passed by Saturdays and Sundays.

    • equals=reference The reference can be in one of two forms: Either, it is a full ISO format date like 2007-01-01 or the year may be missing as in 12-31. In the first case, the test is passed if date is the same as reference. In the second case, the test is passed if the month and day part of date is the same as reference.

      For example, the test equals=2007-01-10 will only be passed by this particular date. The test equals=05-01 will be passed by every first of May on any year.

    • at least=reference This test works similarly to the equals test, only it is checked whether date is equal to reference or to any later date. Again, the reference can be a full date like 2007-01-01 or a short version like 07-01. For example, at least=07-01 is true for every day in the second half of any year.

    • at most=reference as above.

    • between=start reference and end reference This test checks whether the current date lies between the two given reference dates. Both full and short version may be given.

      For example between=2007-01-01 and 2007-02-28 is true for the days in January and February of 2007.

      For another example, between=05-01 and 05-07 is true for the days of the first week of May of any year.

    • day of month=number Passed by the day of month of the date that is number. For example, the test day of month=1 is passed by every first of every month.

    • end of month=number Passed by the day of month of the date that is number from the end of the month. For example, the test end of month=1 is passed by the last day of every month, the test end of month=2 is passed by the second last day of every month. If number is omitted, it is assumed to be 1.

    • Easter=number This test checks whether the given date is Easter Sunday. The optional number can be used for offsets from Easter Sunday, e.g. Easter=-3 for Maundy Thursday, Easter=-2 for Good Friday, Easter=1 for Easter Monday. Since the dates of other Christian holidays are determined by the date of Easter, these can be accessed as well, e.g. Easter=39 for Feast of the Ascension, Easter=49 for Pentecost, and Easter=50 for Whit Monday.

    In addition to the above checks, you can also define new checks. To do so, you must add a new key to the path /pgf/calendar/ using the \pgfkeys command. The job of the code of this new key is to possibly set the -if \ifpgfcalendarmatches to true (if it is already true, no action should be taken) to indicate that the date passes the test setup by this new key.

    In order to perform the test, the key code needs to know the date that should be checked. The date is available through a macro, but a whole bunch of additional information about this date is also available through the following macros:

    • \pgfcalendarifdatejulian is the Julian day number of the date to be checked.

    • \pgfcalendarifdateweekday is the weekday of the date to be checked.

    • \pgfcalendarifdateyear is the year of the date to be checked.

    • \pgfcalendarifdatemonth is the month of the date to be checked.

    • \pgfcalendarifdateday is the day of month of the date to be checked.

    For example, let us define a new key that checks whether the date is a Workers day (May 1st). This can be done as follows:

    \pgfkeys{/pgf/calendar/workers day/.code=%
    {
      \ifnum\pgfcalendarifdatemonth=5\relax
        \ifnum\pgfcalendarifdateday=1\relax
          \pgfcalendarmatchestrue
        \fi
      \fi
    }}
    
89.1.3 Typesetting Dates
  • \pgfcalendarmonthshortname{month number}

  • As above, only an abbreviated version is produced.

    Example: \pgfcalendarmonthshortname{12} yields Dec.

89.1.4 Localization

All textual representations of week days or months (like “Monday” or “February”) are wrapped with \translate commands from the translator package (it this package is not loaded, no translation takes place). Furthermore, the pgfcalendar package will try to load the translator-months-dictionary, if the translator package is loaded.

If you want to use the translator package, it has to be loaded before the pgfcalendar package or, when you are using the calendar TikZ library, before tikz. Otherwise it will not be properly detected.

The net effect of all this is that all dates will be translated to the current language setup in the translator package. See the documentation of this package for more details.

89.2 Typesetting Calendars
  • \pgfcalendarshorthand{kind}{representation}

  • This command can be used inside a \pgfcalendar, where it will expand to a representation of the current day, month, year or day of week, depending on whether kind is d, m, y or w. The representation can be one of the following: -, =, 0, ., and t. They have the following meanings:

    • The minus sign selects the shortest numerical representation possible (no leading zeros).

    • The equal sign also selects the shortest numerical representation, but a space is added to single digit days and months (thereby ensuring that they have the same length as other days).

    • The zero digit selects a two-digit numerical representation for days and months. For years it is allowed, but has no effect.

    • The letter t selects a textual representation.

    • The dot selects an abbreviated textual representation.

    Normally, you should say \let\%=\pgfcalendarshorthand locally, so that you can write \%wt instead of the much more cumbersome \pgfcalendarshorthand{w}{t}.

    ISO form: 2007-01-20, long form: Saturday, January 20, 2007

    \usepackage {pgfcalendar}
    \let\%=\pgfcalendarshorthand
    \pgfcalendar{cal}{2007-01-20}{2007-01-20}
    { ISO form: \%y0-\%m0-\%d0, long form: \%wt, \%mt \%d-, \%y0}
  • \pgfcalendarsuggestedname

  • This macro expands to a suggested name for nodes representing days in a calendar. If the prefix is empty, it expands to the empty string, otherwise it expands to the prefix of the calendar, followed by a hyphen, followed by the ISO format version of the date. Thus, when the date 2007-01-01 is typeset in a calendar for the prefix mycal, the macro expands to mycal-2007-01-01.