Get timezone from country code / city
java - Is there any way to get TimeZone by only country name? - Stack Overflow
Get time zone(s) from abbreviation and country code
Getting timezone from address (country code, zip code and state)
» npm install countries-and-timezones
If I understand your question, you could try something like this -
String[] tzIds = TimeZone.getAvailableIDs();
List<String> al = new ArrayList<String>();
for (String timeZoneId : tzIds) {
if (timeZoneId.startsWith("Canada")) {
al.add(timeZoneId);
}
}
System.out.println(al);
The output (slightly re-formatted) here is
[Canada/Pacific, Canada/Yukon, Canada/Mountain, Canada/Central,
Canada/East-Saskatchewan, Canada/Saskatchewan, Canada/Eastern,
Canada/Atlantic, Canada/Newfoundland]
You can do:
TimeZone tz = TimeZone.getTimeZone (id);
Where id has to be an Olson name of the form Area/Location, such as America/Los_Angeles.
You can get the available time zones using:
String[] ids = TimeZone.getAvailableIDs(0);
The official documentation: http://developer.android.com/reference/java/util/TimeZone.html
I have weird requirement of getting user's timezone using address data. In this use case I don't have access to client side, so I need to guess timezone purely using address info. I've googled a lot and found https://github.com/infused/ziptz this python library somewhat close to what I want albeit US only. Is there any similar library for got that can achieve something similar in golang?
You do not want to use time zones to determine language. People in the same time zone can speak different languages.
One alternative is to check the accept-language header.
For a similar discussion see: How do you detect a website visitor's country (Specifically, US or not)?
You can't get the country or language from the time zone offset.
For example, take UTC+1 in winter time. This could be France, Spain, Norway, Germany, Belgium, etc. They're all in Central European Time.
If you can get an Olsen name such as "Europe/France" or "Europe/Germany" that clearly gives you a lot more information, but you can't get that just from the offset from UTC.
That functionality has been requested, and is pending in the following pull request:
https://github.com/moment/moment-timezone/pull/410
However, it would accept a country code (such as US), and would return a list of time zone identifiers, such as America/Los_Angeles. Note that the list you gave are not the values that would be returned.
Also keep in mind that one cannot assign a single offset to an entire time zone without considering point-in-time, so that would not be a part of the functionality either. See "time zone != offset" in the timezone tag wiki.
moment-timezone
MomentJS offers a moment-timezone package:
moment.tz.names(); would return an Array of all Timezones. These Timezones are prefixed with their respective continents; America/Detroit. You could easily filter this Array by Continent using plain JavaScript.
countries-and-timezones
To get a country/timezone matching you would definitely need another package like countries-and-timezones which offers a simple API to get Timezones by ISO country code:
countriesAndTimezones.getTimezonesForCountry('MX');
returns:
[ { name: 'America/Mexico_City',
utcOffset: -360,
offsetStr: '-06:00',
countries: [ 'MX' ] },
{ name: 'America/Cancun',
utcOffset: -300,
offsetStr: '-05:00',
countries: [ 'MX' ] },
{ name: 'America/Merida',
utcOffset: -360,
offsetStr: '-06:00',
countries: [ 'MX' ] },
{ name: 'America/Monterrey',
...
Note, that there is an n x m relation between Countries and Timezones (hence a Timezone may live in multiple countries). Therefore this package also contains a getCountriesForTimezone() method.
The builtin Java classes don't offer this, but ICU's TimeZone class does, and TimeZone.getAvailableIDs("US") provides the correct answer.
As per the documentation the getTimeZone method returns the specified TimeZone, or the GMT zone if the given ID cannot be understood. There's no TimeZone ID called US hence it gives the GMT zone. If you really want to get all the list of TimeZones available in US, I would suggest you to use the following.
final List<String> timeZonesInUS = Stream.of(TimeZone.getAvailableIDs())
.filter(zoneId -> zoneId.startsWith("US")).collect(Collectors.toList());
What is the easiest way to get a country code from a time zone in Go? Which libraries could you recommend that parse TZ database, and allow you to do things like this easily?