Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Stopwatch.cpp
Go to the documentation of this file.
1 #include "../include/Stopwatch.hpp"
2 
3 using namespace std;
4 
5 /* =========================================================
6 CONSTRUCTORS
7 ========================================================= */
8 
13  _running = false;
14  _currMilliseconds = 0;
15  _splits = {};
16 }
17 
25 Stopwatch::Stopwatch(int hours, int minutes, int seconds) {
26  int milli = 3600000 * hours + 60000 * minutes + 1000 * seconds;
27  _running = false;
28  _currMilliseconds = milli;
29  start();
30 }
31 
32 /* =========================================================
33 GET CURRENT ELAPSED TIME IN MILLISECONDS
34 ========================================================= */
35 
45  if(_running){
46  auto now = chrono::steady_clock::now();
47  return _currMilliseconds + chrono::duration_cast<chrono::milliseconds>(now - _startTime).count();
48  }
49  else{
50  return _currMilliseconds;
51  }
52 }
53 
54 /* =========================================================
55 SPLIT TIME
56 ========================================================= */
57 
65  int splitMilliseconds = 0;
66  if (_running) {
67  splitMilliseconds = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - _startTime).count();
68  splitMilliseconds += _currMilliseconds;
69  //cout << "Split time: " << _currMilliseconds + splitMilliseconds << " milliseconds" << endl;
70  }
71  else {
72  splitMilliseconds = _currMilliseconds;
73 
74  //cout << "Split time: " << _currMilliseconds << " milliseconds" << endl;
75  }
76  _splits.push_back(splitMilliseconds);
77 }
78 
83  _splits = {};
84 }
85 
90 vector<int> Stopwatch::getSplits(){
91  return _splits;
92 }
93 
94 /* =========================================================
95 START THE STOPWATCH
96 ========================================================= */
97 
104  if (!_running) {
105  _running = true;
106  _startTime = chrono::steady_clock::now();
107  }
108 }
109 
110 /* =========================================================
111 PAUSE THE STOPWATCH
112 ========================================================= */
113 
120  if (_running) {
121  _pauseTime = chrono::steady_clock::now();
122  _currMilliseconds += chrono::duration_cast<chrono::milliseconds>(_pauseTime - _startTime).count();
123  _running = false;
124  }
125 }
126 
127 /* =========================================================
128 RESUME THE STOPWATCH
129 ========================================================= */
130 
135  if (!_running) {
136  _startTime = chrono::steady_clock::now();
137  _running = true;
138  }
139 }
140 
141 /* =========================================================
142 RESET THE STOPWATCH
143 ========================================================= */
144 
149  _running = false;
150  clearSplits();
151  _currMilliseconds = 0;
152 }
153 
154 /* =========================================================
155 CHECK IF STOPWATCH IS RUNNING
156 ========================================================= */
157 
164  return _running;
165 }
void clearSplits()
Clears the splits vector.
Definition: Stopwatch.cpp:82
void addSplit()
Records and prints the current split time in milliseconds.
Definition: Stopwatch.cpp:64
STL namespace.
void reset()
Resets the stopwatch to 0 milliseconds and stops it.
Definition: Stopwatch.cpp:148
void resume()
Resumes the stopwatch from the last paused time.
Definition: Stopwatch.cpp:134
void start()
Starts the stopwatch.
Definition: Stopwatch.cpp:103
int currentMilliseconds()
Returns the current elapsed time in milliseconds.
Definition: Stopwatch.cpp:44
vector< int > getSplits()
Returns the splits vector.
Definition: Stopwatch.cpp:90
Stopwatch()
Default constructor initializes a Stopwatch with 0 milliseconds and not running.
Definition: Stopwatch.cpp:12
void pause()
Pauses the stopwatch.
Definition: Stopwatch.cpp:119
bool isRunning()
Checks if the stopwatch is currently running.
Definition: Stopwatch.cpp:163