Skip to content

Commit

Permalink
Merge pull request #31 from AstroCB/master
Browse files Browse the repository at this point in the history
Use inferred timezone for time field in output
  • Loading branch information
fulldecent committed Aug 18, 2020
2 parents 6d3a0c9 + 0097228 commit f73a1b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 16 additions & 1 deletion CoreLocationCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ class Delegate: NSObject, CLLocationManagerDelegate {
? ""
: CNPostalAddressFormatter.string(from: placemark!.postalAddress!, style: CNPostalAddressFormatterStyle.mailingAddress)

// Attempt to infer timezone for timestamp string
var locatedTime: String?
if let locatedTimeZone = placemark?.timeZone {
let time = location.timestamp

let formatter = DateFormatter()
formatter.timeZone = locatedTimeZone
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

locatedTime = formatter.string(from: time)
}

switch format {
case .json:
let outputObject: [String: String?] = [
Expand All @@ -75,6 +87,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
"subThoroughfare": placemark?.subThoroughfare,
"region": placemark?.region?.identifier,
"timeZone": placemark?.timeZone?.identifier,
"time_local": locatedTime,

// Address
"address": formattedPostalAddress
Expand Down Expand Up @@ -104,6 +117,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
output = output.replacingOccurrences(of: "%subThoroughfare", with: String(placemark?.subThoroughfare ?? ""))
output = output.replacingOccurrences(of: "%region", with: String(placemark?.region?.identifier ?? ""))
output = output.replacingOccurrences(of: "%timeZone", with: String(placemark?.timeZone?.identifier ?? ""))
output = output.replacingOccurrences(of: "%time_local", with: String(locatedTime ?? ""))

// Address
output = output.replacingOccurrences(of: "%address", with: formattedPostalAddress)
Expand Down Expand Up @@ -198,6 +212,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
%subThoroughfare Additional street-level information
%region Reverse geocoded geographic region
%timeZone Reverse geocoded time zone
%time_local Localized time using reverse geocoded time zone
-json Prints a JSON object with all information available
Default format if not specified is: %latitude %longitude.
Expand All @@ -220,7 +235,7 @@ for (i, argument) in ProcessInfo().arguments.enumerated() {
case "-format":
if ProcessInfo().arguments.count > i+1 {
delegate.format = .string(ProcessInfo().arguments[i+1])
let placemarkStrings = ["%address", "%name", "%isoCountryCode", "%country", "%postalCode", "%administrativeArea", "%subAdministrativeArea", "%locality", "%subLocality", "%thoroughfare", "%subThoroughfare", "%region", "%timeZone"]
let placemarkStrings = ["%address", "%name", "%isoCountryCode", "%country", "%postalCode", "%administrativeArea", "%subAdministrativeArea", "%locality", "%subLocality", "%thoroughfare", "%subThoroughfare", "%region", "%timeZone", "%time_local"]
if placemarkStrings.contains(where:ProcessInfo().arguments[i+1].contains) {
delegate.requiresPlacemarkLookup = true
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CoreLocationCLI [-follow] [-verbose] -json
| `%subThoroughfare` | additional street-level information |
| `%region` | Reverse geocoded geographic region |
| `%timeZone` | Reverse geocoded time zone |
| `%time_local` | Localized time using reverse geocoded time zone |

The default format is: `%latitude %longitude`.

Expand Down Expand Up @@ -74,7 +75,7 @@ The default format is: `%latitude %longitude`.
```

>```json
>{"address":"407 Keats Rd\nLower Moreland PA 19006\nUnited States","locality":"nLower Moreland","subThoroughfare":"407","time":"2019-10-03 04:10:05 +0000","subLocality":null,"administrativeArea":"PA","country":"United States","thoroughfare":"Keats Rd","region":"<+40.141196,-75.034815> radius 35.91","speed":"-1","latitude":"40.141196","name":"1354 Panther Rd","altitude":"92.00","timeZone":"America\/New_York","isoCountryCode":"US","longitude":"-75.034815","v_accuracy":"65","postalCode":"19006","direction":"-1.0","h_accuracy":"65","subAdministrativeArea":"Montgomery"}
>{"address":"407 Keats Rd\nLower Moreland PA 19006\nUnited States","locality":"nLower Moreland","subThoroughfare":"407","time":"2019-10-03 04:10:05 +0000","subLocality":null,"administrativeArea":"PA","country":"United States","thoroughfare":"Keats Rd","region":"<+40.141196,-75.034815> radius 35.91","speed":"-1","latitude":"40.141196","name":"1354 Panther Rd","altitude":"92.00","timeZone":"America\/New_York","time_local": "2019-10-02 23:10:05 -0400","isoCountryCode":"US","longitude":"-75.034815","v_accuracy":"65","postalCode":"19006","direction":"-1.0","h_accuracy":"65","subAdministrativeArea":"Montgomery"}
> ```
## Installation
Expand Down

0 comments on commit f73a1b4

Please sign in to comment.