diff options
| author | Fuwn <[email protected]> | 2025-07-09 06:44:53 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-09 06:44:53 -0700 |
| commit | 2da802f18849acec127ec948ccc301250494fe3a (patch) | |
| tree | a2ead9b33a30c78fa0bf4be478948f007e9ebf8d | |
| parent | docs: Add README (diff) | |
| download | oguri-2da802f18849acec127ec948ccc301250494fe3a.tar.xz oguri-2da802f18849acec127ec948ccc301250494fe3a.zip | |
feat(schedule): Allow specifying timezone using TZ
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | requirements-dev.lock | 2 | ||||
| -rw-r--r-- | requirements.lock | 2 | ||||
| -rw-r--r-- | src/oguri/schedule.py | 24 |
4 files changed, 18 insertions, 11 deletions
diff --git a/pyproject.toml b/pyproject.toml index b7c2036..7d78926 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,7 @@ dependencies = [ "asyncio>=3.4.3", "rich>=14.0.0", "click-aliases>=1.0.5", + "pytz>=2025.2", ] requires-python = ">= 3.13" diff --git a/requirements-dev.lock b/requirements-dev.lock index cd15afc..1b5ffcb 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -65,6 +65,8 @@ pyflakes==3.4.0 # via autoflake pygments==2.19.2 # via rich +pytz==2025.2 + # via oguri rich==14.0.0 # via oguri ruff==0.12.2 diff --git a/requirements.lock b/requirements.lock index e3e259d..b220435 100644 --- a/requirements.lock +++ b/requirements.lock @@ -64,6 +64,8 @@ pyflakes==3.4.0 # via autoflake pygments==2.19.2 # via rich +pytz==2025.2 + # via oguri rich==14.0.0 # via oguri sniffio==1.3.1 diff --git a/src/oguri/schedule.py b/src/oguri/schedule.py index 7aa29b6..a900294 100644 --- a/src/oguri/schedule.py +++ b/src/oguri/schedule.py @@ -1,3 +1,5 @@ +import os +import pytz from datetime import datetime, timedelta from rich.console import Console from rich.table import Table @@ -28,7 +30,9 @@ async def show_schedule( genres: list[str] = constants.GENRES, ): client = Client(url="https://graphql.anilist.co") - now = datetime.now() + tz_str = os.environ.get("TZ") + timezone = pytz.timezone(tz_str) if tz_str else None + now = datetime.now(timezone) start_of_day, end_of_day = _calculate_date_range(now, days_offset, year, month) all_airing_schedules = await _fetch_airing_schedules(client, start_of_day, @@ -38,7 +42,7 @@ async def show_schedule( formats, sources, genres, sort_by, reverse_order) - _display_schedule_table(airing_schedules, exact_time) + _display_schedule_table(airing_schedules, exact_time, now) def _calculate_date_range(now, days_offset, year, month): @@ -140,7 +144,7 @@ def _filter_and_sort_schedules(all_airing_schedules, countries, formats, return airing_schedules -def _display_schedule_table(airing_schedules, exact_time): +def _display_schedule_table(airing_schedules, exact_time, now): console = Console() table = Table(show_header=True, header_style="bold magenta") @@ -161,15 +165,15 @@ def _display_schedule_table(airing_schedules, exact_time): genres_string = ", ".join(media_genres) if airing_at: - airing_at_date = datetime.fromtimestamp(airing_at) + airing_at_date = datetime.fromtimestamp(airing_at, tz=now.tzinfo) airing_time_string = "" if exact_time: airing_time_string = format_exact_time(airing_at_date) - elif datetime.now() > airing_at_date: - airing_time_string = (f"Aired {relative_time(airing_at_date)}") + elif now > airing_at_date: + airing_time_string = (f"Aired {relative_time(airing_at_date, now)}") else: - airing_time_string = format_future_airing_time(airing_at_date) + airing_time_string = format_future_airing_time(airing_at_date, now) table.add_row( f"[link={site_url}]{title}[/link]", @@ -196,8 +200,7 @@ def format_exact_time(date): return date.strftime(f"%A, %B {day}, %Y, at {hour}{minute_string} {am_pm}") -def format_future_airing_time(date): - now = datetime.now() +def format_future_airing_time(date, now): delta = date - now hours, remainder = divmod(int(delta.total_seconds()), 3600) minutes, _ = divmod(remainder, 60) @@ -216,8 +219,7 @@ def format_future_airing_time(date): return f"In {' '.join(parts)} at {time_string}" -def relative_time(date): - now = datetime.now() +def relative_time(date, now): delta = now - date if delta.days >= 365: |