timeClass.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #ifndef _PLATFORM_H_
  28. #include "platform/platform.h"
  29. #endif
  30. #if defined(TORQUE_COMPILER_VISUALC)
  31. #define TORQUE_CONSTANT_S64(a) (a##I64)
  32. #define TORQUE_CONSTANT_U64(a) (a##UI64)
  33. #else
  34. #define TORQUE_CONSTANT_S64(a) (a##LL) ///< Used to declare signed 64 bit constants @hideinitializer
  35. #define TORQUE_CONSTANT_U64(a) (a##ULL) ///< Used to declare unsigned 64 bit constants @hideinitializer
  36. #endif
  37. namespace Torque
  38. {
  39. //-----------------------------------------------------------------------------
  40. /// 64 bit time representation with ten microsecond resolution.
  41. class Time
  42. {
  43. class Tester;
  44. public:
  45. struct DateTime
  46. {
  47. S32 year;
  48. S32 month;
  49. S32 day;
  50. S32 hour;
  51. S32 minute;
  52. S32 second;
  53. S32 microsecond;
  54. };
  55. static void getCurrentDateTime(DateTime &dateTime);
  56. static Time getCurrentTime();
  57. static const S64 OneDay = TORQUE_CONSTANT_S64(8640000000);
  58. static const S64 OneHour = TORQUE_CONSTANT_S64( 360000000);
  59. static const S64 OneMinute = TORQUE_CONSTANT_S64( 6000000);
  60. static const S64 OneSecond = TORQUE_CONSTANT_S64( 100000);
  61. static const S64 OneMillisecond = TORQUE_CONSTANT_S64( 100);
  62. Time();
  63. explicit Time(S64 time);
  64. Time(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond);
  65. Time(const DateTime &dateTime);
  66. bool set(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond);
  67. void get(S32 *year, S32 *month, S32 *day, S32 *hour, S32 *minute, S32 *second, S32 *microsecond) const;
  68. Time operator+() const;
  69. Time operator-() const;
  70. Time operator+(const Time &time) const;
  71. Time operator-(const Time &time) const;
  72. S64 operator/(const Time &time) const;
  73. const Time& operator+=(const Time time);
  74. const Time& operator-=(const Time time);
  75. template<typename T> Time operator*(T scaler) const { return Time(_time * scaler); }
  76. template<typename T> Time operator/(T scaler) const { return Time(_time / scaler); }
  77. template<typename T> friend Time operator*(T scaler,Time t) { return t * scaler; }
  78. bool operator==(const Time &time) const;
  79. bool operator!=(const Time &time) const;
  80. bool operator<(const Time &time) const;
  81. bool operator>(const Time &time) const;
  82. bool operator<=(const Time &time) const;
  83. bool operator>=(const Time &time) const;
  84. operator Tester*() const
  85. {
  86. static Tester test;
  87. return (_time == 0)? 0: &test;
  88. }
  89. bool operator!() const;
  90. S64 getSeconds() const;
  91. S64 getMilliseconds() const;
  92. S64 getMicroseconds() const;
  93. S64 getInternalRepresentation() const;
  94. Platform::LocalTime toLocalTime();
  95. private:
  96. class Tester
  97. {
  98. void operator delete(void*) {}
  99. };
  100. S64 _time;
  101. bool _isLeapYear(S32 year) const;
  102. S32 _daysInMonth(S32 month, S32 year) const;
  103. };
  104. namespace TimeConstant
  105. {
  106. const Time OneDay (Time::OneDay);
  107. const Time OneHour (Time::OneHour);
  108. const Time OneMinute (Time::OneMinute);
  109. const Time OneSecond (Time::OneSecond);
  110. const Time OneMillisecond (Time::OneMillisecond);
  111. }
  112. //-----------------------------------------------------------------------------
  113. inline Time::Time()
  114. {
  115. _time = 0;
  116. }
  117. inline Time::Time(S64 time)
  118. {
  119. _time = time;
  120. }
  121. inline Time::Time(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond)
  122. {
  123. set(year, month, day, hour, minute, second, microsecond);
  124. }
  125. inline Time::Time(const Time::DateTime &dateTime)
  126. {
  127. set(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute, dateTime.second, dateTime.microsecond);
  128. }
  129. inline Time Time::operator+() const
  130. {
  131. return Time(_time);
  132. }
  133. inline Time Time::operator-() const
  134. {
  135. return Time(-_time);
  136. }
  137. inline Time Time::operator+(const Time &time) const
  138. {
  139. return Time(_time + time._time);
  140. }
  141. inline Time Time::operator-(const Time &time) const
  142. {
  143. return Time(_time - time._time);
  144. }
  145. inline S64 Time::operator/(const Time &time) const
  146. {
  147. return S64(_time / time._time);
  148. }
  149. inline const Time& Time::operator+=(const Time time)
  150. {
  151. _time += time._time;
  152. return *this;
  153. }
  154. inline const Time& Time::operator-=(const Time time)
  155. {
  156. _time -= time._time;
  157. return *this;
  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 Time &time) const
  180. {
  181. return (_time >= time._time);
  182. }
  183. inline bool Time::operator !() const
  184. {
  185. return _time == 0;
  186. }
  187. inline S64 Time::getSeconds() const
  188. {
  189. return _time / TimeConstant::OneSecond._time;
  190. }
  191. inline S64 Time::getMilliseconds() const
  192. {
  193. return _time / TimeConstant::OneMillisecond._time;
  194. }
  195. inline S64 Time::getMicroseconds() const
  196. {
  197. return _time * 10;
  198. }
  199. inline S64 Time::getInternalRepresentation() const
  200. {
  201. return _time;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // time i/o time functions
  205. template<class S> inline bool read(S &stream, Time *theTime)
  206. {
  207. S64 time;
  208. bool ret = read(stream, &time);
  209. *theTime = Time(time);
  210. return ret;
  211. }
  212. template<class S> inline bool write(S &stream, const Time &theTime)
  213. {
  214. S64 time = theTime.getInternalRepresentation();
  215. return write(stream, time);
  216. }
  217. //-----------------------------------------------------------------------------
  218. inline Time UnixTimeToTime(U32 t)
  219. {
  220. // Converts "unix" time, seconds since 00:00:00 UTC, January 1, 1970
  221. return Time(((S64)(t)) * 100000 + TORQUE_CONSTANT_S64(6213568320000000));
  222. }
  223. inline Time Win32FileTimeToTime(U32 low,U32 high)
  224. {
  225. // Converts Win32 "file" time, 100 nanosecond intervals since 00:00:00 UTC, January 1, 1601
  226. S64 t = (((S64)high) << 32) + low;
  227. return Time(t / 100 + TORQUE_CONSTANT_S64(5049120960000000));
  228. }
  229. } // Namespace
  230. #endif