-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
79 lines (68 loc) · 2.92 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Copyright © 2023 Charles Kerr. All rights reserved.
#include "timer.hpp"
#include <iostream>
#include <stdexcept>
#include <thread>
#include <sstream>
#include <time.h>
#include <iomanip>
using namespace std::string_literals;
namespace util{
//=================================================================================
auto timer_t::now() ->std::string {
std::stringstream msg;
auto now_time = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() );
struct tm buffer;
#if defined(_WIN32)
localtime_s( &buffer, &now_time );
auto now_tm = &buffer;
#else
auto now_tm = localtime_r( &now_time, &buffer );
#endif
msg << std::put_time( now_tm, "%c" );
return msg.str();
}
//=================================================================================
auto timer_t::current() ->std::chrono::system_clock::time_point {
return std::chrono::system_clock::now() ;
}
//=================================================================================
auto timer_t::delta(const std::chrono::steady_clock::time_point &time) ->std::int64_t {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - time).count();
}
//=================================================================================
timer_t::timer_t(){
start_time=std::chrono::steady_clock::now();
time_duration = 0 ;
}
//=================================================================================
timer_t::timer_t(std::int64_t milliseconds,bool block):timer_t(){
time(milliseconds,block);
}
//=================================================================================
auto timer_t::time(std::int64_t milliseconds,bool block)->void {
start_time=std::chrono::steady_clock::now();
time_duration = milliseconds;
if (block){
std::this_thread::sleep_for(std::chrono::duration<std::int64_t,std::milli>(milliseconds));
}
}
//================================================================================
auto timer_t::start() ->void {
start_time=std::chrono::steady_clock::now();
time_duration = 0 ;
}
//=================================================================================
auto timer_t::elapsed() const ->std::int64_t {
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
}
//=================================================================================
auto timer_t::expired() const ->bool{
return elapsed()>= time_duration;
}
//=================================================================================
auto timer_t::remaining() const -> std::int64_t {
return time_duration - elapsed();
}
}