Search code examples
bashdatetimezonemsysgitgithub-for-windows

GitHub's Git Bash (MSysGit) showing wrong time(zone) on Windows


I'm on GMT-3, but the date command in the bash port bundled by GitHub's Git for Windows doesn't see this, instead picking the UTC time and thinking it's local time too.

    enter image description here

In comparison, here's a healthy box running on GMT-4:

    enter image description here

How can I make my scripts show the correct time, in a way that works cross-platform even in MSysGit?

(Related: a question about a similar issue with Cygwin)


Solution

  • This function will give you the correct time, whether you run it on Windows or Linux:

    # Are we running on Windows?
    isWindows() { [[ -n "$WINDIR" ]]; }
    
    # Get time, cross-platform.
    getTime() {
        if isWindows; then
            cmd.exe "/c echo %time%" | head -c 8 | tr ' ' 0 # pad single-digit hours.
        else
            date +%T
        fi
    }
    

    Getting the date too is left as an exercise for the read (hint hint: %date%).