mirror of
https://github.com/samsonjs/SwiftTimeZoneLookup.git
synced 2026-04-27 14:57:39 +00:00
refactor
This commit is contained in:
parent
f235ad0801
commit
f31401ae5f
2 changed files with 41 additions and 5 deletions
38
README.md
38
README.md
|
|
@ -2,11 +2,47 @@
|
||||||
|
|
||||||
[](https://github.com/patrick-zippenfenig/SwiftTimeZoneLookup/actions/workflows/test.yml)
|
[](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
|
## Build database
|
||||||
|
SwiftTimeZoneLookup comes with an integrated database. The database can be updated with the following commands:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
git clone --recurse-submodules git@github.com:patrick-zippenfenig/SwiftTimeZoneLookup.git
|
||||||
|
|
||||||
brew install shapelib wget
|
brew install shapelib wget
|
||||||
cd Submodules/ZoneDetect/database/builder
|
cd Submodules/ZoneDetect/database/builder
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,17 +26,17 @@ public final class SwiftTimeZoneLookup {
|
||||||
|
|
||||||
/// Throws if the timezone database could not be opened
|
/// Throws if the timezone database could not be opened
|
||||||
public init() throws {
|
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
|
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
|
throw SwiftTimeZoneLookupError.couldNotFindTimezone21bin
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let database21 = timezone21.withUnsafeFileSystemRepresentation(ZDOpenDatabase) else {
|
guard let database21 = ZDOpenDatabase(timezone21) else {
|
||||||
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
|
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
|
||||||
}
|
}
|
||||||
guard let database16 = timezone16.withUnsafeFileSystemRepresentation(ZDOpenDatabase) else {
|
guard let database16 = ZDOpenDatabase(timezone16) else {
|
||||||
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
|
throw SwiftTimeZoneLookupError.couldNotOpenDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue