Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pytz #1442

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = { text = "MIT" }
authors = [
{ name = "Michael van Tellingen", email = "[email protected]" }
]
requires-python = ">=3.8"
requires-python = ">=3.9"
classifiers = [
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: MIT License",
Expand All @@ -27,7 +27,6 @@ dependencies = [
"requests>=2.7.0",
"requests-toolbelt>=0.7.1",
"requests-file>=1.5.1",
"pytz",
]

[project.urls]
Expand Down
5 changes: 2 additions & 3 deletions src/zeep/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Dict, Tuple, Union

import platformdirs
import pytz

# The sqlite3 is not available on Google App Engine so we handle the
# ImportError here and set the sqlite3 var to None.
Expand Down Expand Up @@ -169,8 +168,8 @@ def _is_expired(value, timeout):
if timeout is None:
return False

now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=pytz.utc)
max_age = value.replace(tzinfo=pytz.utc)
now = datetime.datetime.now(datetime.timezone.utc)
max_age = value.replace(tzinfo=datetime.timezone.utc)
max_age += datetime.timedelta(seconds=timeout)
return now > max_age

Expand Down
3 changes: 1 addition & 2 deletions src/zeep/wsse/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
from uuid import uuid4

import pytz
from lxml import etree
from lxml.builder import ElementMaker

Expand Down Expand Up @@ -29,7 +28,7 @@ def get_security_header(doc):

def get_timestamp(timestamp=None, zulu_timestamp=None):
timestamp = timestamp or datetime.datetime.now(datetime.timezone.utc)
timestamp = timestamp.replace(tzinfo=pytz.utc, microsecond=0)
timestamp = timestamp.replace(tzinfo=datetime.timezone.utc, microsecond=0)
if zulu_timestamp:
return timestamp.isoformat().replace("+00:00", "Z")
else:
Expand Down
13 changes: 6 additions & 7 deletions src/zeep/xsd/types/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from decimal import Decimal as _Decimal

import isodate
import pytz

from zeep.xsd.const import xsd_ns
from zeep.xsd.types.any import AnyType
Expand Down Expand Up @@ -548,31 +547,31 @@ class PositiveInteger(NonNegativeInteger):
##
# Other
def _parse_timezone(val):
"""Return a pytz.tzinfo object"""
"""Return a datetime.timezone object"""
if not val:
return

if val == "Z" or val == "+00:00":
return pytz.utc
return datetime.timezone.utc

negative = val.startswith("-")
minutes = int(val[-2:])
minutes += int(val[1:3]) * 60

if negative:
minutes = 0 - minutes
return pytz.FixedOffset(minutes)
return datetime.timezone(datetime.timedelta(minutes=minutes))


def _unparse_timezone(tzinfo):
if not tzinfo:
return ""

if tzinfo == pytz.utc:
if tzinfo == datetime.timezone.utc:
return "Z"

hours = math.floor(tzinfo._minutes / 60)
minutes = tzinfo._minutes % 60
hours = math.floor(tzinfo.utcoffset(None).total_seconds() / 60 / 60)
minutes = tzinfo.utcoffset(None).total_seconds() / 60 / 60 - hours

if hours > 0:
return "+%02d:%02d" % (hours, minutes)
Expand Down
50 changes: 25 additions & 25 deletions tests/test_xsd_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import isodate
import pytest
import pytz
from datetime import timedelta, timezone

from zeep.xsd.types import builtins

Expand Down Expand Up @@ -161,13 +161,13 @@ def test_xmlvalue(self):
value = datetime.datetime(2016, 3, 4, 21, 14, 42)
assert instance.xmlvalue(value) == "2016-03-04T21:14:42"

value = datetime.datetime(2016, 3, 4, 21, 14, 42, tzinfo=pytz.utc)
value = datetime.datetime(2016, 3, 4, 21, 14, 42, tzinfo=timezone.utc)
assert instance.xmlvalue(value) == "2016-03-04T21:14:42Z"

value = datetime.datetime(2016, 3, 4, 21, 14, 42, 123456, tzinfo=pytz.utc)
value = datetime.datetime(2016, 3, 4, 21, 14, 42, 123456, tzinfo=timezone.utc)
assert instance.xmlvalue(value) == "2016-03-04T21:14:42.123456Z"

value = datetime.datetime(2016, 3, 4, 21, 14, 42, tzinfo=pytz.utc)
value = datetime.datetime(2016, 3, 4, 21, 14, 42, tzinfo=timezone.utc)
value = value.astimezone(pytz.timezone("Europe/Amsterdam"))
assert instance.xmlvalue(value) == "2016-03-04T22:14:42+01:00"

Expand Down Expand Up @@ -262,18 +262,18 @@ class TestgYearMonth:
def test_xmlvalue(self):
instance = builtins.gYearMonth()
assert instance.xmlvalue((2012, 10, None)) == "2012-10"
assert instance.xmlvalue((2012, 10, pytz.utc)) == "2012-10Z"
assert instance.xmlvalue((2012, 10, timezone.utc)) == "2012-10Z"

def test_pythonvalue(self):
instance = builtins.gYearMonth()
assert instance.pythonvalue("2001-10") == (2001, 10, None)
assert instance.pythonvalue("2001-10+02:00") == (
2001,
10,
pytz.FixedOffset(120),
timezone(timedelta(minutes=120)),
)
assert instance.pythonvalue("2001-10Z") == (2001, 10, pytz.utc)
assert instance.pythonvalue("2001-10+00:00") == (2001, 10, pytz.utc)
assert instance.pythonvalue("2001-10Z") == (2001, 10, timezone.utc)
assert instance.pythonvalue("2001-10+00:00") == (2001, 10, timezone.utc)
assert instance.pythonvalue("-2001-10") == (-2001, 10, None)
assert instance.pythonvalue("-20001-10") == (-20001, 10, None)

Expand All @@ -285,19 +285,19 @@ class TestgYear:
def test_xmlvalue(self):
instance = builtins.gYear()
assert instance.xmlvalue((2001, None)) == "2001"
assert instance.xmlvalue((2001, pytz.utc)) == "2001Z"
assert instance.xmlvalue((2001, timezone.utc)) == "2001Z"

def test_pythonvalue(self):
instance = builtins.gYear()
assert instance.pythonvalue("2001") == (2001, None)
assert instance.pythonvalue("2001+02:00") == (2001, pytz.FixedOffset(120))
assert instance.pythonvalue("2001Z") == (2001, pytz.utc)
assert instance.pythonvalue("2001+00:00") == (2001, pytz.utc)
assert instance.pythonvalue("2001+02:00") == (2001, timezone(timedelta(minutes=120)))
assert instance.pythonvalue("2001Z") == (2001, timezone.utc)
assert instance.pythonvalue("2001+00:00") == (2001, timezone.utc)
assert instance.pythonvalue("-2001") == (-2001, None)
assert instance.pythonvalue("-20000") == (-20000, None)
assert instance.pythonvalue(" \t2001+02:00\r\n ") == (
2001,
pytz.FixedOffset(120),
timezone(timedelta(minutes=120)),
)

with pytest.raises(builtins.ParseError):
Expand All @@ -312,9 +312,9 @@ def test_xmlvalue(self):
def test_pythonvalue(self):
instance = builtins.gMonthDay()
assert instance.pythonvalue("--05-01") == (5, 1, None)
assert instance.pythonvalue("--11-01Z") == (11, 1, pytz.utc)
assert instance.pythonvalue("--11-01+02:00") == (11, 1, pytz.FixedOffset(120))
assert instance.pythonvalue("--11-01-04:00") == (11, 1, pytz.FixedOffset(-240))
assert instance.pythonvalue("--11-01Z") == (11, 1, timezone.utc)
assert instance.pythonvalue("--11-01+02:00") == (11, 1, timezone(timedelta(minutes=120)))
assert instance.pythonvalue("--11-01-04:00") == (11, 1, timezone(timedelta(minutes=-240)))
assert instance.pythonvalue("--11-15") == (11, 15, None)
assert instance.pythonvalue("--02-29") == (2, 29, None)
assert instance.pythonvalue("\t\r\n --05-01 ") == (5, 1, None)
Expand All @@ -331,12 +331,12 @@ def test_xmlvalue(self):
def test_pythonvalue(self):
instance = builtins.gMonth()
assert instance.pythonvalue("--05") == (5, None)
assert instance.pythonvalue("--11Z") == (11, pytz.utc)
assert instance.pythonvalue("--11+02:00") == (11, pytz.FixedOffset(120))
assert instance.pythonvalue("--11-04:00") == (11, pytz.FixedOffset(-240))
assert instance.pythonvalue("--11Z") == (11, timezone.utc)
assert instance.pythonvalue("--11+02:00") == (11, timezone(timedelta(minutes=120)))
assert instance.pythonvalue("--11-04:00") == (11, timezone(timedelta(minutes=-240)))
assert instance.pythonvalue("--11") == (11, None)
assert instance.pythonvalue("--02") == (2, None)
assert instance.pythonvalue("\n\t --11Z \r") == (11, pytz.utc)
assert instance.pythonvalue("\n\t --11Z \r") == (11, timezone.utc)

with pytest.raises(builtins.ParseError):
assert instance.pythonvalue("99")
Expand All @@ -349,18 +349,18 @@ def test_xmlvalue(self):
value = (1, None)
assert instance.xmlvalue(value) == "---01"

value = (1, pytz.FixedOffset(120))
value = (1, timezone(timedelta(minutes=120)))
assert instance.xmlvalue(value) == "---01+02:00"

value = (1, pytz.FixedOffset(-240))
value = (1, timezone(timedelta(minutes=-240)))
assert instance.xmlvalue(value) == "---01-04:00"

def test_pythonvalue(self):
instance = builtins.gDay()
assert instance.pythonvalue("---01") == (1, None)
assert instance.pythonvalue("---01Z") == (1, pytz.utc)
assert instance.pythonvalue("---01+02:00") == (1, pytz.FixedOffset(120))
assert instance.pythonvalue("---01-04:00") == (1, pytz.FixedOffset(-240))
assert instance.pythonvalue("---01Z") == (1, timezone.utc)
assert instance.pythonvalue("---01+02:00") == (1, timezone(timedelta(minutes=120)))
assert instance.pythonvalue("---01-04:00") == (1, timezone(timedelta(minutes=-240)))
assert instance.pythonvalue("---15") == (15, None)
assert instance.pythonvalue("---31") == (31, None)
assert instance.pythonvalue("\r\n \t---31 ") == (31, None)
Expand Down