A high-performance TypeScript/JavaScript library for managing city timezone information worldwide. Uses optimized data structures and binary search for extremely fast lookups.
- 🚀 High-performance binary search for lookups
- 🌍 Comprehensive worldwide city database with coordinates
- 🔍 Fast city search with smart partial matching and fuzzy search
- 🕒 Timezone offset and time difference calculations
- 🌐 ISO country code support
- ✨ Full TypeScript support
- 📦 Zero dependencies
- ⚡ Optimized for speed
- Node.js >= 18.0.0
npm install metropolis-tz
import CityTimezones from 'metropolis-tz';
const cityTz = new CityTimezones();
// Get timezone for a city
const timezone = cityTz.getTimezone('New York', 'US');
console.log(timezone); // 'America/New_York'
// Search for cities (with typo tolerance)
const cities = cityTz.searchCitiesFuzzy('londun', 2);
// Will find 'London' despite the typo
// Validate city and timezone
const isValidCity = cityTz.isValidCity('Paris', 'FR'); // true
const isValidTz = cityTz.isValidTimezone('Europe/Paris'); // true
// Get time difference
const diff = cityTz.getTimeDifference('New York', 'US', 'London', 'GB');
console.log(cityTz.formatTimeDifference(diff!));
// "behind by 5 hours" (during EDT)
Gets the IANA timezone identifier for a city.
const timezone = cityTz.getTimezone('Paris', 'FR');
// Returns: 'Europe/Paris'
Searches for cities by name (case-insensitive, partial match).
const cities = cityTz.searchCities('san');
// Returns: San Francisco, San Diego, San Jose, etc.
Searches for cities with typo tolerance using Levenshtein distance.
const cities = cityTz.searchCitiesFuzzy('londun', 2);
// Returns: London (despite the typo)
Checks if a city exists in the database.
const isValid = cityTz.isValidCity('Paris', 'FR'); // true
Checks if a timezone is supported.
const isValid = cityTz.isValidTimezone('Europe/Paris'); // true
Gets the current UTC offset in minutes for a timezone.
const offset = cityTz.getTimezoneOffset('America/Los_Angeles');
// Returns: -420 (7 hours behind UTC during PDT)
getTimeDifference(fromCity: string, fromCountry: string, toCity: string, toCountry: string): number | null
Calculates time difference between cities in minutes.
const diff = cityTz.getTimeDifference('Tokyo', 'JP', 'London', 'GB');
console.log(cityTz.formatTimeDifference(diff!));
// Returns human-readable time difference
convertTime(time: Date, fromCity: string, fromCountry: string, toCity: string, toCountry: string): Date | null
Converts a time from one city's timezone to another's.
const time = new Date('2024-03-15T12:00:00');
const converted = cityTz.convertTime(time, 'New York', 'US', 'Tokyo', 'JP');
Finds cities closest to given coordinates.
const nearby = cityTz.findNearestCities(51.5074, -0.1278, 3); // Near London
Formats city information using a template string.
const city = cityTz.findNearestCities(51.5074, -0.1278, 1)[0];
console.log(cityTz.formatCityInfo(city, '{name}, {country} ({coordinates})'));
// "London, GB (51.5074,-0.1278)"
Returns all cities in the database.
Gets all cities in a specific timezone.
Returns all unique country codes.
Returns all unique timezone identifiers.
Returns all cities in a specific country.
interface CityInfo {
name: string; // City name
country: string; // ISO country code (e.g., 'US', 'GB')
timezone: string; // IANA timezone (e.g., 'America/New_York')
latitude: number; // Geographical latitude
longitude: number; // Geographical longitude
}
The library uses optimized data structures and algorithms:
- Binary search for exact matches (O(log n))
- Pre-computed search keys
- Efficient string operations
- Smart partial matching
- Fuzzy search with Levenshtein distance
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
Jace Sleeman