![LaTeX \usepackage{datetime} 和 \usepackage{scrtime} 相差一個小時](https://rvso.com/image/286970/LaTeX%20%5Cusepackage%7Bdatetime%7D%20%E5%92%8C%20%5Cusepackage%7Bscrtime%7D%20%E7%9B%B8%E5%B7%AE%E4%B8%80%E5%80%8B%E5%B0%8F%E6%99%82.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,你想提前兩個小時...
僅將此視為快速修復。