This commit is contained in:
patrick-zippenfenig 2022-08-07 13:45:47 +02:00
parent f235ad0801
commit f31401ae5f
2 changed files with 41 additions and 5 deletions

View file

@ -2,11 +2,47 @@
[![Test](https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup/actions/workflows/test.yml/badge.svg)](https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup/actions/workflows/test.yml)
A description of this package.
Resolve geographical coordinates to timezones and countries. This is a Swift wrapper for [ZoneDetect](https://github.com/BertoldVdb/ZoneDetect)
## Usage
Add `SwiftTimeZoneLookup` as a dependency to your `Package.swift`
```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"),
])
]
```
In your code
```swift
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"))
```
## Build database
SwiftTimeZoneLookup comes with an integrated database. The database can be updated with the following commands:
```bash
git clone --recurse-submodules git@github.com:patrick-zippenfenig/SwiftTimeZoneLookup.git
brew install shapelib wget
cd Submodules/ZoneDetect/database/builder

View file

@ -26,17 +26,17 @@ public final class SwiftTimeZoneLookup {
/// Throws if the timezone database could not be opened
public init() throws {
guard let timezone21 = Bundle.module.url(forResource: "timezone21", withExtension: "bin") else {
guard let timezone21 = Bundle.module.path(forResource: "timezone21", ofType: "bin") else {
throw SwiftTimeZoneLookupError.couldNotFindTimezone21bin
}
guard let timezone16 = Bundle.module.url(forResource: "timezone16", withExtension: "bin") else {
guard let timezone16 = Bundle.module.path(forResource: "timezone16", ofType: "bin") else {
throw SwiftTimeZoneLookupError.couldNotFindTimezone21bin
}
guard let database21 = timezone21.withUnsafeFileSystemRepresentation(ZDOpenDatabase) else {
guard let database21 = ZDOpenDatabase(timezone21) else {
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
}
guard let database16 = timezone16.withUnsafeFileSystemRepresentation(ZDOpenDatabase) else {
guard let database16 = ZDOpenDatabase(timezone16) else {
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
}