timeClass.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TIMECLASS_H_
  23. #define _TIMECLASS_H_
  24. #ifndef _TORQUE_TYPES_H_
  25. #include "platform/types.h"
  26. #endif
  27. #if defined(TORQUE_COMPILER_VISUALC)
  28. #define TORQUE_CONSTANT_S64(a) (a##I64)
  29. #define TORQUE_CONSTANT_U64(a) (a##UI64)
  30. #else
  31. #define TORQUE_CONSTANT_S64(a) (a##LL) ///< Used to declare signed 64 bit constants @hideinitializer
  32. #define TORQUE_CONSTANT_U64(a) (a##ULL) ///< Used to declare unsigned 64 bit constants @hideinitializer
  33. #endif
  34. namespace Torque
  35. {
  36. //-----------------------------------------------------------------------------
  37. /// 64 bit time representation with ten microsecond resolution.
  38. class Time
  39. {
  40. class Tester;
  41. public:
  42. struct DateTime
  43. {
  44. S32 year;
  45. S32 month;
  46. S32 day;
  47. S32 hour;
  48. S32 minute;
  49. S32 second;
  50. S32 microsecond;
  51. };
  52. static void getCurrentDateTime(DateTime &dateTime);
  53. static Time getCurrentTime();
  54. static const S64 OneDay = TORQUE_CONSTANT_S64(8640000000);
  55. static const S64 OneHour = TORQUE_CONSTANT_S64( 360000000);
  56. static const S64 OneMinute = TORQUE_CONSTANT_S64( 6000000);
  57. static const S64 OneSecond = TORQUE_CONSTANT_S64( 100000);
  58. static const S64 OneMillisecond = TORQUE_CONSTANT_S64( 100);
  59. Time();
  60. explicit Time(S64 time);
  61. Time(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond);
  62. Time(const DateTime &dateTime);
  63. bool set(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond);
  64. void get(S32 *year, S32 *month, S32 *day, S32 *hour, S32 *minute, S32 *second, S32 *microsecond) const;
  65. Time operator+() const;
  66. Time operator-() const;
  67. Time operator+(const Time &time) const;
  68. Time operator-(const Time &time) const;
  69. S64 operator/(const Time &time) const;
  70. const Time& operator+=(const Time time);
  71. const Time& operator-=(const Time time);
  72. template<typename T> Time operator*(T scaler) const { return Time(_time * scaler); }
  73. template<typename T> Time operator/(T scaler) const { return Time(_time / scaler); }
  74. template<typename T> friend Time operator*(T scaler,Time t) { return t * scaler; }
  75. bool operator==(const Time &time) const;
  76. bool operator!=(const Time &time) const;
  77. bool operator<(const Time &time) const;
  78. bool operator>(const Time &time) const;
  79. bool operator<=(const Time &time) const;
  80. bool operator>=(const Time &time) const;
  81. operator Tester*() const
  82. {
  83. static Tester test;
  84. return (_time == 0)? 0: &test;
  85. }
  86. bool operator!() const;
  87. S64 getSeconds() const;
  88. S64 getMilliseconds() const;
  89. S64 getMicroseconds() const;
  90. S64 getInternalRepresentation() const;
  91. private:
  92. class Tester
  93. {
  94. void operator delete(void*) {}
  95. };
  96. S64 _time;
  97. bool _isLeapYear(S32 year) const;
  98. S32 _daysInMonth(S32 month, S32 year) const;
  99. };
  100. namespace TimeConstant
  101. {
  102. const Time OneDay (Time::OneDay);
  103. const Time OneHour (Time::OneHour);
  104. const Time OneMinute (Time::OneMinute);
  105. const Time OneSecond (Time::OneSecond);
  106. const Time OneMillisecond (Time::OneMillisecond);
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline Time::Time()
  110. {
  111. _time = 0;
  112. }
  113. inline Time::Time(S64 time)
  114. {
  115. _time = time;
  116. }
  117. inline Time::Time(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond)
  118. {
  119. set(year, month, day, hour, minute, second, microsecond);
  120. }
  121. inline Time::Time(const Time::DateTime &dateTime)
  122. {
  123. set(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute, dateTime.second, dateTime.microsecond);
  124. }
  125. inline Time Time::operator+() const
  126. {
  127. return Time(_time);
  128. }
  129. inline Time Time::operator-() const
  130. {
  131. return Time(-_time);
  132. }
  133. inline Time Time::operator+(const Time &time) const
  134. {
  135. return Time(_time + time._time);
  136. }
  137. inline Time Time::operator-(const Time &time) const
  138. {
  139. return Time(_time - time._time);
  140. }
  141. inline S64 Time::operator/(const Time &time) const
  142. {
  143. return S64(_time / time._time);
  144. }
  145. inline const Time& Time::operator+=(const Time time)
  146. {
  147. _time += time._time;
  148. return *this;
  149. }
  150. inline const Time& Time::operator-=(const Time time)
  151. {
  152. _time -= time._time;
  153. return *this;
  154. }
  155. inline bool Time::operator==(const Time &time) const
  156. {
  157. return (_time == time._time);
  158. }
  159. inline bool Time::operator!=(const Time &time) const
  160. {
  161. return (_time != time._time);
  162. }
  163. inline bool Time::operator<(const Time &time) const
  164. {
  165. return (_time < time._time);
  166. }
  167. inline bool Time::operator>(const Time &time) const
  168. {
  169. return (_time > time._time);
  170. }
  171. inline bool Time::operator<=(const Time &time) const
  172. {
  173. return (_time <= time._time);
  174. }
  175. inline bool Time::operator>=(const Time &time) const
  176. {
  177. return (_time >= time._time);
  178. }
  179. inline bool Time::operator !() const
  180. {
  181. return _time == 0;
  182. }
  183. inline S64 Time::getSeconds() const
  184. {
  185. return _time / TimeConstant::OneSecond._time;
  186. }
  187. inline S64 Time::getMilliseconds() const
  188. {
  189. return _time / TimeConstant::OneMillisecond._time;
  190. }
  191. inline S64 Time::getMicroseconds() const
  192. {
  193. return _time * 10;
  194. }
  195. inline S64 Time::getInternalRepresentation() const
  196. {
  197. return _time;
  198. }
  199. //-----------------------------------------------------------------------------
  200. // time i/o time functions
  201. template<class S> inline bool read(S &stream, Time *theTime)
  202. {
  203. S64 time;
  204. bool ret = read(stream, &time);
  205. *theTime = Time(time);
  206. return ret;
  207. }
  208. template<class S> inline bool write(S &stream, const Time &theTime)
  209. {
  210. S64 time = theTime.getInternalRepresentation();
  211. return write(stream, time);
  212. }
  213. //-----------------------------------------------------------------------------
  214. inline Time UnixTimeToTime(U32 t)
  215. {
  216. // Converts "unix" time, seconds since 00:00:00 UTC, January 1, 1970
  217. return Time(((S64)(t)) * 100000 + TORQUE_CONSTANT_S64(6213568320000000));
  218. }
  219. inline Time Win32FileTimeToTime(U32 low,U32 high)
  220. {
  221. // Converts Win32 "file" time, 100 nanosecond intervals since 00:00:00 UTC, January 1, 1601
  222. S64 t = (((S64)high) << 32) + low;
  223. return Time(t / 100 + TORQUE_CONSTANT_S64(5049120960000000));
  224. }
  225. } // Namespace
  226. #endif