Time Zones
timezones
¶
Assembly: Mappy
Filename: timezones.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
Mappy is a python framework encapsulating the Google Maps functionality.
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Classes¶
Timezone
¶
Timezone(maps: Maps)
Resolve time-zone metadata from geographic coordinates.
Purpose
Wraps the Google Time Zone API through the shared Maps gateway and
normalizes API results for Streamlit display, spreadsheet enrichment, and
row-level data workflows. The class validates coordinates, retrieves full
time-zone payloads, extracts IANA time-zone identifiers, computes total UTC
offsets, converts UTC datetimes into local time, and enriches batches of row
dictionaries with time-zone fields.
Attributes:
| Name | Type | Description |
|---|---|---|
timestamp |
Optional[int]
|
Unix timestamp used for the latest Google Time Zone API request. |
maps |
Optional[Maps]
|
Google Maps gateway used for Time Zone API requests. |
latitude |
Optional[float]
|
Latitude from the latest validated coordinate. |
longitude |
Optional[float]
|
Longitude from the latest validated coordinate. |
data |
Optional[Dict]
|
Raw Google Time Zone API response payload from the latest lookup. |
offset |
Optional[float]
|
Total UTC offset in hours from the latest lookup. |
local |
Optional[str]
|
ISO-formatted local time from the latest local-time conversion. |
rows |
Optional[list]
|
Batch rows from the latest enrichment request. |
lat_field |
Optional[str]
|
Latitude field name used by the latest batch enrichment. |
lng_field |
Optional[str]
|
Longitude field name used by the latest batch enrichment. |
utc_datetime |
Optional[datetime]
|
UTC datetime used by the latest local-time conversion. |
Initialize the Time Zone service wrapper.
Purpose
Stores the shared Google Maps gateway and initializes runtime state used by coordinate validation, lookup execution, offset extraction, local-time conversion, and batch row enrichment. The constructor performs local assignment only and prepares the instance for later API-backed operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maps
|
Maps
|
Google Maps gateway used for Time Zone API requests. |
required |
Source code in timezones.py
Methods:¶
validate_coordinates
¶
Validate and normalize a latitude/longitude pair.
Purpose
Converts coordinate inputs to floats and enforces valid geographic ranges before Google Time Zone API requests are built. The validated coordinate is stored on the instance for later lookup, local-time conversion, and enrichment output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
Validated latitude and longitude. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or conversion fails. |
Source code in timezones.py
lookup
¶
Fetch the full Google Time Zone API payload for a coordinate.
Purpose
Validates latitude and longitude, captures the current Unix timestamp,
stores coordinate and timestamp state on the instance, and calls the shared
Google Maps gateway using the timezone/json endpoint. The raw response
payload is retained for ID extraction, offset calculation, local-time
conversion, and batch enrichment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
Returns:
| Type | Description |
|---|---|
Dict | None
|
Dict | None: Full Google Time Zone API response payload. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation, request construction, gateway execution, or response handling fails. |
Source code in timezones.py
get_id
¶
Fetch the IANA time-zone identifier for a coordinate.
Purpose
Delegates to lookup and extracts the timeZoneId field from the
Google Time Zone API payload. The method preserves the latest raw payload
on the instance for diagnostics and downstream workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: IANA time-zone identifier when available; otherwise |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when lookup or extraction fails. |
Source code in timezones.py
offset_hours
¶
Fetch the total UTC offset in hours for a coordinate.
Purpose
Delegates to lookup and combines the Google Time Zone API
rawOffset and dstOffset values into a decimal-hour offset. The
computed value is stored on the instance for later display and enrichment
workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Optional[float]: Total UTC offset in hours when available; otherwise
|
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when lookup or offset conversion fails. |
Source code in timezones.py
local_time
¶
Convert UTC time into local time for a coordinate.
Purpose
Uses the Google Time Zone API rawOffset and dstOffset fields to
translate a UTC datetime into local time at the supplied coordinate. Naive
datetimes are treated as UTC, timezone-aware datetimes are normalized to
UTC, and the resulting time-zone metadata, UTC time, local time, offset,
latitude, and longitude are returned as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
utc_datetime
|
Optional[datetime]
|
Optional UTC datetime. When omitted, the current UTC time is used. |
None
|
Returns:
| Type | Description |
|---|---|
Dict | None
|
Dict | None: Time-zone metadata and converted local-time fields when a
payload is available; otherwise |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when lookup, datetime normalization, or conversion fails. |
Source code in timezones.py
batch_lookup
¶
Enrich row dictionaries with Google Time Zone API fields.
Purpose
Iterates through copied row dictionaries, reads latitude and longitude values from configurable field names, performs a time-zone lookup for each row, and appends time-zone metadata, UTC offset, row-level status, and row-level error fields. Per-row failures are captured on the row so a batch can continue processing even when individual records are invalid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list
|
List of row dictionaries containing latitude and longitude values. |
required |
lat_field
|
str
|
Field name containing latitude values. |
'lat'
|
lng_field
|
str
|
Field name containing longitude values. |
'lng'
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Copied row dictionaries with appended time-zone fields. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when batch-level validation or setup fails. |
Source code in timezones.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
Functions:¶
throw_if
¶
Validate that a required time-zone value is present.
Purpose
Provides a lightweight guard for required Time Zone API inputs before coordinate validation, gateway request construction, UTC offset conversion, local-time conversion, and batch enrichment. The function rejects missing objects and blank strings so time-zone workflows fail before issuing ambiguous external API calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the argument being validated. |
required |
value
|
object
|
Runtime value to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |