I want to retrieve a given file's last-modified date and time, and format it as a string. I want to do so in a way that is thread- and memory-safe; cross-platform compatible; and does not require the use of custom libraries. The date and time should be formatted according to the user's preferred locale and date/time format, as returned by the operating system.
Using C++11/14 features is fine. So is using Boost. Speed of execution does not particularly matter.
Here is what I have at the moment:
std::string text;
text += filename;
text = "Savegame: "+text+lf+"_________________"+lf;
{
text += "Saved on: ";
const boost::filesystem::path file_name_path{filename};
const boost::filesystem::path save_dir_path{getSaveDir()};
const boost::filesystem::path& full_file_path = boost::filesystem::absolute(file_name_path, save_dir_path);
std::time_t last_saved_time{};
std::stringstream last_saved_string_stream{};
last_saved_string_stream.exceptions(std::ios_base::failbit);
try
{
std::locale users_preferred_locale{std::locale("")};
boost::gregorian::date_facet output_facet{};
last_saved_string_stream.imbue(users_preferred_locale);
last_saved_string_stream.imbue(std::locale(last_saved_string_stream.getloc(), &output_facet));
output_facet.format("%x");
last_saved_time = boost::filesystem::last_write_time(full_file_path);
auto last_saved_time_point{boost::chrono::system_clock::from_time_t(last_saved_time)};
last_saved_string_stream << last_saved_time_point;
}
catch (boost::filesystem::filesystem_error& fse)
{
VS_LOG_AND_FLUSH(fatal, (boost::format("Filesystem Error determining savegame's last saved date/time: %1%") % fse.what()));
}
catch (std::exception& e)
{
VS_LOG_AND_FLUSH(fatal, (boost::format("General Error determining savegame's last saved date/time: %1%") % e.what()));
}
catch (...)
{
VS_LOG_AND_FLUSH(fatal, "Really bad error determining savegame's last saved date/time! What just happened??");
}
text += last_saved_string_stream.str() + lf;
}
VS_LOG_AND_FLUSH is a macro (I know, I know) that calls BOOST_LOG_TRIVIAL with the specified log level and message, and then flushes all the boost log sinks, plus stdout and stderr.
The implementation of getSaveDir()
is not important for purposes of this question. Let's just accept that it returns a std::string
containing the directory in which this program's savegames are stored.
Even getting this far was way more difficult than it should have been. I found surprisingly few examples of people actually using these libraries -- especially using them together.
This code compiles, but crashes silently when run on Windows. Meanwhile, on Linux, it runs, but displays the file's timestamp as some huge number of nanoseconds since January 1st, 1970. This is not the correct format. The format should be something like "9/10/2021 5:30 PM" in the US, or "10/09/2021 17:30:00" in some European countries, for example.
What am I doing wrong? And what should I do instead?
I have tried a couple of different format strings on the output_facet.format(...
line. I've tried "%c"
and "%x"
, each with the same result.