]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Don't generate random timestamps BC
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 7 Oct 2021 21:20:49 +0000 (23:20 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 7 Oct 2021 21:20:49 +0000 (23:20 +0200)
It might happen to go below datetime.datetime.min if the timezone is
involved.

Today the random data generator is having a field day...

tests/fix_faker.py

index 75f660854669176bc0326731a1bfef5da5929817..63c933e7338631177d7adb0508e1c59a10f9ed12 100644 (file)
@@ -346,13 +346,16 @@ class Faker:
         return self.schema_time(cls)
 
     def make_datetime(self, spec):
+        # Add a day because with timezone we might go BC
+        dtmin = dt.datetime.min + dt.timedelta(days=1)
+
         # Comparisons might get unreliable too far in the future
         # https://bugs.python.org/issue45347
-        # delta = dt.datetime.max - dt.datetime.min
+        # delta = dt.datetime.max - dtmin
         delta = dt.timedelta(days=3000 * 365)
 
         micros = randrange((delta.days + 1) * 24 * 60 * 60 * 1_000_000)
-        rv = dt.datetime.min + dt.timedelta(microseconds=micros)
+        rv = dtmin + dt.timedelta(microseconds=micros)
         if spec[1]:
             rv = rv.replace(tzinfo=self._make_tz(spec))
         return rv