![从固定日期开始计算,如何添加天数](https://linux22.com/image/469006/%E4%BB%8E%E5%9B%BA%E5%AE%9A%E6%97%A5%E6%9C%9F%E5%BC%80%E5%A7%8B%E8%AE%A1%E7%AE%97%EF%BC%8C%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0%E5%A4%A9%E6%95%B0.png)
在写小说时,我希望将日历日期和事件之间的时间跨度保持在源文件本身中,因此需要做一些事情(我认为很简单)来定义我自己的日期,例如
伪代码:
\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 ...
我一直在搜索 TeX/LaTeX 日期和时间,但几乎只找到了 \today,并正在寻找库,感觉 datetime2 涵盖了所有类型的 \today 格式,但不允许用儒略日定义日期(认为它可以转换为,我想这两种方式都可以为我做到)忘了启用 \addtodate{}{}(这让我怀疑我错过了一些我遗漏的简单日历算术),pgf 打印日历,再次只能使用固定日期规范而没有找到算术,...
我确实认为我只是遗漏了一些非常明显的东西,但是——请有人告诉我该看什么/在哪里看。任何帮助(除了使用寄存器和自己编写 GregorianClendar 模块)都将不胜感激。
答案1
我相信这是可能的datetime
,datetime2
但这里先从PGFCalendar 包。
它的\pgfcalendardatetojulian
宏用于将日期转换为是-米-d[+Δ] 格式转换为整数,可用于轻松计算日期之间的差异。宏\pgfcalendarjuliantodate
执行相反的操作,还将用于存储每个日期的年、月和日。
这PGFKeys 软件包无论如何,PGFCalendar 都会加载它,用于存储日期。我已添加了一些错误处理。
您可以根据需要调整宏\printdate
和。虽然支持将月份数字转换为月份名称并通过和包进行翻译,但该包本身也有一些日期打印功能。\printdays
pgfcalendar
translator
babel
babel
代码
\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}
输出
1905年10月12日,即1900年2月12日之后的2068天……