Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
EndTime.h
1/*
2 * Copyright (C) 2005-2020 Team Kodi
3 * https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9#pragma once
10
11#ifdef __cplusplus
12
13#include <chrono>
14
15namespace kodi
16{
17namespace tools
18{
19
20//==============================================================================
64{
65public:
66 //============================================================================
70 inline CEndTime() = default;
71 //============================================================================
77 inline explicit CEndTime(unsigned int millisecondsIntoTheFuture)
78 : m_startTime(std::chrono::system_clock::now().time_since_epoch()),
79 m_totalWaitTime(std::chrono::milliseconds(millisecondsIntoTheFuture))
80 {
81 }
82 //----------------------------------------------------------------------------
83
84 //============================================================================
90 inline void Set(unsigned int millisecondsIntoTheFuture)
91 {
92 using namespace std::chrono;
93
94 m_startTime = system_clock::now().time_since_epoch();
95 m_totalWaitTime = milliseconds(millisecondsIntoTheFuture);
96 }
97 //----------------------------------------------------------------------------
98
99 //============================================================================
105 inline bool IsTimePast() const
106 {
107 using namespace std::chrono;
108
109 // timer is infinite
110 if (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max())
111 return false;
112
113 if (m_totalWaitTime.count() == 0)
114 return true;
115 else
116 return (system_clock::now().time_since_epoch() - m_startTime) >= m_totalWaitTime;
117 }
118 //----------------------------------------------------------------------------
119
120 //============================================================================
126 inline unsigned int MillisLeft() const
127 {
128 using namespace std::chrono;
129
130 // timer is infinite
131 if (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max())
132 return std::numeric_limits<unsigned int>::max();
133
134 if (m_totalWaitTime.count() == 0)
135 return 0;
136
137 auto elapsed = system_clock::now().time_since_epoch() - m_startTime;
138
139 auto timeWaitedAlready = duration_cast<milliseconds>(elapsed).count();
140
141 if (timeWaitedAlready >= m_totalWaitTime.count())
142 return 0;
143
144 return static_cast<unsigned int>(m_totalWaitTime.count() - timeWaitedAlready);
145 }
146 //----------------------------------------------------------------------------
147
148 //============================================================================
152 inline void SetExpired()
153 {
154 using namespace std::chrono;
155 m_totalWaitTime = milliseconds(0);
156 }
157 //----------------------------------------------------------------------------
158
159 //============================================================================
163 inline void SetInfinite()
164 {
165 using namespace std::chrono;
166 m_totalWaitTime = milliseconds(std::numeric_limits<unsigned int>::max());
167 }
168 //----------------------------------------------------------------------------
169
170 //============================================================================
176 inline bool IsInfinite(void) const
177 {
178 return (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max());
179 }
180 //----------------------------------------------------------------------------
181
182 //============================================================================
188 inline unsigned int GetInitialTimeoutValue(void) const
189 {
190 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(m_totalWaitTime);
191 return static_cast<unsigned int>(value.count());
192 }
193
194 //============================================================================
200 inline uint64_t GetStartTime(void) const
201 {
202 auto value = std::chrono::duration_cast<std::chrono::milliseconds>(m_startTime);
203 return value.count();
204 }
205 //----------------------------------------------------------------------------
206
207private:
208 std::chrono::system_clock::duration m_startTime;
209 std::chrono::system_clock::duration m_totalWaitTime;
210};
211
212} /* namespace tools */
213} /* namespace kodi */
214
215#endif /* __cplusplus */
Definition EndTime.h:64
unsigned int MillisLeft() const
The amount of time left till this timer expires.
Definition EndTime.h:126
bool IsInfinite(void) const
Check if the timer has been set to infinite expiry.
Definition EndTime.h:176
bool IsTimePast() const
Check if the expiry time has been reached.
Definition EndTime.h:105
CEndTime(unsigned int millisecondsIntoTheFuture)
Class constructor to set future time when timer has expired.
Definition EndTime.h:77
uint64_t GetStartTime(void) const
Get the time this timer started.
Definition EndTime.h:200
unsigned int GetInitialTimeoutValue(void) const
Get the initial timeout value this timer had.
Definition EndTime.h:188
void SetInfinite()
Set this timer as never expiring.
Definition EndTime.h:163
void Set(unsigned int millisecondsIntoTheFuture)
Set the time in the future we cosider this timer as expired.
Definition EndTime.h:90
void SetExpired()
Consider this timer expired.
Definition EndTime.h:152
CEndTime()=default
Class constructor with no time to expiry set.