简体   繁体   中英

Retrieving timezone in pretty formatted git log output (format specifier)?

I'm trying to print out a git log with UTC/UNIX epoch seconds timestamp, timezone of commit, and short hash.

I have noticed https://git-scm.com/docs/git-log/2.17.1 :

%ad: author date (format respects --date= option)

%at: author date, UNIX timestamp

  • --date=raw shows the date as seconds since the epoch (1970-01-01 00:00:00 UTC), followed by a space, and then the timezone as an offset from UTC (a + or - with four digits; the first two are hours, and the second two are minutes). Ie, as if the timestamp were formatted with strftime("%s %z") ). Note that the -local option does not affect the seconds-since-epoch value (which is always measured in UTC), but does switch the accompanying timezone value.

So, I naively tried this first:

$ git --no-pager log --reverse --pretty='%at %z %h'
1528917616 %z 1cabbe9
1528960200 %z 3fad300
1528964789 %z ba4e746
1528964798 %z d39a4f8
...

Clearly, the %z doesn't expand within git log --pretty / --format .

Then, somewhat by accident, I tried this:

$ git --no-pager log --reverse --date=raw --pretty='%ad %h'
1528917616 +0000 1cabbe9
1528960200 +0000 3fad300
1528964789 +0000 ba4e746
1528964798 +0000 d39a4f8
1528967981 +0000 a9cdecb
1528970465 +0000 3e9cee9
....

... and that is exactly what I wanted! Except, here I don't have a separate format argument for the timezone, so I cannot move it elsewhere if I want to, at least not directly in the git log command.

So, does anyone know, if there is a format specifier that I could use directly in git log , and which will obtain me only the timezone of the timestamp of the commit?

In git log , --pretty (or --format ) does not have a placeholder for the timezone. But --date=format: does. It seems --date=raw is equivalent to --date=format:"%s %z" . So if you want only the timezone of a specific commit, you can try:

git log -1 <commit> --pretty=%ad --date=format:%z

Note that %ad is for author date. If you want commit date, use %cd instead. Besides, there are several placeholders for predefined date formats, like %ai , %at , %cD , etc. If any of these placeholders is used, --date=format is just ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM