views:

187

answers:

6

Right now I'm doing:

bool UseMetricByDefault() {
    return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours >= 0;
}

This works for distinguishing USA from Europe and Asia, but it ignores South America.

Is there a better way?

+2  A: 

Looks like there's already an implementation in System.Globalization.RegionInfo (regionInfo.IsMetric).

You could use the CultureInfo class which can return the Culture of the operating system you're running on - usually in the format en-US en-GB de-DE etc. Cultures are more often used for translating applications from one language to another depending on the culture of the operating system.

This is all for specified localisation - you may want to read up on some tutorials and articles regarding it (ie here).

Daniel May
Don't you mean the RegionInfo class? CultureInfo gets this related to language - RegionInfo has things language, measurement, and currency.
Charlie Salts
Yep, yep. I've left info about `CultureInfo` in there anyway, looks like I could do with a break for a half hour or so...
Daniel May
+11  A: 

Use the RegionInfo class in the System.Globalization namespace:

bool isMetric = RegionInfo.CurrentRegion.IsMetric;

If you want a specific region, you can use one of the following:

// from a CultureInfo
CultureInfo culture = ...;
RegionInfo r = new RegionInfo(culture.Name);

// from a string
RegionInfo r = new RegionInfo("us");
Wim Hollebrandse
Awesome. Thanks.
Daniel Straight
Updated to use the user's culture (CurrentRegion). Revert if you don't like it. :)
280Z28
No probs, good edit.
Wim Hollebrandse
+2  A: 

Part of the problem is that even if you use their RegionInfo, not everyone in a region uses the same system. In the US, scientists often use metric in their research, and here in Canada, its a bit of a mix, since we get a lot of food packaging from the US but we teach our children in metric (but only since recently - my parents still think in imperial).

That being said, you can make a fair guess using the RegionInfo class.

Charlie Salts
A fair guess is good enough for me, but thanks for the tip.
Daniel Straight
A: 

Only a few small countries, including some un-listed Caribbean nations heavily influenced by the U.S., have not formally adopted the use of SI. Among countries not claiming to be metric, the U.S. is the only significant holdout.

http://lamar.colostate.edu/~hillger/internat.htm#chart

Just check if the user is from the US. This should be good enough for most purposes. There are a few exceptions, but it's probably not worth coding it unless one of those regions is a significant market for you.

Mark Byers
+2  A: 

It's a property on the RegionInfo class:

// en-US, en-GB, de-DE etc.
string cultureName = CultureInfo.CurrentCulture.Name;
RegionInfo regionInfo = new RegionInfo(cultureName);
Console.WriteLine("You prefer to see {0}", regionInfo.IsMetric ? "metric" : "imperial");
Tim Robinson
A: 

For a "stab in the dark" the RegionInfo information is good enough.

However in practice places like the UK have exceptions. For example:

  • anything traffic related is imperial
  • temperatures for weather are metric, but some people prefer imperial
  • measurements of equipment are metric... mostly

Much depends on the generation you're in and your attitude to change.

So - in practice - provide both, take a guess at a sensible default, but if it's important always allow the user to override the preference, and make sure these changes are remembered.

Jeremy McGee