.NET CF 3.5 SetTimeZoneInformation问题 [英] .NET CF 3.5 SetTimeZoneInformation problem

查看:51
本文介绍了.NET CF 3.5 SetTimeZoneInformation问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式在C#CF 3.5应用程序中设置时区.我要执行此操作的原因是,该应用程序已安装在数百个Motorola MC55设备上,并且其中一些设备会定期放电.充电后,时区设备的时区设置为不等于我的时区(UTC + 1华沙)的某个奇异"值.我使用OpenNETCF.WindowsCE.DateTimeHelper.SetTimeZoneInformation API来设置时区.我通过获取OpenNETCF.WindowsCE.TimeZoneCollection的列表并搜索关键字"Warsaw"来获取OpenNETCF.WindowsCE.TimeZoneInformation对象.因此,我有一个正确设置的OpenNETCF.WindowsCE.TimeZoneInformation对象,该对象传递给OpenNETCF.WindowsCE.DateTimeHelper.SetTimeZoneInformation API调用.问题在于该时区是夏令时",并且最近(在设置了最近的夏季时间之后)我的应用程序中的本地时间比原本应晚1个小时.最好的是,问题仅存在于某些设备上,而不是全部存在.

I want to programatically set time zone in C# CF 3.5 application. The reason I want to do this is the fact that the application is installed on hundreds of Motorola MC55 devices and some of them get periodically discharged. After charging, the timezone device timezone is set to some "exotic" value not equal to my time zone (UTC+1 Warsaw). I use OpenNETCF.WindowsCE.DateTimeHelper.SetTimeZoneInformation API to set the timezone. I get the OpenNETCF.WindowsCE.TimeZoneInformation object by getting list of OpenNETCF.WindowsCE.TimeZoneCollection and searching the keyword "Warsaw". Thus I have a correctly set OpenNETCF.WindowsCE.TimeZoneInformation object that I pass to OpenNETCF.WindowsCE.DateTimeHelper.SetTimeZoneInformation API call. The problem is that this timezone is "Day light saving" and recently (after recent summer time set) the local time in my application is 1 hour late than it should be. The best is that the problem exists on some devices only, not on all.

可能是什么原因?

推荐答案

OpenNETCF TimeZoneCollection直接从OS获取数据,它没有做任何事情,只是充当数据封送处理程序.如果您看到的DST偏移量,DST日期或TZ偏差不正确,那是因为您的操作系统定义不正确.

The OpenNETCF TimeZoneCollection is getting data directly from the OS, it's not doing anything but acting as a data marshaler. If you're seeing incorrect DST offsets, DST dates or TZ biases, it's because the OS you have has these defined incorrectly.

这很常见,特别是因为世界各地喜欢定期更改本地时间计算的定义,并且操作系统构建不知道这些事情已经发生.因此,有大量的CE 5.0和更早的设备在美国仍然无法正确计算DST更改日期.

This is not unusual, especially since different parts of the world like to periodically change the definitions for local time calculations and the OS build has no idea that these have happened. THere a loads of CE 5.0 and earlier devices out there that still improperly calculate DST change dates in the US because of this.

解决方案是使用使用正确定义构建的操作系统来更新设备.

The solution is to update the device with an OS that was built with the proper definitions.

现在,如果您遇到将TimeZone设置为"UTC +1 PlaceA"之类的问题并返回"UTC +1 PlaceB"的问题,那么我认为这是一个已知问题(我想我还记得一些关于无论如何,这都是过去5年的结果),因为它在操作系统中根本无法正常运行.只需查看源代码即可:

Now if you're seeing a problem where you set the TimeZone to something like "UTC +1 PlaceA" and you get back "UTC +1 PlaceB" then I think that's an known issue (I think I recall some questions on this in the past 5 years anyway) because it's not handled well at all down in the OS. Just look at the source:

public static void SetTimeZoneInformation( TimeZoneInformation tzi )
{
    // Call CE function (implicit conversion occurs to
    // byte[]).
    if (!NativeMethods.SetTimeZoneInformation(tzi))
    {
        throw new System.ComponentModel.Win32Exception(
            Marshal.GetLastWin32Error(), "Cannot Set Time Zone");
    }
}

您可以看到,我们只是传递从设备中检索到的TZI(对于非WinMo,是从注册表中获得的),并组装在TimeZoneCollection类中(从第129行开始).

You can see, we're just passing the TZI retrieved from the device (from the registry in the case of non-WinMo) and assembled in the TimeZoneCollection class (starting at line 129).

在我们创建的必须处理时区,DST等的解决方案中,我们实际上最终编写了自己的服务来处理所有DST/TZ计算.我们使用操作系统存储GMT,仅此而已,然后手动进行了所有补偿.售出的产品是涵盖多个时区的几条快递,而且我们没有听说过任何问题(而且由于它是一个时钟,所以任何时间问题都会变得非常明显).

In a solution we created that had to handle timezones, DST, etc. we actually ended up writing our own service that handled all DST/TZ calculations. We used the OS to store GMT and nothing else, then did all offsets manually. The product is sold is several couries covering several time zones and we've heard of no problems (and since it's a time clock, any issues with time would become evident really quick).

这篇关于.NET CF 3.5 SetTimeZoneInformation问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆