00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <time.h>
00015 #include <stdio.h>
00016
00017 #include "TimeVal.h"
00018 #include "Logger.h"
00019
00020 using namespace ASSA;
00021
00022
00023
00024
00025
00026 TimeVal TimeVal::m_zero;
00027 static const long ONE_SECOND = 1000000;
00028
00029
00030
00031
00032
00033 TimeVal&
00034 TimeVal::
00035 operator+=(const TimeVal& rhs_)
00036 {
00037 tv_sec += rhs_.tv_sec;
00038 tv_usec += rhs_.tv_usec;
00039
00040 if (tv_usec >= ONE_SECOND) {
00041 tv_usec -= ONE_SECOND;
00042 tv_sec++;
00043 }
00044 else if (tv_sec >= 1 && tv_usec < 0) {
00045 tv_usec += ONE_SECOND;
00046 tv_sec--;
00047 }
00048 normalize ();
00049 return *this;
00050 }
00051
00052 TimeVal&
00053 TimeVal::
00054 operator-=(const TimeVal& rhs_)
00055 {
00056 tv_sec -= rhs_.tv_sec;
00057 tv_usec -= rhs_.tv_usec;
00058
00059 if (tv_usec < 0) {
00060 tv_usec += ONE_SECOND;
00061 tv_sec--;
00062 }
00063 else if (tv_usec >= ONE_SECOND) {
00064 tv_usec -= ONE_SECOND;
00065 tv_sec++;
00066 }
00067 normalize ();
00068 return *this;
00069 }
00070
00071 void
00072 TimeVal::
00073 normalize ()
00074 {
00075 if (tv_usec >= ONE_SECOND) {
00076 do {
00077 tv_sec++;
00078 tv_usec -= ONE_SECOND;
00079 }
00080 while (tv_usec >= ONE_SECOND);
00081 }
00082 else if (tv_usec <= -ONE_SECOND) {
00083 do {
00084 tv_sec--;
00085 tv_usec += ONE_SECOND;
00086 }
00087 while (tv_usec <= -ONE_SECOND);
00088 }
00089
00090 if (tv_sec >= 1 && tv_usec < 0) {
00091 tv_sec--;
00092 tv_usec += ONE_SECOND;
00093 }
00094 else if (tv_sec < 0 && tv_usec > 0) {
00095 tv_sec++;
00096 tv_usec -= ONE_SECOND;
00097 }
00098 }
00099
00100
00101
00102
00103
00104
00105 string
00106 TimeVal::
00107 fmtString (const char* fmt_) const
00108 {
00109 struct tm ct;
00110 char buf[80];
00111 memset (buf, 0, 80);
00112
00113 if (m_tz == gmt)
00114 ct = *( localtime ((const time_t*) &tv_sec) );
00115 else
00116 ct = *( gmtime ((const time_t*) &tv_sec) );
00117
00118 if (fmt_ == NULL) {
00119 strftime (buf, 80, "%Y/%j %H:%M:%S", &ct);
00120 sprintf (buf + strlen(buf),
00121 ".%03ld", (tv_usec %1000000)/1000);
00122 }
00123 else {
00124 strftime(buf, 80, fmt_, &ct);
00125 }
00126 return string (buf);
00127 }
00128
00129 string
00130 TimeVal::
00131 fmt_hh_mm_ss_mls () const
00132 {
00133 struct tm ct;
00134 char buf [80];
00135 memset (buf, 0, 80);
00136
00137 if (m_tz == gmt)
00138 ct = *( localtime ((const time_t*) &tv_sec) );
00139 else
00140 ct = *( gmtime ((const time_t*) &tv_sec) );
00141
00142 strftime (buf, 80, "%H:%M:%S", &ct);
00143 sprintf (buf + strlen(buf), ".%03ld", millisec ());
00144
00145 return string (buf);
00146 }
00147
00148 string
00149 TimeVal::
00150 fmt_mm_ss_mls () const
00151 {
00152 struct tm ct;
00153 char buf [80];
00154 memset (buf, 0, 80);
00155
00156 if (m_tz == gmt)
00157 ct = *( localtime ((const time_t*) &tv_sec) );
00158 else
00159 ct = *( gmtime ((const time_t*) &tv_sec) );
00160
00161 strftime (buf, 80, "%M:%S", &ct);
00162 sprintf (buf + strlen(buf), ".%03ld", millisec ());
00163
00164 return string (buf);
00165 }
00166
00167 string
00168 TimeVal::
00169 fmt_ss_mls () const
00170 {
00171 struct tm ct;
00172 char buf [80];
00173 memset (buf, 0, 80);
00174
00175 if (m_tz == gmt)
00176 ct = *( localtime ((const time_t*) &tv_sec) );
00177 else
00178 ct = *( gmtime ((const time_t*) &tv_sec) );
00179
00180 strftime (buf, 80, "%S", &ct);
00181 sprintf (buf + strlen(buf), ".%03ld", millisec ());
00182
00183 return string (buf);
00184 }
00185
00186 void
00187 TimeVal::
00188 dump_to_log (const string& var_name_) const
00189 {
00190 static const char self []="TimeVal::dump_to_log"; trace(self);
00191
00192 if (Logger::get_instance ()->group_enabled (REACT))
00193 {
00194 DL((REACT,"=== TimeVal %s ===\n", var_name_.c_str ()));
00195 DL((REACT,"MM:SS:MLS = %s\n", fmt_mm_ss_mls ().c_str ()));
00196 DL((REACT,"tv_sec = %d, tv_msec = %d, tv_mls = %d\n",
00197 sec (), msec (), millisec ()));
00198 DL((REACT,"(double) = %7.4f\n", double (*this)));
00199 DL((REACT,"==================\n"));
00200 }
00201 }
00202