From 659863098426f27cd428dcbb17b88ae97324832c Mon Sep 17 00:00:00 2001 From: Jann Stute <46534683+Computerdores@users.noreply.github.com> Date: Sun, 4 May 2025 20:49:52 +0200 Subject: [PATCH] feat: datetime fields settings integration (#926) * refactor: move datetime formatting to global settings * feat: datetime fields settings integration --- src/tagstudio/core/global_settings.py | 20 +++++++++++++ .../qt/widgets/preview/field_containers.py | 5 ++-- .../qt/widgets/preview/file_attributes.py | 28 ++++--------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/tagstudio/core/global_settings.py b/src/tagstudio/core/global_settings.py index a2e1b1c2..4c95ec77 100644 --- a/src/tagstudio/core/global_settings.py +++ b/src/tagstudio/core/global_settings.py @@ -2,6 +2,7 @@ # Created for TagStudio: https://github.com/CyanVoxel/TagStudio import platform +from datetime import datetime from enum import Enum from pathlib import Path from typing import override @@ -49,6 +50,7 @@ class GlobalSettings(BaseModel): page_size: int = Field(default=100) show_filepath: ShowFilepathOption = Field(default=ShowFilepathOption.DEFAULT) theme: Theme = Field(default=Theme.SYSTEM) + date_format: str = Field(default="%x") hour_format: bool = Field(default=True) zero_padding: bool = Field(default=True) @@ -72,3 +74,21 @@ class GlobalSettings(BaseModel): with open(path, "w") as f: toml.dump(dict(self), f, encoder=TomlEnumEncoder()) + + def format_datetime(self, dt: datetime) -> str: + date_format = self.date_format + is_24h = self.hour_format + hour_format = "%H:%M:%S" if is_24h else "%I:%M:%S %p" + zero_padding = self.zero_padding + zero_padding_symbol = "" + + if not zero_padding: + zero_padding_symbol = "#" if platform.system() == "Windows" else "-" + date_format = date_format.replace("%d", f"%{zero_padding_symbol}d").replace( + "%m", f"%{zero_padding_symbol}m" + ) + hour_format = hour_format.replace("%H", f"%{zero_padding_symbol}H").replace( + "%I", f"%{zero_padding_symbol}I" + ) + + return datetime.strftime(dt, f"{date_format}, {hour_format}") diff --git a/src/tagstudio/qt/widgets/preview/field_containers.py b/src/tagstudio/qt/widgets/preview/field_containers.py index b29fe580..bb366808 100644 --- a/src/tagstudio/qt/widgets/preview/field_containers.py +++ b/src/tagstudio/qt/widgets/preview/field_containers.py @@ -392,8 +392,9 @@ class FieldContainers(QWidget): container.set_inline(False) try: title = f"{field.type.name} (Date)" - # TODO: Localize this and/or add preferences. - text = dt.strptime(field.value or "", "%Y-%m-%d %H:%M:%S").strftime("%D - %r") + text = self.driver.settings.format_datetime( + dt.strptime(field.value or "", "%Y-%m-%d %H:%M:%S") + ) except ValueError: title = f"{field.type.name} (Date) (Unknown Format)" text = str(field.value) diff --git a/src/tagstudio/qt/widgets/preview/file_attributes.py b/src/tagstudio/qt/widgets/preview/file_attributes.py index 49098d5f..2ca9010a 100644 --- a/src/tagstudio/qt/widgets/preview/file_attributes.py +++ b/src/tagstudio/qt/widgets/preview/file_attributes.py @@ -102,18 +102,19 @@ class FileAttributes(QWidget): def update_date_label(self, filepath: Path | None = None) -> None: """Update the "Date Created" and "Date Modified" file property labels.""" if filepath and filepath.is_file(): - created: dt = None + created: dt if platform.system() == "Windows" or platform.system() == "Darwin": created = dt.fromtimestamp(filepath.stat().st_birthtime) # type: ignore[attr-defined, unused-ignore] else: created = dt.fromtimestamp(filepath.stat().st_ctime) modified: dt = dt.fromtimestamp(filepath.stat().st_mtime) self.date_created_label.setText( - f"{Translations['file.date_created']}: {self.get_date_with_format(created)}" + f"{Translations['file.date_created']}:" + + f" {self.driver.settings.format_datetime(created)}" ) self.date_modified_label.setText( f"{Translations['file.date_modified']}: " - f"{self.get_date_with_format(modified)}" + f"{self.driver.settings.format_datetime(modified)}" ) self.date_created_label.setHidden(False) self.date_modified_label.setHidden(False) @@ -130,7 +131,7 @@ class FileAttributes(QWidget): self.date_created_label.setHidden(True) self.date_modified_label.setHidden(True) - def update_stats(self, filepath: Path | None = None, ext: str = ".", stats: dict = None): + def update_stats(self, filepath: Path | None = None, ext: str = ".", stats: dict | None = None): """Render the panel widgets with the newest data from the Library.""" if not stats: stats = {} @@ -149,6 +150,7 @@ class FileAttributes(QWidget): if self.driver.settings.show_filepath == ShowFilepathOption.SHOW_FULL_PATHS: display_path = filepath elif self.driver.settings.show_filepath == ShowFilepathOption.SHOW_RELATIVE_PATHS: + assert self.library_path is not None display_path = Path(filepath).relative_to(self.library_path) elif self.driver.settings.show_filepath == ShowFilepathOption.SHOW_FILENAMES_ONLY: display_path = Path(filepath.name) @@ -246,21 +248,3 @@ class FileAttributes(QWidget): self.file_label.set_file_path("") self.dimensions_label.setText("") self.dimensions_label.setHidden(True) - - def get_date_with_format(self, date: dt) -> str: - date_format = self.driver.settings.date_format - is_24h = self.driver.settings.hour_format - hour_format = "%H:%M:%S" if is_24h else "%I:%M:%S %p" - zero_padding = self.driver.settings.zero_padding - zero_padding_symbol = "" - - if not zero_padding: - zero_padding_symbol = "#" if platform.system() == "Windows" else "-" - date_format = date_format.replace("%d", f"%{zero_padding_symbol}d").replace( - "%m", f"%{zero_padding_symbol}m" - ) - hour_format = hour_format.replace("%H", f"%{zero_padding_symbol}H").replace( - "%I", f"%{zero_padding_symbol}I" - ) - - return dt.strftime(date, f"{date_format}, {hour_format}")