renderd7/source/Time.cpp

31 lines
855 B
C++
Raw Permalink Normal View History

2022-11-12 23:19:41 +01:00
#include <fstream>
2022-07-29 13:19:31 +02:00
#include <memory>
2022-11-12 23:19:41 +01:00
#include <renderd7/Time.hpp>
#include <stdarg.h>
2022-07-29 13:19:31 +02:00
#include <stdio.h>
2022-07-31 01:23:39 +02:00
#include <string.h>
#include <string>
2022-11-12 23:19:41 +01:00
#include <time.h>
#include <unistd.h>
2022-07-31 01:23:39 +02:00
2022-11-12 23:19:41 +01:00
std::string RenderD7::FormatString(std::string fmt_str, ...) {
va_list ap;
char *fp = NULL;
va_start(ap, fmt_str);
vasprintf(&fp, fmt_str.c_str(), ap);
va_end(ap);
std::unique_ptr<char, decltype(free) *> formatted(fp, free);
return std::string(formatted.get());
2022-07-29 13:19:31 +02:00
}
2021-12-01 20:38:44 +01:00
2022-11-12 23:19:41 +01:00
std::string RenderD7::GetTimeStr(void) {
time_t unixTime;
struct tm timeStruct;
time(&unixTime);
localtime_r(&unixTime, &timeStruct);
return FormatString("%04i-%02i-%02i_%02i-%02i-%02i",
timeStruct.tm_year + 1900, timeStruct.tm_mon + 1,
timeStruct.tm_mday, timeStruct.tm_hour, timeStruct.tm_min,
timeStruct.tm_sec);
2021-12-01 20:38:44 +01:00
}