Updated German translation
[dasher.git] / Src / DasherCore / TimeSpan.cpp
blobaef3b419b243360cc41ad470ebd6f7f8ad1e38cf
2 #include "../Common/Common.h"
4 #include <cstring>
5 #include "TimeSpan.h"
7 #ifdef _WIN32
8 #include <sys/timeb.h>
9 #else
10 #include <sys/time.h>
11 #endif
13 #ifdef _WIN32
14 // In order to track leaks to line number, we need this at the top of every file
15 #include "MemoryLeak.h"
16 #ifdef _DEBUG
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
21 #endif
23 CTimeSpan::CTimeSpan(const string& strName, bool bAddDate)
25 InitMemberVars();
27 m_strName = strName;
28 m_strStartTime = GetTimeStamp();
29 m_pTimer = new CSimpleTimer();
30 m_dElapsed = 0.0;
31 m_strEndTime = "";
32 m_strStartDate = "";
34 // A time span can optionally record the current date
35 if (bAddDate)
36 m_strStartDate = GetDateStamp();
39 CTimeSpan::~CTimeSpan()
41 if (m_pTimer != NULL)
43 delete m_pTimer;
44 m_pTimer = NULL;
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
50 // elapsed time.
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)
57 Stop();
59 strResult += strPrefix;
60 strResult += "<";
61 strResult += m_strName;
62 strResult += ">\n";
64 if (!bSinglePointInTime)
66 strResult += strPrefix;
67 strResult += "\t<Elapsed>";
68 char strNum[256];
69 sprintf(strNum, "%0.3f", m_dElapsed);
70 strResult += strNum;
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";
94 else
96 strResult += strPrefix;
97 strResult += "\t<Time>";
98 strResult += m_strStartTime;
99 strResult += "</Time>\n";
102 strResult += strPrefix;
103 strResult += "</";
104 strResult += m_strName;
105 strResult += ">\n";
107 return strResult;
110 string CTimeSpan::GetTimeStamp()
112 string strTimeStamp = "";
113 #ifdef _WIN32
114 struct timeb sTimeBuffer;
115 #else
116 struct timeval sTimeBuffer;
117 struct timezone sTimezoneBuffer;
118 time_t t;
119 #endif
120 char* szTimeLine = NULL;
122 #ifdef _WIN32
123 ftime(&sTimeBuffer);
124 szTimeLine = ctime(&(sTimeBuffer.time));
125 #else
126 gettimeofday(&sTimeBuffer, &sTimezoneBuffer);
127 t = sTimeBuffer.tv_sec;
128 szTimeLine = ctime(&t);
129 #endif
131 if ((szTimeLine != NULL) && (strlen(szTimeLine) > 18))
133 for (int i = 11; i < 19; i++)
134 strTimeStamp += szTimeLine[i];
135 strTimeStamp += ".";
136 char szMs[16];
137 #ifdef _WIN32
138 sprintf(szMs, "%d", sTimeBuffer.millitm);
139 #else
140 sprintf(szMs, "%d", static_cast<int>(sTimeBuffer.tv_usec / 1000));
141 #endif
142 if (strlen(szMs) == 1)
143 strTimeStamp += "00";
144 else if (strlen(szMs) == 2)
145 strTimeStamp += "0";
146 strTimeStamp += szMs;
149 return strTimeStamp;
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()
168 m_strEndTime = "";
169 m_dElapsed = 0.0;
172 bool CTimeSpan::IsStopped()
174 if (m_strEndTime.length() > 0)
175 return true;
177 return false;
180 double CTimeSpan::GetElapsed()
182 return m_dElapsed;
185 string CTimeSpan::GetDateStamp()
187 std::string strDateStamp = "";
188 char* szTimeLine = NULL;
189 time_t t;
191 t = time(NULL);
192 szTimeLine = ctime(&t);
194 // Format is:
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];
205 return strDateStamp;
208 void CTimeSpan::InitMemberVars()
210 m_pTimer = NULL;
211 m_strName = "";
212 m_strStartTime = "";
213 m_dElapsed = 0.0;
214 m_strEndTime = "";
215 m_strStartDate = "";
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)
225 InitMemberVars();
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);
233 m_strName = strName;