Skip to content

Commit 5eca6d3

Browse files
committed
Merge branch 'carlosvargas-utc-to-local'
2 parents 58b6067 + 752fb61 commit 5eca6d3

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

soccer/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ def _get(url):
5050
raise APIErrorException('You have exceeded your allowed requests per minute/day')
5151

5252

53-
def get_live_scores(writer):
53+
def get_live_scores(writer, use_12_hour_format):
5454
"""Gets the live scores"""
5555
req = requests.get(LIVE_URL)
5656
if req.status_code == requests.codes.ok:
5757
scores = req.json()
5858
if len(scores["games"]) == 0:
5959
click.secho("No live action currently", fg="red", bold=True)
6060
return
61-
writer.live_scores(scores)
61+
writer.live_scores(scores, use_12_hour_format)
6262
else:
6363
click.secho("There was problem getting live scores", fg="red", bold=True)
6464

@@ -149,6 +149,7 @@ def get_team_players(team, writer):
149149

150150
@click.command()
151151
@click.option('--live', is_flag=True, help="Shows live scores from various leagues")
152+
@click.option('--use12hour', is_flag=True, default=False, help="Displays the time using 12 hour format instead of 24 (default).")
152153
@click.option('--standings', is_flag=True, help="Standings for a particular league")
153154
@click.option('--league', '-league', type=click.Choice(LEAGUE_IDS.keys()),
154155
help=("Choose the league whose fixtures you want to see. "
@@ -167,7 +168,7 @@ def get_team_players(team, writer):
167168
help='Output in JSON format')
168169
@click.option('-o', '--output-file', default=None,
169170
help="Save output to a file (only if csv or json option is provided)")
170-
def main(league, time, standings, team, live, players, output_format, output_file):
171+
def main(league, time, standings, team, live, use12hour, players, output_format, output_file):
171172
"""A CLI for live and past football scores from various football leagues"""
172173
try:
173174
if output_format == 'stdout' and output_file:
@@ -176,7 +177,7 @@ def main(league, time, standings, team, live, players, output_format, output_fil
176177
writer = get_writer(output_format, output_file)
177178

178179
if live:
179-
get_live_scores(writer)
180+
get_live_scores(writer, use12hour)
180181
return
181182

182183
if standings:

soccer/writers.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ def __init__(self, output_file):
7878
)
7979
self.colors = type('Enum', (), enums)
8080

81-
def live_scores(self, live_scores):
81+
def live_scores(self, live_scores, use_12_hour_format):
8282
"""Prints the live scores in a pretty format"""
8383
scores = sorted(live_scores["games"], key=lambda x: x["league"])
8484
for league, games in groupby(scores, key=lambda x: x["league"]):
8585
self.league_header(league)
8686
for game in games:
8787
self.scores(self.parse_result(game), add_new_line=False)
88-
click.secho(' %s' % game["time"], fg=self.colors.TIME)
88+
click.secho(' %s' % Stdout.convert_utc_to_local_time(game["time"], use_12_hour_format),
89+
fg=self.colors.TIME)
8990
click.echo()
9091

9192
def team_scores(self, team_scores, time):
@@ -223,6 +224,26 @@ def valid_score(score):
223224

224225
return result
225226

227+
@staticmethod
228+
def convert_utc_to_local_time(time_str, use_12_hour_format):
229+
"""Converts the API UTC time string to the local user time."""
230+
if not time_str.endswith(" UTC"):
231+
return time_str
232+
233+
today_utc = datetime.datetime.utcnow()
234+
utc_local_diff = today_utc - datetime.datetime.now()
235+
236+
time_str, _ = time_str.split(" UTC")
237+
utc_time = datetime.datetime.strptime(time_str,'%I:%M %p')
238+
utc_datetime = datetime.datetime(today_utc.year, today_utc.month, today_utc.day,
239+
utc_time.hour, utc_time.minute)
240+
local_time = utc_datetime - utc_local_diff
241+
242+
if use_12_hour_format:
243+
return datetime.datetime.strftime(local_time,'%I:%M %p')
244+
else:
245+
return datetime.datetime.strftime(local_time,'%H:%M')
246+
226247

227248
class Csv(BaseWriter):
228249

0 commit comments

Comments
 (0)