]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/msec.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / msec.cpp
1 /*_############################################################################
2   _## 
3   _##  msec.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
14   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15   _##  Permission to use, copy, modify, distribute and/or sell this software 
16   _##  and/or its documentation is hereby granted without fee. User agrees 
17   _##  to display the above copyright notice and this license notice in all 
18   _##  copies of the software and any documentation of the software. User 
19   _##  agrees to assume all liability for the use of the software; 
20   _##  Hewlett-Packard and Jochen Katz make no representations about the 
21   _##  suitability of this software for any purpose. It is provided 
22   _##  "AS-IS" without warranty of any kind, either express or implied. User 
23   _##  hereby grants a royalty-free license to any and all derivatives based
24   _##  upon this software code base. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*
30   Copyright (c) 1999
31   Hewlett-Packard Company
32
33   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
34   Permission to use, copy, modify, distribute and/or sell this software
35   and/or its documentation is hereby granted without fee. User agrees
36   to display the above copyright notice and this license notice in all
37   copies of the software and any documentation of the software. User
38   agrees to assume all liability for the use of the software; Hewlett-Packard
39   makes no representations about the suitability of this software for any
40   purpose. It is provided "AS-IS" without warranty of any kind,either express
41   or implied. User hereby grants a royalty-free license to any and all
42   derivatives based upon this software code base.
43 */
44 char msec_cpp_version[]="@(#) SNMP++ $Id: msec.cpp 318 2007-11-02 19:25:56Z katz $";
45
46 #include "snmp_pp/msec.h"
47 #include "snmp_pp/smival.h"
48 #include "snmp_pp/config_snmp_pp.h"
49
50 #include <stdio.h>  // for sprintf
51 #include <string.h> // for strcat
52
53 #ifdef WIN32
54 #include <sys/types.h> // for _timeb
55 #include <sys/timeb.h> // and _ftime
56
57 #ifdef __BCPLUSPLUS__
58 #define _timeb timeb
59 #define _ftime ftime
60 #endif
61
62 #endif
63
64 #ifdef SNMP_PP_NAMESPACE
65 namespace Snmp_pp {
66 #endif
67
68 #if !defined HAVE_LOCALTIME_R && !defined HAVE_REENTRANT_LOCALTIME
69 #ifdef _THREADS
70 SnmpSynchronized msec::m_localtime_mutex;
71 #endif
72 #endif
73
74 int operator==(const msec &t1, const msec &t2)
75 {
76   return((t1.m_time.tv_sec == t2.m_time.tv_sec) &&
77          (t1.m_time.tv_usec == t2.m_time.tv_usec));
78 }
79
80 int operator!=(const msec &t1, const msec &t2)
81 {
82   return((t1.m_time.tv_sec != t2.m_time.tv_sec) ||
83          (t1.m_time.tv_usec != t2.m_time.tv_usec));
84 }
85
86 int operator<(const msec &t1, const msec &t2)
87 {
88   if (t1.IsInfinite()) return 0;
89   if (t2.IsInfinite()) return 1;
90   if ((t1.m_time.tv_sec < t2.m_time.tv_sec) ||
91       ((t1.m_time.tv_sec == t2.m_time.tv_sec) &&
92        (t1.m_time.tv_usec < t2.m_time.tv_usec)))
93     return 1;
94   return 0;
95 }
96
97 int operator>(const msec &t1, const msec &t2)
98 {
99   if (t2.IsInfinite()) return 0;
100   if (t1.IsInfinite()) return 1;
101   if ((t1.m_time.tv_sec > t2.m_time.tv_sec) ||
102       ((t1.m_time.tv_sec == t2.m_time.tv_sec) &&
103        (t1.m_time.tv_usec > t2.m_time.tv_usec)))
104     return 1;
105   return 0;
106 }
107
108 msec &msec::operator-=(const long millisec)
109 {
110   timeval t1;
111
112   // create a timeval
113   t1.tv_sec = millisec / 1000;
114   t1.tv_usec = (millisec % 1000) * 1000;
115   // subtract it from this
116   *this -= t1; // add m_changed = true if this line is removed!
117   return *this;
118 }
119
120 msec &msec::operator-=(const timeval &t1)
121 {
122   long tmp_usec = t1.tv_usec/1000;// convert usec to millisec
123   if (!this->IsInfinite())
124   {
125     if (m_time.tv_usec < t1.tv_usec) {
126       // borrow
127       m_time.tv_sec--;
128       m_time.tv_usec += 1000;
129     }
130     m_time.tv_usec -= tmp_usec;
131     m_time.tv_sec -= t1.tv_sec;
132   }
133   m_changed = true;
134   return *this;
135 }
136
137 msec &msec::operator+=(const long millisec)
138 {
139   timeval t1;
140
141   // create a timeval
142   t1.tv_sec = millisec / 1000;
143   t1.tv_usec = (millisec % 1000) * 1000;
144   // add it to this
145   *this += t1; // add m_changed = true if this line is removed!
146   return *this;
147 }
148
149 msec &msec::operator+=(const timeval &t1)
150 {
151   long tmp_usec = t1.tv_usec/1000;// convert usec to millisec
152   if (!this->IsInfinite())
153   {
154     m_time.tv_usec += tmp_usec;
155     if (m_time.tv_usec > 1000) {
156       // carry
157       m_time.tv_sec +=  m_time.tv_usec / 1000;
158       m_time.tv_usec = m_time.tv_usec % 1000;
159     }
160     m_time.tv_sec += t1.tv_sec;
161   }
162   m_changed = true;
163   return *this;
164 }
165
166 msec &msec::operator=(const timeval &t1)
167 {
168   m_time.tv_sec  = t1.tv_sec;
169   m_time.tv_usec = t1.tv_usec/1000; // convert usec to millisec
170   m_changed = true;
171   return *this;
172 }
173
174 #if defined (CPU) && CPU == PPC603
175
176   struct SCommTimer
177   {
178         unsigned long NumMS;
179         unsigned long FractMS;
180   };
181
182   extern "C"
183   {
184   // The GetTime call is not part of the VxWorks library!
185   // If it is not already available in your environment,
186   // you will need to implement it!
187   void GetTime (struct SCommTimer *  Time);
188   }
189 #endif
190
191 void msec::refresh()
192 {
193 #ifdef WIN32
194   struct _timeb timebuffer;
195   _ftime( &timebuffer );
196   m_time.tv_usec = timebuffer.millitm;
197   m_time.tv_sec  = SAFE_ULONG_CAST(timebuffer.time);
198 #elif defined (CPU) && CPU == PPC603
199
200   SCommTimer theTime;
201
202   GetTime(&theTime);
203
204   m_time.tv_sec = theTime.NumMS/1000;
205   m_time.tv_usec = theTime.NumMS % 1000;
206
207 #else
208   class timezone tzone;
209   gettimeofday((timeval *)&m_time, &tzone);
210   m_time.tv_usec /= 1000; // convert usec to millisec
211 #endif
212   m_changed = true;
213 }
214
215 #ifndef MAX_ALARM
216 #define MAX_ALARM 1000000000L
217 #endif
218
219 void msec::GetDelta(const msec &future, timeval &timeout) const
220 {
221   if (future.IsInfinite())
222   {
223     timeout.tv_sec = MAX_ALARM; // max allowable select timeout
224     timeout.tv_usec = 0;
225   }
226   else if (future > *this)
227   {
228     if (future.m_time.tv_usec < m_time.tv_usec)
229     {
230       timeout.tv_sec  = future.m_time.tv_sec  - 1    - m_time.tv_sec;
231       timeout.tv_usec = future.m_time.tv_usec + 1000 - m_time.tv_usec;
232     }
233     else
234     {
235       timeout.tv_sec  = future.m_time.tv_sec  - m_time.tv_sec;
236       timeout.tv_usec = future.m_time.tv_usec - m_time.tv_usec;
237     }
238     timeout.tv_usec *= 1000 ;// convert back to usec
239   }
240   else // Never give back negative timeval's they make select() hurl
241   {
242     timeout.tv_sec = 0;
243     timeout.tv_usec = 0;
244   }
245 }
246
247 // FIXME: does not print years and days!
248 const char *msec::get_printable() const
249 {
250   if (m_changed == false) return m_output_buffer;
251
252   char msec_buffer[5];
253   msec *nc_this = PP_CONST_CAST(msec*, this);
254
255 #ifdef HAVE_LOCALTIME_R
256   struct tm stm;
257   localtime_r((const time_t *)&m_time.tv_sec, &stm);
258   strftime(nc_this->m_output_buffer, sizeof(m_output_buffer),
259            "%H:%M:%S.", &stm);
260 #else
261 #if defined _THREADS && !defined HAVE_REENTRANT_LOCALTIME
262   SnmpSynchronize s(m_localtime_mutex);  // Serialize all calls to localtime!
263 #endif
264   struct tm *tmptr;
265   tmptr = localtime((time_t *)&m_time.tv_sec);
266   strftime(nc_this->m_output_buffer, sizeof(m_output_buffer),
267            "%H:%M:%S.", tmptr);
268 #endif
269
270   sprintf(msec_buffer, "%.3ld", (long)m_time.tv_usec);
271   strcat(nc_this->m_output_buffer, msec_buffer);
272
273   nc_this->m_changed = false;
274
275   return m_output_buffer;
276 }
277
278 #ifdef SNMP_PP_NAMESPACE
279 }; // end of namespace Snmp_pp
280 #endif