Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set timezone to UTC in SQLAlchemy when connecting to PostgreSQL #3363

Merged
merged 2 commits into from
Aug 15, 2024

Conversation

dekkers
Copy link
Contributor

@dekkers dekkers commented Aug 14, 2024

Changes

Set timezone to UTC in SQLAlchemy when connecting to PostgreSQL

Issue

The fixes the following exception:

  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/rocky/rocky/views/ooi_detail_related_object.py", line 78, in get
    return super().get(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/generic/list.py", line 174, in get
    context = self.get_context_data()
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/rocky/rocky/views/ooi_detail.py", line 96, in get_context_data
    context.update(self.get_origins(self.ooi.reference, self.organization))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/rocky/rocky/views/mixins.py", line 175, in get_origins
    boefje_meta["ended_at"] = datetime.strptime(boefje_meta["ended_at"], "%Y-%m-%dT%H:%M:%S.%fZ")
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/_strptime.py", line 567, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: ValueError at /en/test/objects/detail/
Exception Value: time data '2024-08-11T17:07:15.263467+02:00' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

The cause is that when connecting to PostgreSQL that is configured with a different timezone than UTC (which as far as I know is normal when you run Debian packages of PostgreSQL), PostgreSQL returns the datetime with the configured timezone and the datetime gets serialized with a timezone. Specifying UTC as the timezone when connecting to PostgreSQL means we always get UTC back and resolves this problem.

QA notes

In docker the timezone is already UTC, so there shouldn't be any change when the standard docker development environment.


Checklist for code reviewers:

Copy-paste the checklist from the docs/source/templates folder into your comment.


Checklist for QA:

Copy-paste the checklist from the docs/source/templates folder into your comment.

@dekkers dekkers marked this pull request as ready for review August 14, 2024 21:14
@dekkers dekkers requested a review from a team as a code owner August 14, 2024 21:14
@dekkers dekkers added this to the OpenKAT v1.17 milestone Aug 14, 2024
@stephanie0x00
Copy link
Contributor

Checklist for QA:

  • I have checked out this branch, and successfully ran a fresh make reset.
  • I confirmed that there are no unintended functional regressions in this branch:
    • I have managed to pass the onboarding flow
    • Objects and Findings are created properly
    • Tasks are created and completed properly
  • I confirmed that the PR's advertised feature or hotfix works as intended.
  • I checked the logs for errors and/or warnings and made issues where necessary

What works:

Seems to look good. Nothing obvious is broken.

What doesn't work:

n/a

Bug or feature?:

n/a

@underdarknl underdarknl merged commit 25f265e into main Aug 15, 2024
19 checks passed
@underdarknl underdarknl deleted the timezone-utc branch August 15, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants