Skip to content

[AAP-62956] Change datetime to use utc as timestamp instead of local timezone#921

Open
RudneiBertolJr wants to merge 1 commit intoansible:develfrom
RudneiBertolJr:AAP-62956
Open

[AAP-62956] Change datetime to use utc as timestamp instead of local timezone#921
RudneiBertolJr wants to merge 1 commit intoansible:develfrom
RudneiBertolJr:AAP-62956

Conversation

@RudneiBertolJr
Copy link
Copy Markdown

Fixing the issue reported at AAP-62956

Description

  • What is being changed?
    Changing the module datetime to use UTC data to generate the expiration timestamp for the token.
  • Why is this change needed?
    When customer has controller node with TIME_ZONE variable configured to a timezone with timezone grater than 1h, generated tokens are generated already expired.
  • How does this change address the issue?
    Now, it does not matter the timezone, the token will respect the expiration time set.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Self-Review Checklist

  • I have performed a self-review of my code
  • I have added relevant comments to complex code sections
  • I have updated documentation where needed
  • I have considered the security impact of these changes
  • I have considered performance implications
  • I have thought about error handling and edge cases
  • I have tested the changes in my local environment

Testing Instructions

You can use the instructions for the reproduce at https://issues.redhat.com/browse/AAP-62956

Expected Results

Now does not matter the timezone between client and server, the expiration timestamp should respect the expiration time based on the UTC timezone.

@RudneiBertolJr RudneiBertolJr force-pushed the AAP-62956 branch 3 times, most recently from 2aa641e to da8bc7f Compare January 19, 2026 12:30
@AlanCoding
Copy link
Copy Markdown
Member

I would just like to have some form of testing for this, but I am struggling to see what that would be.

Ultimately, this is compared against some time diff, but on another server. It's this other side of the token expiration implementation that this need to match. And that needs to use UTC. Presumably that could be hunted down, and if in DAB, tested against.

@RudneiBertolJr
Copy link
Copy Markdown
Author

Hey @AlanCoding,
I was able to test it with one controller+gateway node on AAP, which uses this library. I do have a reproducer in place. Let me know if you would like to meet and check it.


if expiration is not None:
payload["exp"] = datetime.now() + timedelta(seconds=expiration)
payload["exp"] = datetime.now(timezone.utc) + timedelta(seconds=expiration)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if you have the ability to re-test, I would kind of prefer

from django.utils import timezone

now = timezone.now()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In [1]: from django.utils.timezone import now

In [2]: now()
Out[2]: datetime.datetime(2026, 1, 20, 21, 44, 41, 966889, tzinfo=datetime.timezone.utc)

In [3]: from datetime import datetime, timedelta, timezone

In [4]: datetime.now(timezone.utc)
Out[4]: datetime.datetime(2026, 1, 20, 21, 44, 54, 837486, tzinfo=datetime.timezone.utc)

In [5]: now()
Out[5]: datetime.datetime(2026, 1, 20, 21, 44, 59, 91693, tzinfo=datetime.timezone.utc)

They seem to be the same thing. I had the wrong import, it is from django.utils.timezone import now. This is the most standard thing to use now().

Copy link
Copy Markdown
Author

@RudneiBertolJr RudneiBertolJr Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, I have tested and they are the same. I will change my PR to use now().

>>> from django.utils.timezone import now
>>> now()
datetime.datetime(2026, 1, 20, 22, 5, 38, 890072, tzinfo=datetime.timezone.utc)

>>> from datetime import datetime, timedelta, timezone
>>> datetime.now()
datetime.datetime(2026, 1, 20, 14, 6, 8, 126278)
>>> datetime.now(timezone.utc)
datetime.datetime(2026, 1, 20, 22, 6, 24, 138297, tzinfo=datetime.timezone.utc)

>>> now()
datetime.datetime(2026, 1, 20, 22, 8, 12, 107243, tzinfo=datetime.timezone.utc)

Copy link
Copy Markdown
Member

@AlanCoding AlanCoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth it to put in the now() pattern, because this happens so many times in related code we should standardzie

@github-actions
Copy link
Copy Markdown

DVCS PR Check Results:

PR appears valid (JIRA key(s) found)

@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants