除了本地 UTC 偏移量之外,git 是否还会记录本地时区作为 git 提交的时间戳?

问题描述 投票:0回答:1

我已经看到了git:时区和时间戳格式,但不幸的是我仍然不清楚

git
在创建提交时是否保存本地时区信息,除了UTC偏移量。

那么让我举个例子。我的 Ubuntu 20.04 PC 设置为欧洲/哥本哈根时区:

$ timedatectl | grep "Time zone"
                Time zone: Europe/Copenhagen (CET, +0100)

...这就是

date
知道我的(缩写)时区是 CET(或 CEST)的方式:

$ date
Thu 29 Feb 2024 02:23:59 AM CET

无论如何,这就是让我感到困惑的地方 - https://git-scm.com/docs/git-log 说:

--date=raw
将日期显示为自纪元 (1970-01-01 00:00:00 UTC) 以来的秒数,后跟一个空格,然后是与 UTC 的偏移量的时区

好的,所以如果我在我本地管理的一个存储库上运行这个,我会得到:

$ git --no-pager log --all --date=raw '--format=%ad %h' -3
1709162393 +0100 74e81f7
1709154897 +0100 00d4811
1709152471 +0100 2c878cc

好的,所以我得到了预期的Unix纪元(

1709162393
)和UTC偏移量(
+0100
)——但是,UTC偏移量是不是时区:Python中人类可读的时区?注释:

这些 3 个字母的名称不明确且不标准;
...
多个时区使用[一个]偏移量(或[已经]在其历史中使用[它]一次)
...
因此,根据偏移量,您无法仅获得one单个时区。您能做的最好的事情就是获取候选人名单:

所以,我有点希望,

git
将时区信息保存在进行提交的本地PC上,这样我就可以获得CET/CEST缩写时区(对于我的存储库;或者为任何其他地方进行原始提交) repos)在
git log
日期输出中。

https://git-scm.com/docs/git-log进一步说明:

--date=human
如果时区与当前时区不匹配,则显示时区,并且...

好的,所以如果我在自己的本地存储库上尝试这个,我不应该期望看到时区 - 这确实是真的:

$ git --no-pager log --all --date=human '--format=%ad %h' -3
2 hours ago 74e81f7
Wed 22:14 00d4811
Wed 21:34 2c878cc

...但是后来,我发现了一个来自澳大利亚的 git 存储库,它肯定应该有来自与我本地时区不同的时区的提交。或者至少,不同的 UTC 偏移量 - 我发现了这个:

$ git clone https://github.com/rubyaustralia/rubyconfau-2013-cfp
Cloning into 'rubyconfau-2013-cfp'...
remote: Enumerating objects: 297, done.
remote: Total 297 (delta 0), reused 0 (delta 0), pack-reused 297
Receiving objects: 100% (297/297), 4.19 MiB | 7.59 MiB/s, done.
Resolving deltas: 100% (66/66), done.

$ cd rubyconfau-2013-cfp/
$ git --no-pager log --all --date=raw '--format=%ad %h' -3
1360562749 -0800 2af69d3
1355123238 +1100 be827a9
1355123013 -0800 975f272

好吧,UTC 偏移量绝对与我的不同。那么

--date=human
“如果时区与当前时区不匹配,则显示时区”吗?嗯:

$  git --no-pager log --all --date=human '--format=%ad %h' -3
Feb 10 2013 2af69d3
Dec 10 2012 be827a9
Dec 9 2012 975f272

...不(甚至没有 UTC 偏移)。

作为最后的手段,我会尝试

%z
%Z
strftime 格式说明符,尽管 https://git-scm.com/docs/git-log 说:

--date=format:...
将格式
...
提供给您的系统
strftime
,%s、%z 和 %Z 除外,这些是在内部处理的

...这实际上提高了我的希望,即实际时区也保存在 git 提交时间戳中。唉,没有 - 在我的本地存储库上:

$ git --no-pager log --all "--date=format:%Y-%m-%d %z >%Z<" '--format=%ad %h' -3
2024-02-29 +0100 >< 74e81f7
2024-02-28 +0100 >< 00d4811
2024-02-28 +0100 >< 2c878cc

..以及 rubyconf 存储库:

$ git --no-pager log --all "--date=format:%Y-%m-%d %z >%Z<" '--format=%ad %h' -3
2013-02-10 -0800 >< 2af69d3
2012-12-10 +1100 >< be827a9
2012-12-09 -0800 >< 975f272

...看不到任何

%Z
缩写的本地时区,只能看到
%z
UTC 偏移量;否则对于相同的格式字符串,我本地的
date
显示:

$  date +"%Y-%m-%d %z >%Z<"
2024-02-29 +0100 >CET<

所以,我的结论是,在 git 提交时保存时间戳时,

git
仅保存本地 UTC 偏移量,但本地时区。 谁能确认以上是否正确?如果不正确,我如何获取 git 中提交时间戳的本地时区? (顺便说一句,我上面使用的是 git 版本 2.25.1)

git datetime timestamp timezone
1个回答
0
投票
raw

提交信息可以看到: $ git cat-file -p @ tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 author Fulanito D'Tal <fulanito@dtal> 1709194740 +0100 committer Fulanito D'Tal <fulanito@dtal> 1709194740 +0100 hello world

这就是
提交本身,你可以看到它如何保持

偏移量(我在荷兰,所以目前是+0100......就像许多其他国家一样)。

© www.soinside.com 2019 - 2024. All rights reserved.