开发者

silverlight Time Zone converting

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.

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

Is there a way to convert DateTim开发者_开发问答e from any time zone to any other time zone in SilverLight?


Unfortunately currently there is no standard functionality to do that.

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;
    }
}

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);

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 :)


Kind of a little late, but I did this a while back and posted it up on the MS community site for people to use because it was always being asked. I didn't do the timezone IDs lookup, but I used the FromSerializedString() method within my SL app. My web services would pass the ToSerializedString() data as a string property and in SL I would just consume this object as needed. I did a reflector of the TimeZoneInfo object.

https://github.com/TWhidden/DevStuff/blob/master/StackOverflow/TimeZoneInfo.cs

Not sure if this helps you (or any future reader of this post).

It was fun to do, and solved my problem. I was using this for V3 Silverlight, so it should be good for 4/5, but also could be improved by Microsoft already.

Updated 2017-04-12: Link to GitHub, using this in a PCL lib. Should work for SL, but we dropped that like MS did years ago.


In case anyone runs across this thread, here's what I've found...

The Microsoft reference source code for TimeZoneInfo (in the full .NET 4 CLR) includes a TryGetTimeZoneByRegistryKey() method, which gets the time zones from the registry key HKLM\Software\Microsoft\Windows NT\CurrentVersion\Time Zones.

I guess if you were writing a trusted Silverlight app, you could use P/Invoke to do the registry lookup.

For our app, we don't want to show the dialog asking for full trust permissions. I did some more searching and found Stephane Delcroix's open-source implementation - I'm not sure if there's a more official source than that link. It works with the Unix time zone database format (zoneinfo), which you can download from IANA.

The time zone database is quite large, and would bloat the size of a Silverlight app if you included it. But we only needed to support a few time zones. I took a subset of the database and packaged it into our Silverlight app as a resource; then I modified Stephane's code to read from a resource stream rather than from the filesystem. I had to remove all the local time functionality, since there is no /etc/localtime. But we only needed named time zones, and the ability to convert between these time zones and UTC. This approach seems to work OK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜