From 3b9b9c0cf9104c80feb73a2fa0c08fabebc2a325 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Mon, 20 Apr 2026 20:16:15 +0300 Subject: [PATCH] Improved date time formatting for file access time --- include/Helpers/StringHelpers.cpp | 55 +++++++++++++++++++++++-------- include/Helpers/StringHelpers.hpp | 4 +-- src/Screens/FileSelectScreen.cpp | 21 ++++++++---- src/Screens/FileSelectScreen.hpp | 4 +-- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/include/Helpers/StringHelpers.cpp b/include/Helpers/StringHelpers.cpp index e15a5c7..b99114e 100644 --- a/include/Helpers/StringHelpers.cpp +++ b/include/Helpers/StringHelpers.cpp @@ -1,8 +1,10 @@ #include "StringHelpers.hpp" +#include #include #include #include #include +#include #include #include @@ -117,19 +119,46 @@ std::string ensure_string_unique(const std::vector& stringList, std return str; } -std::chrono::system_clock::time_point sdl_time_to_chrono_time(const SDL_Time& t) { - time_t unixTime = SDL_NS_TO_SECONDS(t); - return std::chrono::system_clock::from_time_t(unixTime); -} - -std::string chrono_time_to_nice_date(const std::chrono::system_clock::time_point& t, SDL_DateFormat f) { - switch(f) { - case SDL_DATE_FORMAT_YYYYMMDD: - return std::format("{:%Y-%m-%d}", t); - case SDL_DATE_FORMAT_DDMMYYYY: - return std::format("{:%d %b, %Y}", t); - case SDL_DATE_FORMAT_MMDDYYYY: - return std::format("{:%b %d, %Y}", t); +std::string sdl_time_to_nice_access_time(const SDL_DateTime& t, SDL_DateFormat dF, SDL_TimeFormat tF) { + SDL_DateTime currentDt; + { + SDL_Time currentTime; + SDL_GetCurrentTime(¤tTime); + SDL_TimeToDateTime(currentTime, ¤tDt, true); + } + if(currentDt.year == t.year && currentDt.month == t.month && currentDt.day == t.day) { + switch(tF) { + case SDL_TIME_FORMAT_24HR: + return std::format("{0}:{1:0>2}", t.hour, t.minute); + case SDL_TIME_FORMAT_12HR: { + std::string pmAm = t.hour >= 12 ? "PM" : "AM"; + int displayedHour; + if(t.hour == 0) + displayedHour = 12; + else if(t.hour > 12) + displayedHour = t.hour - 12; + else + displayedHour = t.hour; + return std::format("{0}:{1:0>2} {2}", displayedHour, t.minute, pmAm); + } + } + } + else { + switch(dF) { + case SDL_DATE_FORMAT_YYYYMMDD: + return std::format("{0}-{1}-{2}", t.year, t.month, t.day); + case SDL_DATE_FORMAT_DDMMYYYY: + if(currentDt.year == t.year) + return std::format("{0} {1}", t.day, std::chrono::month(t.month)); + else + return std::format("{0} {1}, {2}", t.day, std::chrono::month(t.month), t.year); + case SDL_DATE_FORMAT_MMDDYYYY: { + if(currentDt.year == t.year) + return std::format("{1} {0}", t.day, std::chrono::month(t.month)); + else + return std::format("{1} {0}, {2}", t.day, std::chrono::month(t.month), t.year); + } + } } return ""; } diff --git a/include/Helpers/StringHelpers.hpp b/include/Helpers/StringHelpers.hpp index d2e0b66..b0552c6 100644 --- a/include/Helpers/StringHelpers.hpp +++ b/include/Helpers/StringHelpers.hpp @@ -4,7 +4,6 @@ #include #include #include -#include std::vector split_string_by_token(std::string str, std::string token); @@ -15,5 +14,4 @@ std::string read_file_to_string(const std::filesystem::path& filePath); bool is_valid_http_url(const std::string& str); std::string remove_carriage_returns_from_str(std::string s); std::string ensure_string_unique(const std::vector& stringList, std::string str); -std::chrono::system_clock::time_point sdl_time_to_chrono_time(const SDL_Time& t); -std::string chrono_time_to_nice_date(const std::chrono::system_clock::time_point& t, SDL_DateFormat f); +std::string sdl_time_to_nice_access_time(const SDL_DateTime& t, SDL_DateFormat dF, SDL_TimeFormat tF); diff --git a/src/Screens/FileSelectScreen.cpp b/src/Screens/FileSelectScreen.cpp index b411dac..395eb24 100644 --- a/src/Screens/FileSelectScreen.cpp +++ b/src/Screens/FileSelectScreen.cpp @@ -7,6 +7,7 @@ #include "../GUIStuff/Elements/ImageDisplay.hpp" #include "../World.hpp" #include "Helpers/StringHelpers.hpp" +#include FileSelectScreen::FileSelectScreen(MainProgram& m): Screen(m) {} @@ -51,7 +52,7 @@ void FileSelectScreen::gui_layout_run() { }); } text_label(gui, fileList[i].fileName); - text_label_light(gui, fileList[i].lastAccessDate); + text_label_light(gui, fileList[i].lastModifyDate); } } }); @@ -70,12 +71,20 @@ const std::vector& FileSelectScreen::get_file_list() std::filesystem::path p = filesInPath[i]; std::string fExt = p.extension().string(); if(fExt == "." + World::FILE_EXTENSION) { + std::filesystem::path fullPath = savePath / filesInPath[i]; + FileInfo fileInfoToAdd; + fileInfoToAdd.fileName = p.stem().string(); + SDL_PathInfo pathInfo; - SDL_DateTime pathDt; - SDL_GetPathInfo(savePath.string().c_str(), &pathInfo); - SDL_TimeToDateTime(pathInfo.access_time, &pathDt, true); - SDL_NS_TO_SECONDS(pathInfo.access_time); - fL.emplace_back(p.stem().string(), pathInfo.access_time, chrono_time_to_nice_date(sdl_time_to_chrono_time(pathInfo.access_time), main.conf.dateFormat)); + if(SDL_GetPathInfo(fullPath.string().c_str(), &pathInfo)) { + SDL_DateTime pathDt; + fileInfoToAdd.lastModifyTime = pathInfo.modify_time; + if(SDL_TimeToDateTime(pathInfo.modify_time, &pathDt, true)) { + fileInfoToAdd.lastModifyDate = sdl_time_to_nice_access_time(pathDt, main.conf.dateFormat, main.conf.timeFormat); + } + } + + fL.emplace_back(fileInfoToAdd); } } } diff --git a/src/Screens/FileSelectScreen.hpp b/src/Screens/FileSelectScreen.hpp index a2e757c..c8bbbb4 100644 --- a/src/Screens/FileSelectScreen.hpp +++ b/src/Screens/FileSelectScreen.hpp @@ -9,8 +9,8 @@ class FileSelectScreen : public Screen { private: struct FileInfo { std::string fileName; - SDL_Time lastAccessTime; - std::string lastAccessDate; + SDL_Time lastModifyTime; + std::string lastModifyDate; }; const std::vector& get_file_list(); std::optional> fileListOptional;