Pārlūkot izejas kodu

Merge pull request #426 from Kosmorro/fix/timezone-summer-times

tags/v1.0.0rc2
Deuchnord pirms 1 mēnesi
committed by GitHub
vecāks
revīzija
85767ec7e7
Šim parakstam datu bāzē netika atrasta zināma atslēga GPG atslēgas ID: B5690EEEBB952194
3 mainītis faili ar 101 papildinājumiem un 74 dzēšanām
  1. +3
    -3
      kosmorro/__main__.py
  2. +5
    -3
      kosmorro/utils.py
  3. +93
    -68
      tests/timezone.py

+ 3
- 3
kosmorro/__main__.py Parādīt failu

@@ -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(


+ 5
- 3
kosmorro/utils.py Parādīt failu

@@ -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

+ 93
- 68
tests/timezone.py Parādīt failu

@@ -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

Notiek ielāde…
Atcelt
Saglabāt