LaTeX \usepackage{datetime} 和 \usepackage{scrtime} 相差一小时

LaTeX \usepackage{datetime} 和 \usepackage{scrtime} 相差一小时

我想在 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}

对我来说,这显示:

本文档创建于:2014-03-26 15:35:16 +00:00

编辑:

有了这些信息,现在就可以转换为另一个时区。以下示例转换为祖鲁时间 (+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 而你想将其提前两个小时……

仅将其视为一种快速修复方法。

相关内容