initial commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#include "date_format.h"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace radiantwave::datetime {
|
||||
|
||||
std::vector<DateFormatOption> getDateFormatOptions() {
|
||||
return {
|
||||
{"Month/Day/Year", "%m/%d/%Y"},
|
||||
{"Day/Month/Year", "%d/%m/%Y"},
|
||||
{"Year-Month-Day", "%Y-%m-%d"},
|
||||
{"Month.Day.Year", "%m.%d.%Y"},
|
||||
{"Day.Month.Year", "%d.%m.%Y"},
|
||||
{"Month Day, Year", "%B %d, %Y"},
|
||||
{"Day Month Year", "%d %B %Y"},
|
||||
};
|
||||
}
|
||||
|
||||
std::string formatDate(std::time_t t, const std::string& pattern) {
|
||||
std::tm local{};
|
||||
if (!localtime_r(&t, &local)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char buffer[128];
|
||||
size_t written = strftime(buffer, sizeof(buffer), pattern.c_str(), &local);
|
||||
if (written == 0) {
|
||||
return "";
|
||||
}
|
||||
return std::string(buffer, written);
|
||||
}
|
||||
|
||||
std::string formatTime(std::time_t t) {
|
||||
return formatDate(t, "%H:%M:%S");
|
||||
}
|
||||
|
||||
} // namespace radiantwave::datetime
|
||||
Reference in New Issue
Block a user