Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Alarm.cpp
Go to the documentation of this file.
1 #include "../include/Alarm.hpp"
2 
18 Alarm::Alarm(int h, int m){
20  _endTime = getTime(h, m);
22  if(_startTime >= _endTime){
23  _endTime = getTime(h+24, m);
24  }
25  _running = true;
26 }
27 
38 chrono::time_point<chrono::system_clock> Alarm::getTime(int h, int m) {
39 
40  auto now = currentTime();
41 
42  time_t currentTime = chrono::system_clock::to_time_t(now);
43  tm* localTime = localtime(&currentTime);
44 
45  localTime->tm_hour = h;
46  localTime->tm_min = m;
47  localTime->tm_sec = 0;
48 
49  auto futureTime = chrono::system_clock::from_time_t(mktime(localTime));
50  return futureTime;
51 }
52 
59 string Alarm::timeToString(chrono::time_point<chrono::system_clock> t) {
60 
61  time_t currentTime = chrono::system_clock::to_time_t(t);
62  tm* localTime = localtime(&currentTime);
63 
64  stringstream ss;
65  ss << setw(2) << setfill('0') << localTime->tm_hour << ":"
66  << setw(2) << setfill('0') << localTime->tm_min;
67 
68  return ss.str();
69 }
70 
79 
80  auto totalMilliseconds = chrono::duration_cast<chrono::milliseconds>(_endTime - _startTime).count();
81 
82  if (totalMilliseconds == 0) {
83  return 100.0;
84  }
85 
86  auto now = currentTime();
87  auto elapsedMilliseconds = chrono::duration_cast<chrono::milliseconds>(now - _startTime).count();
88 
89  if (elapsedMilliseconds >= totalMilliseconds) {
90  return 100.0;
91  }
92 
93  double percentage = (static_cast<double>(elapsedMilliseconds) / totalMilliseconds) * 100.0;
94  return percentage;
95 }
96 
103 
104  auto now = currentTime();
105  auto ms = chrono::duration_cast<chrono::milliseconds>(_endTime - now).count();
106  return ms;
107 }
108 
114 chrono::time_point<chrono::system_clock> Alarm::getStartTime(){
115  return _startTime;
116 }
117 
123 chrono::time_point<chrono::system_clock> Alarm::getEndTime(){
124  return _endTime;
125 }
126 
135  int remaining = remainingMilliseconds();
136  if (remaining > 0) {
137  return false;
138  } else {
139  _running = false;
141  return true;
142  }
143 }
144 
151  return _running;
152 }
153 
159 chrono::time_point<chrono::system_clock> Alarm::currentTime(){
160  return chrono::system_clock::now();
161 }
string timeToString(chrono::time_point< chrono::system_clock > t)
Converts a given time point to a string format (HH:MM).
Definition: Alarm.cpp:59
bool isRunning()
Checks if the alarm is currently running.
Definition: Alarm.cpp:150
chrono::time_point< chrono::system_clock > getEndTime()
Returns the alarm&#39;s end time.
Definition: Alarm.cpp:123
double percentElapsed()
Calculates the percentage of time that has elapsed from start to end.
Definition: Alarm.cpp:78
bool isDone()
Checks if the alarm has finished.
Definition: Alarm.cpp:134
chrono::time_point< chrono::system_clock > _startTime
Definition: Alarm.hpp:41
chrono::time_point< chrono::system_clock > getTime(int h, int m)
Creates the nearest future time point with hours h and minutes m.
Definition: Alarm.cpp:38
bool _running
Definition: Alarm.hpp:39
chrono::time_point< chrono::system_clock > _endTime
Definition: Alarm.hpp:42
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
Alarm(int h, int m)
Constructor for the Alarm object.
Definition: Alarm.cpp:18
chrono::time_point< chrono::system_clock > getStartTime()
Returns the alarm&#39;s start time.
Definition: Alarm.cpp:114
int remainingMilliseconds()
Calculates the remaining time in milliseconds until the alarm ends.
Definition: Alarm.cpp:102
int _remainingMilliseconds
Definition: Alarm.hpp:37