Chi-Tech
chi_timer.cc
Go to the documentation of this file.
1#include"chi_timer.h"
2
3#include <cmath>
4#include <ctime>
5#include <thread>
6
7//################################################################### Default constr
8/** Default constructor.*/
10{
11 start_time_ = std::chrono::steady_clock::now();
12}
13
14//################################################################### Reset
15/** Resets the timer to zero.*/
17{
18 start_time_ = std::chrono::steady_clock::now();
19}
20
21//################################################################### Get time
22/** Gets the current timer value in milliseconds.*/
23double chi::Timer::GetTime() const
24{
25 using namespace std::chrono;
26
27 steady_clock::time_point newTime = std::chrono::steady_clock::now();
28 duration<double> time_span =
29 duration_cast<duration<double>>(newTime - start_time_);
30
31 return time_span.count()*1000.0;
32}
33
34//################################################################### Get string
35/**Obtains a string in the format of hh:mm::ss.
36 *
37 * */
38std::string chi::Timer::GetTimeString() const
39{
40 double time_sec = this->GetTime()/1000.0;
41 int hours = std::floor(time_sec/60/60);
42 int minutes = std::floor((time_sec-60*60*hours)/60);
43 int seconds = (int)time_sec - 3600*hours - 60*minutes;
44
45 char buff[100];
46 snprintf(buff,100,"%02d:%02d:%02d",hours,minutes,seconds);
47
48 return {buff};
49}
50
51//################################################################### Get date
52/**Obtains a string in the format YYYY-MM-DD hh:mm:ss
53 *
54 * */
56{
57 using namespace std::chrono;
58 std::time_t now = system_clock::to_time_t(system_clock::now());
59
60 char s[30];
61 size_t end = std::strftime(s, 30, "%Y-%m-%d %H:%M:%S", std::localtime(&now));
62 s[29] = '\0';
63 if (end < 30) s[end]='\0';
64 return s;
65}
66
67//###################################################################
68void chi::Sleep(std::chrono::duration<double> time)
69{
70 std::this_thread::sleep_for(time);
71}
void Reset()
Definition: chi_timer.cc:16
double GetTime() const
Definition: chi_timer.cc:23
static std::string GetLocalDateTimeString()
Definition: chi_timer.cc:55
std::string GetTimeString() const
Definition: chi_timer.cc:38
Timer() noexcept
Definition: chi_timer.cc:9
std::chrono::steady_clock::time_point start_time_
Definition: chi_timer.h:15
void Sleep(std::chrono::duration< double > time)
Definition: chi_timer.cc:68