Skip to content

Instantly share code, notes, and snippets.

@smarenkov
Created January 17, 2026 16:54
Show Gist options
  • Select an option

  • Save smarenkov/0ddeff72e84409c9f6a07cdd17e51cd0 to your computer and use it in GitHub Desktop.

Select an option

Save smarenkov/0ddeff72e84409c9f6a07cdd17e51cd0 to your computer and use it in GitHub Desktop.
timezone
import { CityData, cityMapping } from 'city-timezones';
import { Country, getTimezone, getCountry as getCountryByCode } from 'countries-and-timezones';
/**
* Retrieves the country associated with a given timezone.
* @param timezone - The IANA timezone string (e.g., 'Europe/Paris').
* @returns The country object or null if not found.
*/
function getCountryByTimezone(timezone: string): Country | null {
const timezoneData = getTimezone(timezone);
if (timezoneData && timezoneData.countries.length > 0) {
const countryCode = timezoneData.countries[0]; // Assuming the first country is the primary one
const country = getCountryByCode(countryCode);
return country ? country : null;
}
return null;
}
/**
* Retrieves cities associated with a given timezone.
* @param {string} timezone - The IANA timezone string (e.g., 'Europe/Paris').
* @returns {Array} An array of city objects associated with the timezone.
*/
function getCitiesByTimezone(timezone: string): CityData[] {
return cityMapping.filter(city => city.timezone === timezone);
}
function getCountry(timezone: string): string | null {
const country = getCountryByTimezone(timezone);
return country ? country.name : null;
}
function getCity(timezone: string): string | null {
const cities = getCitiesByTimezone(timezone);
if (!cities || cities.length === 0) {
return null;
}
const cityLikeTimezone = cities.find(city => timezone.toLowerCase().includes(city.city.toLowerCase()));
if (cityLikeTimezone) {
return cityLikeTimezone.city;
}
return cities[0].city;
}
export { getCountry, getCity };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment