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

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

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?

有帮助吗?

解决方案

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 (NTP-based; for usage see WinXP support article). 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, 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 and a related one from May 2010.

There is a work-around for Python already posted on SO and exactly the same for C++, 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 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 – but gets no reply. December 2012 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 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 for more resources.

其他提示

Since this is the only relevant search result I got when I search for "emacs time wrong windows", I will note what I did.

I use emacs on two different time zones, so I wrote a script to update my location for emacs from ipinfo.io and output following:

(setenv "TZ" "Asia/Shanghai")
(set-time-zone-rule "Asia/Shanghai")

This works fine on Linux and macOS, however, it doesn't work on Windows 10. The function set-time-zone-rule on Windows is somehow broken. Running

(current-time-zone)

gives (28800 "CST") on Ubuntu and (0 "Asi") on Windows. So I got the wrong time displayed on Windows.

However, following does work:

(set-time-zone-rule nil)

and (current-time-zone) gives (28800 "China Standard Time"), which works.

I also find out, if I set TZ environmental variable to "Asia/Shanghai", then emacs won't have the correct time. If I unset TZ, time is correct. Anyway, set-time-zone-nil to nil works, as emacs will get time from the system in this case.

My guess is OP might have setup TZ environmental variable, and that messed up emacs and git.

Hope this will be useful.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top