Contents

Zsh function to commit in local timezone

Contents

The issue

I discovered a couple of days ago that my git installation is showing UTC timestamps instead of my local offset (CDT at this moment of the year).

This is the output when executing git log:

/2022/10/local-timezone-commit-with-zsh/utc_timestamp.png
UTC timestamp

The +0000 part determines the UTC offset, i.e. having +0000 means that we are talking about UTC, whereas -0500 represents a delay (or negative offset) of five hours with respect to UTC, which is known as CDT.

This behavior of Git doesn’t bother me most of the time, but there’s one use case when I’d like to have local timestamps, which is correctly displaying the last modified date in my website posts.

The solution

The easiest way to achieve the desired behavior is to tweak the Git environment variables, since I want to enable both committing with UTC and local timestamps.

Because I’m using zsh, I added the following function in my ~/.zshrc file:

1
2
3
4
5
function gcomdate() {
  export GIT_AUTHOR_DATE=$(date);
  export GIT_COMMITTER_DATE=$(date);
  git commit "$@"
}
Tip
To activate the function right away, execute source ~/.zshrc or restart the shell.

That’s a shortcut to setting both environment variables each time that I want local timestamps in my commits. For UTC timestamps I commit the usual way.

Input:

1
git commit -m <message>

Output:

/2022/10/local-timezone-commit-with-zsh/example_utc.png
UTC timestamp

Input:

1
gcomdate -m <message>

Output:

/2022/10/local-timezone-commit-with-zsh/example_local.png
Local (CDT) timestamp

The function is available in my dotfiles.