Skip to content

Get timezone via longitude and latitude in Swift in a fast way

License

MIT, ODbL-1.0 licenses found

Licenses found

MIT
LICENSE
ODbL-1.0
LICENSE_DATA
Notifications You must be signed in to change notification settings

ringsaturn/tzf-swift

Repository files navigation

tzf-swift: a fast timezone finder for Swift

Swift

Note

This package use a simplified polygon data and not so accurate around borders.

Usage

Add the dependency to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/ringsaturn/tzf-swift.git", from: "{latest_version}")
]

Then add something like this:

import Foundation
import tzf

do {
    let finder = try DefaultFinder()

    // Test for Beijing
    let timezone = try finder.getTimezone(lng: 116.3833, lat: 39.9167)
    print("Beijing timezone:", timezone)

    // Test for a location with multiple possible timezones
    let timezones = try finder.getTimezones(lng: 87.5703, lat: 43.8146)
    print("Multiple possible timezones:", timezones)

    // Get data version
    print("Data version:", finder.dataVersion())

} catch {
    print("Error:", error)
}

Output:

Build of product 'demo' complete! (0.15s)
Beijing timezone: Asia/Shanghai
Multiple possible timezones: ["Asia/Shanghai", "Asia/Urumqi"]
Data version: 2025a/2025a

Performance

Just like tzf packages in Go/Rust/Python, the Swift version is also fast, and designed for server-side high-performance use cases.

Hardware: MacBook Pro with Apple M3 Max.

Processing 1 million queries took 4500ms. Benchmark Summary:

Implementation Test Scale Execution Time (ms) Success Rate Operations per Second (op/sec) Memory Usage (Peak MB) Instructions (G)
TZF.DefaultFinder 1,000,000 4,717 100% ~212,000 129 73
TZF.Finder 1,000,000 19,000 100% ~52,632 115 324
TZF.PreindexFinder 1,000,000 1,548 ~85% ~646,000 129 23
SwiftTimeZoneLookup.lookup 10,000 3,077 100% ~3,250 105 42
SwiftTimeZoneLookup.simple 10,000 3,209 100% ~3,116 104 45

Full benchmark results can be viewed in benchmark_baseline.txt.

Related Projects

Language or Sever Link Note
Go ringsaturn/tzf
Ruby HarlemSquirrel/tzf-rb build with tzf-rs
Rust ringsaturn/tzf-rs
Swift ringsaturn/tzf-swift
Python ringsaturn/tzfpy build with tzf-rs
HTTP API ringsaturn/tzf-server build with tzf
HTTP API racemap/rust-tz-service build with tzf-rs
Redis Server ringsaturn/tzf-server build with tzf
Redis Server ringsaturn/redizone build with tzf-rs
JS via Wasm(browser only) ringsaturn/tzf-wasm build with tzf-rs
Online ringsaturn/tzf-web build with tzf-wasm

License

This project is licensed under the MIT License. See the LICENSE file for details.

The data is licensed under ODbL-1.0 license, which compiled from https://github.com/evansiroky/timezone-boundary-builder