mirror of
https://github.com/apache/superset.git
synced 2026-07-13 18:25:58 +00:00
Python 3.12 deprecated datetime.utcnow() and datetime.utcfromtimestamp(). This sweeps all production occurrences under superset/ (8 files) and replaces them with behavior-preserving equivalents that keep the existing naive-UTC semantics: datetime.utcnow() -> datetime.now(timezone.utc).replace(tzinfo=None) datetime.utcfromtimestamp(x) -> datetime.fromtimestamp(x, timezone.utc).replace(tzinfo=None) Keeping the values naive (rather than switching to aware datetimes) avoids naive/aware comparison errors against timestamps already stored naive in the metadata DB (e.g. the Log.dttm prune query on PostgreSQL) and keeps isoformat()/cache-key output byte-identical. This is the key difference from the earlier #37538, which switched to aware datetimes. superset/security/session_invalidation.py already uses now(timezone.utc) and is left unchanged. Test files still use the deprecated calls and can be migrated in a focused follow-up. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
# Licensed to the Apache Software Foundation (ASF) under one
|
|
# or more contributor license agreements. See the NOTICE file
|
|
# distributed with this work for additional information
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
# to you under the Apache License, Version 2.0 (the
|
|
# "License"); you may not use this file except in compliance
|
|
# with the License. You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing,
|
|
# software distributed under the License is distributed on an
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
# KIND, either express or implied. See the License for the
|
|
# specific language governing permissions and limitations
|
|
# under the License.
|
|
from datetime import datetime, timezone
|
|
|
|
import pytz
|
|
|
|
EPOCH = datetime(1970, 1, 1)
|
|
|
|
|
|
def datetime_to_epoch(dttm: datetime) -> float:
|
|
"""Convert datetime to milliseconds to epoch"""
|
|
if dttm.tzinfo:
|
|
dttm = dttm.astimezone(pytz.utc)
|
|
epoch_with_tz = pytz.utc.localize(EPOCH)
|
|
return (dttm - epoch_with_tz).total_seconds() * 1000
|
|
return (dttm - EPOCH).total_seconds() * 1000
|
|
|
|
|
|
def now_as_float() -> float:
|
|
return datetime_to_epoch(datetime.now(timezone.utc).replace(tzinfo=None))
|