diff --git a/kosmorro/__main__.py b/kosmorro/__main__.py index 5155cd5..84d46bd 100644 --- a/kosmorro/__main__.py +++ b/kosmorro/__main__.py @@ -105,9 +105,9 @@ def run(): try: if args.timezone is not None: - timezone = get_timezone(args.timezone) + timezone = get_timezone(args.timezone, compute_date) elif env_vars.tz is not None: - timezone = get_timezone(env_vars.tz) + timezone = get_timezone(env_vars.tz, compute_date) elif env_vars.timezone is not None: print_stderr( colored( @@ -117,7 +117,7 @@ def run(): "yellow", ) ) - timezone = get_timezone(env_vars.timezone) + timezone = get_timezone(env_vars.timezone, compute_date) except pytz.UnknownTimeZoneError as error: print_stderr( colored( diff --git a/kosmorro/utils.py b/kosmorro/utils.py index 892d9c4..047b2c6 100644 --- a/kosmorro/utils.py +++ b/kosmorro/utils.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from datetime import datetime +from datetime import datetime, date import pytz from termcolor import colored as do_color @@ -31,10 +31,12 @@ def print_stderr(*values: object): print(*values, file=stderr) -def get_timezone(value: int | str) -> float: +def get_timezone(value: int | str, dt: datetime | date) -> float: try: timezone = float(value) except ValueError: - timezone = pytz.timezone(value).utcoffset(datetime.now()).total_seconds() / 3600 + if isinstance(dt, date): + dt = datetime.combine(dt, datetime.min.time()) + timezone = pytz.timezone(value).utcoffset(dt).total_seconds() / 3600 return timezone diff --git a/tests/timezone.py b/tests/timezone.py index 6226f48..0430bda 100644 --- a/tests/timezone.py +++ b/tests/timezone.py @@ -1,45 +1,79 @@ #!/usr/bin/env python3 +import pytest + from .utils import ( execute, KOSMORRO, ) -def test_timezone_with_command_line_arg(): - result = execute(KOSMORRO + ["--timezone=1", "-d2020-01-27"]) - assert result.successful - assert "Note: All the hours are given in the UTC+1.0 timezone." in result.stdout - - result = execute(KOSMORRO + ["--timezone=Europe/Paris", "-d2020-01-27"]) - assert result.successful - assert "Note: All the hours are given in the UTC+1.0 timezone." not in result.stdout - - result = execute(KOSMORRO + ["--timezone=-5", "-d2020-01-27"]) - assert result.successful - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout - - result = execute(KOSMORRO + ["--timezone=America/Chicago", "-d2020-01-27"]) - assert result.successful - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout - - -def test_timezone_with_env_var(): - result = execute(KOSMORRO + ["-d2020-01-27"], environment={"TZ": "1"}) - assert result.successful - assert "Note: All the hours are given in the UTC+1.0 timezone." in result.stdout - - result = execute(KOSMORRO + ["-d2020-01-27"], environment={"TZ": "Europe/Paris"}) - assert result.successful - assert "Note: All the hours are given in the UTC+1.0 timezone." not in result.stdout - - result = execute(KOSMORRO + ["-d2020-01-27"], environment={"TZ": "-5"}) +def timezone_with_command_line_arg_input(): + yield [ + "--timezone=1", + "-d2020-01-27", + ], "Note: All the hours are given in the UTC+1.0 timezone." + yield [ + "--timezone=Europe/Paris", + "-d2020-01-27", + ], "Note: All the hours are given in the UTC+1.0 timezone." + # Paris is at UTC+2 in July: + yield [ + "--timezone=Europe/Paris", + "-d2020-07-27", + ], "Note: All the hours are given in the UTC+2.0 timezone." + yield [ + "--timezone=-5", + "-d2020-01-27", + ], "Note: All the hours are given in the UTC-5.0 timezone." + yield [ + "--timezone=America/Chicago", + "-d2020-01-27", + ], "Note: All the hours are given in the UTC-6.0 timezone." + # Chicago is at UTC+6 in July: + yield [ + "--timezone=America/Chicago", + "-d2020-07-27", + ], "Note: All the hours are given in the UTC-5.0 timezone." + + +@pytest.mark.parametrize( + "args, expected_stdout_line", timezone_with_command_line_arg_input() +) +def test_timezone_with_command_line_arg(args, expected_stdout_line): + result = execute(KOSMORRO + args) assert result.successful - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout - - result = execute(KOSMORRO + ["-d2020-01-27"], environment={"TZ": "America/Chicago"}) + assert expected_stdout_line in result.stdout + + +def timezone_with_env_var_input(): + yield {"TZ": "1"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC+1.0 timezone." + yield {"TZ": "Europe/Paris"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC+1.0 timezone." + yield {"TZ": "Europe/Paris"}, [ + "-d2020-07-27" + ], "Note: All the hours are given in the UTC+2.0 timezone." + yield {"TZ": "-5"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC-5.0 timezone." + yield {"TZ": "America/Chicago"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC-6.0 timezone." + yield {"TZ": "America/Chicago"}, [ + "-d2020-07-27" + ], "Note: All the hours are given in the UTC-5.0 timezone." + + +@pytest.mark.parametrize( + "environment, args, expected_stdout_line", timezone_with_env_var_input() +) +def test_timezone_with_env_var(environment, args, expected_stdout_line): + result = execute(KOSMORRO + args, environment=environment) assert result.successful - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout + assert expected_stdout_line in result.stdout def test_timezone_with_env_var_and_command_line_arg(): @@ -50,44 +84,35 @@ def test_timezone_with_env_var_and_command_line_arg(): assert "Note: All the hours are given in the UTC+3.0 timezone." in result.stdout -def test_timezone_with_deprecated_env_var(): - result = execute( - KOSMORRO + ["-d2020-01-27"], environment={"KOSMORRO_TIMEZONE": "1"} - ) - assert result.successful - assert ( - "Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, which is more standard." - in result.stderr - ) - assert "Note: All the hours are given in the UTC+1.0 timezone." in result.stdout - - result = execute( - KOSMORRO + ["-d2020-01-27"], environment={"KOSMORRO_TIMEZONE": "Europe/Paris"} - ) - assert result.successful - assert ( - "Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, which is more standard." - in result.stderr - ) - assert "Note: All the hours are given in the UTC+1.0 timezone." not in result.stdout - - result = execute( - KOSMORRO + ["-d2020-01-27"], environment={"KOSMORRO_TIMEZONE": "-5"} - ) - assert result.successful - assert ( - "Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, which is more standard." - in result.stderr - ) - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout - - result = execute( - KOSMORRO + ["-d2020-01-27"], - environment={"KOSMORRO_TIMEZONE": "America/Chicago"}, - ) +def timezone_with_deprecated_env_var_input(): + yield {"KOSMORRO_TIMEZONE": "1"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC+1.0 timezone." + yield {"KOSMORRO_TIMEZONE": "Europe/Paris"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC+1.0 timezone." + yield {"KOSMORRO_TIMEZONE": "Europe/Paris"}, [ + "-d2020-07-27" + ], "Note: All the hours are given in the UTC+2.0 timezone." + yield {"KOSMORRO_TIMEZONE": "-5"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC-5.0 timezone." + yield {"KOSMORRO_TIMEZONE": "America/Chicago"}, [ + "-d2020-01-27" + ], "Note: All the hours are given in the UTC-6.0 timezone." + yield {"KOSMORRO_TIMEZONE": "America/Chicago"}, [ + "-d2020-07-27" + ], "Note: All the hours are given in the UTC-5.0 timezone." + + +@pytest.mark.parametrize( + "environment, args, expected_stdout_line", timezone_with_deprecated_env_var_input() +) +def test_timezone_with_deprecated_env_var(environment, args, expected_stdout_line): + result = execute(KOSMORRO + args, environment=environment) assert result.successful assert ( "Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, which is more standard." in result.stderr ) - assert "Note: All the hours are given in the UTC-5.0 timezone." in result.stdout + assert expected_stdout_line in result.stdout