1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
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.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
31 Hewlett-Packard Company
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.
44 char msec_cpp_version[]="@(#) SNMP++ $Id: msec.cpp 318 2007-11-02 19:25:56Z katz $";
46 #include "snmp_pp/msec.h"
47 #include "snmp_pp/smival.h"
48 #include "snmp_pp/config_snmp_pp.h"
50 #include <stdio.h> // for sprintf
51 #include <string.h> // for strcat
54 #include <sys/types.h> // for _timeb
55 #include <sys/timeb.h> // and _ftime
64 #ifdef SNMP_PP_NAMESPACE
68 #if !defined HAVE_LOCALTIME_R && !defined HAVE_REENTRANT_LOCALTIME
70 SnmpSynchronized msec::m_localtime_mutex;
74 int operator==(const msec &t1, const msec &t2)
76 return((t1.m_time.tv_sec == t2.m_time.tv_sec) &&
77 (t1.m_time.tv_usec == t2.m_time.tv_usec));
80 int operator!=(const msec &t1, const msec &t2)
82 return((t1.m_time.tv_sec != t2.m_time.tv_sec) ||
83 (t1.m_time.tv_usec != t2.m_time.tv_usec));
86 int operator<(const msec &t1, const msec &t2)
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)))
97 int operator>(const msec &t1, const msec &t2)
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)))
108 msec &msec::operator-=(const long millisec)
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!
120 msec &msec::operator-=(const timeval &t1)
122 long tmp_usec = t1.tv_usec/1000;// convert usec to millisec
123 if (!this->IsInfinite())
125 if (m_time.tv_usec < t1.tv_usec) {
128 m_time.tv_usec += 1000;
130 m_time.tv_usec -= tmp_usec;
131 m_time.tv_sec -= t1.tv_sec;
137 msec &msec::operator+=(const long millisec)
142 t1.tv_sec = millisec / 1000;
143 t1.tv_usec = (millisec % 1000) * 1000;
145 *this += t1; // add m_changed = true if this line is removed!
149 msec &msec::operator+=(const timeval &t1)
151 long tmp_usec = t1.tv_usec/1000;// convert usec to millisec
152 if (!this->IsInfinite())
154 m_time.tv_usec += tmp_usec;
155 if (m_time.tv_usec > 1000) {
157 m_time.tv_sec += m_time.tv_usec / 1000;
158 m_time.tv_usec = m_time.tv_usec % 1000;
160 m_time.tv_sec += t1.tv_sec;
166 msec &msec::operator=(const timeval &t1)
168 m_time.tv_sec = t1.tv_sec;
169 m_time.tv_usec = t1.tv_usec/1000; // convert usec to millisec
174 #if defined (CPU) && CPU == PPC603
179 unsigned long FractMS;
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);
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
204 m_time.tv_sec = theTime.NumMS/1000;
205 m_time.tv_usec = theTime.NumMS % 1000;
208 class timezone tzone;
209 gettimeofday((timeval *)&m_time, &tzone);
210 m_time.tv_usec /= 1000; // convert usec to millisec
216 #define MAX_ALARM 1000000000L
219 void msec::GetDelta(const msec &future, timeval &timeout) const
221 if (future.IsInfinite())
223 timeout.tv_sec = MAX_ALARM; // max allowable select timeout
226 else if (future > *this)
228 if (future.m_time.tv_usec < m_time.tv_usec)
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;
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;
238 timeout.tv_usec *= 1000 ;// convert back to usec
240 else // Never give back negative timeval's they make select() hurl
247 // FIXME: does not print years and days!
248 const char *msec::get_printable() const
250 if (m_changed == false) return m_output_buffer;
253 msec *nc_this = PP_CONST_CAST(msec*, this);
255 #ifdef HAVE_LOCALTIME_R
257 localtime_r((const time_t *)&m_time.tv_sec, &stm);
258 strftime(nc_this->m_output_buffer, sizeof(m_output_buffer),
261 #if defined _THREADS && !defined HAVE_REENTRANT_LOCALTIME
262 SnmpSynchronize s(m_localtime_mutex); // Serialize all calls to localtime!
265 tmptr = localtime((time_t *)&m_time.tv_sec);
266 strftime(nc_this->m_output_buffer, sizeof(m_output_buffer),
270 sprintf(msec_buffer, "%.3ld", (long)m_time.tv_usec);
271 strcat(nc_this->m_output_buffer, msec_buffer);
273 nc_this->m_changed = false;
275 return m_output_buffer;
278 #ifdef SNMP_PP_NAMESPACE
279 }; // end of namespace Snmp_pp