This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add timezone subcommand * Fix timezone info so it support ISO timezones correctly * Update timestamp.cpp * Update gridlabd-timezone * Update gridlabd-timezone * Create test_timezone.glm * Update test_timezone.glm * Added support for timezome format options Co-authored-by: Alyona Teyber <[email protected]>
- Loading branch information
Showing
8 changed files
with
234 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[[/Subommand/Timezone]] -- Subcommand to obtain the timezone at a location | ||
|
||
# Synopsis | ||
|
||
~~~ | ||
bash$ gridlabd timezone [LATITUDE,LONGITUDE] | ||
~~~ | ||
|
||
# Description | ||
|
||
The gridlabd timezone subcommand obtains the timezone for the location given by | ||
LATITUDE,LONGITUDE or the local timezone based on the hostname's IP address. | ||
|
||
## Options | ||
|
||
* `-d|--debug`: output debugging output on error | ||
* `-h|--help|help`: print full documentation | ||
* `-v|--verbose`: output intermediate results | ||
|
||
# Examples | ||
|
||
The following command obtains the timezone for the current location as determine by the IP address of the host machine: | ||
|
||
~~~ | ||
shell$ gridlabd timezone | ||
~~~ | ||
|
||
The following command obtains the timezone for the location specified: | ||
|
||
~~~ | ||
shell$ gridlabd timezone 37.5,-122.2 | ||
~~~ | ||
|
||
# See also | ||
|
||
* [[/GLM/Directive/Clock]] |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
clock | ||
{ | ||
timezone PST+8PDT; | ||
timezone "US/CA/Los Angeles"; | ||
timezone ${SHELL gridlabd timezone local}; | ||
} | ||
|
||
#assert $(gridlabd timezone 37.5,-122.2) == "America/Los_Angeles" | ||
#assert $(gridlabd timezone 33,-110) == "America/Phoenix" | ||
#assert $(gridlabd timezone 36,-110) == "America/Denver" | ||
#assert $(gridlabd timezone 40,-100) == "America/Chicago" | ||
#assert $(gridlabd timezone 40,-80) == "America/New_York" | ||
#assert $(gridlabd timezone 50,-120) == "America/Vancouver" | ||
#assert $(gridlabd timezone 37.5,-122.2 -f=TZSPEC) == "PST+8PDT" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#!/usr/local/bin/python3 | ||
"""Syntax: gridlabd timezone [OPTIONS...] LOCATION | ||
The gridlabd timezone subcommand obtains the timezone for the location given | ||
by LOCATION, which may be in the form `LATITUDE,LONGITUDE`, `local`, or an IP | ||
address of the form `A.B.C.D`. | ||
OPTIONS: | ||
-d|--debug output debugging output on error | ||
-h|--help|help print full documentation | ||
-v|--verbose output intermediate results | ||
-f|--format[=FORMAT] change timezone format | ||
FORMATS | ||
TZINFO Timezone database standard (default, e.g., "America/Los_Angeles") | ||
TZSPEC Timezone abbreviation standard (e.g., "PST+8PDT") | ||
EXAMPLES: | ||
The following command obtains the timezone for the current location as | ||
determine by the IP address of the host machine: | ||
shell$ gridlabd timezone local | ||
America/Los_Angeles | ||
The following command obtains the timezone for the location specified: | ||
shell$ gridlabd timezone 37.5,-122.2 | ||
America/Los_Angeles | ||
The following GLM directive sets the model timezone to the local timezone: | ||
clock | ||
{ | ||
timezone ${SHELL gridlabd timezone local}; | ||
} | ||
""" | ||
|
||
import sys, os, subprocess | ||
|
||
verbose = False | ||
debug = False | ||
location = None | ||
tz_format = 'TZINFO' | ||
|
||
E_OK = 0 | ||
E_SYNTAX = 1 | ||
E_INVALID = 2 | ||
E_FAILED = 3 | ||
|
||
shared = os.getenv("GLD_ETC") | ||
if not shared: | ||
shared = "/usr/local/share/gridlabd/" | ||
elif shared[-1] != '/': | ||
shared += '/' | ||
|
||
def output_verbose(msg): | ||
if verbose: | ||
print(f"VERBOSE [{os.path.basename(sys.argv[0])}]: {msg}",file=sys.stderr) | ||
|
||
def output_error(msg,code=None): | ||
print(f"ERROR [{os.path.basename(sys.argv[0])}]: {msg}",file=sys.stderr) | ||
if type(code) is int: | ||
exit(code) | ||
|
||
def output_help(): | ||
print(__doc__) | ||
exit(E_OK) | ||
|
||
def output_syntax(): | ||
print(__doc__.split("\n")[0]) | ||
exit(E_SYNTAX) | ||
|
||
def timezone_format(tz_info,tz_format=tz_format): | ||
|
||
if tz_format == 'TZINFO': | ||
return tz_info | ||
elif tz_format == 'TZSPEC': | ||
tzinfo_file = shared+"tzinfo.txt" | ||
with open(tzinfo_file,"r") as f: | ||
rule = None | ||
found = False | ||
for line in f.readlines(): | ||
if not line or line[0] == ';' or line[0] == '\n': | ||
continue | ||
elif line[0] != ' ': | ||
rule = line | ||
elif line.strip() == tz_info: | ||
found = True | ||
break | ||
if found: | ||
return rule.split(";")[0].split(",")[0] | ||
else: | ||
output_error(f"{tz_info} not found in {tzinfo_file}",E_FAILED) | ||
else: | ||
output_error(f"timezone format '{tz_format}' is not valid",E_INVALID) | ||
|
||
try: | ||
from timezonefinder import TimezoneFinder | ||
import geocoder as gc | ||
|
||
if len(sys.argv) == 1: | ||
output_syntax() | ||
|
||
while len(sys.argv) > 1: | ||
args = sys.argv[1].split("=") | ||
if len(args) == 1: | ||
arg = args[0] | ||
value = None | ||
else: | ||
arg = args[0] | ||
value = '='.join(args[1:]) | ||
if arg in ["-d","--debug"]: | ||
debug = True | ||
elif arg in ["-v","--verbose"]: | ||
verbose = True | ||
elif arg in ["-h","--help","help"]: | ||
output_help() | ||
elif arg in ["-f","--format"]: | ||
if value: | ||
tz_format = value | ||
elif arg[0] == '-': | ||
output_error(f"option '{sys.argv[1]}' is not valid",E_SYNTAX) | ||
else: | ||
location = sys.argv[1] | ||
del sys.argv[1] | ||
|
||
if not location: | ||
output_error("no location specified",E_SYNTAX) | ||
elif location == 'local': | ||
latlon = gc.ip('me').latlng | ||
elif location.find(",") > 0: | ||
latlon = list(map(lambda x:float(x),location.split(","))) | ||
else: | ||
latlon = gc.ip(location).latlng | ||
if not type(latlon) is list or len(latlon) != 2 \ | ||
or type(latlon[0]) != float or type(latlon[1]) != float: | ||
output_error("latlon format is not valid",E_INVALID) | ||
output_verbose(f"latlon = {latlon}") | ||
|
||
tz_info = TimezoneFinder().timezone_at(lat=latlon[0],lng=latlon[1]) | ||
print(timezone_format(tz_info,tz_format),file=sys.stdout) | ||
|
||
exit(E_OK) | ||
|
||
except Exception as err: | ||
|
||
if debug: | ||
raise | ||
else: | ||
output_error(str(err),E_FAILED) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ geopandas==0.9.0 | |
PyGithub==1.54.1 | ||
matplotlib==3.3.3 | ||
geopy==2.1.0 | ||
timezonefinder==5.2.0 | ||
geocoder==1.38.1 |
This file contains 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
This file contains 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