From d6fcc89da3d8a74a109d2cade06c5b67c368faf6 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 4 May 2025 17:03:39 +0200 Subject: [PATCH] Explicitly convert date and datetime to QDate and QDateTime (#2325) --- novelwriter/config.py | 13 +++++++++---- tests/test_base/test_base_config.py | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/novelwriter/config.py b/novelwriter/config.py index 78a4b843..35690669 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -34,8 +34,8 @@ from time import time from typing import TYPE_CHECKING, Final from PyQt6.QtCore import ( - PYQT_VERSION, PYQT_VERSION_STR, QT_VERSION, QT_VERSION_STR, QLibraryInfo, - QLocale, QStandardPaths, QSysInfo, QTranslator + PYQT_VERSION, PYQT_VERSION_STR, QT_VERSION, QT_VERSION_STR, QDate, + QDateTime, QLibraryInfo, QLocale, QStandardPaths, QSysInfo, QTranslator ) from PyQt6.QtGui import QFont, QFontDatabase, QFontMetrics from PyQt6.QtWidgets import QApplication @@ -467,11 +467,16 @@ class Config: def localDate(self, value: datetime) -> str: """Return a localised date format.""" - return self._dLocale.toString(value, self._dShortDate) + # Explicitly convert the date first, see bug #2325 + return self._dLocale.toString(QDate(value.year, value.month, value.day), self._dShortDate) def localDateTime(self, value: datetime) -> str: """Return a localised datetime format.""" - return self._dLocale.toString(value, self._dShortDateTime) + # Explicitly convert the datetime first, see bug #2325 + return self._dLocale.toString( + QDateTime(value.year, value.month, value.day, value.hour, value.minute, value.second), + self._dShortDateTime, + ) def listLanguages(self, lngSet: int) -> list[tuple[str, str]]: """List localisation files in the i18n folder. The default GUI diff --git a/tests/test_base/test_base_config.py b/tests/test_base/test_base_config.py index 74468629..be805df0 100644 --- a/tests/test_base/test_base_config.py +++ b/tests/test_base/test_base_config.py @@ -20,6 +20,7 @@ along with this program. If not, see . """ from __future__ import annotations +import datetime import json import sys @@ -189,6 +190,12 @@ def testBaseConfig_Localisation(fncPath, tstPaths): languages = tstConf.listLanguages(tstConf.LANG_NW) assert languages == [("en_GB", "British English"), ("fr", "Français")] + # Date Formats + # Checks for bug #2325 + assert CONFIG._dLocale.bcp47Name() == "en-GB" + assert CONFIG.localDate(datetime.datetime.fromtimestamp(1746370775)) == "04/05/2025" + assert CONFIG.localDateTime(datetime.datetime.fromtimestamp(1746370775)) == "04/05/2025 16:59" + @pytest.mark.base def testBaseConfig_Methods(fncPath):