2 #include "../Common/Common.h"
14 // In order to track leaks to line number, we need this at the top of every file
15 #include "MemoryLeak.h"
19 static char THIS_FILE
[] = __FILE__
;
23 CTimeSpan::CTimeSpan(const string
& strName
, bool bAddDate
)
28 m_strStartTime
= GetTimeStamp();
29 m_pTimer
= new CSimpleTimer();
34 // A time span can optionally record the current date
36 m_strStartDate
= GetDateStamp();
39 CTimeSpan::~CTimeSpan()
48 // Get the XML for this TimeSpan object. If bSinglePointInTime is true, then
49 // this is just a single point in time and we don't need the end time or
51 string
CTimeSpan::GetXML(const string
& strPrefix
, bool bSinglePointInTime
)
53 string strResult
= "";
55 // Only stop if we haven't called Stop() explicitly
56 if (m_strEndTime
.size() == 0)
59 strResult
+= strPrefix
;
61 strResult
+= m_strName
;
64 if (!bSinglePointInTime
)
66 strResult
+= strPrefix
;
67 strResult
+= "\t<Elapsed>";
69 sprintf(strNum
, "%0.3f", m_dElapsed
);
71 strResult
+= "</Elapsed>\n";
74 if (m_strStartDate
.length() > 0)
76 strResult
+= strPrefix
;
77 strResult
+= "\t<Date>";
78 strResult
+= m_strStartDate
;
79 strResult
+= "</Date>\n";
82 if (!bSinglePointInTime
)
84 strResult
+= strPrefix
;
85 strResult
+= "\t<Start>";
86 strResult
+= m_strStartTime
;
87 strResult
+= "</Start>\n";
89 strResult
+= strPrefix
;
90 strResult
+= "\t<End>";
91 strResult
+= m_strEndTime
;
92 strResult
+= "</End>\n";
96 strResult
+= strPrefix
;
97 strResult
+= "\t<Time>";
98 strResult
+= m_strStartTime
;
99 strResult
+= "</Time>\n";
102 strResult
+= strPrefix
;
104 strResult
+= m_strName
;
110 string
CTimeSpan::GetTimeStamp()
112 string strTimeStamp
= "";
114 struct timeb sTimeBuffer
;
116 struct timeval sTimeBuffer
;
117 struct timezone sTimezoneBuffer
;
120 char* szTimeLine
= NULL
;
124 szTimeLine
= ctime(&(sTimeBuffer
.time
));
126 gettimeofday(&sTimeBuffer
, &sTimezoneBuffer
);
127 t
= sTimeBuffer
.tv_sec
;
128 szTimeLine
= ctime(&t
);
131 if ((szTimeLine
!= NULL
) && (strlen(szTimeLine
) > 18))
133 for (int i
= 11; i
< 19; i
++)
134 strTimeStamp
+= szTimeLine
[i
];
138 sprintf(szMs
, "%d", sTimeBuffer
.millitm
);
140 sprintf(szMs
, "%d", static_cast<int>(sTimeBuffer
.tv_usec
/ 1000));
142 if (strlen(szMs
) == 1)
143 strTimeStamp
+= "00";
144 else if (strlen(szMs
) == 2)
146 strTimeStamp
+= szMs
;
152 void CTimeSpan::Stop()
154 // Only do this if we actually have a timer, if we were
155 // created from XML then we don't want to change what
156 // we read from the file.
157 if (m_pTimer
!= NULL
)
159 m_strEndTime
= GetTimeStamp();
160 m_dElapsed
= m_pTimer
->GetElapsed();
164 // We allow a time span to continue to erase the
165 // effects of a previous Stop() call
166 void CTimeSpan::Continue()
172 bool CTimeSpan::IsStopped()
174 if (m_strEndTime
.length() > 0)
180 double CTimeSpan::GetElapsed()
185 string
CTimeSpan::GetDateStamp()
187 std::string strDateStamp
= "";
188 char* szTimeLine
= NULL
;
192 szTimeLine
= ctime(&t
);
195 // Wed Jun 22 10:22:00 2005
196 // 0123456789012345678901234
197 if ((szTimeLine
!= NULL
) && (strlen(szTimeLine
) > 23))
199 for (int i
= 4; i
< 10; i
++)
200 strDateStamp
+= szTimeLine
[i
];
201 for (int i
= 19; i
< 24; i
++)
202 strDateStamp
+= szTimeLine
[i
];
208 void CTimeSpan::InitMemberVars()
218 // Construct based on some yummy XML like:
219 // <Elapsed>12.062</Elapsed>
220 // <Date>Jul 04 2005</Date>
221 // <Start>15:48:52.625</Start>
222 // <End>15:49:04.687</End>
223 CTimeSpan::CTimeSpan(const string
& strName
, const string
& strXML
)
227 m_dElapsed
= (double) XMLUtil::GetElementFloat("Elapsed", strXML
);
229 m_strStartDate
= XMLUtil::GetElementString("Date", strXML
);
230 m_strStartTime
= XMLUtil::GetElementString("Start", strXML
);
231 m_strEndTime
= XMLUtil::GetElementString("End", strXML
);