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
12 // C Includes
13 #include <stddef.h>
14 #include <time.h>
15 #include <cstring>
16 // C++ Includes
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Core/Stream.h"
20
21
22 using namespace lldb_private;
23
24 //----------------------------------------------------------------------
25 // TimeValue constructor
26 //----------------------------------------------------------------------
TimeValue()27 TimeValue::TimeValue() :
28 m_nano_seconds (0)
29 {
30 }
31
32 //----------------------------------------------------------------------
33 // TimeValue copy constructor
34 //----------------------------------------------------------------------
TimeValue(const TimeValue & rhs)35 TimeValue::TimeValue(const TimeValue& rhs) :
36 m_nano_seconds (rhs.m_nano_seconds)
37 {
38 }
39
TimeValue(const struct timespec & ts)40 TimeValue::TimeValue(const struct timespec& ts) :
41 m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
42 {
43 }
44
TimeValue(const struct timeval & tv)45 TimeValue::TimeValue(const struct timeval& tv) :
46 m_nano_seconds ((uint64_t) tv.tv_sec * NanoSecPerSec + (uint64_t) tv.tv_usec * NanoSecPerMicroSec)
47 {
48 }
49
50 //----------------------------------------------------------------------
51 // Destructor
52 //----------------------------------------------------------------------
~TimeValue()53 TimeValue::~TimeValue()
54 {
55 }
56
57
58 uint64_t
GetAsNanoSecondsSinceJan1_1970() const59 TimeValue::GetAsNanoSecondsSinceJan1_1970() const
60 {
61 return m_nano_seconds;
62 }
63
64 uint64_t
GetAsMicroSecondsSinceJan1_1970() const65 TimeValue::GetAsMicroSecondsSinceJan1_1970() const
66 {
67 return m_nano_seconds / NanoSecPerMicroSec;
68 }
69
70 uint64_t
GetAsSecondsSinceJan1_1970() const71 TimeValue::GetAsSecondsSinceJan1_1970() const
72 {
73 return m_nano_seconds / NanoSecPerSec;
74 }
75
76
77
78 struct timespec
GetAsTimeSpec() const79 TimeValue::GetAsTimeSpec () const
80 {
81 struct timespec ts;
82 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
83 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
84 return ts;
85 }
86
87 struct timeval
GetAsTimeVal() const88 TimeValue::GetAsTimeVal () const
89 {
90 struct timeval tv;
91 tv.tv_sec = m_nano_seconds / NanoSecPerSec;
92 tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec;
93 return tv;
94 }
95
96 void
Clear()97 TimeValue::Clear ()
98 {
99 m_nano_seconds = 0;
100 }
101
102 bool
IsValid() const103 TimeValue::IsValid () const
104 {
105 return m_nano_seconds != 0;
106 }
107
108 void
OffsetWithSeconds(uint64_t sec)109 TimeValue::OffsetWithSeconds (uint64_t sec)
110 {
111 m_nano_seconds += sec * NanoSecPerSec;
112 }
113
114 void
OffsetWithMicroSeconds(uint64_t usec)115 TimeValue::OffsetWithMicroSeconds (uint64_t usec)
116 {
117 m_nano_seconds += usec * NanoSecPerMicroSec;
118 }
119
120 void
OffsetWithNanoSeconds(uint64_t nsec)121 TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
122 {
123 m_nano_seconds += nsec;
124 }
125
126 TimeValue
Now()127 TimeValue::Now()
128 {
129 struct timeval tv;
130 gettimeofday(&tv, NULL);
131 TimeValue now(tv);
132 return now;
133 }
134
135 //----------------------------------------------------------------------
136 // TimeValue assignment operator
137 //----------------------------------------------------------------------
138 const TimeValue&
operator =(const TimeValue & rhs)139 TimeValue::operator=(const TimeValue& rhs)
140 {
141 m_nano_seconds = rhs.m_nano_seconds;
142 return *this;
143 }
144
145 void
Dump(Stream * s,uint32_t width) const146 TimeValue::Dump (Stream *s, uint32_t width) const
147 {
148 if (s == NULL)
149 return;
150
151 char time_buf[32];
152 time_t time = GetAsSecondsSinceJan1_1970();
153 char *time_cstr = ::ctime_r(&time, time_buf);
154 if (time_cstr)
155 {
156 char *newline = ::strpbrk(time_cstr, "\n\r");
157 if (newline)
158 *newline = '\0';
159 if (width > 0)
160 s->Printf("%-*s", width, time_cstr);
161 else
162 s->PutCString(time_cstr);
163 }
164 else if (width > 0)
165 s->Printf("%-*s", width, "");
166 }
167
168 bool
operator ==(const TimeValue & lhs,const TimeValue & rhs)169 lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
170 {
171 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
172 }
173
174 bool
operator !=(const TimeValue & lhs,const TimeValue & rhs)175 lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
176 {
177 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
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 uint64_t
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
211