summaryrefslogtreecommitdiff
path: root/ace/Time_Value.cpp
blob: c6aef0b8fa42456620826f893291260133f3b4b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Time_Value.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/ACE.h"
#include "ace/Time_Value.h"

#if !defined (__ACE_INLINE__)
#include "ace/Time_Value.i"
#endif /* __ACE_INLINE__ */

// Static constant representing `zero-time'.
const ACE_Time_Value ACE_Time_Value::zero;

ACE_ALLOC_HOOK_DEFINE(ACE_Time_Value)

/* Initializes the ACE_Time_Value object from a timeval. */

ACE_Time_Value::ACE_Time_Value (const timeval &tv)
{
  ACE_TRACE ("ACE_Time_Value::ACE_Time_Value");
  this->set (tv);
}

#if defined(ACE_WIN32)
//  Initializes the ACE_Time_Value object from a Win32 FILETIME

ACE_Time_Value::ACE_Time_Value (const FILETIME &file_time)
{
  ACE_TRACE ("ACE_Time_Value::ACE_Time_Value");
  this->set (file_time);
}

void ACE_Time_Value::set (const FILETIME &file_time)
{
  //  Initializes the ACE_Time_Value object from a Win32 FILETIME 
  ACE_QWORD _100ns = ACE_MAKE_QWORD (file_time.dwLowDateTime, 
				     file_time.dwHighDateTime);
  // Convert 100ns units to seconds;
  this->tv_sec_ = long (_100ns / (10000 * 1000));
  // Convert remainder to microseconds;
  this->tv_usec_ = long ((_100ns - (this->tv_sec_ * (10000 * 1000))) / 10);
}

// Returns the value of the object as a Win32 FILETIME.

ACE_Time_Value::operator FILETIME () const
{
  ACE_TRACE ("ACE_Time_Value::operator FILETIME");
  ACE_QWORD _100ns = ((ACE_QWORD) this->tv_sec_ * (1000 * 1000) + this->tv_usec_) * 10;
  FILETIME file_time;
  file_time.dwLowDateTime = ACE_LOW_DWORD (_100ns);
  file_time.dwHighDateTime = ACE_HIGH_DWORD (_100ns);
  return file_time;
}

#endif

void
ACE_Time_Value::dump (void) const
{
  ACE_TRACE ("ACE_Time_Value::dump");
#if 0
  if (tv.usec () < 0 || tv.sec () < 0)
    stream << "-";

  stream << dec << abs (int (tv.sec ())) << "."
//	 << setw (6) << setfill ('0') 
	 << dec << abs (int (tv.usec ()));
// I assume
   inline int abs(int d) { return (d>0)?d:-d; }
   is defined somewhere */
#endif /* 0 */
}

void
ACE_Time_Value::set (long sec, long usec)
{
  ACE_TRACE ("ACE_Time_Value::set");
  this->tv_sec_ = sec;
  this->tv_usec_ = usec;
}

ACE_Time_Value::ACE_Time_Value (long sec, long usec)
{
  ACE_TRACE ("ACE_Time_Value::ACE_Time_Value");
  this->set (sec, usec);
  this->normalize ();
}

// Returns the value of the object as a timeval.

ACE_Time_Value::operator timeval () const
{
  ACE_TRACE ("ACE_Time_Value::operator timeval");
  timeval tv;
  tv.tv_sec = this->tv_sec_;
  tv.tv_usec = this->tv_usec_;
  return tv;
}

// Add TV to this.

void
ACE_Time_Value::operator+= (const ACE_Time_Value &tv)
{
  ACE_TRACE ("ACE_Time_Value::operator+=");
  this->tv_sec_ += tv.tv_sec_;
  this->tv_usec_ += tv.tv_usec_;
  this->normalize ();
}

// Subtract TV to this.

void
ACE_Time_Value::operator-= (const ACE_Time_Value &tv)
{
  ACE_TRACE ("ACE_Time_Value::operator-=");
  this->tv_sec_ -= tv.tv_sec_;
  this->tv_usec_ -= tv.tv_usec_;
  this->normalize ();
}

// Adds two ACE_Time_Value objects together, returns the sum.

ACE_Time_Value 
operator + (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2)
{
  ACE_TRACE ("operator +");
  ACE_Time_Value sum (tv1.tv_sec_ + tv2.tv_sec_, 
		      tv1.tv_usec_ + tv2.tv_usec_); 

  sum.normalize ();
  return sum;
}

// Subtracts two ACE_Time_Value objects, returns the difference.

ACE_Time_Value 
operator - (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2)
{
  ACE_TRACE ("operator -");
  ACE_Time_Value delta (tv1.tv_sec_ - tv2.tv_sec_, 
			tv1.tv_usec_ - tv2.tv_usec_); 
  delta.normalize ();
  return delta;
}

// True if tv1 > tv2.

int
operator > (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2)
{
  ACE_TRACE ("operator >");
  if (tv1.tv_sec_ > tv2.tv_sec_)
    return 1;
  else if (tv1.tv_sec_ == tv2.tv_sec_ 
	   && tv1.tv_usec_ > tv2.tv_usec_) 
    return 1;
  else
    return 0;
}

// True if tv1 >= tv2.

int
operator >= (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2)
{
  ACE_TRACE ("operator >=");
  if (tv1.tv_sec_ > tv2.tv_sec_)
    return 1;
  else if (tv1.tv_sec_ == tv2.tv_sec_ 
	   && tv1.tv_usec_ >= tv2.tv_usec_)
    return 1;
  else
    return 0;
}

void
ACE_Time_Value::normalize (void)
{
  ACE_TRACE ("ACE_Time_Value::normalize");
  // New code from Hans Rohnert...

  if (this->tv_usec_ >= ONE_SECOND)
    {
      do
	{ 
	  this->tv_sec_++;
	  this->tv_usec_ -= ONE_SECOND;
	}
      while (this->tv_usec_ >= ONE_SECOND);
    }
  else if (this->tv_usec_ <= -ONE_SECOND)
    {
      do
	{ 
	  this->tv_sec_--;
	  this->tv_usec_ += ONE_SECOND;
	}
      while (this->tv_usec_ <= -ONE_SECOND);
    }
 
  if (this->tv_sec_ >= 1 && this->tv_usec_ < 0)
    {
      this->tv_sec_--;
      this->tv_usec_ += ONE_SECOND;
    }
  else if (this->tv_sec_ < 0 && this->tv_usec_ > 0)
    {
      this->tv_sec_++;
      this->tv_usec_ -= ONE_SECOND;
    }

#if 0
  // Old code...
  while ((this->tv_usec_ >= ONE_SECOND) 
	 || (this->tv_sec_ < 0 && this->tv_usec_ > 0 ))
    {
      this->tv_usec_ -= ONE_SECOND;
      this->tv_sec_++;
    }

  while ((this->tv_usec_ <= -ONE_SECOND) 
	 || (this->tv_sec_ > 0 && this->tv_usec_ < 0))
    {
      this->tv_usec_ += ONE_SECOND;
      this->tv_sec_--;
    }
#endif 
}