Skip to content

Commit 57acf32

Browse files
authored
Merge pull request #643 from andrewshaodev/feature/12-hour-time
Feature/12 hour time
2 parents 78f4540 + 2e24375 commit 57acf32

File tree

7 files changed

+43
-7
lines changed

7 files changed

+43
-7
lines changed

config.sample.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ MetricsPort = 8000
3737
## Set localization for ${{pickupdate}}
3838
Locale = en_US
3939

40+
## Time format for pickup dates: "24h" for 24-hour format (default), "12h" for 12-hour format with AM/PM
41+
TimeFormat = 24h
42+
4043
## Disable to not show activity spinner in console
4144
Activity = True
4245

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ repository = "https://github.com/Der-Henning/tgtg"
4141
[tool.poetry]
4242
packages = [{ include = "tgtg_scanner" }]
4343
requires-poetry = '>=2.0.0,<3.0.0'
44-
version = "1.23.1"
44+
version = "1.23.2"
4545

4646
[tool.poetry.group.build.dependencies]
4747
pyinstaller = "^6.3.0"

tests/test_item.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,24 @@ def test_item(tgtg_item: dict, monkeypatch: pytest.MonkeyPatch):
2323
assert item.store_name == tgtg_item.get("store", {}).get("store_name", "-")
2424
assert item.item_logo == tgtg_item.get("item", {}).get("logo_picture", {}).get("current_url", "-")
2525
assert item.item_cover == tgtg_item.get("item", {}).get("cover_picture", {}).get("current_url", "-")
26+
27+
28+
def test_item_pickupdate_24h_format(tgtg_item: dict):
29+
"""Test pickup date formatting with 24-hour time format."""
30+
item = Item(tgtg_item, time_format="24h")
31+
pickupdate = item.pickupdate
32+
# The pickup time should be formatted as 24-hour with colon separator (e.g., "19:00 - 19:30")
33+
# Check that it contains the colon format and doesn't contain AM/PM
34+
assert ":" in pickupdate
35+
assert " - " in pickupdate
36+
assert "AM" not in pickupdate
37+
assert "PM" not in pickupdate
38+
39+
40+
def test_item_pickupdate_12h_format(tgtg_item: dict):
41+
"""Test pickup date formatting with 12-hour time format."""
42+
item = Item(tgtg_item, time_format="12h")
43+
pickupdate = item.pickupdate
44+
# The pickup time should be formatted as 12-hour with AM/PM (e.g., "07:00 PM - 07:30 PM")
45+
assert "PM" in pickupdate or "AM" in pickupdate
46+
assert ":" in pickupdate

tgtg_scanner/models/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ class Config(BaseConfig):
533533
schedule_cron: Cron = field(default_factory=Cron)
534534
debug: bool = False
535535
locale: str = "en_US"
536+
time_format: str = "24h"
536537
metrics: bool = False
537538
metrics_port: int = 8000
538539
disable_tests: bool = False
@@ -611,6 +612,7 @@ def _read_ini(self, parser: configparser.ConfigParser):
611612
self._ini_get_cron(parser, "MAIN", "ScheduleCron", "schedule_cron")
612613
self._ini_get_boolean(parser, "MAIN", "Debug", "debug")
613614
self._ini_get(parser, "MAIN", "Locale", "locale")
615+
self._ini_get(parser, "MAIN", "TimeFormat", "time_format")
614616
self._ini_get_boolean(parser, "MAIN", "Metrics", "metrics")
615617
self._ini_get_int(parser, "MAIN", "MetricsPort", "metrics_port")
616618
self._ini_get_boolean(parser, "MAIN", "DisableTests", "disable_tests")
@@ -624,6 +626,7 @@ def _read_env(self):
624626
self._env_get_cron("SCHEDULE_CRON", "schedule_cron")
625627
self._env_get_boolean("DEBUG", "debug")
626628
self._env_get("LOCALE", "locale")
629+
self._env_get("TIME_FORMAT", "time_format")
627630
self._env_get_boolean("METRICS", "metrics")
628631
self._env_get_int("METRICS_PORT", "metrics_port")
629632
self._env_get_boolean("DISABLE_TESTS", "disable_tests")

tgtg_scanner/models/item.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Item:
5252
returns well formated data for notifications.
5353
"""
5454

55-
def __init__(self, data: dict, location: Union[Location, None] = None, locale: str = "en_US"):
55+
def __init__(self, data: dict, location: Union[Location, None] = None, locale: str = "en_US", time_format: str = "24h"):
5656
self.items_available: int = data.get("items_available", 0)
5757
self.display_name: str = data.get("display_name", "-")
5858
self.favorite: str = "Yes" if data.get("favorite", False) else "No"
@@ -88,6 +88,7 @@ def __init__(self, data: dict, location: Union[Location, None] = None, locale: s
8888
self.scanned_on: str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
8989
self.location = location
9090
self.locale = locale
91+
self.time_format = time_format
9192

9293
@property
9394
def rating(self) -> str:
@@ -171,7 +172,15 @@ def pickupdate(self) -> str:
171172
now = datetime.datetime.now()
172173
pfr = self._datetimeparse(self.pickup_interval_start)
173174
pto = self._datetimeparse(self.pickup_interval_end)
174-
prange = f"{pfr.hour:02d}:{pfr.minute:02d} - {pto.hour:02d}:{pto.minute:02d}"
175+
176+
# Format time based on time_format setting
177+
if self.time_format == "12h":
178+
# 12-hour format with AM/PM
179+
prange = f"{pfr.strftime('%I:%M %p')} - {pto.strftime('%I:%M %p')}"
180+
else:
181+
# Default 24-hour format
182+
prange = f"{pfr.hour:02d}:{pfr.minute:02d} - {pto.hour:02d}:{pto.minute:02d}"
183+
175184
tomorrow = now + datetime.timedelta(days=1)
176185
if now.date() == pfr.date():
177186
return f"{humanize.naturalday(now)}, {prange}"

tgtg_scanner/scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _get_test_item(self) -> Item:
7676
return items[0]
7777
items = sorted(
7878
[
79-
Item(item, self.location, self.config.locale)
79+
Item(item, self.location, self.config.locale, self.config.time_format)
8080
for item in self.tgtg_client.get_items(favorites_only=False, latitude=53.5511, longitude=9.9937, radius=50)
8181
],
8282
key=lambda x: x.items_available,
@@ -95,7 +95,7 @@ def _job(self) -> None:
9595
try:
9696
if item_id != "":
9797
item_dict = self.tgtg_client.get_item(item_id)
98-
items.append(Item(item_dict, self.location, self.config.locale))
98+
items.append(Item(item_dict, self.location, self.config.locale, self.config.time_format))
9999
except TgtgAPIError as err:
100100
log.error(err)
101101
items += self._get_favorites()
@@ -127,7 +127,7 @@ def _get_favorites(self) -> list[Item]:
127127
except TgtgAPIError as err:
128128
log.error(err)
129129
return []
130-
return [Item(item, self.location, self.config.locale) for item in items]
130+
return [Item(item, self.location, self.config.locale, self.config.time_format) for item in items]
131131

132132
def _check_item(self, item: Item) -> None:
133133
"""Checks if the available item amount raised from zero to something

tgtg_scanner/tgtg/tgtg_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
log = logging.getLogger("tgtg")
2626
BASE_URL = "https://apptoogoodtogo.com/api/"
27-
API_ITEM_ENDPOINT = "item/v8/"
27+
API_ITEM_ENDPOINT = "item/v9/"
2828
FAVORITE_ITEM_ENDPOINT = "user/favorite/v1/{}/update"
2929
AUTH_BY_EMAIL_ENDPOINT = "auth/v5/authByEmail"
3030
AUTH_POLLING_ENDPOINT = "auth/v5/authByRequestPollingId"

0 commit comments

Comments
 (0)