2 * Copyright (C) 2005-2013 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
23 #include <ace/OS_NS_sys_time.h>
29 // get current server time
30 static uint32
getMSTime();
32 // get time difference between two timestamps
33 static inline uint32
getMSTimeDiff(const uint32
& oldMSTime
, const uint32
& newMSTime
)
35 if (oldMSTime
> newMSTime
)
37 const uint32 diff_1
= (uint32(0xFFFFFFFF) - oldMSTime
) + newMSTime
;
38 const uint32 diff_2
= oldMSTime
- newMSTime
;
40 return std::min(diff_1
, diff_2
);
43 return newMSTime
- oldMSTime
;
46 // get last world tick time
47 static MANGOS_DLL_SPEC uint32
tickTime();
48 // get previous world tick time
49 static MANGOS_DLL_SPEC uint32
tickPrevTime();
51 static MANGOS_DLL_SPEC uint32
tick();
55 WorldTimer(const WorldTimer
&);
57 // analogue to getMSTime() but it persists m_SystemTickTime
58 static uint32
getMSTime_internal(bool savetime
= false);
60 static MANGOS_DLL_SPEC uint32 m_iTime
;
61 static MANGOS_DLL_SPEC uint32 m_iPrevTime
;
67 IntervalTimer() : _interval(0), _current(0) {}
69 void Update(time_t diff
)
75 bool Passed() const { return _current
>= _interval
; }
78 if (_current
>= _interval
)
79 _current
-= _interval
;
82 void SetCurrent(time_t current
) { _current
= current
; }
83 void SetInterval(time_t interval
) { _interval
= interval
; }
84 time_t GetInterval() const { return _interval
; }
85 time_t GetCurrent() const { return _current
; }
92 class ShortIntervalTimer
95 ShortIntervalTimer() : _interval(0), _current(0) {}
97 void Update(uint32 diff
)
102 bool Passed() const { return _current
>= _interval
; }
105 if (_current
>= _interval
)
106 _current
-= _interval
;
109 void SetCurrent(uint32 current
) { _current
= current
; }
110 void SetInterval(uint32 interval
) { _interval
= interval
; }
111 uint32
GetInterval() const { return _interval
; }
112 uint32
GetCurrent() const { return _current
; }
122 TimeTracker(time_t expiry
) : i_expiryTime(expiry
) {}
123 void Update(time_t diff
) { i_expiryTime
-= diff
; }
124 bool Passed() const { return (i_expiryTime
<= 0); }
125 void Reset(time_t interval
) { i_expiryTime
= interval
; }
126 time_t GetExpiry() const { return i_expiryTime
; }
132 struct ShortTimeTracker
135 ShortTimeTracker(int32 expiry
= 0) : i_expiryTime(expiry
) {}
136 void Update(int32 diff
) { i_expiryTime
-= diff
; }
137 bool Passed() const { return (i_expiryTime
<= 0); }
138 void Reset(int32 interval
) { i_expiryTime
= interval
; }
139 int32
GetExpiry() const { return i_expiryTime
; }