diff --git a/Package.swift b/Package.swift index ad0e9bf..9e870b9 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,8 @@ let package = Package( .target(name: "CZoneDetect"), .target( name: "SwiftTimeZoneLookup", - dependencies: ["CZoneDetect"]), + dependencies: ["CZoneDetect"], + resources: [.process("Resources")]), .testTarget( name: "SwiftTimeZoneLookupTests", dependencies: ["SwiftTimeZoneLookup"]), diff --git a/README.md b/README.md index d80ae25..0b61219 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # SwiftTimeZoneLookup A description of this package. + + +## Build database +```bash +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 + +``` diff --git a/Sources/CZoneDetect/include/include.h b/Sources/CZoneDetect/include/include.h index 83dc748..9d16941 100644 --- a/Sources/CZoneDetect/include/include.h +++ b/Sources/CZoneDetect/include/include.h @@ -1 +1 @@ -#include "../src/timezonedetect.h" \ No newline at end of file +#include "../src/zonedetect.h" diff --git a/Sources/SwiftTimeZoneLookup/Resources/README.md b/Sources/SwiftTimeZoneLookup/Resources/README.md new file mode 100644 index 0000000..f1c1d2f --- /dev/null +++ b/Sources/SwiftTimeZoneLookup/Resources/README.md @@ -0,0 +1,5 @@ +Database version 2021c + +Generated on 2022-08-07 + +https://github.com/evansiroky/timezone-boundary-builder/releases/tag/2021c diff --git a/Sources/SwiftTimeZoneLookup/Resources/timezone16.bin b/Sources/SwiftTimeZoneLookup/Resources/timezone16.bin new file mode 100644 index 0000000..7fca9b9 Binary files /dev/null and b/Sources/SwiftTimeZoneLookup/Resources/timezone16.bin differ diff --git a/Sources/SwiftTimeZoneLookup/Resources/timezone21.bin b/Sources/SwiftTimeZoneLookup/Resources/timezone21.bin new file mode 100644 index 0000000..ea31a61 Binary files /dev/null and b/Sources/SwiftTimeZoneLookup/Resources/timezone21.bin differ diff --git a/Sources/SwiftTimeZoneLookup/SwiftTimeZoneLookup.swift b/Sources/SwiftTimeZoneLookup/SwiftTimeZoneLookup.swift index e6433a0..c23669f 100644 --- a/Sources/SwiftTimeZoneLookup/SwiftTimeZoneLookup.swift +++ b/Sources/SwiftTimeZoneLookup/SwiftTimeZoneLookup.swift @@ -1,6 +1,40 @@ -public struct SwiftTimeZoneLookup { - public private(set) var text = "Hello, World!" +@_implementationOnly import CZoneDetect +import Foundation - public init() { +public enum SwiftTimeZoneLookupError: Error { + case couldNotFindTimezone21bin + case couldNotOpenDatabase +} + +public final class SwiftTimeZoneLookup { + private let database: OpaquePointer + + public init() throws { + guard let timezone21 = Bundle.module.url(forResource: "timezone21", withExtension: "bin") else { + throw SwiftTimeZoneLookupError.couldNotFindTimezone21bin + } + + guard let database = timezone21.withUnsafeFileSystemRepresentation({ timezone21 in + ZDOpenDatabase(timezone21) + }) else { + throw SwiftTimeZoneLookupError.couldNotOpenDatabase + } + self.database = database + + + print(timezone21) + } + + public func lookup(latitude: Float, longitude: Float) -> String { + guard let cTimezone = ZDHelperSimpleLookupString(database, latitude, longitude) else { + fatalError() + } + let timezone = String(cString: cTimezone) + ZDHelperSimpleLookupStringFree(cTimezone) + return timezone + } + + deinit { + ZDCloseDatabase(database) } } diff --git a/Tests/SwiftTimeZoneLookupTests/SwiftTimeZoneLookupTests.swift b/Tests/SwiftTimeZoneLookupTests/SwiftTimeZoneLookupTests.swift index a74b47d..3c4394a 100644 --- a/Tests/SwiftTimeZoneLookupTests/SwiftTimeZoneLookupTests.swift +++ b/Tests/SwiftTimeZoneLookupTests/SwiftTimeZoneLookupTests.swift @@ -2,10 +2,11 @@ import XCTest @testable import SwiftTimeZoneLookup final class SwiftTimeZoneLookupTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(SwiftTimeZoneLookup().text, "Hello, World!") + func testLookup() throws { + let database = try SwiftTimeZoneLookup() + XCTAssertEqual(database.lookup(latitude: 47.5, longitude: 8.6), "Europe/Zurich") + XCTAssertEqual(database.lookup(latitude: 47.5, longitude: -2.6), "Europe/Paris") + XCTAssertEqual(database.lookup(latitude: 47.5, longitude: -8.6), "Etc/GMT+1") + XCTAssertEqual(database.lookup(latitude: 42.5, longitude: -8.6), "Europe/Madrid") } }