Emacs and Git show wrong time on Windows
Question: Emacs is two hours off from the system time. I tried to google for the
problem, but no luck. What do I need to configure to correct this? I suspect
this to be the difference from GMT to where I live (I'm in GMT+2 zone, that
is, if I subtract from system time 2, I'll get the time in Emacs). So... maybe
it's some locale settings?

I just messed up a git repository because of that: commits made through
`magit` used Emacs time, and placed them before the commits made by someone
else :(

![enter image description here](http://i.stack.imgur.com/g1354.png)

Here, I've added a screenshot showing the difference. The output from the
`date` is the correct time, but the time on the modeline fringe is wrong.

**EDIT0:**

It appears Stefan is right, and the time in Git is not connected to time in
Emacs (the screenshot below is from Cygwin terminal).

This question is as relevant to Git as it is to Emacs - somehow they are using
some system API that falls out of sync on my PC - and that is something I need
to set up to align them on it. The question is what is that setting they both
use?

![enter image description here](http://i.stack.imgur.com/Ln7aP.png)

**EDIT1:**

Here's the code that Emacs uses to retrieve the time, afaik:

    
    
    /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95).  */
    int
    gettimeofday (struct timeval *__restrict tv, struct timezone *__restrict tz)
    {
      struct _timeb tb;
      _ftime (&tb);
    
      tv->tv_sec = tb.time;
      tv->tv_usec = tb.millitm * 1000L;
      /* Implementation note: _ftime sometimes doesn't update the dstflag
         according to the new timezone when the system timezone is
         changed.  We could fix that by using GetSystemTime and
         GetTimeZoneInformation, but that doesn't seem necessary, since
         Emacs always calls gettimeofday with the 2nd argument NULL (see
         current_emacs_time).  */
      if (tz)
        {
          tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich  */
          tz->tz_dsttime = tb.dstflag;  /* type of dst correction  */
        }
      return 0;
    }
    

And it looks like it gets `tz` wrong. I don't know what `_ftime` is - but it
doesn't seem to be defined in Emacs' sources, this must come from elsewhere...

**Some more research:**

SBCL installed from MSI gives this:

    
    
    (defconstant *day-names*
        '("Monday" "Tuesday" "Wednesday"
          "Thursday" "Friday" "Saturday" "Sunday"))
    (multiple-value-bind
        (second minute hour date month year day-of-week dst-p tz)
        (get-decoded-time)
        (format t "It is now ~2,'0d:~2,'0d:~2,'0d of ~a, ~d/~2,'0d/~d (GMT~@d)"
              hour minute second (nth day-of-week *day-names*)
              month date year (- tz)))
    

Output: _(actual time is 12:56)_

    
    
    It is now 10:56:55 of Tuesday, 6/04/2013 (GMT+0)
    

Perl from ActivePerl (installed from Cygwin):

    
    
    $now = localtime;
    print $now;
    

Output: _(actual time is 12:52)_

    
    
    Tue Jun  4 12:52:17 2013
    

CPython, installed from MSI.

    
    
    import datetime
    str(datetime.datetime.now())
    

Output: _(actual time is 13:03)_

    
    
    2013-06-04 11:03:49.248000
    

JavaScript, Node.js, installed from MSI:

    
    
    Date();
    

Output: _(actual time is 12:09)_

    
    
    Tue Jun 04 2013 10:09:05 GMT+0000 (IST)
    

Bash (Cygwin):

    
    
    $ date
    

Output: _(actual time is 13:10)_

    
    
    04 Jun, 2013 13:10:37
    

C#:

    
    
    using System;
    
    namespace TestTime
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime d = DateTime.Now;
                Console.WriteLine("Today: {0}", d);
                Console.ReadLine();
            }
        }
    }
    

Output: _(actual time is 13:13)_

    
    
    Today: 04-Jun-13 13:13:37
    

**EDIT2:**

Today our sysadmin gave me a VM to move my stuff to. Interestingly, what
happened there is that this time I got Git through Cygwin, and now Git shows
correct times. Emacs, however, still shows wrong time. Python, (not the one
bundled with Cygwin) shows correct time if launched from Cygwin and wrong time
if launched from Emacs! SBCL shows wrong time no matter how it is launched.

Is it possible this is some network setting? Perhaps something to do with how
Windows synchronizes system time?


Answer: Windows programs get timezone info from the system where you set it via
control panel. Time itself can be set manually too, but it is more usual to
let Windows synchronize with time servers via [Windows Time
Service](http://technet.microsoft.com/cs-
cz/library/cc773263%28v=ws.10%29.aspx) (NTP-based; for usage see [WinXP
support article](http://support.microsoft.com/kb/307897/en-us)). AFAIK Windows
Time Service has nothing to do with timezone and is the only network-related
thing involved in the problem.

Cygwin programs should in theory behave the same way as on any other *NIX.
They get the timezone information from **`/etc/localtime` file** or **`TZ`
environment variable**. In my case (Debian), `/etc/localtime` is a copy of
`/usr/share/zoneinfo/Europe/Prague`, but it could be a symlink pointing to
that location as well. `TZ` can contain path relative to `/usr/share/zoneinfo`
and takes precedence over `/etc/localtime`.

Practice is a different story. According to [a thread on Cygwin mailing
list](http://cygwin.com/ml/cygwin/2012-03/msg00048.html), Microsoft C runtime
(mscrt*.dll) has extremely outdated timezone handling and it is used for non-
Cygwin programs. This is consistent with the results you are getting. Related
technical info is in [a thread from August
2005](http://cygwin.com/ml/cygwin/2005-08/msg00126.html) and [a related one
from May 2010](http://cygwin.com/ml/cygwin/2010-05/msg00455.html).

There is a [work-around for
Python](http://stackoverflow.com/questions/11958870/logging-module-for-python-
reports-incorrect-timezone-under-cygwin) already posted on SO and [exactly the
same for C++](http://stackoverflow.com/questions/11655003/localtime-returns-
gmt-for-windows-programs-running-on-cygwin-shells), which has a more brief
explanation of the MSCRT problem than the previously linked threads.

Maybe Emacs has something special to do with the issue. [A February 2009
message on GNU Emacs developers mailing
list](http://lists.gnu.org/archive/html/emacs-devel/2009-02/msg00305.html)
reports Emacs 22.3 displays correct time while Emacs 23 not. Following advice
from a reply, the author posts [the report to Cygwin mailing
list](http://cygwin.com/ml/cygwin/2009-02/msg00148.html) – but gets no reply.
[December 2012](http://cygwin.com/ml/cygwin/2009-12/msg00972.html) the issue
is solved by docummenting it and its workaround (see
`/usr/share/doc/Cygwin/emacs.README`). _I don't have Cygwin and cannot find
this file in its CVS, so please edit my answer with the info from there._

Personally I think that setting (and exporting) TZ environment variable in
your `~/.profile` would be a good idea. Cygwin obviously has broken timezone
handling, so overriding everything related should be the safest thing to do as
it leaves just a little room for black magic. Maybe
[`tzset`](http://www.cygwin.com/cygwin-ug-net/using-utils.html#tzset) will
work, maybe it won't and you'll have to set your timezone to a constant value.

Try [googling for „timezone“ at
Cygwin.com](https://www.google.com/search?q=timezone+site%3acygwin.com) for
more resources.

