Silverlight时区转换 [英] silverlight Time Zone converting

查看:91
本文介绍了Silverlight时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将WPF应用程序迁移到SilverLight 4.
WPF应用程序使用TimeZoneInfo.FindSystemTimeZoneById()和TimeZoneInfo.ConvertTimeFromUtc()将特定时区的DateTime转换为另一个特定时区的DateTime

I'm trying to migrate a WPF app to SilverLight 4. The WPF app use TimeZoneInfo.FindSystemTimeZoneById() and TimeZoneInfo.ConvertTimeFromUtc() to convert DateTime of speicific time zone into the DateTime of another specific time zone.

但是我在SilverLight 4中找不到这些功能。SilverLight似乎支持Utc和Local之间的时区转换。

But I can't find either of these functions in SilverLight 4. SilverLight seems to support time zone convert betwen Utc and Local only.

有没有办法将Silverlight中的任何时区的DateTime转换为任何其他时区?

Is there a way to convert DateTime from any time zone to any other time zone in SilverLight?

推荐答案

p>不幸的是,目前没有标准的功能来做到这一点。

Unfortunately currently there is no standard functionality to do that.

允许检查(使用反射器)TimeZoneInfo.FindSystemTimeZoneById()方法的工作原理。它只需要s_systemTimeZones字段中的一个值:

Lets check (using reflector) how TimeZoneInfo.FindSystemTimeZoneById() method works. It just takes one of values from s_systemTimeZones field:

private static Dictionary<string, TimeZoneInfo> s_systemTimeZones
{
    get
    {
        if (s_hiddenSystemTimeZones == null)
        {
            s_hiddenSystemTimeZones = new Dictionary<string, TimeZoneInfo>();
        }
        return s_hiddenSystemTimeZones;
    }
    set
    {
        s_hiddenSystemTimeZones = value;
    }
}

此字段存储所有可用的TimeZoneInfo。当您调用FindSystemTimeZoneById(id)时,它只是从预填充字典中选取一些值。我不知道这个字典何时初始化,以及它用于初始化的值。但是来自此线程的人告诉TimeZoneInfo使用注册表中的值:HKEY_LOCAL_MACHINE\\ \\ Software \Microsoft\Windows NT\CurrentVersion\TimeZones

This field stores all available TimeZoneInfo-s. And when you call FindSystemTimeZoneById(id) it just picked some value from prefilled dictionary. I don't know when this dictionary initializes and which values it uses for initialization. But guy from this thread told that TimeZoneInfo use values from registry: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones

最明显的方法是创建自己的字典字典,并用值填充它。这样的东西:

Most obvious way is create own Dictionary dictionary and fill it with values. Something like this:

Dictionary<string, TimeZoneInfo> dictionary = new Dictionary<string, TimeZoneInfo>();
TimeZoneInfo info = new TimeZoneInfo("ID", new TimeSpan(0, 1, 0, 0), "SomeCultureName", "Some Standard Time", "Some Daylight Time", null, true);
dictionary.Add("Some time", info);

但还有另一个问题:TimeZoneInfo构造函数是私有的。所以如果你想使用FindSystemTimeZoneById()和ConvertTimeFromUtc()功能,那么你应该从头开始实现它。创建一个表示时区的课程,用时区信息创建和填写这个类的字典等等...

不是很好,我知道。但是我希望对你有用。:)

But there is another problem: TimeZoneInfo constructor is private. So if you want to use FindSystemTimeZoneById() and ConvertTimeFromUtc() functionality then you should implement it from very scratch. Create some class which represents time zone, create and fill dictionary of this class with time zones information and so on...
Not very good news, I know. But I hope it will be useful for you :)

这篇关于Silverlight时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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