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

Fix local timezone and timezone related conversions #526

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

priezz
Copy link

@priezz priezz commented Dec 18, 2023

This PR addresses issues with

  • handling timezones by making corrections to timezone and local timezone related conversions,
  • time formatting and the use of a time freezing utility without specifying a timezone,
  • ensuring accurate time representation and manipulation across different geographical regions,
  • ensuring that the correct local time is displayed and used throughout the simulation.

Fixes #204, #291, #299, #348, #377, #405, #472, #513.

  • tz_offset parameter is now considered as a local timezone offset
  • tz_offset defaults to the local environment timezone
  • when no timezone is specified in time_to_freeze, it defaults to tz_offset
  • local timezone simulation now affects the environment, so time.strftime works as expected

mkenigs and others added 3 commits November 3, 2021 17:41
now(tz) should return the same time utcnow returns adjusted by whatever
offset is contained by tz. Currently, the offset to freeze_time is also
added, which is removed by this change

The current unit test is wrong because the UTC time is 2:00:00, so GMT5
should be 7:00:00, not 3:00:00

Closes spulec#405
@priezz priezz closed this Dec 18, 2023
@priezz priezz deleted the fix-tz branch December 18, 2023 19:38
@priezz priezz restored the fix-tz branch December 18, 2023 19:38
@priezz priezz reopened this Dec 18, 2023
@priezz priezz marked this pull request as draft December 18, 2023 19:41
@priezz priezz marked this pull request as ready for review December 19, 2023 00:53
Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

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

Hi @priezz, thaank you for the PR! At a first glance, this seems mostly sensible - but I would have to do a second (/third) deep-dive, as there seem to be quite a few functional changes.

Can you extend the tests to account for all changes? That makes it easier to verify that everything works as expected.

@priezz
Copy link
Author

priezz commented Dec 19, 2023

Hi @priezz, thaank you for the PR! At a first glance, this seems mostly sensible - but I would have to do a second (/third) deep-dive, as there seem to be quite a few functional changes.

Can you extend the tests to account for all changes? That makes it easier to verify that everything works as expected.

Sure, if after the deep dive you will confirm that the changes make sense, I will add missing/update existing tests.

@@ -39,6 +40,8 @@
real_date = datetime.date
real_datetime = datetime.datetime
real_date_objects = [real_time, real_localtime, real_gmtime, real_monotonic, real_perf_counter, real_strftime, real_date, real_datetime]
real_tzlocal = dateutil.tz.tzlocal
real_tz_env = os.environ["TZ"] if "TZ" in os.environ else ''
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
real_tz_env = os.environ["TZ"] if "TZ" in os.environ else ''
real_tz_env = os.environ.get("TZ", "")

current_time = get_current_time()
return calendar.timegm(current_time.timetuple()) + current_time.microsecond / 1000000.0

current_time_utc = get_current_time() - datetime.timedelta(seconds=tz_offsets[-1].total_seconds())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
current_time_utc = get_current_time() - datetime.timedelta(seconds=tz_offsets[-1].total_seconds())
current_time_utc = get_current_time() - tz_offsets[-1]

tz_offsets contains a list of datetime.timedelta-objects, so this is functionally equivalent as far as I can see



def fake_gmtime(t=None):
if t is not None:
return real_gmtime(t)
if _should_use_real_time():
return real_gmtime()
return get_current_time().timetuple()

current_time_utc = get_current_time() - datetime.timedelta(seconds=tz_offsets[-1].total_seconds())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this would always have to be in UTC, wouldn't this be get_current_time() - UTC_offset (whatever the value for UTC_offset is)?

@@ -323,9 +342,12 @@ def __sub__(self, other):

@classmethod
def today(cls):
result = cls._date_to_freeze() + cls._tz_offset()
result = cls._date_to_freeze()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we do need some sort of timezone offset here, as the day can be different in different timezones.

Was there a specific bug with this operation?

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.

time.localtime returns unexpected values
3 participants