Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the standard Noda Timezone Id's from English to Localized Language?

Tags:

c#

nodatime

I am currently trying to localize my windowsphone Time App for a few countries. I am using Noda Time as it was extremely easy for a newbie. The problem I am facing that all the Timezone Id's are in standard English and I am searching for a way to get those Id's converted to Local Language strings.

One way would be too make Localized strings for each ID in every language. But it seems to be Highly inefficient as there are 500 timezones. Please suggest a way for me to get directly the TimeZone ID's converted into Local Language in a less time consuming way.

My code:

var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
var tzdb = DateTimeZoneProviders.Tzdb;

var list = from id in tzdb.Ids
           where id.Contains("/") && !id.StartsWith("etc", StringComparison.OrdinalIgnoreCase)
           let tz = tzdb[id]
           let offset = tz.GetUtcOffset(now)
           orderby offset, id
           select new
           {
               DisplayValue = string.Format("(UTC{0}) {1}  {2}  ", offset.ToString("+HH:mm", null), now.WithOffset(offset).TimeOfDay.ToString("hh:mm tt",null),id)
           };
like image 936
aman shivhare Avatar asked Dec 21 '14 21:12

aman shivhare


People also ask

Can I use timezoneinfo instead of Noda time?

They work really well in .NET using the Noda Time library, which I highly recommend using instead of TimeZoneInfo - for a variety of reasons. If your system is already built around the Windows time zone id's that TimeZoneInfo uses, fret not. You can still use Noda Time, along with some simple conversion functions.

What is the best way to represent clock values in Noda time?

Clock-related "point in time" values are usually best represented as Instant values in Noda Time. Even if they have been recorded in a particular time zone, that time zone is rarely relevant other than as a way of converting the local time to a universal one.

What is Noda time and how does it work?

Noda Time will force you to consider what time zone you're interested in, how to handle ambiguities converting back and so on. If a user has entered the value (whether directly into your application or not), Noda Time offers five types which may be appropriate:

Can I use localdatetime If I don't know the time zone?

If you don't have any knowledge of the user's time zone, you're pretty much stuck with LocalDateTime, but even if you do know the time zone, it may not be appropriate to use it. Is the user intending to indicate a specific instant, or just a date and time?


1 Answers

This isn't a current feature of Noda Time, so you will need to get the data elsewhere.

Localizations for time zones (and other items) are best found within the Unicode CLDR project. You can write code to parse the various XML files included with the CLDR releases. You'll also need to understand how the data is represented, and several edge cases.

Or, you can use the implementation that I've already completed.

Install the TimeZoneNames Nuget package:

PM>  Install-Package TimeZoneNames

Then you can easily resolve the time zone names to a localized display value:

// example input values
var names = TZNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");

// example output values
Assert.Equal("Pacific Time", names.Generic);
Assert.Equal("Pacific Standard Time", names.Standard);
Assert.Equal("Pacific Daylight Time", names.Daylight);

This works for non-English locales as well. It will produce valid text in any language, provided that the data exists in the CLDR.

The same library can also be used to get a list of time zone ids for a particular country. This can be used to implement a two-dropdown selection list, where first you pick the country, and then you pick the time zone within the country.

var zones = TZNames.GetTimeZoneIdsForCountry("US");

You can take a look at the project's unit tests for further examples.

like image 160
Matt Johnson-Pint Avatar answered Oct 19 '22 01:10

Matt Johnson-Pint