Repository: joke2k/faker

Issue:
suggestion: pytimezone
In addition to the currently available [timezone](), it would be very useful* to have a similar python object faker, something like:
```python
import pytz

class TheExistingPythonProvider:

    def pytimezone(self, **kwargs):
        return pytz.timezone(self.generator.timezone(**kwargs))
```

For an example use-case, see:

\* FactoryBoy/factory_boy#643

Discussion:
malefice: @fcurella Based on the error logs, it appears that the docker image's locale has not been set to something that supports `UTF-8`. To verify, I tried to replicate the environment by setting up an `i386/ubuntu` docker image, installed `locales`, and then ran the command `locale-gen en_US.UTF-8`. When I checked the default locale, none was set.

```
root@4cf92a376f00:/# cat /etc/default/locale
#  File generated by update-locale
```

The `build32bit.sh` file needs to be updated to set the locale manually. Try appending the command `update-locale LANG=en_US.UTF-8` after the `locale-gen` command.

malefice: @fcurella @OJFord So, I finally got the time to set up travis for my fork. I applied my recommendations on top of the work done in this pull request, and the 32bit build passes. You can see it [here](https://travis-ci.com/malefice/faker/builds/136106896).

Just add `export LANG='en_US.UTF-8' in `build32bit.sh` as shown [here](https://github.com/malefice/faker/blob/89584bcc8cfb5db8ef00975ae083bf7b7b210534/build32bit.sh#L13).
fcurella: Thank @malefice, I've committed the fix to master.

@OJFord could you rebase your branch against `master`?

Patch:
diff --git a/faker/providers/date_time/__init__.py b/faker/providers/date_time/__init__.py
index 39dab838..529db175 100644
--- a/faker/providers/date_time/__init__.py
+++ b/faker/providers/date_time/__init__.py
@@ -4,7 +4,7 @@ from calendar import timegm
 from datetime import MAXYEAR, timedelta
 
 from dateutil import relativedelta
-from dateutil.tz import tzlocal, tzutc
+from dateutil.tz import gettz, tzlocal, tzutc
 
 from faker.utils.datetime_safe import date, datetime, real_date, real_datetime
 
@@ -1962,6 +1962,17 @@ class Provider(BaseProvider):
         return self.generator.random.choice(
             self.random_element(self.countries)['timezones'])
 
+    def pytimezone(self, *args, **kwargs):
+        """
+        Generate a random timezone (see `faker.timezone` for any args)
+        and return as a python object usable as a `tzinfo` to `datetime`
+        or other fakers.
+
+        :example faker.pytimezone()
+        :return dateutil.tz.tz.tzfile
+        """
+        return gettz(self.timezone(*args, **kwargs))
+
     def date_of_birth(self, tzinfo=None, minimum_age=0, maximum_age=115):
         """
         Generate a random date of birth represented as a Date object,


Test patch:
diff --git a/tests/providers/test_date_time.py b/tests/providers/test_date_time.py
index 30e9cf09..ad951a5f 100644
--- a/tests/providers/test_date_time.py
+++ b/tests/providers/test_date_time.py
@@ -138,6 +138,16 @@ class TestDateTime(unittest.TestCase):
         today_back = datetime.fromtimestamp(timestamp, utc).date()
         assert today == today_back
 
+    def test_pytimezone(self):
+        import dateutil
+        pytz = self.fake.pytimezone()
+        assert isinstance(pytz, dateutil.tz.tz.tzfile)
+
+    def test_pytimezone_usable(self):
+        pytz = self.fake.pytimezone()
+        date = datetime(2000, 1, 1, tzinfo=pytz)
+        assert date.tzinfo == pytz
+
     def test_datetime_safe(self):
         from faker.utils import datetime_safe
         # test using example provided in module