-
Notifications
You must be signed in to change notification settings - Fork 686
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
Adding UTC APIs #6909
Merged
ThePotatoGuy
merged 13 commits into
Monika-After-Story:content
from
ThePotatoGuy:UTC-prep
Jan 28, 2021
Merged
Adding UTC APIs #6909
ThePotatoGuy
merged 13 commits into
Monika-After-Story:content
from
ThePotatoGuy:UTC-prep
Jan 28, 2021
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ThePotatoGuy
added
awaiting code review
someone needs to check for syntax/logic/indentation errors
awaiting testing
code needs to be tested
labels
Jan 21, 2021
multimokia
approved these changes
Jan 27, 2021
Booplicate
approved these changes
Jan 28, 2021
Legendkiller21
approved these changes
Jan 28, 2021
ThePotatoGuy
removed
awaiting code review
someone needs to check for syntax/logic/indentation errors
awaiting testing
code needs to be tested
labels
Jan 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Partial #2312
New coding conventions with this PR
Event
databases and docking station log times)datetime.utcnow()
instead ofdatetime.now()
datetime
withtzinfo
populated. If you are doingutc_to_local
, and want to save the result to persistent, you need to clear thetzinfo
with adt.replace(tzinfo=None)
.local_to_utc
will always return adatetime
withouttzinfo
set.datetime
math should be done in UTC. Only convert to local when you need to show the user or thedatetime
needs to be localized because of existing data structure exceptions.Key Changes
mas_utils.local_to_utc
- converts a naive local datetime into UTC - NOTE: this exists mainly for dealing with our current set of non-UTC times. As we transition to UTC, we should almost never have to use this.mas_utils.utc_to_any
- converts a naive utc datetime into a datetime for any timezone, usingpytz.tzinfo
object. This setstzinfo
mas_utils.utc_to_local
- converts a naive utc datetime into a localtime. This setstzinfo
mas_utils.get_localzone
- gets the local timezone objectmas_utils.reload_localzone
- reloads the local timezone object and returns it. this should be used if the current timezone may have changed.pytz
andtzlocal
libraries for time management.pytz
has implementations oftzinfo
objects which are required to use datetimes with timezones.tzlocal
has a function for retrieving the local timezone as apytz.tzinfo
object.pytz
library so it can support loading timezone info locally.MASLocalTz
- special extension ofpytz.tzinfo.StaticTzInfo
that is compatible withpytz
anddatetime.tzinfo
. This builds a timezone naively using thetime.timezone/altzone/daylight
libraries. (Seepytz
considerations section as to why this is needed).pytz
considerationsThe actual
pytz
library contains a bunch of special files that are used to build timezones. Since time is complicated, there are many of these files, and including them would double the filecount of the mod. The only real benefit to having these timezones is to do time math that span timezones. For example, if we had a direct scheduling system in the mod (like planning dates at specific times), then we would need the timezone information to accurately handle things like DST.Since we don't need this information to do accurate utc -> local time, I decided not to include them. If we do need these in the future, however, we should look at all the options for adding the files, like only including them as a zip and then extracting them when installed. (or once we have a full installer, we could simply request the
pytz
package to be installed for the system).So in order to handle timezones without having actual timezone information, I've created
MASLocalTz
, which can build apytz.tzinfo
object that works as a local timezone just as well as a real timezone for standard utc-local conversions. What it cannot do is determine if a specific date and time should be in DST or not for the local timezone. All it can do is adjust itself automatically when the local timezone changes (either to/from DST or to any specific timezone, like when the user changes it).timezone load order
When MAS loads,
pytz
will attempt to load timezone info from file. This happens as follows:PYTZ_TZDATADIR
is checked. This would likely be set if a user installspytz
to their system. If this env var is found, timezone data will be loaded from this directory.python-packages/pytz/zoneinfo/
will be checked. This would be how we would load timezones if we shipped them with the modMASLocalTz
is always used, and we will not have standard timezone information.Testing
pytz
installed and the env var set, you can trymas_utils.get_localzone()
in console. You should get a real timezone in this case. (You may need to add the env var toenvironment.txt
if renpy doesn't find it. I've had mixed results).zoneinfo
folder in thepytz
python-package. Should get a real timezone as well.zoneinfo.zip