1

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)

Community
  • 1
  • 1
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
  • This isn't about Git or any programming issue. This is about MinGW32 and the `date` command. – Matt Johnson-Pint Jul 03 '14 at 04:39
  • If you're just talking about the git log output, you might also be interested in [a more git-specific answer](http://stackoverflow.com/a/23900181/634824) – Matt Johnson-Pint Jul 03 '14 at 04:42
  • It *is* a programming issue if you need to use the date command on a script and you can't rely on its output. Under certain conditions, if you want to have Windows/Linux compatibility in bash, you'd have to put a hack together and since after looking I couldn't find anything, I figured I'd share a solution with the world. About the tag, yeah, tagged it wrongly as `git`, but I'd appreciate if you'd reconsider the close vote as this actually is about programming, namely scripting. The close reason you've chosen mentions "unless it involves tools used primarily for programming" - this is the case – Camilo Martin Jul 04 '14 at 05:56

1 Answers1

1

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%).

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154