Skip to content

Instantly share code, notes, and snippets.

@doraTeX
Last active December 21, 2023 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doraTeX/7a78916cec2fa8c9f5f0c88ac0512026 to your computer and use it in GitHub Desktop.
Save doraTeX/7a78916cec2fa8c9f5f0c88ac0512026 to your computer and use it in GitHub Desktop.
%#!uplatex
\documentclass[autodetect-engine,dvipdfmx]{jsarticle}
\usepackage{mydate}
\begin{document}
% テスト:n日後の日付を出力する
2023年4月1日の247日後は\n日後を出力{2023}{4}{1}{247}です。
% テスト:2つの日付の差を出力する
\dateDiff{\year}{\month}{\day}{2024}{1}{13}
大学入学共通テストまで残り\the\dateDiffCount 日です。
% テスト:今日の日付に応じて出力を変える
% 区間[#1,#2]に属するか否かで場合分け
\時限実行{2023/4/1}{2024/3/31}{今は2023年度内です。}{今は2023年度内ではありません。}\par
\時限実行{2024/4/1}{2025/3/31}{今は2024年度内です。}{今は2024年度内ではありません。}\par
% 区間(-∞,#2]に属するか否かで場合分け
\時限実行{}{2023/3/31}{まだ2023年度が到来していません。}{既に2023年度は到来しました。}\par
\時限実行{}{2024/3/31}{まだ2024年度が到来していません。}{既に2024年度は到来しました。}\par
% 区間[#1,+∞)に属するか否かで場合分け
\時限実行{2023/4/1}{}{既に2023年度は到来しました。}{まだ2023年度が到来していません。}\par
\時限実行{2024/4/1}{}{既に2024年度は到来しました。}{まだ2024年度が到来していません。}\par
\end{document}
\RequirePackage{ifthen}
% #1 を #2 で割った余りを算出
\def\moduloop#1#2{%
\numexpr
\ifnum\numexpr((#1)-((#1)/(#2))*(#2))\relax<0
((#1)-((#1)/(#2))*(#2)+(#2))%
\else
((#1)-((#1)/(#2))*(#2))%
\fi
\relax
}
% #1 を #2 で割った商(四捨五入ではなく切り捨て)を算出
\def\truncdiv#1#2{\numexpr(((#1)-\moduloop{#1}{#2})/(#2))\relax}
\newcount\@tempY
\newcount\@tempM
\newcount\@tempD
\newcount\JD
% グレゴリオ暦 → ユリウス通日(結果は \JD に格納される)
\def\YMDtoJD#1#2#3{%
\@tempY=\numexpr#1\relax
\@tempM=\numexpr#2\relax
\@tempD=\numexpr#3\relax
\JD=-32044\relax
\ifnum\@tempM<3
\advance\@tempY\m@ne
\advance\@tempM12\relax
\fi
\advance\@tempY4800\relax
\advance\@tempM-3\relax
\advance\@tempD\m@ne
\@tempcnta=\truncdiv{\@tempY}{400}\relax
\advance\@tempY\numexpr-\@tempcnta*400\relax
\advance\JD\numexpr\@tempcnta*146097\relax
\@tempcnta=\truncdiv{\@tempY}{100}\relax
\advance\@tempY\numexpr-\@tempcnta*100\relax
\advance\JD\numexpr\@tempcnta*36524\relax
\@tempcnta=\truncdiv{\@tempY}{4}\relax
\advance\@tempY\numexpr-\@tempcnta*4\relax
\advance\JD\numexpr\@tempcnta*1461+\@tempY*365\relax
\@tempcnta=\truncdiv{\@tempM}{5}\relax
\advance\@tempM\numexpr-\@tempcnta*5\relax
\advance\JD\numexpr\@tempcnta*153\relax
\@tempcnta=\truncdiv{\@tempM}{2}\relax
\advance\@tempM\numexpr-\@tempcnta*2\relax
\advance\JD\numexpr\@tempcnta*61+\@tempM*31+\@tempD\relax
}
% 2023/12/1 のような文字列からユリウス通日に変換する処理(結果は \JD に格納される)
\def\dateToJD#1{\@dateToJD#1\@nil}
\def\@dateToJD#1/#2/#3\@nil{\YMDtoJD{#1}{#2}{#3}}
\newcount\GregorianY
\newcount\GregorianM
\newcount\GregorianD
% ユリウス通日 → グレゴリオ暦(結果は \GregorianY / \GregorianM / \GregorianD に格納される)
\def\JDtoYMD#1{%
\@tempcnta=\numexpr#1+32044\relax
\GregorianY=-4800\relax
\GregorianM=3\relax
\GregorianD=\@ne
%
\@tempcntb=\truncdiv{\@tempcnta}{146097}\relax
\advance\@tempcnta\numexpr-\@tempcntb*146097\relax
\advance\GregorianY\numexpr\@tempcntb*400\relax
%
\@tempcntb=\truncdiv{\@tempcnta}{36524}\relax
\advance\@tempcnta\numexpr-\@tempcntb*36524\relax
%
\ifnum\@tempcntb=4
\advance\@tempcntb\m@ne
\advance\@tempcnta36524\relax
\fi
\advance\GregorianY\numexpr\@tempcntb*100\relax
%
\@tempcntb=\truncdiv{\@tempcnta}{1461}\relax
\advance\@tempcnta\numexpr-\@tempcntb*1461\relax
\advance\GregorianY\numexpr\@tempcntb*4\relax
%
\@tempcntb=\truncdiv{\@tempcnta}{365}\relax
\advance\@tempcnta\numexpr-\@tempcntb*365\relax
%
\ifnum\@tempcntb=4
\advance\@tempcntb\m@ne
\advance\@tempcnta365\relax
\fi
\advance\GregorianY\@tempcntb
%
\@tempcntb=\truncdiv{\@tempcnta}{153}\relax
\advance\@tempcnta\numexpr-\@tempcntb*153\relax
\advance\GregorianM\numexpr\@tempcntb*5\relax
%
\@tempcntb=\truncdiv{\@tempcnta}{61}\relax
\advance\@tempcnta\numexpr-\@tempcntb*61\relax
\advance\GregorianM\numexpr\@tempcntb*2\relax
%
\@tempcntb=\truncdiv{\@tempcnta}{31}\relax
\advance\@tempcnta\numexpr-\@tempcntb*31\relax
\advance\GregorianM\@tempcntb
%
\advance\GregorianD\@tempcnta
%
\ifnum\GregorianM>12
\advance\GregorianY\@ne
\advance\GregorianM-12\relax
\fi
}
%%%%%%%% n日後の日付を出力する処理
% 用法:\n日後を出力{#1}{#2}{#3}{#4}
% 説明:日付 #1/#2/#3 の #4 日後の日付・曜日を出力する
\def\n日後を出力#1#2#3#4{%
% まずは元日付のユリウス通日を算出
\YMDtoJD{#1}{#2}{#3}%
% 日数を加算
\advance\JD#4
% グレゴリオ暦に復元
\JDtoYMD{\JD}%
% 結果出力
\the\GregorianY 年\the\GregorianM 月\the\GregorianD 日(
% 曜日は7で割ればよい
\ifcase\moduloop{\JD}{7}月\or 火\or 水\or 木\or 金\or 土\or 日\fi
曜日)
}
%%%%%%%% 2つの日付の差を出力する処理
\newcount\dateDiffCount
\newcount\@tempcntc
% 用法:\dateDiff{#1}{#2}{#3}{#4}{#5}{#6}
% 説明:#1年#2月#3日 と #4年#5月#6日 の日数差を求めて \dateDiffCount に代入する
\def\dateDiff#1#2#3#4#5#6{%
% 起点となる日付のユリウス通日を保存
\YMDtoJD{#1}{#2}{#3}%
\@tempcntc=\JD
% 終点となる日付のユリウス通日を算出
\YMDtoJD{#4}{#5}{#6}%
% 差を算出
\dateDiffCount=\numexpr\JD-\@tempcntc\relax
}
%%%%%%%% 現在の日付に応じて出力を変える処理
\newcount\@startJD
\newcount\@endJD
\newcount\@currentJD
% 用法:\時限実行{#1}{#2}{#3}{#4}
% #1: 開始日付(年/月/日 の形式の文字列,空文字列ならば -∞ 扱い)
% #2: 終了日付(年/月/日 の形式の文字列,空文字列ならば +∞ 扱い)
% #3: 今日が [#1, #2] に属している場合に実行する処理
% #4: そうでないときに実行する処理
\newcommand\時限実行[4]{%
\if"#1"\@startJD=-2147483647\else
\dateToJD{#1}%
\@startJD=\JD
\fi
\if"#2"\@endJD=2147483647\else
\dateToJD{#2}%
\@endJD=\JD
\fi
\YMDtoJD{\year}{\month}{\day}%
\@currentJD=\JD
\ifthenelse{\@currentJD<\@startJD\OR\@currentJD>\@endJD}{#4}{#3}%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment