SwiftTimeZoneLookup

Test

将地理坐标解析为时区和国家。这是一个 ZoneDetect 的 Swift 封装。

一个精度高达 20 米的时区数据库被嵌入为捆绑资源(约 5 MB)。如果您想手动指定数据库位置,请使用 try SwiftTimeZoneLookup(databasePath: "/path_to_timezone16_21_files/..")

嵌入的时区数据库版本 2024a (2024-03-14)

此库是线程安全的。

用法

SwiftTimeZoneLookup 作为依赖项添加到您的 Package.swift

  dependencies: [
    .package(url: "https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup.git", from: "1.0.0")
  ],
  targets: [
    .target(name: "MyApp", dependencies: [
      .product(name: "SwiftTimeZoneLookup", package: "SwiftTimeZoneLookup"),
    ])
  ]

在您的代码中

import SwiftTimeZoneLookup


let database = try SwiftTimeZoneLookup()
guard let timezone = database.simple(latitude: 47.5, longitude: 8.6) else {
  fatalError("Timezone not found, coordinates invalid?")
}
print(timezone) // "Europe/Zurich"


guard let lookup = database.lookup(latitude: 47.5, longitude: 8.6) else {
  fatalError("Timezone not found, coordinates invalid?")
}
print(lookup) // SwiftTimeZoneLookupResult(timezone: "Europe/Zurich", countryName: Optional("Switzerland"), countryAlpha2: Optional("CH"))

构建数据库

SwiftTimeZoneLookup 自带集成的数据库。数据库可以使用以下命令更新

git clone --recurse-submodules git@github.com:patrick-zippenfenig/SwiftTimeZoneLookup.git

brew install shapelib wget
cd Submodules/ZoneDetect/database/builder

# make sure to select to newest version in make.db script

LIBRARY_PATH=/opt/homebrew/Cellar/shapelib/1.5.0/lib CPATH=/opt/homebrew/Cellar/shapelib/1.5.0/include ./makedb.sh

cp out_v1/timezone* ../../../../Sources/SwiftTimeZoneLookup/Resources

鸣谢

ZoneDetect 作为上游库。

包含来自 Natural Earth 的数据,置于公共领域。包含来自 https://github.com/evansiroky/timezone-boundary-builder 的信息,这些信息在此处根据开放数据库许可 (ODbL) 提供。