Quero imprimir a hora atual em meu arquivo LaTeX, mas ambos, \usepackage{datetime}
e \usepackage{scrtime}
mostrar a hora errada. Posso corrigir para um fuso horário diferente? Ajustar para o horário de verão?
Estou escrevendo, https://www.sharelatex.com/
então suponho que seja o horário do servidor que está sendo mostrado.
Responder1
Você pode acessar o fuso horário via \pdfcreationdate
:
\documentclass{article}
\usepackage{etoolbox}
\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\thetimezonehour{+00}%
\def\thetimezoneminute{00}%
}%
{%
\parsepdftimezone#7%
}%
}
\def\parsepdftimezone#1'#2'{%
\def\thetimezonehour{#1}%
\def\thetimezoneminute{#2}%
}
\newcommand*{\thetimezone}{\thetimezonehour:\thetimezoneminute}
\begin{document}
\expandafter\parsepdfdatetime\pdfcreationdate\endparsepdfdatetime
This document was created on:
\theyear-\themonth-\theday\ \thehour:\theminute:\thesecond\ \thetimezone.
\end{document}
Para mim, isso exibe:
Editar:
Com esta informação, agora é possível converter para outro fuso horário. O exemplo abaixo converte para o horário Zulu (+00:00):
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfkeys,pgfcalendar}
\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\thetimezonehour{+00}%
\def\thetimezoneminute{00}%
}%
{%
\parsepdftimezone#7%
}%
}
\def\parsepdftimezone#1'#2'{%
\def\thetimezonehour{#1}%
\def\thetimezoneminute{#2}%
}
\newcommand*{\thetimezone}{\thetimezonehour:\thetimezoneminute}
\newcount\julianday
\newcount\dayofweek
\makeatletter
\newcommand*{\setzulutime}{%
\pgfcalendardatetojulian{\theyear-\themonth-\theday}{\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}%
}
\makeatother
\begin{document}
\parsepdfdatetime D:20130408144644+01'00'\endparsepdfdatetime
This document was created on:
\theyear-\themonth-\theday\ \thehour:\theminute:\thesecond\ \thetimezone.
\setzulutime
UTC+00:00 time stamp:
\theyear-\themonth-\theday\ \thehour:\theminute:\thesecond.
\end{document}
Isso produz:
Responder2
Como, na verdade, apenas a hora precisa ser ajustada ao seu fuso horário, você também pode simplesmente aumentar/diminuir o\hora atualcadastro, fornecido pelodata horapacote.
\documentclass{article}
\usepackage[nodayofweek]{datetime}
\begin{document}
% Format output
\ddmmyyyydate
% Un-adjusted
[Compiled \today, \currenttime]
\par
% advance the hour register by nr of hours
% negative values if you want to subtract
\advance\currenthour by 2
%Adjusted output
[Compiled \today, \the\currenthour:\the\currentminute]
\end{document}
Isso resultará no seguinte:
Observaçãoque esta solução alternativa precisa de verificação manual. Se, por exemplo, o servidor sharelatex.com no qual você está compilando estiver em um fuso horário diferente, sua saída será alterada de acordo.
EDITAR: Acabei de perceber que a data também deve ser considerada, quando você está ajustando o horário em torno da mudança do dia, por exemplo, o horário no servidor é 23h30 e você quer avançar duas horas...
Considere isso apenas como uma solução rápida.