Created
January 17, 2025 15:49
-
-
Save stand-sure/0240e3213e1f9b4175902b442ea855f5 to your computer and use it in GitHub Desktop.
Deno script for getting timezone from address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"tasks": { | |
"dev": "deno run --watch main.ts" | |
}, | |
"imports": { | |
"@googlemaps/google-maps-services-js": "npm:@googlemaps/google-maps-services-js@^3.4.0", | |
"@std/assert": "jsr:@std/assert@1", | |
"@types/node": "npm:@types/node@^22.10.7" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.ts | |
// deno --allow-env --allow-read --allow-net main.ts | |
import * as process from "node:process"; | |
import { | |
Client, | |
GeocodeRequest, | |
GeocodeResponse, | |
LatLngLiteral, | |
TimeZoneRequest, | |
TimeZoneResponse, | |
} from "@googlemaps/google-maps-services-js"; | |
const client: Client = new Client({}); | |
const key: string = process.env.GOOGLE_MAPS_API_KEY as string; | |
let latLng: LatLngLiteral; | |
try { | |
const geocodeRequest: GeocodeRequest = { | |
params: { | |
address: "num abc street, anytown, st zip", | |
key: key, | |
}, | |
timeout: 1000, | |
}; | |
const geocodeResponse: GeocodeResponse = await client.geocode(geocodeRequest); | |
latLng = geocodeResponse.data?.results[0]?.geometry.location; | |
} catch (e) { | |
console.log(e.response.data.error_message); | |
} | |
try { | |
const timeZoneRequest : TimeZoneRequest = { | |
params: { | |
location: latLng, | |
key: key, | |
timestamp: new Date(), | |
}, | |
timeout: 1000, | |
} | |
const timeZoneResponse : TimeZoneResponse = await client.timezone(timeZoneRequest); | |
const {dstOffset, rawOffset, status, timeZoneId, timeZoneName} = timeZoneResponse.data; | |
console.log(JSON.stringify({dstOffset, rawOffset, status, timeZoneId, timeZoneName}, null, 2)); | |
} | |
catch (e) { | |
console.log(e.response.data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment