]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/timetick.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / timetick.cpp
1 /*_############################################################################
2   _## 
3   _##  timetick.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
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
34   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35   Permission to use, copy, modify, distribute and/or sell this software
36   and/or its documentation is hereby granted without fee. User agrees
37   to display the above copyright notice and this license notice in all
38   copies of the software and any documentation of the software. User
39   agrees to assume all liability for the use of the software; Hewlett-Packard
40   makes no representations about the suitability of this software for any
41   purpose. It is provided "AS-IS" without warranty of any kind,either express
42   or implied. User hereby grants a royalty-free license to any and all
43   derivatives based upon this software code base.
44
45   T I M E T I C K. C P P
46
47   TIMETICK CLASS IMPLEMENTATION
48
49   DESIGN + AUTHOR:  Peter E Mellquist
50
51   DESCRIPTION:
52   Class implentation for SMI Timeticks class.
53 =====================================================================*/
54 char timetick_cpp_version[]="#(@) SNMP++ $Id: timetick.cpp 1542 2009-05-29 11:38:48Z katz $";
55
56 #include "snmp_pp/timetick.h"          // include header file for timetick class
57 #include <stdio.h>             // for sprintf() usage.
58
59 #ifdef SNMP_PP_NAMESPACE
60 namespace Snmp_pp {
61 #endif
62
63 // Copy constructor
64 TimeTicks::TimeTicks(const TimeTicks &t)
65 {
66   smival.value.uNumber = t.smival.value.uNumber;
67   smival.syntax = sNMP_SYNTAX_TIMETICKS;
68 }
69
70 // general assignment from any Value
71 SnmpSyntax& TimeTicks::operator=(const SnmpSyntax &in_val)
72 {
73   if (this == &in_val) return *this; // handle assignement from itself
74
75   valid_flag = false;           // will get set true if really valid
76   if (in_val.valid())
77   {
78     switch (in_val.get_syntax())
79     {
80       case sNMP_SYNTAX_UINT32:
81    // case sNMP_SYNTAX_GAUGE32:         .. indistinquishable from UINT32
82       case sNMP_SYNTAX_CNTR32:
83       case sNMP_SYNTAX_TIMETICKS:
84       case sNMP_SYNTAX_INT32:           // implied cast int -> uint
85           smival.value.uNumber =
86                 ((TimeTicks &)in_val).smival.value.uNumber;
87           valid_flag = true;
88           break;
89     }
90   }
91   m_changed = true;
92   return *this;
93 }
94
95 // ASCII format return
96 const char *TimeTicks::get_printable() const
97 {
98   if (m_changed == false) return output_buffer;
99
100   unsigned long hseconds, seconds, minutes, hours, days;
101   unsigned long tt = smival.value.uNumber;
102   TimeTicks *nc_this = PP_CONST_CAST(TimeTicks*, this);
103
104   days = tt / 8640000;
105   tt %= 8640000;
106
107   hours = tt / 360000;
108   tt %= 360000;
109
110   minutes = tt / 6000;
111   tt %= 6000;
112
113   seconds = tt / 100;
114   tt %= 100;
115
116   hseconds = tt;
117
118   if (days == 0)
119     sprintf(nc_this->output_buffer, "%lu:%02lu:%02lu.%02lu",
120             hours, minutes, seconds, hseconds);
121   else if (days == 1)
122     sprintf(nc_this->output_buffer, "1 day %lu:%02lu:%02lu.%02lu",
123             hours, minutes, seconds, hseconds);
124   else
125     sprintf(nc_this->output_buffer, "%lu days, %lu:%02lu:%02lu.%02lu",
126             days, hours, minutes, seconds, hseconds);
127
128   nc_this->m_changed = false;
129   return output_buffer;
130 }
131
132 #ifdef SNMP_PP_NAMESPACE
133 }; // end of namespace Snmp_pp
134 #endif