2021-06-12 09:12:16 -04:00
|
|
|
// Created on: 3/28/21
|
|
|
|
|
|
|
|
|
|
#include "TimeUtility.hpp"
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
//#include <ctime>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include "ConfigOS.h"
|
|
|
|
|
|
2022-10-24 18:39:33 +08:00
|
|
|
namespace timeutil
|
|
|
|
|
{
|
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2021-06-12 09:12:16 -04:00
|
|
|
//
|
|
|
|
|
// Methods for TimeUtilities
|
|
|
|
|
//
|
2022-10-24 18:39:33 +08:00
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2021-06-12 09:12:16 -04:00
|
|
|
|
2022-10-24 18:39:33 +08:00
|
|
|
std::string timeNow()
|
|
|
|
|
{
|
|
|
|
|
std::stringstream msg;
|
2021-06-12 09:12:16 -04:00
|
|
|
|
2022-10-24 18:39:33 +08:00
|
|
|
auto now_time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
|
|
|
|
|
struct tm buffer;
|
2021-06-12 09:12:16 -04:00
|
|
|
#if PLATFORM == WINDOWS
|
2022-10-24 18:39:33 +08:00
|
|
|
localtime_s( &buffer, &now_time );
|
|
|
|
|
auto now_tm = &buffer;
|
2021-06-12 09:12:16 -04:00
|
|
|
#else
|
2022-10-24 18:39:33 +08:00
|
|
|
auto now_tm = localtime_r( &now_time, &buffer );
|
2021-06-12 09:12:16 -04:00
|
|
|
#endif
|
2022-10-24 18:39:33 +08:00
|
|
|
msg << std::put_time( now_tm, "%c" );
|
2021-06-12 09:12:16 -04:00
|
|
|
return msg.str();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 18:39:33 +08:00
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Function - Interval_st::Interval_st()
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Purpose - Constructor
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
Interval_st::Interval_st()
|
|
|
|
|
{
|
|
|
|
|
startTime = std::chrono::steady_clock::now();
|
2021-06-12 09:12:16 -04:00
|
|
|
}
|
2022-10-24 18:39:33 +08:00
|
|
|
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Function - Interval_st::Start()
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Purpose - Set a start time point representing current time
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
void Interval_st::Start()
|
|
|
|
|
{
|
|
|
|
|
startTime = std::chrono::steady_clock::now();
|
2021-06-12 09:12:16 -04:00
|
|
|
}
|
2022-10-24 18:39:33 +08:00
|
|
|
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Function - Interval_st::Elapsed()
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Purpose - Return elapsed time since start time was set
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
long long Interval_st::Elapsed()
|
|
|
|
|
{
|
2021-06-12 09:12:16 -04:00
|
|
|
auto end = std::chrono::steady_clock::now();
|
2022-10-24 18:39:33 +08:00
|
|
|
auto diff = end - startTime;
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>( diff ).count();
|
2021-06-12 09:12:16 -04:00
|
|
|
}
|
2022-10-24 18:39:33 +08:00
|
|
|
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Function - Interval_st::Stop()
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
//| Purpose - Reset start time and return time elapsed since start time was previously set
|
|
|
|
|
//o-----------------------------------------------------------------------------------------------o
|
|
|
|
|
long long Interval_st::Stop()
|
|
|
|
|
{
|
2021-06-12 09:12:16 -04:00
|
|
|
auto end = std::chrono::steady_clock::now();
|
2022-10-24 18:39:33 +08:00
|
|
|
auto diff = end - startTime;
|
|
|
|
|
startTime = std::chrono::steady_clock::now();
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>( diff ).count();
|
2021-06-12 09:12:16 -04:00
|
|
|
}
|
|
|
|
|
}
|