Я хочу напечатать текущее время в моем файле LaTeX, но оба \usepackage{datetime}
и \usepackage{scrtime}
показывают неправильное время. Могу ли я исправить на другой часовой пояс? Настроить на летнее время?
Я пишу, https://www.sharelatex.com/
так что предполагаю, что отображается время сервера.
решение1
Вы можете получить доступ к часовому поясу через \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}
Для меня это отображает:
Редактировать:
С этой информацией теперь можно конвертировать в другой часовой пояс. Пример ниже конвертирует в зулусское время (+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}
Это производит:
решение2
Поскольку, по сути, для вашего часового пояса нужно настроить только час, вы также можете просто увеличить/уменьшить\текущийчасрегистр, предоставленныйдата и времяупаковка.
\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}
Это приведет к следующему:
Примечаниечто этот обходной путь требует ручной проверки. Если, например, сервер sharelatex.com, на котором вы компилируете, находится в другом часовом поясе, ваш вывод изменится соответствующим образом.
РЕДАКТИРОВАТЬ: Я только что понял, что дату также необходимо учитывать, когда вы корректируете время в связи со сменой дня, например, время на сервере 23:30, а вы хотите перейти на два часа вперед...
Рассматривайте это только как быстрое решение.