1

My Xamarin form for iOS and android is heavily use the device time and require to setup using different time in other countries.

I do a search and found this to list all the timezone available:

foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            Console.WriteLine(z.Id);

In my scenario, user can choose any of the sites and set the correct timezone based on where their sites are so the time will be shown relative to their chosen site.

I ran the code and least all the timezone available. Looks like the iOS and android have different presentation of the Timezone Id. I assumed is because of the different system.

I am just worry that the timezone id not fully listed coz our application runs internationally, actually most application run internationally.

var utcTime = DateTime.UtcNow;
        var zone = TimeZoneInfo.FindSystemTimeZoneById("Hongkong");
        var zoneTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, zone);
        System.Diagnostics.Debug.WriteLine("Device Timezone : " + DateTime.Now + "  " + TimeZone.CurrentTimeZone.StandardName);
        System.Diagnostics.Debug.WriteLine("Hong Kong Time : " + zoneTime.ToString("G"));

Another note: Will the above code taking care of the day light saving as well? e.g. In NZ, each year NZ have to move time forward or background by an hour where other countries are not.

LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • 1
    You might want to look at `Noda Time` (http://nodatime.org), also you should read https://stackoverflow.com/a/2532962/4984832 – SushiHangover Oct 19 '17 at 21:07
  • Will u explain the problem if using above code instead of nodatime you suggested? On my iOS, I change the date back to May which move back an hour and display the time, the time return is correct e.g. For Oct (now): 10:00 nz time and 5:00 in hk time. For May, 10:00 nz time and 6:00 – LittleFunny Oct 19 '17 at 21:19
  • It is unclear how you are using a UTC time vs. a local TZ converted time (you use now in your question and a previous UTC time in your comment) and timezones are not the same as offsets. If you are scheduling a "local" event, use a local time that includes the offset vs. a UTC time as the difference is dependent upon when it happens in the future and converting a UTC back to a local time can result in a different local time than originally requested. Read over the linked SO answer and comments as it covered these subjects. (I recommended reviewing Noda as it removes this confusion). – SushiHangover Oct 19 '17 at 21:34
  • I will have a look to the link first. thanks – LittleFunny Oct 19 '17 at 21:48

1 Answers1

0

TimeZoneInfo.GetSystemTimeZones() does exactly what it says - gets the time zones available on the system it is running on. In general, one cannot assume that all systems will have the same time zones installed.

It's a little safer to assume that if all systems are Windows desktop computers, but even then it will depend on whether those computers are kept updated with the latest time zones changes (delivered through Windows Update and OS builds in Windows 10).

In a cross-platform world, all bets are off. You might get Windows time zone IDs. You might get IANA time zone IDs. You might get something else.

You showed an example of "Hongkong". That is indeed a valid alias to "Asia/Hong_Kong" in the IANA time zone database. So probably your environment is giving IANA IDs. (You should prefer "Asia/Hong_Kong" though.)

If you think you might need to convert between IANA and Windows time zones, you can use my TimeZoneConverter library. However, in your case it seems like it would be much safer to use Noda Time, as it will bundle the time zone data with your application, rather than relying on the operating system to provide the data.

Yes, that code does correctly handle daylight saving time. For New Zealand, you'd use "Pacific/Auckland" for the IANA time zone ID. However, you should choose either TimeZoneInfo.Local.StandardName or TimeZoneInfo.DaylightName depending on TimeZoneInfo.IsDaylightSavingTime result. Or, if you want more accurate and localized names, you can use my TimeZoneNames library.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575