Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: show dates in UTC

Tags:

git

utc

metadata

The following command shows some metadata about the last git commit in json format:

git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"

{
  "hash":"0fc0fc0", 
  "author":"Adam Matan",
  "commit date":"Sun Jan 26 12:26:19 2014 +0200"}
}

Is there a way to present the date in the UTC/GMT time zone, e.g. "Sun Jan 26 10:26:19 2014" ?

like image 553
Adam Matan Avatar asked Jan 26 '14 12:01

Adam Matan


People also ask

Does GIT store time zones?

Each committer's timezone is surely saved in github's server.

Does git commit timestamp?

There are actually two different timestamps recorded by Git for each commit: the author date and the commit date. When the commit is created both the timestamps are set to the current time of the machine where the commit was made.


2 Answers

You can use this:

TZ=UTC0 git show --quiet --date=local --format="%cd"

If you want to control the date format, you can do this:

TZ=UTC0 git show --quiet --date='format-local:%Y%m%dT%H%M%SZ' --format="%cd"

Update 2021-10-16: changed TZ=UTC to TZ=UTC0 thanks to @alex-shpilkin.

like image 81
robinst Avatar answered Sep 22 '22 04:09

robinst


I don't see an utc format in the log data formats (listed this answer).

The closest I get from your format is:

git config log.date local

C:\Users\VonC\prog\git\git\>git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"
{"hash":"b594c97", "author":"Junio C Hamano", "commit date":"Thu Jan 23 10:00:28 2014 -0800"}

C:\Users\VonC\prog\git\git\>git config log.date local

C:\Users\VonC\prog\git\git\Documentation\technical>git show --quiet HEAD --pretty=format:"{\"hash\":\"%h\", \"author\":\"%cn\", \"commit date\":\"%cd\"}"
{"hash":"b594c97", "author":"Junio C Hamano", "commit date":"Thu Jan 23 19:00:28 2014"}

So from iso:

"Thu Jan 23 10:00:28 2014 -0800"

To local:

"Thu Jan 23 19:00:28 2014"

As commented, this is not UTC, unless the your machine local time is already UTC.


That was discussed on the mailing list:

Adding user.hideTimezone for setting UTC timezone

Authoring and sharing a commit by default exposes the user's time zone.

"commit --date=YYYY-MM-DDThh:mm:ss+0000" suffices to put the author time in UTC but not the commit time in UTC.
But the user shouldn't have to pass a flag at all.

Git should entirely stop accessing, recording, and sharing the user's time zone, full stop.

Failing that, git should by default stop accessing, recording, and sharing the user's time zone, but if individual users want to have their time zones on their commits, they can opt into it.

Junio C. Hamano, Git maintainer, answered:

You are free to run

$ TZ=GMT git commit

if you wanted to opt out of the feature, but this has been the default since day one and people expect Git to behave this way.

Also:

For now, using the --date argument on git commit allows you to also pass a timezone:

git commit --date="$(TZ=PST date)"

This patch (for adding a user.hideTimezone) is yet to be fully developed.

like image 32
VonC Avatar answered Sep 21 '22 04:09

VonC