1 // libTorrent - BitTorrent library
2 // Copyright (C) 2005-2007, Jari Sundell
3 //
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.
8 //
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.
13 //
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
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL.  If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so.  If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact:  Jari Sundell <jaris@ifi.uio.no>
33 //
34 //           Skomakerveien 33
35 //           3185 Skoppum, NORWAY
36 
37 #ifndef RAK_TIMER_H
38 #define RAK_TIMER_H
39 
40 #include <limits>
41 #include <inttypes.h>
42 #include <sys/time.h>
43 
44 namespace rak {
45 
46 // Don't convert negative Timer to timeval and then back to Timer, that will bork.
47 class timer {
48  public:
49   timer(int64_t usec = 0) : m_time(usec) {}
50   timer(timeval tv) : m_time((int64_t)(uint32_t)tv.tv_sec * 1000000 + (int64_t)(uint32_t)tv.tv_usec % 1000000) {}
51 
52   bool                is_zero() const                    { return m_time == 0; }
53   bool                is_not_zero() const                { return m_time != 0; }
54 
55   int32_t             seconds() const                    { return m_time / 1000000; }
56   int32_t             seconds_ceiling() const            { return (m_time + 1000000 - 1) / 1000000; }
57   int64_t             usec() const                       { return m_time; }
58 
59   timer               round_seconds() const              { return (m_time / 1000000) * 1000000; }
60   timer               round_seconds_ceiling() const      { return ((m_time + 1000000 - 1) / 1000000) * 1000000; }
61 
62   timeval             tval() const {
63     timeval val;
64     val.tv_sec  = m_time / 1000000;
65     val.tv_usec = m_time % 1000000;
66     return val;
67   }
68 
69   static timer        current();
70   static int64_t      current_seconds()                   { return current().seconds(); }
71   static int64_t      current_usec()                      { return current().usec(); }
72   static timer        from_minutes(uint32_t minutes)      { return rak::timer((uint64_t)minutes * 60 * 1000000); }
73   static timer        from_seconds(uint32_t seconds)      { return rak::timer((uint64_t)seconds * 1000000); }
74   static timer        from_milliseconds(uint32_t msec)    { return rak::timer((uint64_t)msec * 1000); }
75 
76   static timer        max()                              { return std::numeric_limits<int64_t>::max(); }
77 
78   bool                operator <  (const timer& t) const { return m_time < t.m_time; }
79   bool                operator >  (const timer& t) const { return m_time > t.m_time; }
80   bool                operator <= (const timer& t) const { return m_time <= t.m_time; }
81   bool                operator >= (const timer& t) const { return m_time >= t.m_time; }
82   bool                operator == (const timer& t) const { return m_time == t.m_time; }
83   bool                operator != (const timer& t) const { return m_time != t.m_time; }
84 
85   timer               operator - (const timer& t) const  { return timer(m_time - t.m_time); }
86   timer               operator + (const timer& t) const  { return timer(m_time + t.m_time); }
87   timer               operator * (int64_t t) const       { return timer(m_time * t); }
88   timer               operator / (int64_t t) const       { return timer(m_time / t); }
89 
90   timer               operator -= (int64_t t)            { m_time -= t; return *this; }
91   timer               operator -= (const timer& t)       { m_time -= t.m_time; return *this; }
92 
93   timer               operator += (int64_t t)            { m_time += t; return *this; }
94   timer               operator += (const timer& t)       { m_time += t.m_time; return *this; }
95 
96  private:
97   int64_t             m_time;
98 };
99 
100 inline timer
101 timer::current() {
102   timeval t;
103   gettimeofday(&t, 0);
104 
105   return timer(t);
106 }
107 
108 }
109 
110 #endif
111