Skip to content

Commit 668dbde

Browse files
authored
Merge pull request #109 from GetStream/add-timezone
Set timezone as utc in serialization hooks
2 parents 3fa71ac + dbaaa1c commit 668dbde

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if sys.version_info < (2, 7, 9):
1717
requests = "requests[security]>=2.4.1,<3"
1818

19-
install_requires = ["pycryptodomex>=3.4.7,<4", requests, "six>=1.8.0"]
19+
install_requires = ["pycryptodomex>=3.4.7,<4", requests, "six>=1.8.0", "pytz>=2019.3"]
2020

2121
if sys.version_info < (2, 7, 0):
2222
install_requires.append("pyOpenSSL<18.0.0")

stream/serializer.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import json
33
import six
4+
import pytz
45

56
"""
67
Adds the ability to send date and datetime objects to the API
@@ -13,7 +14,9 @@
1314

1415
def _datetime_encoder(obj):
1516
if isinstance(obj, datetime.datetime):
16-
return datetime.datetime.strftime(obj, DATETIME_FORMAT)
17+
if obj.utcoffset() is None: # support for <= 3.3
18+
obj = pytz.utc.localize(obj)
19+
return datetime.datetime.strftime(obj.astimezone(pytz.utc), DATETIME_FORMAT)
1720
if isinstance(obj, datetime.date):
1821
return datetime.datetime.strftime(obj, DATE_FORMAT)
1922

@@ -31,13 +34,17 @@ def _datetime_decoder(dict_):
3134
try:
3235
# The api always returns times like this
3336
# 2014-07-25T09:12:24.735
34-
datetime_obj = datetime.datetime.strptime(value, DATETIME_FORMAT)
37+
datetime_obj = pytz.utc.localize(
38+
datetime.datetime.strptime(value, DATETIME_FORMAT)
39+
)
3540
dict_[key] = datetime_obj
3641
except (ValueError, TypeError):
3742
try:
3843
# The api always returns times like this
3944
# 2014-07-25T09:12:24.735
40-
datetime_obj = datetime.datetime.strptime(value, DATE_FORMAT)
45+
datetime_obj = pytz.utc.localize(
46+
datetime.datetime.strptime(value, DATE_FORMAT)
47+
)
4148
dict_[key] = datetime_obj.date()
4249
except (ValueError, TypeError):
4350
continue

stream/tests/test_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from stream.exceptions import ApiKeyException, InputException
55
import random
66
import jwt
7+
import pytz
78

89
try:
910
from unittest.case import TestCase
@@ -831,7 +832,7 @@ def test_uniqueness(self):
831832
b.) The same time and foreign id
832833
"""
833834

834-
utcnow = datetime.datetime.utcnow()
835+
utcnow = datetime.datetime.now(tz=pytz.utc)
835836
activity_data = {"actor": 1, "verb": "tweet", "object": 1, "time": utcnow}
836837
self.user1.add_activity(activity_data)
837838
self.user1.add_activity(activity_data)
@@ -869,7 +870,7 @@ def test_uniqueness_topic(self):
869870

870871
def test_uniqueness_foreign_id(self):
871872
now = datetime.datetime.now(tzlocal())
872-
utcnow = (now - now.utcoffset()).replace(tzinfo=None)
873+
utcnow = now.astimezone(pytz.utc)
873874

874875
activity_data = {
875876
"actor": 1,
@@ -900,15 +901,15 @@ def test_uniqueness_foreign_id(self):
900901

901902
def test_time_ordering(self):
902903
"""
903-
datetime.datetime.utcnow() is our recommended approach
904+
datetime.datetime.now(tz=pytz.utc) is our recommended approach
904905
so if we add an activity
905906
add one using time
906907
add another activity it should be in the right spot
907908
"""
908909

909910
# timedelta is used to "make sure" that ordering is known even though
910911
# server time is not
911-
custom_time = datetime.datetime.utcnow() - dt.timedelta(days=1)
912+
custom_time = datetime.datetime.now(tz=pytz.utc) - dt.timedelta(days=1)
912913

913914
feed = self.user2
914915
for index, activity_time in enumerate([None, custom_time, None]):
@@ -952,8 +953,8 @@ def test_wrong_feed_spec(self):
952953

953954
def test_serialization(self):
954955
today = datetime.date.today()
955-
then = datetime.datetime.now().replace(microsecond=0)
956-
now = datetime.datetime.now()
956+
now = datetime.datetime.now(tz=pytz.utc)
957+
then = now.replace(microsecond=0)
957958
data = dict(
958959
string="string",
959960
float=0.1,

0 commit comments

Comments
 (0)