Repository: joke2k/faker
Pull request #526 (2017-06-02 19:25:35)

Add `future_date`, `future_datetime`, `past_date` and `past_datetime`


Patch:
diff --git a/faker/providers/date_time/__init__.py b/faker/providers/date_time/__init__.py
index a1e6fff3..f8d344ae 100644
--- a/faker/providers/date_time/__init__.py
+++ b/faker/providers/date_time/__init__.py
@@ -361,6 +361,62 @@ class Provider(BaseProvider):
         timestamp = random.randint(start_date, end_date)
         return datetime.fromtimestamp(timestamp, tzinfo)
 
+    @classmethod
+    def future_datetime(cls, end_date='+30d', tzinfo=None):
+        """
+        Get a DateTime object based on a random date between now and a given date.
+        Accepts date strings that can be recognized by strtotime().
+
+        :param end_date Defaults to "+30d"
+        :param tzinfo: timezone, instance of datetime.tzinfo subclass
+        :example DateTime('1999-02-02 11:42:52')
+        :return DateTime
+        """
+        return cls.date_time_between(
+            start_date=timedelta(seconds=1), end_date=end_date, tzinfo=tzinfo,
+        )
+
+    @classmethod
+    def future_date(cls, end_date='+30d', tzinfo=None):
+        """
+        Get a Date object based on a random date between now and a given date.
+        Accepts date strings that can be recognized by strtotime().
+
+        :param end_date Defaults to "+30d"
+        :param tzinfo: timezone, instance of datetime.tzinfo subclass
+        :example DateTime('1999-02-02 11:42:52')
+        :return DateTime
+        """
+        return cls.future_datetime(end_date=end_date, tzinfo=tzinfo).date()
+
+    @classmethod
+    def past_datetime(cls, start_date='-30d', tzinfo=None):
+        """
+        Get a DateTime object based on a random date between a given date and now.
+        Accepts date strings that can be recognized by strtotime().
+
+        :param start_date Defaults to "-30d"
+        :param tzinfo: timezone, instance of datetime.tzinfo subclass
+        :example DateTime('1999-02-02 11:42:52')
+        :return DateTime
+        """
+        return cls.date_time_between(
+            start_date=start_date, end_date=timedelta(seconds=-1), tzinfo=tzinfo,
+        )
+
+    @classmethod
+    def past_date(cls, start_date='-30d', tzinfo=None):
+        """
+        Get a Date object based on a random date between a given date and now.
+        Accepts date strings that can be recognized by strtotime().
+
+        :param start_date Defaults to "-30d"
+        :param tzinfo: timezone, instance of datetime.tzinfo subclass
+        :example DateTime('1999-02-02 11:42:52')
+        :return DateTime
+        """
+        return cls.past_datetime(start_date=start_date, tzinfo=tzinfo).date()
+
     @classmethod
     def date_time_between_dates(cls, datetime_start=None, datetime_end=None, tzinfo=None):
         """


Test patch:
diff --git a/tests/providers/date_time.py b/tests/providers/date_time.py
index 20079207..f31e15a5 100644
--- a/tests/providers/date_time.py
+++ b/tests/providers/date_time.py
@@ -1,17 +1,18 @@
 # coding: utf-8
-
 from __future__ import unicode_literals
 
+from datetime import date, datetime
 import unittest
+
 from faker import Factory
+from faker.providers.date_time import Provider as DatetimeProvider
 from .. import string_types
 
 
-class TestHuHU(unittest.TestCase):
-    """ Tests date_time in hu_HU locale. """
+class TestDateTime(unittest.TestCase):
 
     def setUp(self):
-        self.factory = Factory.create('hu_HU')
+        self.factory = Factory.create()
 
     def test_day(self):
         day = self.factory.day_of_week()
@@ -21,6 +22,27 @@ class TestHuHU(unittest.TestCase):
         month = self.factory.month()
         assert isinstance(month, string_types)
 
+    def test_past_datetime(self):
+        past_datetime = self.factory.past_datetime()
+        self.assertTrue(past_datetime < datetime.now())
+
+    def test_past_date(self):
+        past_date = self.factory.past_date()
+        self.assertTrue(past_date < date.today())
+
+    def test_future_datetime(self):
+        future_datetime, now = self.factory.future_datetime(), datetime.now()
+        self.assertTrue(future_datetime > now)
+
+    def test_future_date(self):
+        future_date = self.factory.future_date()
+        self.assertTrue(future_date > date.today())
+
+    def test_parse_date_time(self):
+        timestamp = DatetimeProvider._parse_date_time('+30d')
+        now = DatetimeProvider._parse_date_time('now')
+        self.assertTrue(timestamp > now)
+
 
 class TestPlPL(unittest.TestCase):