Merge pull request #2258 from likema/log-msg-reset-ostream
[ACE_TAO.git] / ACE / ASNMP / asnmp / timetick.cpp
blob02513a3230b95af527f0b52440d1b3fe03329b59
2 //=============================================================================
3 /**
4 * @file timetick.cpp
6 * Class implentation for SMI Timeticks class.
8 * @author Peter E MellquistMichael R MacFaden mrm@cisco.com - rework & ACE port
9 */
10 //=============================================================================
12 /*===================================================================
13 Copyright (c) 1996
14 Hewlett-Packard Company
16 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
17 Permission to use, copy, modify, distribute and/or sell this software
18 and/or its documentation is hereby granted without fee. User agrees
19 to display the above copyright notice and this license notice in all
20 copies of the software and any documentation of the software. User
21 agrees to assume all liability for the use of the software; Hewlett-Packard
22 makes no representations about the suitability of this software for any
23 purpose. It is provided "AS-IS without warranty of any kind,either express
24 or implied. User hereby grants a royalty-free license to any and all
25 derivatives based upon this software code base.
26 =====================================================================*/
28 #include "asnmp/timetick.h" // include header file for timetick class
29 #include "ace/OS_NS_stdio.h"
31 // constructor with a value
32 TimeTicks::TimeTicks( const unsigned long i):SnmpUInt32(i)
34 smival.syntax = sNMP_SYNTAX_TIMETICKS;
37 // copy constructor
38 TimeTicks::TimeTicks( const TimeTicks &t)
39 : SnmpUInt32 (t)
41 smival.value.uNumber = t.smival.value.uNumber;
42 smival.syntax = sNMP_SYNTAX_TIMETICKS;
45 // destructor
46 TimeTicks::~TimeTicks()
50 // syntax type
51 SmiUINT32 TimeTicks::get_syntax()
53 return sNMP_SYNTAX_TIMETICKS;
56 // create a new instance of this Value
57 SnmpSyntax *TimeTicks::clone() const
59 return (SnmpSyntax *) new TimeTicks(*this);
62 // overloaded assignement from ulong
63 TimeTicks& TimeTicks::operator=( const unsigned long int i)
65 smival.value.uNumber =i; return *this;
68 // overloaded assignment from TimeTicks
69 TimeTicks& TimeTicks::operator=( const TimeTicks &uli)
71 this->smival.value.uNumber = uli.smival.value.uNumber; return *this;
74 // general assignment from any Value
75 SnmpSyntax& TimeTicks::operator=( SnmpSyntax &in_val)
77 if ( this == &in_val ) // handle assignement from itself
78 return *this;
80 valid_flag = 0; // will get set true if really valid
81 if (in_val.valid()) {
82 switch (in_val.get_syntax()) {
83 case sNMP_SYNTAX_UINT32:
84 // case sNMP_SYNTAX_GAUGE32: .. indistinquishable from UINT32
85 case sNMP_SYNTAX_CNTR32:
86 case sNMP_SYNTAX_TIMETICKS:
87 case sNMP_SYNTAX_INT32: // implied cast int -> uint
88 this->smival.value.uNumber =
89 ((TimeTicks &)in_val).smival.value.uNumber;
90 valid_flag = 1;
91 break;
94 return *this;
97 // otherwise, behave like an unsigned long
98 TimeTicks::operator unsigned long()
100 return smival.value.uNumber;
104 // ASCII format return
105 const char * TimeTicks::to_string()
106 /* Should do something nicer like days:hours:minutes... */
108 unsigned long tt, hseconds, seconds, minutes, hours, days;
109 tt = this->smival.value.uNumber;
111 // days
112 days = tt / 8640000;
113 tt %= 8640000;
115 // hours
116 hours = tt / 360000;
117 tt %= 360000;
119 // minutes
120 minutes = tt / 6000;
121 tt %= 6000;
123 seconds = tt / 100;
124 tt %= 100;
126 hseconds = tt;
128 if ( days ==0)
129 ACE_OS::sprintf( output_buffer,"%ld:%02ld:%02ld.%02ld", hours,
130 minutes,seconds,hseconds);
131 else if ( days==1)
132 ACE_OS::sprintf( output_buffer,"1 day %ld:%02ld:%02ld.%02ld", hours,
133 minutes,seconds,hseconds);
134 else
135 ACE_OS::sprintf( output_buffer,"%ld days, %ld:%02ld:%02ld.%02ld",
136 days,hours, minutes,seconds, hseconds);
138 return output_buffer;