0

Newbie here. Actually, first question ever.

I've been using NodaTime (on ASP.Net MVC 3 C#) to get around different time zones and am really happy with the results. But... I was wondering if there's a way to get the user's culture info based on the time zone - ie: to display the date/time.

Thanks in advance for any help!

UPDATE

Let me be more clear on this...

I'm able to determine the user time zone using jsTimeZoneDetect and NodaTime. It's working perfectly. Now, what I need is to find out the user locale info (date/time format, etc) to be able to display these info correctly. ie: An user based on US would see the date as 3/21/2013.

I've tried to obtain the locale from the Request.UserLanguages with no avail. IE returns my locale correctly (en-GB), but both Firefox and Chrome are always returning en-US. I've also changed the web.config and added the following element:

<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true" />

And that's the piece of code I've been working on:

var userLanguages = Request.UserLanguages;

CultureInfo ci;

if (userLanguages.Length > 0)
{
    try
    {
        ci = new CultureInfo(userLanguages[0]);
    }
    catch (CultureNotFoundException)
    {
        ci = CultureInfo.InvariantCulture;
    }
}
else
{
    ci = CultureInfo.InvariantCulture;
}

string cultureName = ci.Name /* Always returns en-US */

Just as another point, to ask the user to select their locale info is not an option for me. ie: DropdownList on the site, etc.

Any further help? Thanks in advance.

UPDATE 2

Following @JonSkeet advice, I've changed the question title and labels to fit my issue better.

UPDATE 3

Just found out something. The way the language (locale) is returned, depends on the browser, as follows:

  • Internet Explorer returns the language set on the OS settings, or the browser ones.
  • Firefox, Chrome, Safari and Opera return the language set on the browser settings.

Considering this, I can't even think about using this information as it comes, because there's no way to guarantee that every single user will change their browser settings. The only info I could rely on is the one coming from the OS settings.

I don't have a clue on what to do now.

emerson.marini
  • 9,331
  • 2
  • 29
  • 46
  • 1
    How are you getting the user's time zone to start with? I'd expect it to be easier to get the culture, via the HTTP request information. – Jon Skeet Mar 20 '13 at 17:24
  • 1
    Time zone really isn't granular enough to determine culture. There are several time zones that contain multiple culture types. – jmoerdyk Mar 20 '13 at 17:28

1 Answers1

0

To answer the question as asked, regardless of the clarification requested...

In 1.0, we don't have any geographical information available. In 1.1, we're exposing the info from zoneinfo.tab which associates a location with each time zone (or at least most of them) including the country. It doesn't go as far as full culture information (it doesn't differentiate between French Canadian and English Canadian for example, or the various languages spoken in Switzerland) but it gives you some idea. You'd access the information via TzdbDateTimeZoneSource.GeoLocations. (It's only available for TZDB.)

Of course, 1.1 isn't released yet - but we're hoping to do so in the next couple of weeks, almost certainly without any large API changes.

I'd still recommend detecting the culture via the HTTP request normally though.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi @JonSkeet. Thanks for quick reply. That's the piece of code: `code` var zone1 = tzdb["UTC"]; var ldt1 = new LocalDateTime(2013, 3 20, 8, 0); var zdt1 = zone1.AtLeniently(ldt1); var zone2 = tzdb["America/Los_Angeles"]; var zdt2 = zdt1.ToInstant().InZone(zone2); var ldt2 = zdt2.LocalDateTime; `code` – emerson.marini Mar 20 '13 at 17:35
  • @MelanciaUK: It's not clear what that code has to do with your question though... that code doesn't use culture at all, and doesn't explain how you're inferring the user's time zone. – Jon Skeet Mar 20 '13 at 17:36
  • The only piece of info I'd need now is the date/time format. Will research about HTTP requests as pointed out by you. Cheers – emerson.marini Mar 20 '13 at 17:36
  • Got it. It'd be: `code` Console.Write(ldt2.ToString(null, new CultureInfo("en-US"))); `code` – emerson.marini Mar 20 '13 at 17:37
  • 1
    @MelanciaUK: See http://mikehadlow.blogspot.co.uk/2010/11/aspnet-httpmodule-to-set-current.html for example. – Jon Skeet Mar 20 '13 at 17:38
  • @MelanciaUK: Well no, because that's *always* the US culture. Aren't you trying to work out the *user's* culture? (I'd also suggest using `LocalDatePattern`, but that's a slightly different matter.) – Jon Skeet Mar 20 '13 at 17:39
  • I thought I had my problem solved by using the technique found on the link you've posted. But no. Let me see if I can be more clear (sorry, English is not my first language). I've updated the post. Cheers – emerson.marini Mar 21 '13 at 11:14
  • is there now. Sorry for the delay. – emerson.marini Mar 21 '13 at 11:23
  • @MelanciaUK: Okay. At this point the question isn't really about time zones or Noda Time at all - that's the reason you need the culture, but it's "down-stream" of the real question of "How do I determine the user's culture on the web?" You may well be best off deleting this question and asking another one which is more focused (after searching to find potential duplicates, of course). – Jon Skeet Mar 21 '13 at 11:27
  • Will do that then. Thanks for your help. – emerson.marini Mar 21 '13 at 11:31