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

Update unit tests to pass in different timezones #323

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

Conversation

dcrowe
Copy link

@dcrowe dcrowe commented Dec 17, 2019

Fixes #322

@dcrowe
Copy link
Author

dcrowe commented Dec 17, 2019

That _Utc class was totalled not needed. It is much simpler now.

I understand that this doesn't interact well with #89, but I'm looking for a smaller issue to chew on for my first contribution. :)

Tested in the following timezones:

# not in DST
TZ=Europe/Berlin make test
# in DST
TZ=Australia/Adelaide make test
# doesn't have DST
TZ=Australia/Queensland make test
# UTC
TZ=UTC make test

utc_time = local_time - datetime.timedelta(seconds=time.timezone)
expected_timestamp = time.mktime(utc_time.timetuple())
utc_time = datetime.datetime(2012, 1, 14)
expected_timestamp = calendar.timegm(utc_time.timetuple())
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a sorta clunky way to get a timestamp for UTC, how about this:

utc_time = datetime.datetime(2012, 1, 14, tzinfo=datetime.timezone.utc)
expected_timestamp = utc_time.timestamp()

datetime_from_time = datetime.datetime.fromtimestamp(time.time())
timezone_adjusted_datetime = datetime_from_time + datetime.timedelta(seconds=time.timezone)
assert timezone_adjusted_datetime == expected_datetime
datetime_from_time = datetime.datetime.utcfromtimestamp(time.time())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced that this is the right thing to do, but I also don't think the assertion this replaces makes much sense.

It seems to be asserting that datetime.datetime.now() should not be equal to datetime.fromtimestamp(time.time()) if the test is run in a locale with an offset, which is not the same behavior as what datetime.datetime does. Fixing this assumption in the tests (and code?) is how this should be fixed, I think.

@pganssle
Copy link
Contributor

I think it would probably be a good idea to add a CI job that runs the tests in some non-UTC locale (or possibly in a few locales). Presumably it's OK to just use whatever the latest Python is and run the test suite in a America/New_York, Australia/Adelaide and Europe/London (or even just one of those, to start).

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.

Unit tests fail when run in Adelaide, Australia
2 participants