p/invoke GetSystemTime() 方法给出错误的输出

发布于 2024-08-31 05:39:50 字数 78 浏览 2 评论 0原文

我在应用程序中使用了 p/invoke GetSystemTime() 方法来获取当前系统日期时间,但它给出了错误的值,任何解决方案都是如此。

I used p/invoke GetSystemTime() method in my application to get the current system date time but it is giving wrong values any solution for this..

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

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

发布评论

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

评论(5

ぽ尐不点ル 2024-09-07 05:39:50

啊,时间还有CF和WinCE。多有趣啊!除了您收到的所有其他好答案之外,还有其他事情需要了解:

  1. 操作系统存储 LocalTime,而不是 UTC,因此 GetSystemTime 最终获取 LocalTime,并根据您的时区和 DST 向后调整该值,因此如果本地时间正确但 SystemTime 不是,那么您的 TZ 或 DST 设置错误。
  2. DST 可能正确,也可能不正确,因为国会对其进行了更改,因此 OEM 可能需要 QFE
  3. DST 可能在注册表中打开或关闭
  4. CF 在启动时缓存时区偏差,因此时区的任何调整都会导致 DateTime.Now 不正确直到您重新启动应用程序为止,
  5. 并非所有设备都可以在断电(甚至重置)时保持时间。
  6. 时间将“浮动”一整天。有多严重(毫秒到秒)取决于实际的硬件实现

Ah, time and the CF and WinCE. What fun! Along with all the other fine answers you've received there are other things to know:

  1. The OS stores LocalTime, not UTC so GetSystemTime ends up getting LocalTime and them adjusting that backwards based on your timezone and DST, so if the local time is right but SystemTime is not, then you have a TZ or DST setting wrong.
  2. DST may or may not be right due to congress changing it, so a QFE may be required by the OEM
  3. DST may be on or off in the registry
  4. The CF caches timezone bias as startup, so any adjustment of the timezone renders DateTime.Now incorrect until you restart your app
  5. Not all devices can persist time across a power loss (or even a reset)
  6. Time will "float" throughout the day. how badly (milliseconds to seconds) depends on the actual hardware implementation
还在原地等你 2024-09-07 05:39:50

为什么不使用 DateTime.Now

Why don't you use DateTime.Now ?

深巷少女 2024-09-07 05:39:50

问题是什么?

您的 p/invoke 签名正确吗?

您的结构布局正确吗?

您如何处理“返回”的结构指针?

What is the problem?

Is your p/invoke signature correct?

Is you struct laid out correctly?

How are you dealing with the struct pointer being 'returned' ?

尴尬癌患者 2024-09-07 05:39:50

如果返回的时间相差一小时,则说明您遇到了夏令时错误(可以使用 修补程序)。

GetSystemTime 返回协调世界时 (UTC)。您可能只想查找本地时间,在这种情况下,您需要调用 GetLocalTime (或者仅使用 DateTime.NowDateTime.UtcNow >,并跳过 PInvoke 内容)。

If the time returned is off by one hour, then you're running into a Daylight Savings Time bug (which can be fixed with a hotfix).

GetSystemTime returns the Coordinated Universal Time (UTC). You may be looking for just the local time, in which case you want to call GetLocalTime instead (or just use DateTime.Now or DateTime.UtcNow, and skip the PInvoke stuff).

ゃ人海孤独症 2024-09-07 05:39:50

错误的价值观是什么意思?
由于您询问的是 Windows CE,可能是您的系统不保存 RTC 并且在启动时不同步,导致根本没有正确的时间。
这是特定于平台的。任务栏中的时间和日期是否正确(假设图像中有该时间和日期)?

What do you mean by wrong values?
Since you are asking about Windows CE, it might be that your system does not save the RTC and does not sync on boot resulting in not having the correct time at all.
This is platform specific. Is the time and date correct in the taskbar (assuming you have that in the image)?

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