1 //===-- TimeValue.cpp -------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/TimeValue.h"
11 #include "lldb/Host/Config.h"
12
13 // C Includes
14 #include <stddef.h>
15 #include <time.h>
16 #include <cstring>
17
18 #ifdef _MSC_VER
19 #include "lldb/Host/windows/windows.h"
20 #endif
21
22 // C++ Includes
23 // Other libraries and framework includes
24 // Project includes
25 #include "lldb/Core/Stream.h"
26
27
28 using namespace lldb_private;
29
30 //----------------------------------------------------------------------
31 // TimeValue constructor
32 //----------------------------------------------------------------------
TimeValue()33 TimeValue::TimeValue() :
34 m_nano_seconds (0)
35 {
36 }
37
38 //----------------------------------------------------------------------
39 // TimeValue copy constructor
40 //----------------------------------------------------------------------
TimeValue(const TimeValue & rhs)41 TimeValue::TimeValue(const TimeValue& rhs) :
42 m_nano_seconds (rhs.m_nano_seconds)
43 {
44 }
45
TimeValue(const struct timespec & ts)46 TimeValue::TimeValue(const struct timespec& ts) :
47 m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
48 {
49 }
50
TimeValue(uint32_t seconds,uint32_t nanos)51 TimeValue::TimeValue(uint32_t seconds, uint32_t nanos) :
52 m_nano_seconds((uint64_t) seconds * NanoSecPerSec + nanos)
53 {
54 }
55
56 //----------------------------------------------------------------------
57 // Destructor
58 //----------------------------------------------------------------------
~TimeValue()59 TimeValue::~TimeValue()
60 {
61 }
62
63
64 uint64_t
GetAsNanoSecondsSinceJan1_1970() const65 TimeValue::GetAsNanoSecondsSinceJan1_1970() const
66 {
67 return m_nano_seconds;
68 }
69
70 uint64_t
GetAsMicroSecondsSinceJan1_1970() const71 TimeValue::GetAsMicroSecondsSinceJan1_1970() const
72 {
73 return m_nano_seconds / NanoSecPerMicroSec;
74 }
75
76 uint64_t
GetAsSecondsSinceJan1_1970() const77 TimeValue::GetAsSecondsSinceJan1_1970() const
78 {
79 return m_nano_seconds / NanoSecPerSec;
80 }
81
82
83
84 struct timespec
GetAsTimeSpec() const85 TimeValue::GetAsTimeSpec () const
86 {
87 struct timespec ts;
88 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
89 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
90 return ts;
91 }
92
93 void
Clear()94 TimeValue::Clear ()
95 {
96 m_nano_seconds = 0;
97 }
98
99 bool
IsValid() const100 TimeValue::IsValid () const
101 {
102 return m_nano_seconds != 0;
103 }
104
105 void
OffsetWithSeconds(uint64_t sec)106 TimeValue::OffsetWithSeconds (uint64_t sec)
107 {
108 m_nano_seconds += sec * NanoSecPerSec;
109 }
110
111 void
OffsetWithMicroSeconds(uint64_t usec)112 TimeValue::OffsetWithMicroSeconds (uint64_t usec)
113 {
114 m_nano_seconds += usec * NanoSecPerMicroSec;
115 }
116
117 void
OffsetWithNanoSeconds(uint64_t nsec)118 TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
119 {
120 m_nano_seconds += nsec;
121 }
122
123 TimeValue
Now()124 TimeValue::Now()
125 {
126 uint32_t seconds, nanoseconds;
127 #if _MSC_VER
128 SYSTEMTIME st;
129 GetSystemTime(&st);
130 nanoseconds = st.wMilliseconds * 1000000;
131 FILETIME ft;
132 SystemTimeToFileTime(&st, &ft);
133
134 seconds = ((((uint64_t)ft.dwHighDateTime) << 32 | ft.dwLowDateTime) / 10000000) - 11644473600ULL;
135 #else
136 struct timeval tv;
137 gettimeofday(&tv, NULL);
138 seconds = tv.tv_sec;
139 nanoseconds = tv.tv_usec * NanoSecPerMicroSec;
140 #endif
141 TimeValue now(seconds, nanoseconds);
142 return now;
143 }
144
145 //----------------------------------------------------------------------
146 // TimeValue assignment operator
147 //----------------------------------------------------------------------
148 const TimeValue&
operator =(const TimeValue & rhs)149 TimeValue::operator=(const TimeValue& rhs)
150 {
151 m_nano_seconds = rhs.m_nano_seconds;
152 return *this;
153 }
154
155 void
Dump(Stream * s,uint32_t width) const156 TimeValue::Dump (Stream *s, uint32_t width) const
157 {
158 if (s == NULL)
159 return;
160
161 #ifndef LLDB_DISABLE_POSIX
162 char time_buf[32];
163 time_t time = GetAsSecondsSinceJan1_1970();
164 char *time_cstr = ::ctime_r(&time, time_buf);
165 if (time_cstr)
166 {
167 char *newline = ::strpbrk(time_cstr, "\n\r");
168 if (newline)
169 *newline = '\0';
170 if (width > 0)
171 s->Printf("%-*s", width, time_cstr);
172 else
173 s->PutCString(time_cstr);
174 }
175 else if (width > 0)
176 s->Printf("%-*s", width, "");
177 #endif
178 }
179
180 bool
operator ==(const TimeValue & lhs,const TimeValue & rhs)181 lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
182 {
183 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
184 }
185
186 bool
operator !=(const TimeValue & lhs,const TimeValue & rhs)187 lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
188 {
189 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
190 }
191
192 bool
operator <(const TimeValue & lhs,const TimeValue & rhs)193 lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs)
194 {
195 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
196 }
197
198 bool
operator <=(const TimeValue & lhs,const TimeValue & rhs)199 lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
200 {
201 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
202 }
203
204 bool
operator >(const TimeValue & lhs,const TimeValue & rhs)205 lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs)
206 {
207 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
208 }
209
210 bool
operator >=(const TimeValue & lhs,const TimeValue & rhs)211 lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
212 {
213 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
214 }
215
216 uint64_t
operator -(const TimeValue & lhs,const TimeValue & rhs)217 lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
218 {
219 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();
220 }
221
222
223