windows - Emacs 和 Git 在 Windows 上显示错误的时间

标签 windows git emacs time gmt

Emacs 与系统时间相差两个小时。我试图用谷歌搜索这个问题,但没有运气。我需要配置什么来纠正这个问题?我怀疑这是格林威治标准时间与我住的地方之间的差异(我在格林威治标准时间+2 区,也就是说,如果我从系统时间 2 中减去,我将得到 Emacs 中的时间)。所以...也许是一些区域设置?

我只是因为这个弄乱了一个 git 存储库:通过 magit 进行的提交使用了 Emacs 时间,并将它们放在其他人进行的提交之前:(

enter image description here

在这里,我添加了显示差异的屏幕截图。 date 的输出是正确的时间,但 modeline 边缘的时间是错误的。

编辑0:

看来 Stefan 是对的,Git 中的时间与 Emacs 中的时间没有关联(下面的屏幕截图来自 Cygwin 终端)。

这个问题既与 Git 相关,也与 Emacs 相关——不知何故,他们使用的一些系统 API 在我的 PC 上不同步——这是我需要设置的东西,以使它们在其上保持一致。问题是他们都使用的设置是什么?

enter image description here

编辑 1:

这是 Emacs 用来检索时间的代码,据我所知:

/* 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;
}

看起来 tz 出错了。我不知道 _ftime 是什么 - 但它似乎没有在 Emacs 的源代码中定义,这一定来自其他地方...

更多研究:

从 MSI 安装的 SBCL 给出了这个:

(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)))

输出:(实际时间是 12:56)

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

来自 ActivePerl 的 Perl(从 Cygwin 安装):

$now = localtime;
print $now;

输出:(实际时间为 12:52)

Tue Jun  4 12:52:17 2013

CPython,从 MSI 安装。

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

输出:(实际时间为 13:03)

2013-06-04 11:03:49.248000

JavaScript、Node.js,从 MSI 安装:

Date();

输出:(实际时间为 12:09)

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

重击(Cygwin):

$ date

输出:(实际时间为 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();
        }
    }
}

输出:(实际时间是 13:13)

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

编辑 2:

今天我们的系统管理员给了我一个 VM 来移动我的东西。有趣的是,这次我通过 Cygwin 获得了 Git,现在 Git 显示了正确的时间。然而,Emacs 仍然显示错误的时间。如果从 Cygwin 启动,Python(不是与 Cygwin 捆绑在一起的那个)显示正确的时间,如果从 Emacs 启动则显示错误的时间! SBCL不管怎么启动都显示错误。

这可能是一些网络设置吗?也许与 Windows 如何同步系统时间有关?

最佳答案

Windows 程序从您通过控制面板设置的系统获取时区信息。时间本身也可以手动设置,但更常见的是让 Windows 通过 Windows Time Service 与时间服务器同步(基于 NTP;有关用法,请参阅 WinXP support article)。 AFAIK Windows 时间服务与时区无关,并且是该问题中唯一涉及的网络相关内容。

Cygwin 程序在理论上应该与任何其他 *NIX 上的行为相同。他们从 /etc/localtime 文件TZ 环境变量 获取时区信息。在我的例子 (Debian) 中,/etc/localtime/usr/share/zoneinfo/Europe/Prague 的副本,但它可能是指向该位置的符号链接(symbolic link)以及。 TZ 可以包含相对于 /usr/share/zoneinfo 的路径并优先于 /etc/localtime

实践是另一回事。根据a thread on Cygwin mailing list , Microsoft C 运行时 (mscrt*.dll) 具有非常过时的时区处理,它用于非 Cygwin 程序。这与您得到的结果一致。相关技术资料在a thread from August 2005a related one from May 2010 .

有一个work-around for Python已经发布在 SO 和 exactly the same for C++ 上,它对 MSCRT 问题的解释比以前链接的线程更简短。

也许 Emacs 对这个问题有特殊的作用。 A February 2009 message on GNU Emacs developers mailing list报告 Emacs 22.3 显示正确的时间,而 Emacs 23 则没有。根据回复的建议,作者发布了 the report to Cygwin mailing list – 但没有得到回复。 December 2012这个问题通过记录它和它的解决方法来解决(参见 /usr/share/doc/Cygwin/emacs.README)。 我没有 Cygwin,无法在其 CVS 中找到此文件,因此请使用那里的信息编辑我的答案。

我个人认为在 ~/.profile 中设置(和导出)TZ 环境变量是个好主意。 Cygwin 显然已经破坏了时区处理,所以覆盖所有相关的东西应该是最安全的事情,因为它只为黑魔法留下了一点空间。也许tzset会工作,也许不会,你必须将你的时区设置为一个常量值。

尝试 googling for „timezone“ at Cygwin.com获取更多资源。

关于windows - Emacs 和 Git 在 Windows 上显示错误的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884170/

相关文章:

windows - 用于创建具有属性的快捷方式的 Powershell 脚本

git - Vim:在没有 Fugitive 的 lightline 状态行中显示当前的 git 分支

emacs - 退出 emacs 之前运行函数

git - 如何从父分支 rebase ?

Emacs:如何保持很长的换行的缩进级别

performance - Emacs 诊断 : Org-mode unbearably slow and often stalls

windows - node, forever, coffee - 在 linux 上运行良好但在 windows 上运行不佳

windows - 无效 ID : Process leaked file descriptors. Jenkins

windows - windows如何唯一识别一个USB设备?

git - 如何从git中删除已删除的文件?