About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Smile for the Camera: a new cybercrime short story ebook.

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}[1]{%
  \@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}[1]{%
  \@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
      \advance\count@ by \thetimezoneminute\relax
    \else
      \advance\count@ by -\thetimezoneminute\relax
    \fi
    \edef\theminute{\number\count@}%
% Does the hour need adjusting?
    \ifnum\count@<0\relax
      \advance\count@ by 60\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}%
% Day needs adjusting.
       \advance\julianday by -1\relax
      \else
% Subtract 1 from the hour
        \count@ = \thehour\relax
        \advance\count@ by -1\relax
        \edef\thehour{\number\count@}%
      \fi
    \else
% Minute isn't negative. Is it >= 60?
      \ifnum\count@>59\relax
        \advance\count@ by -60\relax
        \edef\theminute{\number\count@}%
% Add 1 to the hour
        \count@ = \thehour\relax
        \advance\count@ by 1\relax
        \edef\thehour{\number\count@}%
% Does the day need adjusting?
        \ifnum\thehour=24\relax
          \def\thehour{00}%
          \advance\julianday by 1\relax
        \fi
      \fi
    \fi
  \fi
% Now adjust the hour offset if non-zero
  \ifnum\thetimezonehour=0\relax
  \else
    \count@=\thehour\relax
    \advance\count@ by -\thetimezonehour\relax
% Does the day need adjusting?
    \ifnum\count@<0\relax
      \advance\count@ by 24\relax
      \edef\thehour{\number\count@}%
      \advance\julianday by -1\relax
    \else
      \ifnum\count@>23\relax
      \advance\count@ by -24\relax
      \edef\thehour{\number\count@}%
      \advance\julianday by 1\relax
      \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}{}%
}

Download mycustomdatetime.sty.

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}

Download disptimes.tex or disptimes.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs