Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Timezone awareness #53

Merged
merged 8 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run_tests(self):
"Programming Language :: Python :: 3.8",
"Topic :: Software Development :: Libraries :: Python Modules"],
install_requires=[
"cryptojwt>=1.6.0",
"cryptojwt==1.6.0",
"pyOpenSSL",
"filelock>=3.0.12",
'pyyaml>=5.1.2'
Expand Down
38 changes: 1 addition & 37 deletions src/oidcmsg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "Roland Hedberg"
__version__ = "1.5.1"
__version__ = "1.5.3"

import os
from typing import Dict
Expand Down Expand Up @@ -32,39 +32,3 @@ def proper_path(path):
path += "/"

return path


# def add_base_path(conf: Dict[str, str], item_paths: dict, base_path: str):
# """
# This is for adding a base path to path specified in a configuration
#
# :param conf: Configuration
# :param item_paths: The relative item path
# :param base_path: An absolute path to add to the relative
# """
# for section, items in item_paths.items():
# if section == "":
# part = conf
# else:
# part = conf.get(section)
#
# if part:
# if isinstance(items, list):
# for attr in items:
# _path = part.get(attr)
# if _path:
# if _path.startswith("/"):
# continue
# elif _path == "":
# part[attr] = "./" + _path
# else:
# part[attr] = os.path.join(base_path, _path)
# elif items is None:
# if part.startswith("/"):
# continue
# elif part == "":
# conf[section] = "./"
# else:
# conf[section] = os.path.join(base_path, part)
# else: # Assume items is dictionary like
# add_base_path(part, items, base_path)
9 changes: 6 additions & 3 deletions src/oidcmsg/time_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import time
from datetime import datetime
from datetime import timedelta
from datetime import timezone

TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
TIME_FORMAT_WITH_FRAGMENT = re.compile("^(\d{4,4}-\d{2,2}-\d{2,2}T\d{2,2}:\d{2,2}:\d{2,2})\.\d*Z$")
Expand Down Expand Up @@ -181,7 +182,8 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0
:return: datetime instance using UTC time
"""
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
return datetime.utcnow() + delta
res = datetime.now(timezone.utc) + delta
return res.replace(tzinfo=None)


def time_a_while_ago(
Expand All @@ -200,7 +202,8 @@ def time_a_while_ago(
:return: datetime instance using UTC time
"""
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
return datetime.utcnow() - delta
res = datetime.now(timezone.utc) - delta
return res.replace(tzinfo=None)
Copy link
Member

Choose a reason for hiding this comment

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

why do we need to strip the timezone information?

Copy link
Member Author

Choose a reason for hiding this comment

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

naive and aware datetime can't be compared, so for retrocompatibility I operate with aware datetime but always return naive datetime

Copy link
Member Author

Choose a reason for hiding this comment

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

considering that standard requries utc datetime, it's not important to have it with timezone

Copy link
Member

Choose a reason for hiding this comment

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

What are those comparisons?

It is important it to have it with timezone to ensure that operations on time are done in the needed timezone.
I would argue we need to do the opposite - have all datetimes in UTC to ensure calculations are correct. So, the comparisons that take place, should also have dates loaded as UTC.

Copy link
Member Author

Choose a reason for hiding this comment

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

the comparison doesn't happen here but later in a unit test

______________________________________________________________________________________________ test_time_a_while_ago _______________________________________________________________________________________________

    def test_time_a_while_ago():
        dt = datetime.utcnow()
        t = time_a_while_ago(seconds=10)
>       delta = dt - t  # slightly less than 10
E       TypeError: can't subtract offset-naive and offset-aware datetimes

tests/test_03_time_util.py:204: TypeError

considering that standard require UTC timestamp I didn't see any needs to carrying aware datetimes in the code BUT you kow that I'ma good boy that can spend some more effort to get all happy :)

Roland approved, do we have to put another change or do you think that's good enough as is?

Copy link
Member

Choose a reason for hiding this comment

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

If we are going to change .utcnow() to .now(utc) I think we should do it everywhere.
If we don't want to do it, then let's not do it at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know the conseguences to other libraries and behaviours
I preferred to let the type be the same as before to prevent any breaking changes with this release

that's the answer, feel free to open an issue to keep track of this



def in_a_while(
Expand Down Expand Up @@ -351,7 +354,7 @@ def later_than(after, before):


def utc_time_sans_frac():
now_timestampt = int(datetime.utcnow().timestamp())
now_timestampt = int(datetime.now(timezone.utc).timestamp())
return now_timestampt


Expand Down