Skip to content

ringsaturn/tzf-swift

Repository files navigation

tzf-swift: a fast timezone finder for Swift

Swift FOSSA Status GitHub Tag

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

let finder = try DefaultFinder()

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

let timezones = try finder.getTimezones(lng: 87.5703, lat: 43.8146)
print("Multiple possible timezones:", timezones)

print("Data version:", finder.dataVersion())

if let macauGeoJSON = finder.getTimezoneGeoJSON(timezoneName: "Asia/Macau") {
  print("Asia/Macau features:", macauGeoJSON.features.count)
  print(try macauGeoJSON.toJSONString(pretty: false))
}

Output:

Beijing timezone: Asia/Shanghai
Multiple possible timezones: ["Asia/Shanghai", "Asia/Urumqi"]
Data version: 2026a/2026a
Asia/Macau features: 1
{"type":"FeatureCollection","features":[{"geometry":{"type":"MultiPolygon","coor...

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.

Benchmark Summary:

Implementation Test Scale Execution Time (ms) Success Rate Operations per Second (op/sec) Time per Op Memory Usage (Peak MB) Instructions
TZF.DefaultFinder 1,000,000 435 100% ~2,298,850 435 ns 285 ~3.9 G
TZF.PreindexFinder 1,000,000 322 ~85% ~3,105,590 322 ns 179 ~3.4 G
TZF.Finder 1,000,000 665 100% ~1,503,759 665 ns 268 ~5.7 G
LatLongToTimezone 100,000 17 100% ~5,882,352 170 ns 167 ~0.2 G
SwiftTimeZoneLookup.simple 10,000 2,932 100% ~3,410 293.2 μs 171 37 G
SwiftTimeZoneLookup.lookup 10,000 2,975 100% ~3,361 297.5 μs 170 37 G

Full benchmark results can be viewed in benchmark_baseline.txt.

Run Benchmarks

Benchmarks are isolated in the Benchmarks subpackage so the main package stays compatible with Swift 6.0 while benchmark tooling can use newer SwiftPM features.

make bench
# or:
cd Benchmarks && swift package benchmark --target TimezoneFinderBenchmarks

Related Projects

See Project tzf for more information.

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

FOSSA Status