Skip to content

Commit

Permalink
Do not use mutable object as default value
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Dec 7, 2015
1 parent 35e2de5 commit dc5e61f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,20 @@ def parse_to_aware_datetime(value):
return datetime_obj


def datetime2timestamp(dt, default_timezone=tzutc()):
def datetime2timestamp(dt, default_timezone=None):
"""Calculate the timestamp based on the given datetime instance.
The optional default_timezone is only used when dt is a naive datetime.
:type dt: datetime
:param dt: A datetime object to be converted into timestamp
:type default_timezone: tzinfo
:param default_timezone: If it is provided as None, we treat it as tzutc().
But it is only used when dt is a naive datetime.
:returns: The timestamp
"""
epoch = datetime.datetime(1970, 1, 1)
if dt.tzinfo is None:
if default_timezone is None:
default_timezone = tzutc()
dt = dt.replace(tzinfo=default_timezone)
d = dt.replace(tzinfo=None) - dt.utcoffset() - epoch
if hasattr(d, "total_seconds"):
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ def test_datetime2timestamp_naive(self):
datetime2timestamp(datetime.datetime(1970, 1, 2)), 86400)

def test_datetime2timestamp_aware(self):
tzinfo = tzoffset("BRST", -10800)
self.assertEqual(
datetime2timestamp(datetime.datetime(1970, 1, 2, tzinfo=tzutc())),
86400)
datetime2timestamp(datetime.datetime(1970, 1, 2, tzinfo=tzinfo)),
97200)


class TestParseToUTCDatetime(unittest.TestCase):
Expand Down

0 comments on commit dc5e61f

Please sign in to comment.