Skip to content

Commit 84096aa

Browse files
committed
Remove pytz
Everything that pytz was being utilised for can be serviced perfectly fine by the standard library. Meanwhile, pytz is known as a footgun: https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html
1 parent a6ea045 commit 84096aa

File tree

7 files changed

+21
-31
lines changed

7 files changed

+21
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Support more lenient usernames and group names in FTP servers
1414
([#507](https://github.com/PyFilesystem/pyfilesystem2/pull/507)).
1515
Closes [#506](https://github.com/PyFilesystem/pyfilesystem2/issues/506).
16+
- Removed dependency on pytz ([#518](https://github.com/PyFilesystem/pyfilesystem2/pull/518)).
17+
Closes [#516](https://github.com/PyFilesystem/pyfilesystem2/issues/518).
1618

1719
### Fixed
1820

fs/_ftp_parse.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
from __future__ import unicode_literals
44

55
import unicodedata
6-
import datetime
76
import re
87
import time
9-
10-
from pytz import UTC
8+
from datetime import datetime, timezone
119

1210
from .enums import ResourceType
1311
from .permissions import Permissions
1412

1513

16-
EPOCH_DT = datetime.datetime.fromtimestamp(0, UTC)
14+
EPOCH_DT = datetime.fromtimestamp(0, timezone.utc)
1715

1816

1917
RE_LINUX = re.compile(
@@ -98,7 +96,7 @@ def _parse_time(t, formats):
9896
day = _t.tm_mday
9997
hour = _t.tm_hour
10098
minutes = _t.tm_min
101-
dt = datetime.datetime(year, month, day, hour, minutes, tzinfo=UTC)
99+
dt = datetime(year, month, day, hour, minutes, tzinfo=timezone.utc)
102100

103101
epoch_time = (dt - EPOCH_DT).total_seconds()
104102
return epoch_time

fs/test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from __future__ import absolute_import
99
from __future__ import unicode_literals
1010

11-
from datetime import datetime
11+
from datetime import datetime, timezone
1212
import io
1313
import itertools
1414
import json
@@ -26,7 +26,6 @@
2626
from fs.opener import open_fs
2727
from fs.subfs import ClosingSubFS, SubFS
2828

29-
import pytz
3029
import six
3130
from six import text_type
3231

@@ -1196,17 +1195,17 @@ def test_settimes(self):
11961195
can_write_acccess = info.is_writeable("details", "accessed")
11971196
can_write_modified = info.is_writeable("details", "modified")
11981197
if can_write_acccess:
1199-
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=pytz.UTC))
1198+
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=timezone.utc))
12001199
if can_write_modified:
1201-
self.assertEqual(info.modified, datetime(2016, 7, 5, tzinfo=pytz.UTC))
1200+
self.assertEqual(info.modified, datetime(2016, 7, 5, tzinfo=timezone.utc))
12021201

12031202
def test_touch(self):
12041203
self.fs.touch("new.txt")
12051204
self.assert_isfile("new.txt")
12061205
self.fs.settimes("new.txt", datetime(2016, 7, 5))
12071206
info = self.fs.getinfo("new.txt", namespaces=["details"])
12081207
if info.is_writeable("details", "accessed"):
1209-
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=pytz.UTC))
1208+
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=timezone.utc))
12101209
now = time.time()
12111210
self.fs.touch("new.txt")
12121211
accessed = self.fs.getinfo("new.txt", namespaces=["details"]).raw[

fs/time.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
import typing
88
from calendar import timegm
9-
from datetime import datetime
10-
from pytz import UTC, timezone
9+
from datetime import datetime, timezone
1110

1211
if typing.TYPE_CHECKING:
1312
from typing import Optional
1413

1514

16-
utcfromtimestamp = datetime.utcfromtimestamp
17-
utclocalize = UTC.localize
18-
GMT = timezone("GMT")
19-
20-
2115
def datetime_to_epoch(d):
2216
# type: (datetime) -> int
2317
"""Convert datetime to epoch."""
@@ -39,4 +33,6 @@ def epoch_to_datetime(t): # noqa: D103
3933
def epoch_to_datetime(t):
4034
# type: (Optional[int]) -> Optional[datetime]
4135
"""Convert epoch time to a UTC datetime."""
42-
return utclocalize(utcfromtimestamp(t)) if t is not None else None
36+
if t is None:
37+
return None
38+
return datetime.fromtimestamp(t, tz=timezone.utc)

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ setup_requires =
4343
setuptools >=38.3.0
4444
install_requires =
4545
appdirs~=1.4.3
46-
pytz
4746
setuptools
4847
six ~=1.10
4948
enum34 ~=1.1.6 ; python_version < '3.4'

tests/test_info.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from __future__ import unicode_literals
22

3-
import datetime
3+
from datetime import datetime, timezone
44
import unittest
55

6-
import pytz
7-
86
from fs.enums import ResourceType
97
from fs.info import Info
108
from fs.permissions import Permissions
@@ -71,10 +69,10 @@ def test_basic(self):
7169

7270
def test_details(self):
7371
dates = [
74-
datetime.datetime(2016, 7, 5, tzinfo=pytz.UTC),
75-
datetime.datetime(2016, 7, 6, tzinfo=pytz.UTC),
76-
datetime.datetime(2016, 7, 7, tzinfo=pytz.UTC),
77-
datetime.datetime(2016, 7, 8, tzinfo=pytz.UTC),
72+
datetime(2016, 7, 5, tzinfo=timezone.utc),
73+
datetime(2016, 7, 6, tzinfo=timezone.utc),
74+
datetime(2016, 7, 7, tzinfo=timezone.utc),
75+
datetime(2016, 7, 8, tzinfo=timezone.utc),
7876
]
7977
epochs = [datetime_to_epoch(d) for d in dates]
8078

tests/test_time.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
from __future__ import unicode_literals, print_function
22

3-
from datetime import datetime
3+
from datetime import datetime, timezone
44
import unittest
55

6-
import pytz
7-
86
from fs.time import datetime_to_epoch, epoch_to_datetime
97

108

119
class TestEpoch(unittest.TestCase):
1210
def test_epoch_to_datetime(self):
1311
self.assertEqual(
14-
epoch_to_datetime(142214400), datetime(1974, 7, 5, tzinfo=pytz.UTC)
12+
epoch_to_datetime(142214400), datetime(1974, 7, 5, tzinfo=timezone.utc)
1513
)
1614

1715
def test_datetime_to_epoch(self):
1816
self.assertEqual(
19-
datetime_to_epoch(datetime(1974, 7, 5, tzinfo=pytz.UTC)), 142214400
17+
datetime_to_epoch(datetime(1974, 7, 5, tzinfo=timezone.utc)), 142214400
2018
)

0 commit comments

Comments
 (0)