2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_TIMER_H
20 #define MANGOS_TIMER_H
22 #include "Platform/CompilerDefs.h"
24 #if PLATFORM == PLATFORM_WINDOWS
25 # include <ace/config-all.h>
26 # include <mmsystem.h>
29 # if defined(__APPLE_CC__)
32 # include <sys/time.h>
33 # include <sys/timeb.h>
36 #if PLATFORM == PLATFORM_WINDOWS
37 inline uint32
getMSTime() { return GetTickCount(); }
39 inline uint32
getMSTime()
43 gettimeofday( &tv
, &tz
);
44 return (tv
.tv_sec
* 1000) + (tv
.tv_usec
/ 1000);
48 inline uint32
getMSTimeDiff(uint32 oldMSTime
, uint32 newMSTime
)
50 // getMSTime() have limited data range and this is case when it overflow in this tick
51 if (oldMSTime
> newMSTime
)
52 return (0xFFFFFFFF - oldMSTime
) + newMSTime
;
54 return newMSTime
- oldMSTime
;
60 IntervalTimer() : _interval(0), _current(0) {}
62 void Update(time_t diff
) { _current
+= diff
; if(_current
<0) _current
=0;}
63 bool Passed() { return _current
>= _interval
; }
64 void Reset() { if(_current
>= _interval
) _current
-= _interval
; }
66 void SetCurrent(time_t current
) { _current
= current
; }
67 void SetInterval(time_t interval
) { _interval
= interval
; }
68 time_t GetInterval() const { return _interval
; }
69 time_t GetCurrent() const { return _current
; }
78 TimeTracker(time_t expiry
) : i_expiryTime(expiry
) {}
79 void Update(time_t diff
) { i_expiryTime
-= diff
; }
80 bool Passed(void) const { return (i_expiryTime
<= 0); }
81 void Reset(time_t interval
) { i_expiryTime
= interval
; }
82 time_t GetExpiry(void) const { return i_expiryTime
; }
86 struct TimeTrackerSmall
88 TimeTrackerSmall(int32 expiry
) : i_expiryTime(expiry
) {}
89 void Update(int32 diff
) { i_expiryTime
-= diff
; }
90 bool Passed(void) const { return (i_expiryTime
<= 0); }
91 void Reset(int32 interval
) { i_expiryTime
= interval
; }
92 int32
GetExpiry(void) const { return i_expiryTime
; }