C# 在 Windows Mobile 6 上使用 Compact Framework 设置时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/984550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:54:47  来源:igfitidea点击:

Setting the Time Zone with Compact Framework on Windows Mobile 6

c#compact-frameworktimezone

提问by bjanaszek

First, background: I have a .Net application that runs in kiosk mode on Windows Mobile 6 devices (IPAQ 210s). Our software actually tracks the user's time zone independently of the operating system, so we calculate the displayed time based on their time zone adjustment from UTC. As many may know, true kiosk mode is not easy to achieve on Windows Mobile 6, and final piece of the puzzle for this app is suppressing the DST notifications from the OS.

首先,背景:我有一个 .Net 应用程序,它在 Windows Mobile 6 设备(IPAQ 210s)上以 kiosk 模式运行。我们的软件实际上独立于操作系统跟踪用户的时区,因此我们根据他们与 UTC 的时区调整来计算显示的时间。很多人可能知道,在 Windows Mobile 6 上实现真正的自助服务终端模式并不容易,这个应用程序的最后一个难题是抑制来自操作系统的 DST 通知。

My preferred solution to this would be set the device timezone to GMT Casablanca, since it doesn't observe DST. I've discovered, however, that despite my best efforts (including using the OpenNETCF library) I cannot get the time zone to reliably stick during the device provisioning process. In the date/time applet in the OS, it lookslike the timezone is GMT, but the time is one off from what it should be for that timezone.

我对此的首选解决方案是将设备时区设置为 GMT Casablanca,因为它不遵守 DST。然而,我发现尽管我尽了最大努力(包括使用 OpenNETCF 库),但我无法在设备配置过程中可靠地保持时区。在操作系统的日期/时间小程序中,时区看起来像是格林威治标准时间,但时间与该时区应有的时间相差甚远。

Can anyone offer advice as to how to get the timezone change to stick properly? Or, better yet, does anyone know how to disable the DST notification on WinMob 6?

任何人都可以提供有关如何正确更改时区的建议吗?或者,更好的是,有人知道如何在 WinMob 6 上禁用 DST 通知吗?

回答by Tom van Enckevort

You can set it in the registry. Just set the HKLM\Time\TimeZoneInformation value. There is also a function you can pinvoke: SetTimeZoneInformation.

您可以在注册表中进行设置。只需设置 HKLM\Time\TimeZoneInformation 值。还有一个可以调用的函数:SetTimeZoneInformation

回答by Hyman Bolding

I have also encountered the off by one error when changing timezones but in eVC not CF yet. In the end, I decided that it was being caused by changing the timezone of the device from a timezone that was in DST already to a timezone that was not in DST. It appeared to work correctly for the months of the year that we were on standard time but failed when we were on daylight savings time. Very annoying.

我在更改时区时也遇到了一个错误,但在 eVC 中还不是 CF。最后,我认为这是由于将设备的时区从已经在 DST 中的时区更改为不在 DST 中的时区引起的。它似乎在我们处于标准时间的一年中的几个月中正常工作,但在我们处于夏令时时却失败了。很烦人。

In the end, I ended up setting the timezone and times twice. It was (usually) correct the second time.

最后,我最终设置了时区和时间两次。第二次(通常)是正确的。

回答by MusiGenesis

I've experienced the same off-by-one-hour DST error in Windows Mobile 5. The problem appears to be OS-related rather than .NET-related, since I was using the SetSystemTimeAPI call on the device, passing the server's DateTime.UtcNowvalue (which was always accurate and never off by an hour). I was never able to trace the problem further, because we made the decision to keep all devices set to the same time zone as the server. The error only occurred when setting the system time on a device set to a different time zone than the server.

我在 Windows Mobile 5 中遇到了同样的关闭一小时 DST 错误。问题似乎与操作系统有关,而不是与 .NET 相关,因为我SetSystemTime在设备上使用API 调用,传递服务器的DateTime.UtcNow值(这总是准确的,而且从不偏离一小时)。我永远无法进一步追踪问题,因为我们决定将所有设备设置为与服务器相同的时区。只有在设置为与服务器不同时区的设备上设置系统时间时才会发生错误。

I've never seen this problem reported anywhere, despite extensive searching, which means one of the following is true:

尽管进行了大量搜索,但我从未在任何地方看到过此问题的报告,这意味着以下情况之一是正确的:

  1. Most WinMo device users set their own time and timezone manually, so this problem never comes up
  2. The problem is build-specific rather than generally OS-specific (the problem I saw was on an iPAQ, but I don't remember if I tried to reproduce the problem on a different device)
  3. I'm clinically insane, and this never really happened
  1. 大多数WinMo设备用户手动设置自己的时间和时区,所以这个问题永远不会出现
  2. 问题是特定于构建的,而不是通常特定于操作系统的(我看到的问题是在 iPAQ 上,但我不记得我是否尝试在其他设备上重现该问题)
  3. 我在临床上疯了,这从来没有真正发生过

回答by ctacke

For whatever reason, the CF actually caches timezone info at startup (I think the full framework does too). So when you make changes to the timezone, the CF is unaware of those changes. If you use the GetSystem/LocalTime APIs, you'll get the right time but DateTime.Now will not reflect those changes.

无论出于何种原因,CF 实际上会在启动时缓存时区信息(我认为完整的框架也是如此)。因此,当您更改时区时,CF 不会意识到这些更改。如果您使用 GetSystem/LocalTime API,您将获得正确的时间,但 DateTime.Now 不会反映这些更改。

I reported this back in 2004.

我早在 2004 年就报告了这一点

On the desktop you have the TimezoneInfo.ClearCachedInfomethod, but it doesn't exist in the CF.

在桌面上,您有TimezoneInfo.ClearCachedInfo方法,但它在 CF 中不存在。

回答by Adi

I successfully used OpenNETCF.WindowsCE.DateTimeHelperclass. My platform:

我成功地使用了OpenNETCF.WindowsCE.DateTimeHelper类。我的平台:

Windows Mobile 6.1

视窗手机 6.1

CF 3.5

CF 3.5