BLT/include/blt/std/time.h

150 lines
4.1 KiB
C
Raw Permalink Normal View History

/*
* Created by Brett on 04/01/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#include <chrono>
#include <ctime>
#include <sstream>
2023-01-23 23:53:37 -05:00
#ifndef BLT_TIME_H
#define BLT_TIME_H
2023-09-13 16:49:56 -04:00
namespace blt::system
{
2024-02-22 15:52:01 -05:00
#ifdef WIN32
2024-02-24 14:31:59 -05:00
#define BLT_TIME_FUNC(name) auto t = std::time(nullptr); tm name{}; localtime_s(&name, &t)
2024-02-22 15:52:01 -05:00
#else
2024-02-24 14:31:59 -05:00
#define BLT_TIME_FUNC(name) auto t = std::time(nullptr); auto ptr_##name = std::localtime(&t); auto& name = *ptr_##name
2024-02-22 15:52:01 -05:00
#endif
2023-11-08 18:50:16 -05:00
2023-09-13 16:49:56 -04:00
static inline std::string ensureHasDigits(int current, int digits)
{
2023-01-23 23:53:37 -05:00
std::string asString = std::to_string(current);
auto length = digits - asString.length();
if (length <= 0)
return asString;
std::string zeros;
zeros.reserve(length);
2023-09-13 16:49:56 -04:00
for (unsigned int i = 0; i < length; i++)
{
2023-01-23 23:53:37 -05:00
zeros += '0';
}
return zeros + asString;
}
2023-09-13 16:49:56 -04:00
static inline auto getCurrentTimeNanoseconds()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
2023-09-13 16:49:56 -04:00
static inline auto nanoTime()
{
return getCurrentTimeNanoseconds();
}
2023-09-13 16:49:56 -04:00
static inline auto getCurrentTimeMilliseconds()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
static inline int64_t getCPUThreadTime()
2023-09-13 16:49:56 -04:00
{
#ifdef unix
timespec time{};
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
return time.tv_sec * static_cast<int64_t>(1e9) + time.tv_nsec;
2023-09-13 16:49:56 -04:00
#else
return std::clock();
#endif
}
static inline int64_t getCPUTime()
2023-09-13 16:49:56 -04:00
{
#ifdef unix
timespec time{};
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
return time.tv_sec * static_cast<int64_t>(1e9) + time.tv_nsec;
2023-09-13 16:49:56 -04:00
#else
return std::clock();
#endif
}
static inline auto getCPUTimerResolution()
{
#ifdef unix
timespec res{};
clock_getres(CLOCK_PROCESS_CPUTIME_ID, &res);
return res.tv_sec * static_cast<int64_t>(1e9) + res.tv_nsec;
2023-09-13 16:49:56 -04:00
#else
2023-12-29 01:07:08 -05:00
return 1;
2023-09-13 16:49:56 -04:00
#endif
}
/**
* Standard time string is formatted as follows:
* Year-Month-Date Hour:Min:Second
* If you do not want a space in the string use getTimeStringFS(); (Time String for easy filesystem)
* @return the BLT standard string of time.now
*/
2023-09-13 16:49:56 -04:00
static inline std::string getTimeString()
{
2024-02-24 14:31:59 -05:00
BLT_TIME_FUNC(now);
std::stringstream timeString;
2024-02-21 20:36:22 -05:00
timeString << (1900 + now.tm_year);
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << (1 + now.tm_mon);
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << now.tm_mday;
timeString << " ";
2024-02-21 20:36:22 -05:00
timeString << now.tm_hour;
timeString << ":";
2024-02-21 20:36:22 -05:00
timeString << now.tm_min;
timeString << ":";
2024-02-21 20:36:22 -05:00
timeString << now.tm_sec;
return timeString.str();
}
2023-01-23 23:53:37 -05:00
/**
* Standard logging time string is formatted as follows:
* Hour:Min:Second
* @return the BLT standard logging string of time.now
*/
2023-09-13 16:49:56 -04:00
static inline std::string getTimeStringLog()
{
2024-02-24 14:31:59 -05:00
BLT_TIME_FUNC(now);
2023-01-23 23:53:37 -05:00
std::string timeString = "[";
2024-02-21 20:36:22 -05:00
timeString += ensureHasDigits(now.tm_hour, 2);
2023-01-23 23:53:37 -05:00
timeString += ":";
2024-02-21 20:36:22 -05:00
timeString += ensureHasDigits(now.tm_min, 2);
2023-01-23 23:53:37 -05:00
timeString += ":";
2024-02-21 20:36:22 -05:00
timeString += ensureHasDigits(now.tm_sec, 2);
2023-01-23 23:53:37 -05:00
timeString += "] ";
return timeString;
}
/**
* @return the BLT standard string of time.now (See getTimeString()) that is filesystem friendly (FAT compatible).
*/
2023-09-13 16:49:56 -04:00
static inline std::string getTimeStringFS()
{
2024-02-24 14:31:59 -05:00
BLT_TIME_FUNC(now);
std::stringstream timeString;
2024-02-21 20:36:22 -05:00
timeString << (1900 + now.tm_year);
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << (1 + now.tm_mon);
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << now.tm_mday;
timeString << "_";
2024-02-21 20:36:22 -05:00
timeString << now.tm_hour;
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << now.tm_min;
timeString << "-";
2024-02-21 20:36:22 -05:00
timeString << now.tm_sec;
return timeString.str();
}
}
#endif //BLT_TIME_H