Skip to content

Commit

Permalink
Merge pull request #311 from eric-weaver/issue-287-add-calendar-timegm
Browse files Browse the repository at this point in the history
Add support for calendar.timegm()
  • Loading branch information
boxed committed Oct 14, 2019
2 parents f70b77c + 01b36bd commit 011138d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
17 changes: 15 additions & 2 deletions freezegun/api.py
Expand Up @@ -26,9 +26,10 @@
real_localtime = time.localtime
real_gmtime = time.gmtime
real_strftime = time.strftime
real_timegm = calendar.timegm
real_date = datetime.date
real_datetime = datetime.datetime
real_date_objects = [real_time, real_localtime, real_gmtime, real_strftime, real_date, real_datetime]
real_date_objects = [real_time, real_localtime, real_gmtime, real_strftime, real_timegm, real_date, real_datetime]

if _TIME_NS_PRESENT:
real_time_ns = time.time_ns
Expand Down Expand Up @@ -179,7 +180,7 @@ def fake_time():
if _should_use_real_time():
return real_time()
current_time = get_current_time()
return calendar.timegm(current_time.timetuple()) + current_time.microsecond / 1000000.0
return real_timegm(current_time.timetuple()) + current_time.microsecond / 1000000.0

if _TIME_NS_PRESENT:
def fake_time_ns():
Expand Down Expand Up @@ -215,6 +216,14 @@ def fake_strftime(format, time_to_format=None):
else:
return real_strftime(format, time_to_format)


def fake_timegm(struct_time):
if _should_use_real_time():
return real_timegm(struct_time)
else:
return real_timegm(get_current_time().timetuple())


if real_clock is not None:
def fake_clock():
if _should_use_real_time():
Expand Down Expand Up @@ -586,6 +595,8 @@ def start(self):
return freeze_factory

# Change the modules
calendar.timegm = fake_timegm

datetime.datetime = FakeDatetime
datetime.date = FakeDate

Expand All @@ -609,6 +620,7 @@ def start(self):
('real_localtime', real_localtime, fake_localtime),
('real_strftime', real_strftime, fake_strftime),
('real_time', real_time, fake_time),
('real_timegm', real_timegm, fake_timegm),
]

if _TIME_NS_PRESENT:
Expand Down Expand Up @@ -655,6 +667,7 @@ def stop(self):
tz_offsets.pop()

if not freeze_factories:
calendar.timegm = real_timegm
datetime.datetime = real_datetime
datetime.date = real_date
copyreg.dispatch_table.pop(real_datetime)
Expand Down
10 changes: 10 additions & 0 deletions tests/another_module.py
@@ -1,3 +1,4 @@
from calendar import timegm
from datetime import date, datetime
from time import time, localtime, gmtime, strftime

Expand All @@ -8,6 +9,7 @@
fake_localtime,
fake_gmtime,
fake_strftime,
fake_timegm,
)


Expand Down Expand Up @@ -37,6 +39,10 @@ def get_strftime():
return strftime


def get_timegm():
return timegm


# Fakes

def get_fake_datetime():
Expand All @@ -61,3 +67,7 @@ def get_fake_gmtime():

def get_fake_strftime():
return fake_strftime


def get_fake_timegm():
return fake_timegm
10 changes: 9 additions & 1 deletion tests/test_class_import.py
@@ -1,3 +1,5 @@
import calendar
import datetime
import time
import sys
from .fake_module import (
Expand All @@ -17,8 +19,8 @@
fake_localtime,
fake_gmtime,
fake_strftime,
fake_timegm,
)
import datetime


@freeze_time("2012-01-14")
Expand Down Expand Up @@ -148,6 +150,8 @@ def test_import_after_start():
assert another_module.get_gmtime() is fake_gmtime
assert another_module.get_strftime() is time.strftime
assert another_module.get_strftime() is fake_strftime
assert another_module.get_timegm() is calendar.timegm
assert another_module.get_timegm() is fake_timegm

# Fakes
assert another_module.get_fake_datetime() is FakeDatetime
Expand All @@ -156,6 +160,7 @@ def test_import_after_start():
assert another_module.get_fake_localtime() is fake_localtime
assert another_module.get_fake_gmtime() is fake_gmtime
assert another_module.get_fake_strftime() is fake_strftime
assert another_module.get_fake_timegm() is fake_timegm

# Reals
assert another_module.get_datetime() is datetime.datetime
Expand All @@ -170,6 +175,8 @@ def test_import_after_start():
assert not another_module.get_gmtime() is fake_gmtime
assert another_module.get_strftime() is time.strftime
assert not another_module.get_strftime() is fake_strftime
assert another_module.get_timegm() is calendar.timegm
assert not another_module.get_timegm() is fake_timegm

# Fakes
assert another_module.get_fake_datetime() is FakeDatetime
Expand All @@ -178,6 +185,7 @@ def test_import_after_start():
assert another_module.get_fake_localtime() is fake_localtime
assert another_module.get_fake_gmtime() is fake_gmtime
assert another_module.get_fake_strftime() is fake_strftime
assert another_module.get_fake_timegm() is fake_timegm


def test_none_as_initial():
Expand Down
14 changes: 14 additions & 0 deletions tests/test_datetimes.py
@@ -1,4 +1,5 @@
import time
import calendar
import datetime
import unittest
import locale
Expand Down Expand Up @@ -210,6 +211,13 @@ def test_time_gmtime():
assert time_struct.tm_isdst == -1


def test_calendar_timegm():
time_struct = time.gmtime()
assert calendar.timegm(time_struct) != 1326511294
with freeze_time('2012-01-14 03:21:34'):
assert calendar.timegm(time_struct) == 1326511294


@pytest.mark.skipif(not HAS_CLOCK,
reason="time.clock was removed in Python 3.8")
def test_time_clock():
Expand Down Expand Up @@ -644,6 +652,8 @@ def test_should_use_real_time():
from freezegun import api
api.call_stack_inspection_limit = 100 # just to increase coverage

current_time = time.gmtime()

with freeze_time(frozen):
assert time.time() == expected_frozen
# assert time.localtime() == expected_frozen_local
Expand All @@ -653,6 +663,8 @@ def test_should_use_real_time():
if HAS_TIME_NS:
assert time.time_ns() == expected_frozen * 1e9

assert calendar.timegm(current_time) == expected_frozen

with freeze_time(frozen, ignore=['_pytest', 'nose']):
assert time.time() != expected_frozen
# assert time.localtime() != expected_frozen_local
Expand All @@ -662,6 +674,8 @@ def test_should_use_real_time():
if HAS_TIME_NS:
assert time.time_ns() != expected_frozen * 1e9

assert calendar.timegm(current_time) != expected_frozen


@pytest.mark.skipif(not HAS_TIME_NS,
reason="time.time_ns is present only on 3.7 and above")
Expand Down

0 comments on commit 011138d

Please sign in to comment.