Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 DateTime from any time zone to any other time zone in SilverLight?

like image 918
tk. Avatar asked Jan 06 '11 18:01

tk.


1 Answers

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

like image 121
Igor V Savchenko Avatar answered Oct 16 '22 05:10

Igor V Savchenko