Improved date time formatting for file access time

This commit is contained in:
Yousef Khadadeh 2026-04-20 20:16:15 +03:00
parent 032db7f444
commit 3b9b9c0cf9
4 changed files with 60 additions and 24 deletions

View file

@ -1,8 +1,10 @@
#include "StringHelpers.hpp"
#include <SDL3/SDL_time.h>
#include <SDL3/SDL_timer.h>
#include <chrono>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <regex>
#include <SDL3/SDL_iostream.h>
@ -117,19 +119,46 @@ std::string ensure_string_unique(const std::vector<std::string>& 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(&currentTime);
SDL_TimeToDateTime(currentTime, &currentDt, 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 "";
}

View file

@ -4,7 +4,6 @@
#include <cstdint>
#include <filesystem>
#include <SDL3/SDL_time.h>
#include <chrono>
std::vector<std::string> 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<std::string>& 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);

View file

@ -7,6 +7,7 @@
#include "../GUIStuff/Elements/ImageDisplay.hpp"
#include "../World.hpp"
#include "Helpers/StringHelpers.hpp"
#include <SDL3/SDL_timer.h>
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::FileInfo>& 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);
}
}
}

View file

@ -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<FileInfo>& get_file_list();
std::optional<std::vector<FileInfo>> fileListOptional;