Skip to content

Commit

Permalink
Merge pull request #1677 from ThomasWaldmann/utcfromtimestamp
Browse files Browse the repository at this point in the history
work around some datetime deprecation warnings
  • Loading branch information
ThomasWaldmann authored Apr 9, 2024
2 parents a2b0abd + 8ef6231 commit 735f051
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 31 deletions.
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
repos:
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
files: '(src|_ui_tests|contrib|scripts|quickinstall.py)'
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: ruff
4 changes: 2 additions & 2 deletions contrib/loadtesting/locust/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import argparse
import urllib.request, urllib.error, urllib.parse
from datetime import datetime
from datetime import datetime, timezone
from time import time

from locust import HttpLocust, Locust, TaskSet, HttpUser, task, SequentialTaskSet, between, User, events
Expand Down Expand Up @@ -360,4 +360,4 @@ def logout(self):
print("%s: response.status_code = %s" % (sys._getframe().f_lineno, response.status_code))

def get_time(self):
return datetime.utcfromtimestamp(time()).isoformat()[:-7].replace("T", " ")
return datetime.fromtimestamp(time(), tz=timezone.utc).isoformat()[:19].replace("T", " ")
10 changes: 5 additions & 5 deletions src/moin/apps/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
from moin.constants.itemtypes import ITEMTYPE_DEFAULT, ITEMTYPE_TICKET
from moin.constants.contenttypes import * # noqa
from moin.constants.rights import SUPERUSER
from moin.utils import crypto, rev_navigation, close_file, show_time
from moin.utils import crypto, rev_navigation, close_file, show_time, utcfromtimestamp
from moin.utils.crypto import make_uuid, hash_hexdigest
from moin.utils.interwiki import url_for_item, split_fqname, CompositeName
from moin.utils.mime import Type, type_moin_document
Expand Down Expand Up @@ -1159,9 +1159,9 @@ def log_destroy_action(item, subitem_names, comment, revision=None):
(" Old Name", item.meta[NAME_OLD]),
(" Subitem Names", subitem_names),
(" Namespace", item.meta[NAMESPACE]),
(" Last Modified Time", format_datetime(datetime.utcfromtimestamp(item.meta[MTIME]))),
(" Last Modified Time", format_datetime(utcfromtimestamp(item.meta[MTIME]))),
(" Last Modified By", item.meta[ADDRESS]),
(" Destroyed Time", format_datetime(datetime.utcfromtimestamp(time.time()))),
(" Destroyed Time", format_datetime(utcfromtimestamp(time.time()))),
(" Destroyed By", flaskg.user.name),
(" Content Type", item.meta[CONTENTTYPE]),
(" Revision Number", item.meta[REV_NUMBER]),
Expand Down Expand Up @@ -1633,7 +1633,7 @@ def history(item_name):
terms = [Term(WIKINAME, app.cfg.interwikiname)]
terms.extend(Term(term, value) for term, value in fqname.query.items())
if bookmark_time:
terms.append(DateRange(MTIME, start=datetime.utcfromtimestamp(bookmark_time), end=None))
terms.append(DateRange(MTIME, start=utcfromtimestamp(bookmark_time), end=None))
query = And(terms)

if results_per_page:
Expand Down Expand Up @@ -1722,7 +1722,7 @@ def global_history(namespace):
terms.append(Not(Term(NAMESPACE, NAMESPACE_USERPROFILES)))
bookmark_time = flaskg.user.bookmark
if bookmark_time is not None:
terms.append(DateRange(MTIME, start=datetime.utcfromtimestamp(bookmark_time), end=None))
terms.append(DateRange(MTIME, start=utcfromtimestamp(bookmark_time), end=None))
query = And(terms)

if results_per_page:
Expand Down
3 changes: 2 additions & 1 deletion src/moin/converters/archive_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ._table import TableMixin

from moin.i18n import _
from moin.utils import utcfromtimestamp
from moin.utils.iri import Iri
from moin.utils.tree import moin_page, xlink
from moin.utils.mime import Type, type_moin_document
Expand Down Expand Up @@ -98,7 +99,7 @@ def list_contents(self, fileobj):
for tinfo in tf.getmembers():
if tinfo.isfile():
# display only normal files, not directories
rows.append((tinfo.size, datetime.utcfromtimestamp(tinfo.mtime), tinfo.name))
rows.append((tinfo.size, utcfromtimestamp(tinfo.mtime), tinfo.name))
return rows
except tarfile.TarError as err:
raise ArchiveException(str(err))
Expand Down
5 changes: 3 additions & 2 deletions src/moin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from moin.constants.keys import ITEMID, NAME, LATEST_REVS, NAMESPACE, FQNAME
from moin.constants.namespaces import NAMESPACES_IDENTIFIER
from moin.i18n import _, L_
from moin.utils import utcfromtimestamp
from moin.utils.forms import FileStorage
from moin.storage.middleware.validation import uuid_validator

Expand Down Expand Up @@ -363,7 +364,7 @@ def serialize(self, value):
"""Serializes value to string."""
if isinstance(value, int):
try:
value = datetime.datetime.utcfromtimestamp(value)
value = utcfromtimestamp(value)
except ValueError:
pass
return super().serialize(value)
Expand All @@ -377,7 +378,7 @@ def adapt(self, value):
if isinstance(value, int):
try:
# check if a value is a correct timestamp
dt = datetime.datetime.utcfromtimestamp(value)
dt = utcfromtimestamp(value)
return value
except (ValueError, OSError): # OSError errno 75 "Value too large for defined data type"
raise AdaptationError()
Expand Down
8 changes: 4 additions & 4 deletions src/moin/macros/_tests/test_Date.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"""

import time
from datetime import datetime

import pytest
from flask import g as flaskg

from moin.macros.Date import MacroDateTimeBase, Macro
from moin.utils import utcfromtimestamp
from moin.utils.show_time import format_date_time, format_date


Expand All @@ -24,12 +24,12 @@ def test_parse_time(self):
expected = 1691386691.0
assert ts == expected

result = format_date_time(datetime.utcfromtimestamp(ts))
result = format_date_time(utcfromtimestamp(ts))
expected = "2023-08-07 05:38:11z"
assert result == expected

flaskg.user.valid = True # show_time creates ISO 8601 dates if user is not logged in
result = format_date_time(datetime.utcfromtimestamp(ts))
result = format_date_time(utcfromtimestamp(ts))
expected = ["Aug 7, 2023, 5:38:11\u202fAM", "Aug 7, 2023, 5:38:11 AM"] # TODO: remove 2nd entry later
assert result in expected

Expand All @@ -49,7 +49,7 @@ def test_macro(self):
# when arguments is None
result = macro_obj.macro("content", None, "page_url", "alternative")
test_time = time.time()
test_time = format_date(datetime.utcfromtimestamp(test_time))
test_time = format_date(utcfromtimestamp(test_time))
assert result == test_time

arguments = ["2023-08-07T11:11:11+0533", "argument2"]
Expand Down
4 changes: 2 additions & 2 deletions src/moin/macros/_tests/test_DateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"""

import time
from datetime import datetime

from flask import g as flaskg

import pytest

from moin.macros.DateTime import Macro
from moin.utils import utcfromtimestamp
from moin.utils.show_time import format_date_time


Expand All @@ -26,7 +26,7 @@ def test_Macro():
# get the current time
test_time = time.time()
test_times = [test_time, test_time - 1] # in case our call to time.time happened just after the second rolled over
test_times = [format_date_time(datetime.utcfromtimestamp(t)) for t in test_times]
test_times = [format_date_time(utcfromtimestamp(t)) for t in test_times]
assert result in test_times

arguments = ["2023-08-07T11:11:11", "argument2"]
Expand Down
5 changes: 2 additions & 3 deletions src/moin/storage/middleware/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import os
import sys
import shutil
import datetime
import time

from collections.abc import Mapping
Expand All @@ -76,8 +75,8 @@
from moin.themes import utctimestamp
from moin.storage.middleware.validation import ContentMetaSchema, UserMetaSchema, validate_data
from moin.storage.error import NoSuchItemError, ItemAlreadyExistsError
from moin.utils import utcfromtimestamp
from moin.utils.interwiki import split_fqname, CompositeName

from moin.utils.mime import Type, type_moin_document
from moin.utils.tree import moin_page
from moin.converters import default_registry
Expand Down Expand Up @@ -173,7 +172,7 @@ def backend_to_index(meta, content, schema, wikiname, backend_name):
for key in [MTIME, PTIME]:
if key in doc:
# we have UNIX UTC timestamp (int), whoosh wants datetime
doc[key] = datetime.datetime.utcfromtimestamp(doc[key])
doc[key] = utcfromtimestamp(doc[key])
doc[NAME_EXACT] = doc[NAME]
doc[WIKINAME] = wikiname
doc[CONTENT] = content
Expand Down
11 changes: 11 additions & 0 deletions src/moin/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""


from datetime import datetime, timezone
import re
import pickle
from io import BytesIO
Expand Down Expand Up @@ -86,3 +87,13 @@ def close_file(f):
# some tests reuse BytesIO objects and will fail with I/O operation on closed file.
if hasattr(f, "close") and not f.closed and not isinstance(f, BytesIO):
f.close()


def utcfromtimestamp(timestamp):
"""Returns a naive datetime instance representing the timestamp in the UTC timezone"""
return datetime.fromtimestamp(timestamp, timezone.utc).replace(tzinfo=None)


def utcnow():
"""Returns a naive datetime instance representing the current time in the UTC timezone"""
return datetime.now(timezone.utc).replace(tzinfo=None)
6 changes: 3 additions & 3 deletions src/moin/utils/show_time.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Copyright: 2019 MoinMoin:RogerHaase
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

import datetime
import pytz

from flask import g as flaskg
import flask_babel

from moin.i18n import _
from moin import i18n
from moin.utils import utcfromtimestamp, utcnow


def duration(seconds):
Expand Down Expand Up @@ -52,9 +52,9 @@ def format_date_time(utc_dt=None, fmt="yyyy-MM-dd HH:mm:ss", interval="datetime"
See https://babel.pocoo.org/en/latest/dates.html#date-fields for babel format syntax.
"""
if utc_dt is None:
utc_dt = datetime.datetime.utcnow()
utc_dt = utcnow()
elif isinstance(utc_dt, (float, int)):
utc_dt = datetime.datetime.utcfromtimestamp(utc_dt)
utc_dt = utcfromtimestamp(utc_dt)

if not flaskg.user.valid:
# users who are not logged-in get moin version of ISO 8601: 2019-07-15 07:08:09z
Expand Down

0 comments on commit 735f051

Please sign in to comment.