C stat() 和夏令时

发布于 2024-09-09 10:43:38 字数 820 浏览 0 评论 0原文

我在使用 stat() 函数时遇到严重问题。我有一个在 Windows 7 上的 cygwin 下编译的应用程序,以及在 Windows 7 上使用 MSVC++ 编译的同一应用程序。该应用程序包含以下代码:

struct stat stb;
memset( &stb, 0, sizeof( stb ) );

stat( szPath, &stb );

cout << hex << uppercase << setw(8) << stb.st_mtime << endl;

szPath 是文件的路径。应用程序不会以任何方式修改该文件。问题是,我对某些文件得到了不同的结果。例如:

cygwin version: 40216D72
MSVC++ version: 40217B82

差异始终是 E10 = 3600 = 1 小时

通过使用 google,我发现了这个,这似乎与我看到的问题完全相同。有没有一种可移植的方法来解决这个问题?我无法使用任何 WinAPI 调用。最简单、最可靠的解决方案就是我正在寻找的,但如果它需要复杂,那就这样吧。可靠性和可移植性(win + linux)是这里最重要的。

I'm having serious trouble with the function stat(). I have an application compiled under cygwin ond Windows 7 and the same app compiled with MSVC++ on Windows 7. The app contains the following code:

struct stat stb;
memset( &stb, 0, sizeof( stb ) );

stat( szPath, &stb );

cout << hex << uppercase << setw(8) << stb.st_mtime << endl;

szPath is a path to a file. The file does not get modified in any way by the app. The problem is, that i get different results for some files. For example:

cygwin version: 40216D72
MSVC++ version: 40217B82

The difference is always E10 = 3600 = 1 hour

By using google, i found this, which seems to be exactly the same issue i'm seeing. Is there a portable way how to fix this? I cannot use any WinAPI calls. The most simple and reliable solution is what i'm looking for, but if it needs to be complicated, so be it. Reliability and portability (win + linux) is the most important thing here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我做我的改变 2024-09-16 10:43:38

为了在这里获得可靠性和可移植性(或者在大多数情况下,两个平台用“相同”的代码做不同的事情),您可能需要使用某种形式的目标相关代码,例如

#ifdef _MSC_VER
   // do MSVC++-specific code
#else
   // do Linux/Cygwin/generic code
#endif

:应该能够在 _MSC_VER 部分中使用 WinAPI 调用,因为只有在使用 MSVC++ 时才会编译它

To obtain both reliability and portability here (or in most situations of this sort where two platforms do different things with what should be the "same" code), you will probably need to use some form of target-dependent code, like:

#ifdef _MSC_VER
   // do MSVC++-specific code
#else
   // do Linux/Cygwin/generic code
#endif

You then ought to be able to use WinAPI calls in the _MSC_VER section, because that will only be compiled when you're using MSVC++

左秋 2024-09-16 10:43:38

显然,根据 http://support.microsoft.com/kb/190315 这实际上是一个功能尽管对我来说这确实是一个错误。他们说,您可以通过清除系统时钟的“日期/时间属性”对话框中的“自动调整夏令时更改时钟”来解决此问题。

如果您有文件的日期,则可以使用 dst 的相对状态来确定是否需要仅在 MSVC 中自行进行一小时的调整,但这也很麻烦。

Apparently per http://support.microsoft.com/kb/190315 this is actually a FEATURE although it really seems like a bug to me. They say you can work around it by clearing "Automatically adjust clock for daylight saving changes" in the Date/Time Properties dialog box for the system clock.

If you have the date of the file, you can use the relative state of dst to determine if you need to make a one-hour adjustment yourself in MSVC only, but that's hacky as well.

蒲公英的约定 2024-09-16 10:43:38

不确定你正在使用的功能,但我知道 Windows 和 Linux 使用系统时钟的方式不同。 Windows 将本地时间(包括 DST)存储在系统时钟上。 Linux(至少传统上)将 GMT(或者准确地说是 UTC)存储在系统时钟上。如果这适用于 cygwin,我不知道。

如果 Linux 系统与 Windows 共享硬件,则需要将其配置为像 Windows 一样使用系统时钟,否则每次 Windows 调整 DST 时都会出现混乱。

Not sure about the functions you are using but I do know that Windows and Linux use the system clock differently. Windows stores local time (including DST) on the system clock. Linux (at least traditionally) stores GMT (or UTC to be precise) on the system clock. I don't if this applies to cygwin.

If a linux system shares hardware with windows it needs to be configured to use the system clock like windows or get messed-up every time windows adjusts DST.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文