0

Writing fiction I would like to keep my calendar dates and time spans between events tracking contained within the source itself, and therefore need to do something (I though just as simple as) defining my own dates like

pseudocode:

\newdate{startOfStory}{1900-01-01}
\newdate{fictionalNow}
\setdate{fictionalNow}{startOfStory}
\addtodate{fictionalNow}{42}
\newdate{eventX}
\setdate{eventX}{fictionalNow}
On \displaydate{eventX}, \dateDifference{eventX}{fictionalNow} days after ...

I have been searching for TeX/LaTeX date and time and pretty much only found \today, and looking for libraries, got the impression that datetime2 covers all kinds of formatting \today, but does not allow to define a date by julianday (think it can convert to, having both ways would have done it for me I think) forgot to just enable \addtodate{}{} (which lets me suspect I am missing some simple calendar arithmetic being somewhere that I am missing), pgf printing calendars, again only working with fixed date specification without finding aritmitic, ...

I do asume that I am just missing something very obvious, but -- please someone tell me what/where to look. All kinds of help (except using a register and writing a GregorianClendar module myself) would be much appreciated.

2
  • Some options: a) use Excel or similar, b) use one of the project management tools, c) use the tikz calendar library tikz.dev/library-calender, d) check outhttps://ctan.org/topic/calendar
    – MS-SPO
    Commented Sep 16, 2023 at 12:56
  • Two ways I've used. (1) pgfcalendar with pgfmath or just with counters/TeX calculations. Once you have the Julian date number, it's easy to add 42 or whatever just using LaTeX \addtocounter.... (2) ctan.org/pkg/datenumber. If you look at packages for creating timetables for e.g. teaching you can see how to manipulate the dates since many of them work by, for example, setting a start date and then adding so many days between classes. If you want more help, please post a complete MWE.
    – cfr
    Commented Sep 16, 2023 at 14:37

1 Answer 1

2

I'm sure this is possible with datetime or datetime2 but here's a start with the PGFCalendar package.

Its \pgfcalendardatetojulian macro is used to convert a date in y-m-d[+Δ] format into an integer which can be used to calculate the difference between dates very easily. The macro \pgfcalendarjuliantodate does the opposite and will be used to also store the year, month and date of each date.

The PGFKeys package that's loaded by PGFCalendar anyway is used to store the dates. I've included some error handling.

You can adjust the macros \printdate and \printdays as you wish. While pgfcalendar has support for conversion of month numbers into month names and translation via the translator and babel package, the babel package has some date printing facilities of its own.

Code

\documentclass{article}
\usepackage{pgfcalendar}

% adjust these for babel (\localedate), siunitx (\qty), …
\newcommand*\printdate[3]{\pgfcalendarmonthname{#2} #3, #1}
\newcommand*\printdays[1]{#1}

\makeatletter
\newcommand*\newdate[2]{%
  \pgfcalendardatetojulian{#2}{\count@}%
  \pgfcalendarjuliantodate{\count@}{\Year}{\Month}{\Day}%
  \pgfkeyssetevalue{/storydates/#1}{\the\count@}%
  \pgfkeyssetevalue{/storydates/#1/ymd}{{\Year}{\Month}{\Day}}%
  \ignorespaces}
\newcommand*\setdate[2]{%
  \pgfkeysifdefined{/storydates/#2}{%
    \pgfkeysgetvalue{/storydates/#2}\@temp
    \pgfkeyslet{/storydates/#1}\@temp
    \pgfkeysgetvalue{/storydates/#2/ymd}\@temp
    \pgfkeyslet{/storydates/#1/ymd}\@temp
  }{% Warning and Fallback
    \PackageWarning{story dates}{Date #2 not defined.}%
    \newdate{#1}{\year-\month-\day}}%
  \ignorespaces}
\newcommand*\displaydate[1]{%
  \pgfkeysgetvalue{/storydates/#1/ymd}\@temp
  \expandafter\printdate\@temp}
\makeatother
\newcommand*\addtodate[2]{%
  \pgfkeysifdefined{/storydates/#1}{%
    \pgfkeyssetevalue{/storydates/#1}
      {\the\numexpr\pgfkeysvalueof{/storydates/#1}+#2\relax}%
    \pgfcalendarjuliantodate{\pgfkeysvalueof{/storydates/#1}}{\Year}{\Month}{\Day}%
    \pgfkeyssetevalue{/storydates/#1/ymd}{{\Year}{\Month}{\Day}}%
  }{% Warning and Fallback
    \PackageWarning{story dates}{Date #1 not defined.}%
    \newdate{#1}{\year-\month-\day+#2}}%
  \ignorespaces}
\newcommand*\dateDifference[2]{%
  \pgfkeysifdefined{/storydates/#1}{%
    \pgfkeysifdefined{/storydates/#2}{%
      \expandafter\printdays\expandafter{\the\numexpr\pgfkeysvalueof
        {/storydates/#1}-\pgfkeysvalueof{/storydates/#2}\relax}%
    }{\PackageWarning{story dates}{Date #2 not defined.}}%
  }{\PackageWarning{story dates}{Date #1 not defined.}}}
\begin{document}
\newdate{startOfStory}{1900-01-01}
\setdate{fictionalNow}{startOfStory}
\addtodate{fictionalNow}{42}
\newdate{eventX}{1905-10-12}

On \displaydate{eventX}, \dateDifference{eventX}{fictionalNow}
days after \displaydate{fictionalNow} \dots

\end{document}

Output

On October 12, 1905, 2068 days after February 12, 1900 …

1
  • great, thank you very much indeed.
    – Erik Itter
    Commented Sep 17, 2023 at 13:11

You must log in to answer this question.

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