diff --git a/faker/providers/date_time/__init__.py b/faker/providers/date_time/__init__.py
index 8d1d8ff6..5df3c699 100644
--- a/faker/providers/date_time/__init__.py
+++ b/faker/providers/date_time/__init__.py
@@ -1342,7 +1342,10 @@ class Provider(BaseProvider):
         """
         start_date = self._parse_date_time(start_date, tzinfo=tzinfo)
         end_date = self._parse_date_time(end_date, tzinfo=tzinfo)
-        ts = self.generator.random.randint(start_date, end_date)
+        if end_date - start_date <= 1:
+            ts = start_date + self.generator.random.random()
+        else:
+            ts = self.generator.random.randint(start_date, end_date)
         return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=ts)
 
     def date_between(self, start_date='-30y', end_date='today'):

past_datetime() crashes when start_date is within one second of now
I ran into a problem while generating a bunch of fake dates where I was calling `fake.past_datetime()` with the start date using the result of a previous `fake.past_datetime()` call (simulating a bunch of files being created as part of a collection). The first call could randomly return a value which is within one second of the current time, causing the second call to trigger an exception from `random.randint()` because the `a` and `b` values were the same.

This was pretty easy to solve simply by using `fake.date_time_between(start_date=previous_created_date)` but it might be useful if `past_datetime()` was safe to call like this.
diff --git a/tests/providers/test_date_time.py b/tests/providers/test_date_time.py
index f34250e6..1c81f7c3 100644
--- a/tests/providers/test_date_time.py
+++ b/tests/providers/test_date_time.py
@@ -191,6 +191,10 @@ class TestDateTime(unittest.TestCase):
         self.assertTrue(datetime_start <= random_date)
         self.assertTrue(datetime_end >= random_date)
 
+    def test_past_datetime_within_second(self):
+        # Should not raise a ``ValueError``
+        self.factory.past_datetime(start_date='+1s')
+
     def test_date_between_dates(self):
         date_end = date.today()
         date_start = date_end - timedelta(days=10)

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.6
tests/requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
certifi==2021.5.30
dnspython==2.2.1
email-validator==1.0.3
-e git+https://github.com/joke2k/faker.git@fc7bbc43efc9adf923b58b43db6c979a3c17ac86#egg=Faker
idna==3.10
importlib-metadata==4.8.3
iniconfig==1.1.1
mock==2.0.0
packaging==21.3
pbr==6.1.1
pluggy==1.0.0
py==1.11.0
pyparsing==3.1.4
pytest==7.0.1
python-dateutil==2.9.0.post0
six==1.17.0
text-unidecode==1.3
tomli==1.2.3
typing_extensions==4.1.1
UkPostcodeParser==1.1.2
zipp==3.6.0

name: faker
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - dnspython==2.2.1
      - email-validator==1.0.3
      - idna==3.10
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - mock==2.0.0
      - packaging==21.3
      - pbr==6.1.1
      - pluggy==1.0.0
      - py==1.11.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - python-dateutil==2.9.0.post0
      - six==1.17.0
      - text-unidecode==1.3
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - ukpostcodeparser==1.1.2
      - zipp==3.6.0
prefix: /opt/conda/envs/faker

tests/providers/test_date_time.py::TestDateTime::test_past_datetime_within_second
tests/providers/test_date_time.py::TestKoKR::test_day tests/providers/test_date_time.py::TestKoKR::test_month tests/providers/test_date_time.py::TestDateTime::test_date_between tests/providers/test_date_time.py::TestDateTime::test_date_between_dates tests/providers/test_date_time.py::TestDateTime::test_date_object tests/providers/test_date_time.py::TestDateTime::test_date_this_period tests/providers/test_date_time.py::TestDateTime::test_date_time_between tests/providers/test_date_time.py::TestDateTime::test_date_time_between_dates tests/providers/test_date_time.py::TestDateTime::test_date_time_between_dates_with_tzinfo tests/providers/test_date_time.py::TestDateTime::test_date_time_this_period tests/providers/test_date_time.py::TestDateTime::test_date_time_this_period_with_tzinfo tests/providers/test_date_time.py::TestDateTime::test_datetime_safe tests/providers/test_date_time.py::TestDateTime::test_datetime_safe_new_date tests/providers/test_date_time.py::TestDateTime::test_datetimes_with_and_without_tzinfo tests/providers/test_date_time.py::TestDateTime::test_day tests/providers/test_date_time.py::TestDateTime::test_future_date tests/providers/test_date_time.py::TestDateTime::test_future_datetime tests/providers/test_date_time.py::TestDateTime::test_month tests/providers/test_date_time.py::TestDateTime::test_parse_date tests/providers/test_date_time.py::TestDateTime::test_parse_date_time tests/providers/test_date_time.py::TestDateTime::test_parse_timedelta tests/providers/test_date_time.py::TestDateTime::test_past_date tests/providers/test_date_time.py::TestDateTime::test_past_datetime tests/providers/test_date_time.py::TestDateTime::test_time_object tests/providers/test_date_time.py::TestDateTime::test_time_series tests/providers/test_date_time.py::TestDateTime::test_timezone_conversion tests/providers/test_date_time.py::TestPlPL::test_day tests/providers/test_date_time.py::TestPlPL::test_month tests/providers/test_date_time.py::TestAr::test_ar_aa tests/providers/test_date_time.py::TestAr::test_ar_eg
faker/providers/date_time/__init__.py
faker/providers/date_time/__init__.py