# Exercise 22: Displaying Times (Solution)

This is a solution to Exercise 22.

The solution to the easy part of the exercise is just:

\newcommand*{\printdatetime}{%
\@printdatetime#1\@endprintdatetime
}

\def\@printdatetime#1-#2-#3 #4:#5:#6#7#8\@endprintdatetime{%
\printdate{#1-#2-#3}\ \timefmt{#4}{#5}{#6#7}{#8}%
}

The solution to the more adventurous part requires some arithmetic:
\newcommand*{\printzuludatetime}{%
\@printzuludatetime#1\@endprintzuludatetime
}

\def\@parsetimezone#1#2#3:#4#5{%
\def\thetimezonehour{#1#2#3}%
\def\thetimezoneminute{#4#5}%
}

\def\@printzuludatetime#1-#2-#3 #4:#5:#6#7#8\@endprintzuludatetime{%
\def\theyear{#1}%
\def\themonth{#2}%
\def\theday{#3}%
\def\thehour{#4}%
\def\theminute{#5}%
\def\thesecond{#6#7}%
\@parsetimezone#8%
\pgfcalendardatetojulian{#1-#2-#3}{\julianday}%
%(\count@ is a temporary scratch count register)
% First adjust the minute offset if non-zero
\ifnum\thetimezoneminute=0\relax
\else
\count@=\theminute\relax
% Add or subtract the offset minute
\ifnum\thetimezonehour<0\relax
\else
\fi
\edef\theminute{\number\count@}%
% Does the hour need adjusting?
\ifnum\count@<0\relax
\edef\theminute{\number\count@}%
% Need to subtract 1 from the hour
% but does the day need adjusting?
\ifnum\thehour=0\relax
\def\thehour{23}%
\else
% Subtract 1 from the hour
\count@ = \thehour\relax
\edef\thehour{\number\count@}%
\fi
\else
% Minute isn't negative. Is it >= 60?
\ifnum\count@>59\relax
\edef\theminute{\number\count@}%
% Add 1 to the hour
\count@ = \thehour\relax
\edef\thehour{\number\count@}%
% Does the day need adjusting?
\ifnum\thehour=24\relax
\def\thehour{00}%
\fi
\fi
\fi
\fi
% Now adjust the hour offset if non-zero
\ifnum\thetimezonehour=0\relax
\else
\count@=\thehour\relax
% Does the day need adjusting?
\ifnum\count@<0\relax
\edef\thehour{\number\count@}%
\else
\ifnum\count@>23\relax
\edef\thehour{\number\count@}%
\else
\edef\thehour{\number\count@}%
\fi
\fi
\fi
\pgfcalendarjuliantodate{\julianday}{\theyear}{\themonth}{\theday}%
\pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
% Display result:
\datefmt[\dayofweek]{\theyear}{\themonth}{\theday}\
\timefmt{\thehour}{\theminute}{\thesecond}{}%
}



Here's the code for the sample document:

\documentclass{article}

\usepackage{mycustomdatetime}

\begin{document}
\section{Easy part}

Display dates including time zone:

\printdatetime{2014-03-25 01:23:15+00:00}

\printdatetime{2014-03-24 23:31:58+01:00}

\printdatetime{2014-03-24 16:28:56-06:00}

\printdatetime{2014-03-24 14:45:23+08:00}

\printdatetime{2014-03-25 03:12:04-04:30}

\printdatetime{2014-03-24 03:45:24-04:30}

\printdatetime{2014-03-24 21:20:24+05:45}

\section{Common Time Zone}
Display dates in UTC+00:00 time zone:

\printzuludatetime{2014-03-25 01:23:15+00:00}

\printzuludatetime{2014-03-24 23:31:58+01:00}

\printzuludatetime{2014-03-24 16:28:56-06:00}

\printzuludatetime{2014-03-24 14:45:23+08:00}

\printzuludatetime{2014-03-25 03:12:04-04:30}

\printzuludatetime{2014-03-24 03:45:24-04:30}

\printzuludatetime{2014-03-24 21:20:24+05:45}

\end{document}