Skip to content

Commit

Permalink
Merge ca57472 into ac60f99
Browse files Browse the repository at this point in the history
  • Loading branch information
romuald committed Feb 6, 2017
2 parents ac60f99 + ca57472 commit 9644130
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions freezegun/api.py
Expand Up @@ -3,6 +3,7 @@
import inspect
import sys
import time
import uuid
import calendar
import unittest
import platform
Expand All @@ -18,6 +19,16 @@
real_date = datetime.date
real_datetime = datetime.datetime

try:
real_uuid_generate_time = uuid._uuid_generate_time
except ImportError:
real_uuid_generate_time = None

try:
real_uuid_create = uuid._UuidCreate
except ImportError:
real_uuid_create = None

try:
import copy_reg as copyreg
except ImportError:
Expand Down Expand Up @@ -373,6 +384,8 @@ def start(self):
time.localtime = fake_localtime
time.gmtime = fake_gmtime
time.strftime = fake_strftime
uuid._uuid_generate_time = None
uuid._UuidCreate = None

copyreg.dispatch_table[real_datetime] = pickle_fake_datetime
copyreg.dispatch_table[real_date] = pickle_fake_date
Expand Down Expand Up @@ -473,6 +486,9 @@ def stop(self):
time.localtime = time.localtime.previous_localtime_function
time.strftime = time.strftime.previous_strftime_function

uuid._uuid_generate_time = real_uuid_generate_time
uuid._UuidCreate = real_uuid_create

def decorate_callable(self, func):
def wrapper(*args, **kwargs):
with self:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_uuid.py
@@ -0,0 +1,24 @@
import datetime
import uuid

from freezegun import freeze_time


def time_from_uuid(value):
"""Converts an UUID(1) to it's datetime value"""
uvalue = value if isinstance(value, uuid.UUID) else uuid.UUID(value)

assert uvalue.version == 1

return (datetime.datetime(1582, 10, 15) +
datetime.timedelta(microseconds=uvalue.time // 10))

def test_uuid1():
# Test that the uuid.uuid1() methods generate a value from the freezed date
# This was not always the case as python is
# using the system's one if available through ctypes
target = datetime.datetime(2017, 2, 6, 14, 8, 21)

with freeze_time(target):
assert time_from_uuid(uuid.uuid1()) == target

0 comments on commit 9644130

Please sign in to comment.