Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created September 17, 2021 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rawiriblundell/10ef4183cf3590f2ff549f1fe3ffaaa6 to your computer and use it in GitHub Desktop.
Save rawiriblundell/10ef4183cf3590f2ff549f1fe3ffaaa6 to your computer and use it in GitHub Desktop.
Lookup a timezone based on lat/long coords
tzlookup() {
local err cmd lat long
err=0
for cmd in curl jq; do
command -v "${cmd}" >/dev/null 2>&1 || (( ++err ))
done
(( err > 0 )) && { printf -- '%s\n' "Requires 'curl' and 'jq'" >&2; return 1; }
lat="${1:?No latitude value supplied}"
long="${2:?No longitude value supplied}"
case "${lat}" in
(*[sS]) lat="-${lat/[sS]/}" ;;
(*[nN]) lat="${lat/[nN]/}" ;;
esac
case "${long}" in
(*[wW]) long="-${long/[wW]/}" ;;
(*[eE]) long="${long/[eE]/}" ;;
esac
curl -s "https://timezone.bertold.org/timezone?lat=${lat}&lon=${long}&s=1" |
jq -r '.Result'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment