\documentclass[a4paper,11pt,article]{memoir}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[unicode=true,xetex,colorlinks=true,linkcolor=blue,urlcolor=blue,bookmarksnumbered=true,bookmarksdepth=3]{hyperref}



%%% Hyperref (Information in PDF) %%%
\hypersetup{
unicode=true,
pdfauthor={Claus Tøndering},
pdftitle={Bible Online Learner: Technical Documentation}
}


\title{Converting Julian Day Numbers}
\author{Claus Tøndering}
\date{27 December 2023}

\begin{document}

\maketitle

%\section*{Copyright and disclaimer}
%\begin{quote}
%        This document is Copyright \copyright 2023 by Claus Tøndering.\\
%        E-mail: claus@tondering.dk.\\
%        The document may be freely distributed, provided this
%        copyright notice is included and no money is charged for
%        the document.
% 
%        This document is provided ``as is''. No warranties are made as
%        to its correctness.
%\end{quote}

\chapter*{Introduction}

On the web page \url{https://tondering.dk/claus/cal/julperiod.php}, the following formulas are given:

To convert a date in the Gregorian or Julian calendar to a Julian Day number, $JD$, start by calculating these values:

\begin{align}
  a&=\left\lfloor \frac{14-month}{12} \right\rfloor\label{eq-a}\\
  y&=year+4800-a\label{eq-y}\\
  m&=month+12a-3\label{eq-m}
\end{align}

For a date in the Gregorian calendar, calculate:

\begin{equation}
  JD=day+\left\lfloor \frac{153m+2}{5} \right\rfloor + 365y
  + \left\lfloor \frac{y}{4} \right\rfloor
  - \left\lfloor \frac{y}{100} \right\rfloor
  + \left\lfloor \frac{y}{400} \right\rfloor
  -32045\label{eq-jd-greg}
\end{equation}

For a date in the Julian calendar, calculate:

\begin{equation}
  JD=day+\left\lfloor \frac{153m+2}{5} \right\rfloor + 365y
  + \left\lfloor \frac{y}{4} \right\rfloor
  -32083\label{eq-jd-jul}
\end{equation}

The value $JD$ in equations \ref{eq-jd-greg} and \ref{eq-jd-jul} is the Julian Day that starts at noon TT\footnote{Terrestrial Time.} on the specified date.

The algorithm works fine for AD dates. If you want to use it for BC dates, you must first convert
the BC year to a negative year (e.g., 10~BC = -9). The algorithm works correctly for all dates after
4800~BC, i.e. at least for all positive values of the Julian Day.

To convert the other way (i.e., to convert a Julian Day, $JD$, to a day, month, and year) these formulas can be used:

For the Gregorian calendar, calculate:
\begin{align}
  a&=JD+32044\label{eq-greg-a}\\
  b&=\left\lfloor \frac{4a+3}{146097} \right\rfloor\label{eq-greg-b}\\
  c&=a-\left\lfloor \frac{146097b}{4} \right\rfloor\label{eq-greg-c}
\end{align}

For the Julian calendar, calculate:
\begin{align}
b&=0\label{eq-jul-b}\\
c&=JD + 32082\label{eq-jul-c}
\end{align}
 
Then for both calendars, calculate:
\begin{align}
d&=\left\lfloor \frac{4c+3}{1461} \right\rfloor\label{eq-jg-d}\\
e&=c - \left\lfloor \frac{1461d}{4} \right\rfloor\label{eq-jg-e}\\
m&=\left\lfloor \frac{5e + 2}{153} \right\rfloor\label{eq-jg-m}\\
day&=e-\left\lfloor \frac{153m+2}{5} \right\rfloor +1\label{eq-day}\\
month&=m + 3 - 12 \times \left\lfloor \frac{m}{10} \right\rfloor\label{eq-month} \\
year&=100b + d - 4800 +  \left\lfloor \frac{m}{10} \right\rfloor\label{eq-year}
\end{align}

\leavevmode
\\[\bigskipamount]
\noindent
In the following sections I will try to explain the logic behind these formulas.

\chapter*{Converting To A Julian Day Number}

\emph{The formulas are constructed to work based on a year that starts on 1~March with an era that started
4800 years before the Christian era.}

Equations \ref{eq-a} to \ref{eq-m} make the necessary adjustments. In Equation \ref{eq-a}, $a$ is 1
in January and February, 0 in March through December. This value is used in Equation \ref{eq-y} to
adjust the year, subtracting 1 from the year in January and February. Additionally, 4800 is added to
the year to adjust the era and thus ensure that we will be working with positive year numbers\footnote{In
  theory, there is no reason why we could not use negative years, but sticking to positive values
  may be advantageous in some programming languages that do not handle integer division sensibly.}.

In Equation \ref{eq-m}, $m$ becomes 0 in March, 1 in April, \ldots, 9 in December, 10 in January,
and 11 in February.

In other words, $y$ and $m$ are the year and month in an adjusted calendar that starts in March and
whose year is offset by 4800.

Moving the start of the year to 1~March has two advantages: First, it moves the leap day to the end
of the year; second, it gives us a repeated pattern of month lengths:

\begin{tabular}{ll}
Mar, Apr, May, Jun, Jul & 31, 30, 31, 30, 31\\
Aug, Sep, Oct, Nov, Dec & 31, 30, 31, 30, 31\\
Jan, Feb & 31, X
\end{tabular}

Each 5-month period follows the pattern 31, 30, 31, 30, 31 --- a total of 153 days.

In Equations \ref{eq-jd-greg}, \ref{eq-jd-jul}, and \ref{eq-day} we encounter the term
$\left\lfloor \frac{153m+2}{5} \right\rfloor$. For values of $m$ in the range 0\ldots11 this value
grows exactly following the 31-30-pattern just described. This means that
$\left\lfloor \frac{153m+2}{5} \right\rfloor$ tells us how many days there are from 1 March to the
start of month m. The inverse of this formula, $\left\lfloor \frac{5e+2}{153} \right\rfloor$, where
$e$ is a day count, is used in Equation \ref{eq-jg-m}.

In Equation \ref{eq-jd-greg} the Julian Day is calculated as the sum of these five values:

\begin{itemize}
\item $day$, which is the day of the month.
\item $\left\lfloor \frac{153m+2}{5} \right\rfloor$, which tells us how many days there are from the start
  of the current year to the start of month $m$.
\item $365y$, which is the number of days in $y$ years (not counting leap years).
\item $\left\lfloor \frac{y}{4} \right\rfloor - \left\lfloor \frac{y}{100} \right\rfloor +
  \left\lfloor \frac{y}{400} \right\rfloor$, which adds 1 for each leap year. (Years divisible by 4
  are leap years, but years divisible by 100 are not, but years divisible by 400 are leap years.)
\item $-32045$, which adjusts the value so that JD=0 falls on 1~January~4713~BC (Julian) as it should.
\end{itemize}

\Needspace*{5cm}%

In Equation \ref{eq-jd-jul} the Julian Day is calculated in a similar manner with these two differences:

\begin{itemize}
\item $\left\lfloor \frac{y}{4} \right\rfloor$ adds 1 for each leap year. (Years divisible by 4
  are leap years.)
\item $-32083$ adjusts the value so that JD=0 falls on 1~January~4713~BC (Julian).
\end{itemize}

\chapter*{Converting From A Julian Day Number}

For the Gregorian calendar, Equations \ref{eq-greg-a}-\ref{eq-greg-c} calculate three values, $a$,
$b$, and $c$ which can be briefly described thus:

\begin{tabular}{ll}
  $a$ & is the start of a modified Julian period.\\
  $b$ & is the number of centuries since the start of the modified Julian period.\\
  $c$ & is the number of days since the start of the current century.
\end{tabular}

In the Gregorian calendar 400 years contain 97 leap years. Equation \ref{eq-greg-a} adjusts the
start of the Julian period so that it starts with three ``short'' centuries with 24 leap years each
(each having 36524 days) and one ``long'' century with 25 leap years (having 36525 days).

Equation \ref{eq-greg-b} calculates the number of centuries that have passed since the start of our
modified Julian period. The number of days in 400 Gregorian years is 146097, Equation
\ref{eq-greg-b} increases by 1 every century in the pattern: Three ``short'' centuries, one ``long''
century.

Equation \ref{eq-greg-c} calculates the number of days that have passed since the start of the last century.

For the Julian calendar we use Equations \ref{eq-jul-b} and \ref{eq-jul-c} to calculate similar but
simpler values. We set $b$ to zero, and we simply let $c$
be the number of days since the start of a modified Julian period.

Then for both calendars we perform the following calculations:

In Equation \ref{eq-jg-d} we calculate the number of years, $d$, that are contained in $c$ days. Four
years contain 1461 days\footnote{This is always true in the Julian calendar; in the Gregorian
  calendar it is true as long as we stay within one century, which is always the case with $d$}, and
the formula in Equation \ref{eq-jg-d} counts years of length 365, 365, 365, 366 days.

Equation \ref{eq-jg-e} calculate the number of days, $e$, that have passed since the start of the current
year.

Equation \ref{eq-jg-m} and \ref{eq-day} converts $e$ to months and days, using a month pattern of
31-30-31-30-31 as described previously.

Equation \ref{eq-month} changes the start of the month from March to January by adding 3,
and subtracting 12 if $m\ge 10$.

Finally, Equation \ref{eq-year} calculates the year. In the Julian calendar $b$ is 0 and $d$ is
the number of years since the start of the modified Julian period. This means that for a date in the
Julian calendar, Equation \ref{eq-year} simplifies to

\begin{equation}
  year=d - 4800 +  \left\lfloor \frac{m}{10} \right\rfloor
\end{equation}

Here, we take the year count, $d$, adjust the era by subtracting
4800, and add 1 if the current month is January or February.

For the Gregorian calendar Equation \ref{eq-year} calculates 100 times the number of centuries ($b$)
plus the number of years in the current century ($d$). Finally, we adjust the era by subtracting
4800, and adding 1 if the current month is January or February.

\end{document}
