Commits

Brendan McCollam committed c883c21

Allows passing a naive datetime object as next_run_time.

Comments (0)

Files changed (3)

apscheduler/job.py

             value = changes.pop('next_run_time')
             if value and not isinstance(value, datetime):
                 raise TypeError('next_run_time must be either None or a datetime instance')
+            if value and value.tzinfo is None:
+                value = self._scheduler.timezone.localize(value)
             approved['next_run_time'] = value
 
         if changes:

tests/conftest.py

 @pytest.fixture(scope='session')
 def create_job(job_defaults):
     def create(**kwargs):
-        kwargs.setdefault('scheduler', Mock(BaseScheduler))
+        scheduler_ = Mock(BaseScheduler)
+        scheduler_.timezone = timezone()
+        kwargs.setdefault('scheduler', scheduler_)
         job_kwargs = job_defaults.copy()
         job_kwargs.update(kwargs)
         job_kwargs['trigger'] = BlockingScheduler()._create_trigger(job_kwargs.pop('trigger'),

tests/test_job.py

     assert str(exc.value) == 'next_run_time must be either None or a datetime instance'
 
 
+def test_private_modify_offset_naive_next_run_time(job):
+    """Tests that passing a datetime object without timezone information will get
+    automatically localized to the timezone of the parent scheduler."""
+
+    scheduler_tz = job._scheduler.timezone
+    naive_datetime = datetime(2011, 4, 3, 18, 40)
+    job._modify(next_run_time=naive_datetime)
+    assert job.next_run_time == scheduler_tz.localize(naive_datetime)
+
+
 def test_private_modify_bad_argument(job):
     """Tests that passing an unmodifiable argument type raises an AttributeError."""