Skip to content

Commit f73a1b4

Browse files
authored
Merge pull request #31 from AstroCB/master
Use inferred timezone for time field in output
2 parents 6d3a0c9 + 0097228 commit f73a1b4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

CoreLocationCLI/main.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ class Delegate: NSObject, CLLocationManagerDelegate {
5050
? ""
5151
: CNPostalAddressFormatter.string(from: placemark!.postalAddress!, style: CNPostalAddressFormatterStyle.mailingAddress)
5252

53+
// Attempt to infer timezone for timestamp string
54+
var locatedTime: String?
55+
if let locatedTimeZone = placemark?.timeZone {
56+
let time = location.timestamp
57+
58+
let formatter = DateFormatter()
59+
formatter.timeZone = locatedTimeZone
60+
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
61+
62+
locatedTime = formatter.string(from: time)
63+
}
64+
5365
switch format {
5466
case .json:
5567
let outputObject: [String: String?] = [
@@ -75,6 +87,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
7587
"subThoroughfare": placemark?.subThoroughfare,
7688
"region": placemark?.region?.identifier,
7789
"timeZone": placemark?.timeZone?.identifier,
90+
"time_local": locatedTime,
7891

7992
// Address
8093
"address": formattedPostalAddress
@@ -104,6 +117,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
104117
output = output.replacingOccurrences(of: "%subThoroughfare", with: String(placemark?.subThoroughfare ?? ""))
105118
output = output.replacingOccurrences(of: "%region", with: String(placemark?.region?.identifier ?? ""))
106119
output = output.replacingOccurrences(of: "%timeZone", with: String(placemark?.timeZone?.identifier ?? ""))
120+
output = output.replacingOccurrences(of: "%time_local", with: String(locatedTime ?? ""))
107121

108122
// Address
109123
output = output.replacingOccurrences(of: "%address", with: formattedPostalAddress)
@@ -198,6 +212,7 @@ class Delegate: NSObject, CLLocationManagerDelegate {
198212
%subThoroughfare Additional street-level information
199213
%region Reverse geocoded geographic region
200214
%timeZone Reverse geocoded time zone
215+
%time_local Localized time using reverse geocoded time zone
201216
-json Prints a JSON object with all information available
202217
203218
Default format if not specified is: %latitude %longitude.
@@ -220,7 +235,7 @@ for (i, argument) in ProcessInfo().arguments.enumerated() {
220235
case "-format":
221236
if ProcessInfo().arguments.count > i+1 {
222237
delegate.format = .string(ProcessInfo().arguments[i+1])
223-
let placemarkStrings = ["%address", "%name", "%isoCountryCode", "%country", "%postalCode", "%administrativeArea", "%subAdministrativeArea", "%locality", "%subLocality", "%thoroughfare", "%subThoroughfare", "%region", "%timeZone"]
238+
let placemarkStrings = ["%address", "%name", "%isoCountryCode", "%country", "%postalCode", "%administrativeArea", "%subAdministrativeArea", "%locality", "%subLocality", "%thoroughfare", "%subThoroughfare", "%region", "%timeZone", "%time_local"]
224239
if placemarkStrings.contains(where:ProcessInfo().arguments[i+1].contains) {
225240
delegate.requiresPlacemarkLookup = true
226241
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ CoreLocationCLI [-follow] [-verbose] -json
4545
| `%subThoroughfare` | additional street-level information |
4646
| `%region` | Reverse geocoded geographic region |
4747
| `%timeZone` | Reverse geocoded time zone |
48+
| `%time_local` | Localized time using reverse geocoded time zone |
4849

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

@@ -74,7 +75,7 @@ The default format is: `%latitude %longitude`.
7475
```
7576
7677
>```json
77-
>{"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"}
78+
>{"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"}
7879
> ```
7980
8081
## Installation

0 commit comments

Comments
 (0)