timeClass.h

Engine/source/core/util/timeClass.h

More...

Classes:

class

64 bit time representation with ten microsecond resolution.

Namespaces:

namespace

Public Defines

define

Used to declare signed 64 bit constants.

define

Used to declare unsigned 64 bit constants.

Detailed Description

Public Defines

TORQUE_CONSTANT_S64(a) 

Used to declare signed 64 bit constants.

TORQUE_CONSTANT_U64(a) 

Used to declare unsigned 64 bit constants.

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