Permalink
Browse files

add optional tz argument to now()

  • Loading branch information...
1 parent a14ffde commit 6697dc766faadbe8f3a2fbc32d5da60530f4e703 @spulec committed Mar 24, 2013
Showing with 29 additions and 2 deletions.
  1. +5 −2 freezegun/api.py
  2. +24 −0 tests/test_operations.py
View
7 freezegun/api.py
@@ -58,8 +58,11 @@ def __sub__(self, other):
return result
@classmethod
- def now(cls):
- result = cls.time_to_freeze + datetime.timedelta(hours=cls.tz_offset)
+ def now(cls, tz=None):
+ if tz:
+ result = tz.fromutc(cls.time_to_freeze.replace(tzinfo=tz))
+ else:
+ result = cls.time_to_freeze + datetime.timedelta(hours=cls.tz_offset)
return datetime_to_fakedatetime(result)
@classmethod
View
24 tests/test_operations.py
@@ -1,6 +1,8 @@
import datetime
+import sure
from freezegun import freeze_time
from dateutil.relativedelta import relativedelta
+from datetime import timedelta, tzinfo
@freeze_time("2012-01-14")
@@ -35,3 +37,25 @@ def test_subtraction():
assert isinstance(yesterday, datetime.date)
assert isinstance(other_yesterday, datetime.date)
assert isinstance(how_long, datetime.timedelta)
+
+
+@freeze_time("2012-01-14")
+def test_datetime_timezone_none():
+ now = datetime.datetime.now(tz=None)
+ now.should.equal(datetime.datetime(2012, 1, 14))
+
+
+class GMT5(tzinfo):
+ def utcoffset(self,dt):
+ return timedelta(hours=5)
+ def tzname(self,dt):
+ return "GMT +5"
+ def dst(self,dt):
+ return timedelta(0)
+
+
+@freeze_time("2012-01-14 2:00:00")
+def test_datetime_timezone_real():
+ now = datetime.datetime.now(tz=GMT5())
+ now.should.equal(datetime.datetime(2012, 1, 14, 7, tzinfo=GMT5()))
+ now.utcoffset().should.equal(timedelta(0, 60 * 60 * 5))

0 comments on commit 6697dc7

Please sign in to comment.