replaced naive datetime utc functionality deprecated in python 3.12

This commit is contained in:
Eric 2024-04-25 11:15:05 -07:00
parent e7d3ea98ec
commit 4bd5f895e2
No known key found for this signature in database
GPG Key ID: 89C93F0F8D6D5A98
8 changed files with 109 additions and 84 deletions

88
.efrocachemap generated
View File

@ -4056,50 +4056,50 @@
"build/assets/windows/Win32/ucrtbased.dll": "2def5335207d41b21b9823f6805997f1",
"build/assets/windows/Win32/vc_redist.x86.exe": "b08a55e2e77623fe657bea24f223a3ae",
"build/assets/windows/Win32/vcruntime140d.dll": "865b2af4d1e26a1a8073c89acb06e599",
"build/prefab/full/linux_arm64_gui/debug/ballisticakit": "95d3b3754686e9d8c0c2895ab7d4a9ef",
"build/prefab/full/linux_arm64_gui/release/ballisticakit": "e3a10a6162557f0b64b8bdd9d9229e24",
"build/prefab/full/linux_arm64_server/debug/dist/ballisticakit_headless": "832eaa27b31f6121d0381aaf53fdabfc",
"build/prefab/full/linux_arm64_server/release/dist/ballisticakit_headless": "3ac128c996a7d4deeb9b91e30cce0464",
"build/prefab/full/linux_x86_64_gui/debug/ballisticakit": "c2e844b37743e2da5e2b1919ae793731",
"build/prefab/full/linux_x86_64_gui/release/ballisticakit": "5c982aa2d96322a4a8f09ec1936a9796",
"build/prefab/full/linux_x86_64_server/debug/dist/ballisticakit_headless": "1bbc7ec3c44fdc333a847795883bf31a",
"build/prefab/full/linux_x86_64_server/release/dist/ballisticakit_headless": "a500865463baa4a8c3984652f58d14bf",
"build/prefab/full/mac_arm64_gui/debug/ballisticakit": "29c2a8fff49eead697f3408900f5f728",
"build/prefab/full/mac_arm64_gui/release/ballisticakit": "f77c2b93e5a160df583c446031cdb39e",
"build/prefab/full/mac_arm64_server/debug/dist/ballisticakit_headless": "91faf11638115b66ce588f87f8ba76b5",
"build/prefab/full/mac_arm64_server/release/dist/ballisticakit_headless": "90f4af39be70330e6612934c493f5e30",
"build/prefab/full/mac_x86_64_gui/debug/ballisticakit": "92a16c5de14c19a77c74012035e6c1c4",
"build/prefab/full/mac_x86_64_gui/release/ballisticakit": "0afbcacce3f606ff3715b44ccacb7538",
"build/prefab/full/mac_x86_64_server/debug/dist/ballisticakit_headless": "d1ed86ad1a8895904eb398031cfa59ea",
"build/prefab/full/mac_x86_64_server/release/dist/ballisticakit_headless": "34be50255d63cfac06124e95cb888094",
"build/prefab/full/windows_x86_gui/debug/BallisticaKit.exe": "115d0ed634ce726705798ec06732d068",
"build/prefab/full/windows_x86_gui/release/BallisticaKit.exe": "a30b4a3dea436d366366408833bf1299",
"build/prefab/full/windows_x86_server/debug/dist/BallisticaKitHeadless.exe": "fa21c12d85f2f71ee017d62479296b74",
"build/prefab/full/windows_x86_server/release/dist/BallisticaKitHeadless.exe": "4654985fb946fb41e2ca9a46dd7f47a2",
"build/prefab/lib/linux_arm64_gui/debug/libballisticaplus.a": "d838781c9192c6d1db86bd540478aadf",
"build/prefab/lib/linux_arm64_gui/release/libballisticaplus.a": "b9f7438c554cdadedd0d71aabb3a1d86",
"build/prefab/lib/linux_arm64_server/debug/libballisticaplus.a": "d838781c9192c6d1db86bd540478aadf",
"build/prefab/lib/linux_arm64_server/release/libballisticaplus.a": "b9f7438c554cdadedd0d71aabb3a1d86",
"build/prefab/lib/linux_x86_64_gui/debug/libballisticaplus.a": "7e97bf72f32e0cfe296e514bb9956c66",
"build/prefab/lib/linux_x86_64_gui/release/libballisticaplus.a": "85a1c7eb3dd701914d7d5992205c5bf9",
"build/prefab/lib/linux_x86_64_server/debug/libballisticaplus.a": "7e97bf72f32e0cfe296e514bb9956c66",
"build/prefab/lib/linux_x86_64_server/release/libballisticaplus.a": "85a1c7eb3dd701914d7d5992205c5bf9",
"build/prefab/lib/mac_arm64_gui/debug/libballisticaplus.a": "74b654c23ada4a8b002d546788011680",
"build/prefab/lib/mac_arm64_gui/release/libballisticaplus.a": "c54b3817bc065393fd3f06497d6d6c00",
"build/prefab/lib/mac_arm64_server/debug/libballisticaplus.a": "74b654c23ada4a8b002d546788011680",
"build/prefab/lib/mac_arm64_server/release/libballisticaplus.a": "c54b3817bc065393fd3f06497d6d6c00",
"build/prefab/lib/mac_x86_64_gui/debug/libballisticaplus.a": "8ec7c1d19c225e71fa2be8ed99d2e176",
"build/prefab/lib/mac_x86_64_gui/release/libballisticaplus.a": "5471a38a2f6cc967b6518b7976d48cae",
"build/prefab/lib/mac_x86_64_server/debug/libballisticaplus.a": "807b53c6ca38c392a7477f250d3a85e5",
"build/prefab/lib/mac_x86_64_server/release/libballisticaplus.a": "5471a38a2f6cc967b6518b7976d48cae",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.lib": "e7b856db02f1ce0214a82286c9d132b0",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.pdb": "8fd0542f0c1e7ee1ff8742c4390ea221",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.lib": "1839372e1d8292d2448b11728b31f9e4",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.pdb": "d4a46bf1f28322e5fb0904b0519312bf",
"build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.lib": "318dc16cd148573d1110363d96f64bbb",
"build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.pdb": "3049a3bc0c86d870151856857c88abf2",
"build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.lib": "ccd18401e52bb4a156e25413bf88be31",
"build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.pdb": "68c0c61ecdc257fd0a465b81e48f67ba",
"build/prefab/full/linux_arm64_gui/debug/ballisticakit": "11c7eb77184381209b76dd985e35f440",
"build/prefab/full/linux_arm64_gui/release/ballisticakit": "3550e369b5ddd93e5c3f62c7ea64bfe0",
"build/prefab/full/linux_arm64_server/debug/dist/ballisticakit_headless": "a01f3937958d94b294bb222c715b02dd",
"build/prefab/full/linux_arm64_server/release/dist/ballisticakit_headless": "2ae7379f191a95f6f617258a0888ae24",
"build/prefab/full/linux_x86_64_gui/debug/ballisticakit": "8ceaac4f9ae4f4adff32ddd528e6da6c",
"build/prefab/full/linux_x86_64_gui/release/ballisticakit": "58a89eceb6aa2c78df3b9b6847ee2fc9",
"build/prefab/full/linux_x86_64_server/debug/dist/ballisticakit_headless": "40ca388f1adabc1b2731502c2ee8dcef",
"build/prefab/full/linux_x86_64_server/release/dist/ballisticakit_headless": "0266a836a1ff0631866b89426d857380",
"build/prefab/full/mac_arm64_gui/debug/ballisticakit": "38c04a3cc82a542a77abd1a2ba84fb09",
"build/prefab/full/mac_arm64_gui/release/ballisticakit": "032dac1e6928eccf15e34987809410ce",
"build/prefab/full/mac_arm64_server/debug/dist/ballisticakit_headless": "06f9dba113206423b65af606650e86df",
"build/prefab/full/mac_arm64_server/release/dist/ballisticakit_headless": "56b269b4a8eb027b1a72a8e746cd4295",
"build/prefab/full/mac_x86_64_gui/debug/ballisticakit": "aed20c525d81bc5314ca9f74a9c1980d",
"build/prefab/full/mac_x86_64_gui/release/ballisticakit": "fbaa1005e68f47aba22fc42805bf5386",
"build/prefab/full/mac_x86_64_server/debug/dist/ballisticakit_headless": "54245cd739f3979ad96ddc3734439c58",
"build/prefab/full/mac_x86_64_server/release/dist/ballisticakit_headless": "95e79c56f06b1a399be18eab8ac3b509",
"build/prefab/full/windows_x86_gui/debug/BallisticaKit.exe": "df0d3c8191f51a93fd9d00aecb9afc5d",
"build/prefab/full/windows_x86_gui/release/BallisticaKit.exe": "66c8e922af4ce1134c818e78f6622cf1",
"build/prefab/full/windows_x86_server/debug/dist/BallisticaKitHeadless.exe": "58db9e455e66fb99ac704dae92293cef",
"build/prefab/full/windows_x86_server/release/dist/BallisticaKitHeadless.exe": "a0b044381126cbc09e604b28cbc1b719",
"build/prefab/lib/linux_arm64_gui/debug/libballisticaplus.a": "e40234644df79316d71808a731ba14f0",
"build/prefab/lib/linux_arm64_gui/release/libballisticaplus.a": "8563857336ffcf40abfeac81fc3955eb",
"build/prefab/lib/linux_arm64_server/debug/libballisticaplus.a": "e40234644df79316d71808a731ba14f0",
"build/prefab/lib/linux_arm64_server/release/libballisticaplus.a": "8563857336ffcf40abfeac81fc3955eb",
"build/prefab/lib/linux_x86_64_gui/debug/libballisticaplus.a": "07033318a9b2b299128906c61342f391",
"build/prefab/lib/linux_x86_64_gui/release/libballisticaplus.a": "403c3f754e7d900b044a9f06658fffde",
"build/prefab/lib/linux_x86_64_server/debug/libballisticaplus.a": "07033318a9b2b299128906c61342f391",
"build/prefab/lib/linux_x86_64_server/release/libballisticaplus.a": "403c3f754e7d900b044a9f06658fffde",
"build/prefab/lib/mac_arm64_gui/debug/libballisticaplus.a": "5cbf318e235fc9218abd1df4bca567c4",
"build/prefab/lib/mac_arm64_gui/release/libballisticaplus.a": "4989c44cc8c9854a9a1dc6bf1b633796",
"build/prefab/lib/mac_arm64_server/debug/libballisticaplus.a": "5cbf318e235fc9218abd1df4bca567c4",
"build/prefab/lib/mac_arm64_server/release/libballisticaplus.a": "4989c44cc8c9854a9a1dc6bf1b633796",
"build/prefab/lib/mac_x86_64_gui/debug/libballisticaplus.a": "c40665a3e7f727e0fb7350e21bfd64aa",
"build/prefab/lib/mac_x86_64_gui/release/libballisticaplus.a": "9768548c6dfdff65fb25661540025ffe",
"build/prefab/lib/mac_x86_64_server/debug/libballisticaplus.a": "1a0cc7e78dc66dec6ee85a8a0d68aefd",
"build/prefab/lib/mac_x86_64_server/release/libballisticaplus.a": "9768548c6dfdff65fb25661540025ffe",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.lib": "9695c3f405ae104bad725dacd3c4af0d",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.pdb": "a7bab7a9e3ba04ddda4248cfe93f228d",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.lib": "0fe4f2f032833560d6f3c4f3d864751f",
"build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.pdb": "890e339df2411ab16f427d5a4c604a66",
"build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.lib": "4babfca1f30077fd9ee85e86aa807f05",
"build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.pdb": "dcabf9b8deee7f7fbe2cb23f29641c43",
"build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.lib": "2e705d8658c5d243be6211ea76392c74",
"build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.pdb": "219f144634bce88af3093803204d0366",
"src/assets/ba_data/python/babase/_mgen/__init__.py": "f885fed7f2ed98ff2ba271f9dbe3391c",
"src/assets/ba_data/python/babase/_mgen/enums.py": "b611c090513a21e2fe90e56582724e9d",
"src/ballistica/base/mgen/pyembed/binding_base.inc": "72bfed2cce8ff19741989dec28302f3f",

View File

@ -7,6 +7,8 @@ from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from efro.util import utc_now
import babase
import bascenev1
@ -522,10 +524,10 @@ class StoreSubsystem:
if item in sales_raw:
if not plus.get_purchased(item):
to_end = (
datetime.datetime.utcfromtimestamp(
sales_raw[item]['e']
datetime.datetime.fromtimestamp(
sales_raw[item]['e'], datetime.UTC
)
- datetime.datetime.utcnow()
- utc_now()
).total_seconds()
if to_end > 0:
sale_times.append(int(to_end * 1000))

View File

@ -6,6 +6,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from efro.util import utc_now
import bauiv1 as bui
if TYPE_CHECKING:
@ -541,24 +543,24 @@ class GetCurrencyWindow(bui.Window):
plus = bui.app.plus
assert plus is not None
# if we somehow get signed out, just die..
# If we somehow get signed out, just die.
if plus.get_v1_account_state() != 'signed_in':
self._back()
return
self._ticket_count = plus.get_v1_account_ticket_count()
# update our incentivized ad button depending on whether ads are
# available
# Update our incentivized ad button depending on whether ads are
# available.
if self._ad_button is not None:
next_reward_ad_time = plus.get_v1_account_misc_read_val_2(
'nextRewardAdTime', None
)
if next_reward_ad_time is not None:
next_reward_ad_time = datetime.datetime.utcfromtimestamp(
next_reward_ad_time
next_reward_ad_time = datetime.datetime.fromtimestamp(
next_reward_ad_time, datetime.UTC
)
now = datetime.datetime.utcnow()
now = utc_now()
if plus.have_incentivized_ad() and (
next_reward_ad_time is None or next_reward_ad_time <= now
):
@ -586,8 +588,8 @@ class GetCurrencyWindow(bui.Window):
sval = ''
bui.textwidget(edit=self._ad_time_text, text=sval)
# if this is our first update, assign immediately; otherwise kick
# off a smooth transition if the value has changed
# If this is our first update, assign immediately; otherwise kick
# off a smooth transition if the value has changed.
if self._smooth_ticket_count is None:
self._smooth_ticket_count = float(self._ticket_count)
self._smooth_update() # will set the text widget
@ -614,8 +616,8 @@ class GetCurrencyWindow(bui.Window):
plus = bui.app.plus
assert plus is not None
# if we're on a platform without purchases, inform the user they
# can link their accounts and buy stuff elsewhere
# If we're on a platform without purchases, inform the user they
# can link their accounts and buy stuff elsewhere.
app = bui.app
assert app.classic is not None
if (
@ -652,8 +654,10 @@ class GetCurrencyWindow(bui.Window):
return
appinvite.handle_app_invites_press()
return
# here we ping the server to ask if it's valid for us to
# purchase this.. (better to fail now than after we've paid locally)
# Here we ping the server to ask if it's valid for us to
# purchase this.. (better to fail now than after we've paid
# locally).
app = bui.app
assert app.classic is not None
bui.app.classic.master_server_v1_get(
@ -694,7 +698,7 @@ class GetCurrencyWindow(bui.Window):
color=(1, 0, 0),
)
# actually start the purchase locally..
# Actually start the purchase locally.
def _do_purchase(self, item: str) -> None:
plus = bui.app.plus
assert plus is not None
@ -702,15 +706,15 @@ class GetCurrencyWindow(bui.Window):
if item == 'ad':
import datetime
# if ads are disabled until some time, error..
# If ads are disabled until some time, error.
next_reward_ad_time = plus.get_v1_account_misc_read_val_2(
'nextRewardAdTime', None
)
if next_reward_ad_time is not None:
next_reward_ad_time = datetime.datetime.utcfromtimestamp(
next_reward_ad_time
next_reward_ad_time = datetime.datetime.fromtimestamp(
next_reward_ad_time, datetime.UTC
)
now = datetime.datetime.utcnow()
now = utc_now()
if (
next_reward_ad_time is not None and next_reward_ad_time > now
) or self._ad_button_greyed:
@ -730,7 +734,7 @@ class GetCurrencyWindow(bui.Window):
def _back(self) -> None:
from bauiv1lib.store import browser
# no-op if our underlying widget is dead or on its way out.
# No-op if our underlying widget is dead or on its way out.
if not self._root_widget or self._root_widget.transitioning_out:
return

View File

@ -15,6 +15,7 @@ from enum import Enum
from threading import Thread
from typing import TYPE_CHECKING
from efro.util import utc_now
from efro.error import CommunicationError
import bacommon.cloud
import bauiv1 as bui
@ -681,8 +682,10 @@ class StoreBrowserWindow(bui.Window):
# Look at the current set of sales; filter any with time remaining.
for sale_item, sale_info in list(sales_raw.items()):
to_end = (
datetime.datetime.utcfromtimestamp(sale_info['e'])
- datetime.datetime.utcnow()
datetime.datetime.fromtimestamp(
sale_info['e'], datetime.UTC
)
- utc_now()
).total_seconds()
if to_end > 0:
sales[sale_item] = {

View File

@ -6,6 +6,8 @@ from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from efro.util import utc_now
import bauiv1 as bui
if TYPE_CHECKING:
@ -278,8 +280,10 @@ class StoreButton:
for sale_item, sale_info in list(sales_raw.items()):
if not plus.get_purchased(sale_item):
to_end = (
datetime.datetime.utcfromtimestamp(sale_info['e'])
- datetime.datetime.utcnow()
datetime.datetime.fromtimestamp(
sale_info['e'], datetime.UTC
)
- utc_now()
).total_seconds()
if to_end > 0:
sale_times.append(to_end)

View File

@ -20,7 +20,7 @@ from typing import (
)
import pytest
from efro.util import utc_now
from efro.util import utc_now, utc_now_naive
from efro.dataclassio import (
dataclass_validate,
dataclass_from_dict,
@ -69,8 +69,6 @@ class _NestedClass:
dval: dict[int, str] = field(default_factory=dict)
# We use utc_now() for a test which is deprecated.
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_assign() -> None:
"""Testing various assignments."""
@ -292,8 +290,8 @@ def test_assign() -> None:
with pytest.raises(ValueError):
dataclass_to_dict(_TestClass(datetimeval=datetime.datetime.now()))
with pytest.raises(ValueError):
# This doesn't actually set timezone on the datetime obj.
dataclass_to_dict(_TestClass(datetimeval=datetime.datetime.utcnow()))
# This doesn't have a timezone on the datetime obj.
dataclass_to_dict(_TestClass(datetimeval=utc_now_naive()))
def test_coerce() -> None:

View File

@ -50,10 +50,15 @@ class StateData:
def get_tz_offset_seconds() -> float:
"""Return the offset between utc and local time in seconds."""
tval = time.time()
# Compare naive current and utc times to get our offset from utc.
utc_offset = (
datetime.datetime.fromtimestamp(tval)
- datetime.datetime.utcfromtimestamp(tval)
- datetime.datetime.fromtimestamp(tval, datetime.UTC).replace(
tzinfo=None
)
).total_seconds()
return utc_offset

View File

@ -109,19 +109,28 @@ def check_utc(value: datetime.datetime) -> None:
def utc_now() -> datetime.datetime:
"""Get offset-aware current utc time.
"""Get timezone-aware current utc time.
This should be used for all datetimes getting sent over the network,
used with the entity system, etc.
(datetime.utcnow() gives a utc time value, but it is not timezone-aware
which makes it less safe to use)
Just a shortcut for datetime.datetime.now(datetime.UTC).
Avoid datetime.datetime.utcnow() which is deprecated and gives naive
times.
"""
return datetime.datetime.now(datetime.timezone.utc)
return datetime.datetime.now(datetime.UTC)
def utc_now_naive() -> datetime.datetime:
"""Get naive utc time.
This can be used to replace datetime.utcnow(), which is now deprecated.
Most all code should migrate to use timezone-aware times instead of
this.
"""
return datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
def utc_today() -> datetime.datetime:
"""Get offset-aware midnight in the utc time zone."""
now = datetime.datetime.now(datetime.timezone.utc)
now = datetime.datetime.now(datetime.UTC)
return datetime.datetime(
year=now.year, month=now.month, day=now.day, tzinfo=now.tzinfo
)
@ -129,7 +138,7 @@ def utc_today() -> datetime.datetime:
def utc_this_hour() -> datetime.datetime:
"""Get offset-aware beginning of the current hour in the utc time zone."""
now = datetime.datetime.now(datetime.timezone.utc)
now = datetime.datetime.now(datetime.UTC)
return datetime.datetime(
year=now.year,
month=now.month,
@ -141,7 +150,7 @@ def utc_this_hour() -> datetime.datetime:
def utc_this_minute() -> datetime.datetime:
"""Get offset-aware beginning of current minute in the utc time zone."""
now = datetime.datetime.now(datetime.timezone.utc)
now = datetime.datetime.now(datetime.UTC)
return datetime.datetime(
year=now.year,
month=now.month,