libktorrent  2.2.0
timevalue.h
1 /***************************************************************************
2  * Copyright (C) 2010 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef UTP_TIMEVALUE_H
22 #define UTP_TIMEVALUE_H
23 
24 #include <ktorrent_export.h>
25 #include <util/constants.h>
26 
27 namespace utp
28 {
32  class KTORRENT_EXPORT TimeValue
33  {
34  public:
36  TimeValue();
37  TimeValue(bt::Uint64 secs, bt::Uint64 usecs);
38  TimeValue(const TimeValue & tv);
39 
40  TimeValue & operator = (const TimeValue & tv);
41 
42  bt::Uint32 timestampMicroSeconds() const
43  {
44  bt::Uint64 microsecs = seconds * 1000000 + microseconds;
45  //return microsecs & 0x00000000FFFFFFFF;
46  return microsecs;
47  }
48 
49  void addMilliSeconds(bt::Uint32 ms)
50  {
51  microseconds += ms * 1000;
52  if (microseconds > 1000000)
53  {
54  seconds += microseconds / 1000000;
55  microseconds = microseconds % 1000000;
56  }
57  }
58 
60  bt::TimeStamp toTimeStamp() const {return seconds * 1000 + (bt::Uint64)microseconds * 0.001;}
61 
62  public:
63  bt::Uint64 seconds;
64  bt::Uint64 microseconds;
65  };
66 
67 
69  inline bt::Int64 operator - (const TimeValue & a, const TimeValue & b)
70  {
71  bt::Int64 seconds = a.seconds - b.seconds;
72  bt::Int64 microseconds = a.microseconds - b.microseconds;
73 
74  while (microseconds < 0)
75  {
76  microseconds += 1000000;
77  seconds -= 1;
78  }
79 
80  return (1000000LL * seconds + microseconds) / 1000;
81  }
82 
83  inline bool operator < (const TimeValue & a, const TimeValue & b)
84  {
85  if (a.seconds < b.seconds)
86  return true;
87  else if (a.seconds == b.seconds)
88  return a.microseconds < b.microseconds;
89  else
90  return false;
91  }
92 
93  inline bool operator <= (const TimeValue & a, const TimeValue & b)
94  {
95  if (a.seconds < b.seconds)
96  return true;
97  else if (a.seconds == b.seconds)
98  return a.microseconds <= b.microseconds;
99  else
100  return false;
101  }
102 
103  inline bool operator > (const TimeValue & a, const TimeValue & b)
104  {
105  if (a.seconds > b.seconds)
106  return true;
107  else if (a.seconds == b.seconds)
108  return a.microseconds > b.microseconds;
109  else
110  return false;
111  }
112 
113  inline bool operator >= (const TimeValue & a, const TimeValue & b)
114  {
115  if (a.seconds > b.seconds)
116  return true;
117  else if (a.seconds == b.seconds)
118  return a.microseconds >= b.microseconds;
119  else
120  return false;
121  }
122 
123 }
124 
125 #endif // UTP_TIMEVALUE_H
utp::TimeValue
Definition: timevalue.h:51