![LaTeX \usepackage{datetime} と \usepackage{scrtime} が 1 時間ずれている](https://rvso.com/image/286970/LaTeX%20%5Cusepackage%7Bdatetime%7D%20%E3%81%A8%20%5Cusepackage%7Bscrtime%7D%20%E3%81%8C%201%20%E6%99%82%E9%96%93%E3%81%9A%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B.png)
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 で、2 時間進めたい場合などです...
これを一時的な解決策としてのみ検討してください。