% This is an example document accompanying "LaTeX for Administrative Work"
% See www.dickimaw-books.com/latex/admin/ for the book details,
% including licence.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mycustomdatetime}[2014/03/20 1.1 My custom date and time format]

\RequirePackage{etoolbox}%
\RequirePackage{pgfkeys,pgfcalendar}

% New commands for Exercise 22:

\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}%
}

\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}{}%
}

% Time commands:

\newcommand*{\timefmt}[4]{%
  \two@digits{#1}:\two@digits{#2}%
  \ifstrempty{#3}% test for empty 3rd argument
  {}% no seconds specified
  {:\two@digits{#3}}% seconds
  \timezonefmt{#4}% time zone
  \relax
}

\newcommand*{\timezonefmt}[1]{%
  \edef\thistimezone{#1}%
  \ifdefempty{\thistimezone}%
  {}% empty argument
  {%
     \expandafter\@timezonefmt\thistimezone\@endtimezonefmt%
  }%
}

\def\@timezonefmt#1:#2\@endtimezonefmt{%
  \ifnum #2=0\relax
    \ifnum #1=0\relax
      Z%
    \else
      \ifnum #1<0 $-$\two@digits{-#1}\else +\two@digits{#1}\fi
    \fi
  \else
    \ifnum #1<0$-$\two@digits{-#1}\else +\two@digits{#1}\fi
    :\two@digits{#2}%
  \fi
}

\def\parsepdfdatetime#1:#2#3#4#5#6#7#8#9{%
  \def\theyear{#2#3#4#5}%
  \def\themonth{#6#7}%
  \def\theday{#8#9}%
  \parsepdftime
}

\def\parsepdftime#1#2#3#4#5#6#7\endparsepdfdatetime{%
  \def\thehour{#1#2}%
  \def\theminute{#3#4}%
  \def\thesecond{#5#6}%
  \ifstrequal{#7}{Z}
  {%
    \def\thetimezone{+00:00}%
  }%
  {%
    \parsepdftimezone#7%
  }%
}

\def\parsepdftimezone#1'#2'{%
  \def\thetimezone{#1:#2}%
}

\expandafter\parsepdfdatetime\pdfcreationdate\endparsepdfdatetime
\let\pdfhour\thehour
\let\pdfminute\theminute
\let\pdfsecond\thesecond
\let\pdftimezone\thetimezone

\newcommand*{\pdfnowtime}{\timefmt{\pdfhour}{\pdfminute}{\pdfsecond}{\pdftimezone}}

\newcommand*{\pdfnow}{%
 \number\year-\two@digits{\month}-\two@digits{\day}
 \pdfnowtime
}

\newcommand*{\filedate}[1]{%
  \expandafter\parsepdfdatetime\pdffilemoddate{#1}\endparsepdfdatetime
  \theyear-\two@digits{\themonth}-\two@digits{\theday}\  
  \timefmt{\thehour}{\theminute}{\thesecond}{\thetimezone}%
}

% Define an ordinal command:
\newcommand*{\ord}[1]{%
  \number#1%
  \ifcase#1\or st\or nd\or rd\or th\or th\or th\or th\or th\or
  th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or
  th\or th\or st\or nd\or rd\or th\or th\or th\or th\or th\or
  th\or th\or st\fi
}

% Define registers needed by \printdate:
\newcount\julianday
\newcount\dayofweek

% Define generic date format:
\newcommand*{\datefmt}[4][]{%
  \ifstrempty{#1}
  {}% day of week missing
  {%
    \pgfcalendarweekdayname{#1}\space
  }%
  \ord{#4}~\pgfcalendarmonthname{#3} \number#2%
}

% Define command to convert ISO date to a form that can be used by \datefmt:
\newcommand*{\printdate}[1]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \pgfcalendarjuliantodate{\julianday}{\thisyear}{\thismonth}{\thisday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  % Now display the date:
  \datefmt[\dayofweek]{\thisyear}{\thismonth}{\thisday}%
}

% Redefine \today to use the same format:
\renewcommand*{\today}{%
  \pgfcalendardatetojulian{\year-\month-\day}{\julianday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  \datefmt[\dayofweek]{\year}{\month}{\day}%
}

% Check if babel is used with the british option:
\AtBeginDocument{%
  \ifdef{\datebritish}% check if \datebritish exists
  {% it does exist, so redefine it
    \renewcommand*{\datebritish}{%
      \renewcommand*{\today}{%
        \pgfcalendardatetojulian{\year-\month-\day}{\julianday}%
        \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
        \datefmt[\dayofweek]{\year}{\month}{\day}%
      }%
    }%
  }%
  {}% doesn't exist, do nothing
}

\endinput
