Skip to content

Commit

Permalink
fix(task-view): do not use the timezone.utc timezone to preserve a …
Browse files Browse the repository at this point in the history
…naive datetime
  • Loading branch information
laurent-laporte-pro committed Oct 26, 2023
1 parent 0537e58 commit a3f0f5e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion antarest/core/tasks/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,6 @@ def _update_task_status(
task.result_status = result
task.result = command_result
if status.is_final():
task.completion_date = datetime.datetime.now(datetime.timezone.utc)
# Do not use the `timezone.utc` timezone to preserve a naive datetime.
task.completion_date = datetime.datetime.utcnow()
self.repo.save(task)
3 changes: 2 additions & 1 deletion antarest/launcher/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def update(
job_result.output_id = output_id
final_status = status in [JobStatus.SUCCESS, JobStatus.FAILED]
if final_status:
job_result.completion_date = datetime.now(timezone.utc)
# Do not use the `timezone.utc` timezone to preserve a naive datetime.
job_result.completion_date = datetime.utcnow()
self.job_result_repository.save(job_result)
self.event_bus.push(
Event(
Expand Down
29 changes: 29 additions & 0 deletions tests/core/tasks/test_task_job_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import datetime

from sqlalchemy.orm import Session # type: ignore

from antarest.core.tasks.model import TaskJob


def test_database_date_utc(db_session: Session) -> None:
now = datetime.datetime.utcnow()
later = now + datetime.timedelta(seconds=1)

with db_session:
task_job = TaskJob(name="foo")
db_session.add(task_job)
db_session.commit()

with db_session:
task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one()
assert now <= task_job.creation_date <= later
assert task_job.completion_date is None

with db_session:
task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one()
task_job.completion_date = datetime.datetime.utcnow()
db_session.commit()

with db_session:
task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one()
assert now <= task_job.creation_date <= task_job.completion_date <= later

0 comments on commit a3f0f5e

Please sign in to comment.