Get the Default Timezone for a Country (Via Cultureinfo)

Get the default timezone for a country (via CultureInfo)

As identified in the comments of the question, you aren't going to be able to get a single time zone for each country. There are just too many cases of countries that have multiple time zones.

What you can do is filter the list of standard IANA/Olson time zones down to those available within a specific country.

One way to do this in C# is with Noda Time:

IEnumerable<string> zoneIds = TzdbDateTimeZoneSource.Default.ZoneLocations
.Where(x => x.CountryCode == countryCode)
.Select(x => x.ZoneId);

Pass a two-digit ISO-3166 country code, such as "AU" for Australia. The results are:

"Australia/Lord_Howe",
"Australia/Hobart",
"Australia/Currie",
"Australia/Melbourne",
"Australia/Sydney",
"Australia/Broken_Hill",
"Australia/Brisbane",
"Australia/Lindeman",
"Australia/Adelaide",
"Australia/Darwin",
"Australia/Perth",
"Australia/Eucla"

And if for some reason you'd like Windows time zone identifiers that you can use with the TimeZoneInfo object, Noda Time can map those too:

var source = TzdbDateTimeZoneSource.Default;
IEnumerable<string> windowsZoneIds = source.ZoneLocations
.Where(x => x.CountryCode == countryCode)
.Select(tz => source.WindowsMapping.MapZones
.FirstOrDefault(x => x.TzdbIds.Contains(
source.CanonicalIdMap.First(y => y.Value == tz.ZoneId).Key)))
.Where(x => x != null)
.Select(x => x.WindowsId)
.Distinct()

Again, called with "AU" for Australia returns:

"Tasmania Standard Time",
"AUS Eastern Standard Time",
"Cen. Australia Standard Time",
"E. Australia Standard Time",
"AUS Central Standard Time",
"W. Australia Standard Time"

If you're wondering about how reliable this data is, the country to tzid mapping is part of the IANA time zone database itself, in the zone.tab file. The IANA to Windows mapping data comes from the Unicode CLDR supplemental data. It doesn't get any closer to "official" than that.

How to get timezone from properties in CultureInfo

Short answer: No

Long Answer: This is a 1 to many relationship between culture and timezone. For example:
en-US - English, American contains 6 timezones... so how would you get from en-US to Eastern Daylight time? ... and how do you get the daylight savings time offsets?

It's a confusing cultural conundrum (wow, try saying that 5 times fast) that is not easily solvable with the CLR.

Get timezone by Country and Region

Finally, after long research, I've found the solution. It may not be much accurate, but it does not require the dependency of remote service.

I've found that maxmind's database provides mapping file between regions and time zones from Olsen database (aka tz database): http://www.maxmind.com/timezone.txt

The second step was to convert Olsen timezones in .NET timezones. The best suitable solution was found here: https://www.timdavis.com/posts/olson-time-zone-database-to-standard-windows-time-zone-v0-1
So I've edited it a bit and converted it to CSV file to read it from .NET.

Hope it will help somebody.

Get timezone by current culture info with c#

If all you've got is culture, that's basically some string like: MX (Spanish Mexico), es-CO (Spanish Columbia), and fr-CA (French Canada) then you just have to correspond each on of these to a timezone, except no such one to one relationship exists. So no. You can roughly know the timezone (as pointed out in comments), fr-CA is all in the same timezone, but MX is in four (MX isn't enough information to gauge timezone).

So you don't have enough data to know, what can you do. If you need to know their timezone on the server side try geo-location:
How can I find a user's GEO Location using ASP.NET?

However, if it's just the web page that needs to know, try putting that logic in the JavaScript which is executing on their machine.

Or you could just... ask them.

Get the culture info from a time zone or time zone offset c#

As others have said, they are unrelated. You might think their is some correlation, and you may be able to find examples where they are, but then you'll find many where they are not.

Consider that my culture may be en-US, even if I'm visiting or living in Japan. The culture code means "English as spoken in the United States". It doesn't refer to the current physical location of a user.

Also, if you only have a time zone offset, you certainly can't make any correlation, as there are countries in both North American and South America that share the same offset at some point or another, but use different date formats.

If you have an actual time zone identifier, then it depends on which type you have. For location-based IANA identifiers such as "America/New_York", you can look up the country code, and guess the format from there - though you'll still have the foreign visitor issue I described above. Also, there are IANA identifiers like "Etc/GMT-2" that are not country specific. And then if you have Windows time zone identifiers (such as those used with TimeZoneInfo in .NET), there are several that span multiple countries. There's no guarantee every country that uses the time zone uses the same date format.

Take a look at date formats by country on Wikipedia. You'll see that there are many places where more than one date format is in use, depending on context. The case of the Philippines, is of particular interest, as they use use both MDY and DMY formats.

DateTime.Now and Culture/Timezone specific

It sounds like you need to store a DateTimeOffset instead of a DateTime. You could just store the local DateTime to the user creating the value, but that means you can't perform any ordering operations etc. You can't just use DateTime.UtcNow, as that won't store anything to indicate the local date/time of the user when the record was created.

Alternatively, you could store an instant in time along with the user's time zone - that's harder to achieve, but would give you more information as then you'd be able to say things like "What is the user's local time one hour later?"

The hosting of the server should be irrelevant - you should never use the server's time zone. However, you will need to know the appropriate UTC offset (or time zone) for the user. This cannot be done based on just the culture - you'll want to use Javascript on the user's machine to determine the UTC offset at the time you're interested in (not necessarily "now").

Once you've worked out how to store the value, retrieving it is simple - if you've already stored the UTC instant and an offset, you just apply that offset and you'll get back to the original user's local time. You haven't said how you're converting values to text, but it should just drop out simply - just format the value, and you should get the original local time.

If you decide to use Noda Time, you'd just use OffsetDateTime instead of DateTimeOffset.

Get current culture info based on TimeZoneId in C# application

Time zone and culture/locale are orthogonal concepts. They are completely disconnected. One cannot tell the culture from the time zone or vice versa.

For example, I may be an English speaking American (culture code en-US), but I may be traveling or living in Japan (IANA time zone Asia/Tokyo, Windows time zone ID Tokyo Standard Time).

To be more specific in .NET:

CultureInfo.CurrentCulture    // how do you want your numbers and dates to be formatted?
CultureInfo.CurrentUICulture // what language and dialect do you speak?
TimeZoneInfo.Local.Id // what time zone are you observing? (on the local computer)

Get current UTC date in c# based on country code

UTC stands for Universal Time Co-ordinated. The whole point of it is that it is always the same, regardless of time zone or country. At the moment, in Britain the local time is 9:01 am as I type this, but the time in UTC is 8:01.

See https://www.timeanddate.com/time/aboututc.html

You are better off storing all timestamps in UTC and then converting them to local time for display.

So, to answer your question, you can't do it. Country and timezone are meaningless to UTC.

Hope this helps,

Adam.



Related Topics



Leave a reply



Submit