Repository: joke2k/faker
Pull request #965 (2019-05-29 15:40:58)

Issue/944/throw error in unix time generation
### What does this changes

Replace the `time.time()` by `datetime_to_timestamp(datetime.now())` to avoid problems with negative offsets in machine as specified in #944

### What was wrong

These lines of code used to throw an error
```python
import faker
f = faker.Faker()
f.unix_time(start_datetime='now')
```

### How to fix it
Changed the calculation with `end_datetime=None`. Added a test to set an offset during tests to avoid this :bug: to happen again.


fcurella: Have you tried setting `os.environ["TZ"]` temporarily to a negative offset as suggested in https://stackoverflow.com/a/1301528 ?
bluesheeptoken: @fcurella Thanks for pointing out, I have read the documentation too fast.

This is fixed, however, I do not know what I messed up for the appveyor integration... I might need some help here.
bluesheeptoken: @fcurella 
I am strill struggling with the appveyor integration with python3.5. Apparently, he cannot find [importlib-metadata](https://ci.appveyor.com/project/joke2k/faker/builds/25000045/job/ukj46n9hlcgacuej#L261) which is required by pluggy. Pluggy is required by Pytest.

Any idea where can this come from? Any idea how to fix that?
tomaszrycz: Hi @bluesheeptoken 

I have the same issue with my pull request #964.
I've analysed the problem and I think it can be something wrong in  `importlib-metadata` building setup. There is a warning in log:
```
C:\Python35\lib\distutils\dist.py:261: UserWarning: Unknown distribution option: 'use_scm_version'
  warnings.warn(msg)
```
and then builder is copying a file with a strange name 
```
Copying unknown-0.0.0-py3.5.egg to c:\projects\faker\.eggs
```
In my opinion ` unknown-0.0.0-py3.5.egg` is actually `importlib-metadata` package but due to wrong name `pkg_resources` can't find it.

Unfortunately, I don't know how to resolve this problem.

bluesheeptoken: Hi @tomaszrycz, thanks for telling me !

I arrived at the same conclusion... (But I did not know about pkg_resources!)

I tried to check pytest's issues to see if it came from here. But did not find anything either... 
I will investigate again tomorrow and keep you in touch !

Thanks for replying !
fcurella: @bluesheeptoken appveyor is fixed on master. Do you mind rebasing your branch?
bluesheeptoken: @fcurella , thank you this is done.
No idea why coverage reacts like this though
fcurella: @bluesheeptoken don't worry about the coverage. It's flaky because some tests are non-deterministic.

Thank you so much! ✨ 

Patch:
diff --git a/faker/providers/date_time/__init__.py b/faker/providers/date_time/__init__.py
index 8422a8f4..54a5094f 100644
--- a/faker/providers/date_time/__init__.py
+++ b/faker/providers/date_time/__init__.py
@@ -6,7 +6,6 @@ import re
 
 from calendar import timegm
 from datetime import timedelta, MAXYEAR
-from time import time
 
 from dateutil import relativedelta
 from dateutil.tz import tzlocal, tzutc
@@ -1474,7 +1473,7 @@ class Provider(BaseProvider):
     @classmethod
     def _parse_end_datetime(cls, value):
         if value is None:
-            return int(time())
+            return datetime_to_timestamp(datetime.now())
 
         return cls._parse_date_time(value)
 


Test patch:
diff --git a/tests/providers/test_date_time.py b/tests/providers/test_date_time.py
index fb9e760d..818fdec4 100644
--- a/tests/providers/test_date_time.py
+++ b/tests/providers/test_date_time.py
@@ -3,10 +3,13 @@ from __future__ import unicode_literals
 
 from datetime import date, datetime, timedelta, tzinfo
 from datetime import time as datetime_time
-import time
-import unittest
+import os
+import platform
+import pytest
 import random
 import sys
+import time
+import unittest
 
 import six
 
@@ -17,8 +20,6 @@ from faker.providers.date_time.ar_AA import Provider as ArProvider
 from faker.providers.date_time.ar_EG import Provider as EgProvider
 from faker.providers.date_time.hy_AM import Provider as HyAmProvider
 
-import pytest
-
 
 def is64bit():
     return sys.maxsize > 2**32
@@ -448,7 +449,7 @@ class TestDateTime(unittest.TestCase):
         from faker.providers.date_time import datetime_to_timestamp
 
         for _ in range(100):
-            now = datetime.now(utc).replace(microsecond=0)
+            now = datetime.now().replace(microsecond=0)
             epoch_start = datetime(1970, 1, 1, tzinfo=utc)
 
             # Ensure doubly-constrained unix_times are generated correctly
@@ -490,6 +491,14 @@ class TestDateTime(unittest.TestCase):
             self.assertIsInstance(constrained_unix_time, int)
             self.assertBetween(constrained_unix_time, 0, datetime_to_timestamp(now))
 
+            # Ensure it does not throw error with startdate='now' for machines with negative offset
+            if platform.system() != 'Windows':
+                os.environ['TZ'] = 'Europe/Paris'
+                time.tzset()
+            self.factory.unix_time(start_datetime='now')
+            if platform.system() != 'Windows':
+                del os.environ['TZ']
+
 
 class TestPlPL(unittest.TestCase):